Bootstrap FreeKB - Apache (Web Server) - Virtual Hosts
Apache (Web Server) - Virtual Hosts

Updated:   |  Apache (Web Server) articles

Before web servers had virtual hosts, you would have had to had a physical server for each website that you wanted to produce.

 

Virtual hosts allow you to run multiple sites from a single physical server.

 

The HTTPD example index.html page is located at /usr/share/httpd/noindex/index.html. If you want to use the HTTPD example index.html page as a template for your site, copy the index.html to /var/www/html.

Be certain to use the cp (copy) command and not the mv (move) command. The mv command can create files with an improper SELinux context.

[root@server1 ~]# cp /usr/share/httpd/noindex/index.html /var/www/html

 

By default, the /etc/httpd/conf/httpd.conf file has the following. This markup is what tells HTTPD to serve content from the /var/www/html directory, and to serve index.html as the default document, when navigating to www.emample.com. As an example, if your index.html file is in the /var/www/html directory, when requesting www.example.com, HTTPD knows to get the index.html file from the /var/www/html directory. This is the ideal configuration if HTTPD will only be serving one website.

ServerName www.example.com

DocumentRoot "/var/www/html"

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

 

 


Virtual hosts

Create a unique directory for each site.

~]# mkdir /var/www/site1
~]# mkdir /var/www/site2

 

Add an index.html file for each site.

~]# touch /var/www/site1/index.html
~]# touch /var/www/site2/index.html

 

Add unqiue text to each index.html file.

~]# echo "Welcome to site1" > /var/www/site1/index.html
~]# echo "Welcome to site2" >/var/www/site2/index.html

 

In the /etc/httpd/conf/httpd.conf file, comment out or remove the ServerName and DocumentRoot lines. Also, add IncludeOptional sites-enabled/*.conf to the very end of the file.

# ServerName www.example.com
# DocumentRoot "/var/www/html"
IncludeOptional sites-enabled/*.conf

 

Create the sites-enabled directory.

[root@server1 ~]# mkdir /etc/httpd/sites-enabled

 

Create a file named vhosts.conf in the sites-enabled directory.

[root@server1 ~]# touch /etc/httpd/sites-enabled/vhosts.conf

 

Add the following to /etc/httpd/sites-enabled/vhosts.conf file.

<VirtualHost *:80>
    ServerName www.site1.com
    DocumentRoot "/var/www/site1"
</VirtualHost>

<VirtualHost *:80>
    ServerName www.site2.com
    DocumentRoot "/var/www/site2"
</VirtualHost>

 

The ps command can be used to determine if your system is using init or systemd. If PID 1 is init, then you will use the service command. If PID 1 is systemd, then you will use the systemctl command.

If your system is using systemd, use the systemctl command to start and enable httpd.

systemctl start httpd
systemctl status httpd

 

If your system is using init, use the service command to start and enable httpd.

service httpd start
service httpd status

 

Configure your DNS server to resolve www.site1.com and www.site2.com to the IP address of your HTTPD server.

You should now be able to navigate to www.site1.com and www.site2.com, and get unique .html files.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c415c9 in the box below so that we can be sure you are a human.