First, just in case you haven't recently, let's update our OS:
# dfn check-update
# dnf update
We have two choices about which files we will edit to include our virtual hosts. We can edit:
/etc/httpd/conf/httpd.conf
And add the virtual host info there; or, the above file includes a line (the
IncludeOptional conf.d/*.conf
line) that allows us to create additional
configuration files for Apache2 in the /etc/conf.d/
directory. Per that
above line, the files need to end with .conf
. Let's do the latter. We'll
name the files after our pretend domain names. I'll create a domain called
linuxsysadmins and another one called websysadmins:
# cd /etc/httpd/conf.d/
# touch linuxsysadmins.conf
Then we'll add the following info to each file, making sure to make changes according to the specifics:
<VirtualHost *:80>
ServerAdmin webmaster@your_domain.com
DocumentRoot "/var/www/html/your_domain/"
ServerName your_domain.com
ServerAlias www.your_domain.com
ErrorLog "/var/log/httpd/your_domain.com-error_log"
CustomLog "/var/log/httpd/your_domain.com-access_log" combined
<Directory "/var/www/html/your_domain/">
DirectoryIndex index.html index.php
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Then, copy the above file to create our second virtual host:
# cp linuxsysadmins.conf websysadmins.conf
And edit websysadmins.conf
accordingly.
When you're done, exit out of nano
and check your configuration syntax with
the following command:
# httpd -t
You should get an error stating that he sites don't exist at the
DocumentRoot, but we'll fix that in a second. For now, you want to get a
Syntax OK
message.
Now, the above two files tell Apache2 to look for the respective websites in
/var/www/html/your_domain
. We need to create those locations. I'll do that
now for my two domains:
# cd /var/www/html/
# mkdir linuxsysadmins.com websysadmins.com
Now let's create some basic web pages in each domain directory:
# cd linuxsysadmins.com
# echo "<h1>Linux Sys Admins</h1>" >> index.html
# cd ../websysadmins.com
# echo "<h1>Web Sys Admins</h1>" >> index.html
And now we have to make sure that the user apache
owns those two diretories
and all future files in them. We use the user apache
because the main
Apache2 configuration file (/etc/httpd/conf/httpd.conf
) has two directives
that state that the names of the User/Group should be apache
:
# chown -R apache:apache linuxsysadmins.com
# chown -R apache:apache websysadmins.com
You can run ls -l
on those directories and files to confirm that the
apache
owner owns them. You can also run httpd -t
again to confirm that
all the syntax is good.
/etc/hosts
fileNow, depending on the domain names you chose, they could already exist and have
real DNS records that point to other IP addresses. Therefore, we need to edit
the /etc/hosts
file and let our system know that each of those domain names
point to localhost. For me, I'll add the following lines:
# nano /etc/hosts
127.0.0.1 linuxsysadmins.com
127.0.0.1 websysadmins.com
Now, let's restart Apache2 and see if we can visit our sites.
# systemctl restart httpd.service
# w3m http://linuxsysadmins.com ## Comment: then quit
# w3m http://websysadmins.com
Success!