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.

 

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 bb4bad in the box below so that we can be sure you are a human.