Share This:

If you have been reading my blog post. Chances are you have server up and running by now. And after yesterdays blog post. Your probably interested in how to configure Apache to host multiple websites. This is pretty easy it’s called VirtualHost.

So lets look at Apaches configuration file. To do this we will use Nano and type the following commands.


nano /etc/httpd/conf/httpd.conf

The above will make nano display the contents of the Apache configuration file. If we scroll all the way down we will see the following.


# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
#NameVirtualHost *:80
#<VirtualHost *:80>
#DocumentRoot /www/example1
#ServerName www.example1.com
# Other directives here
#</VirtualHost>

As you can see the above has certain things commented out using the # sign to make apache ignore the configuration settings. So we will simply uncomment those parts. Like the following.


# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here
</VirtualHost>

So let me try to explain what this section does.

  • NameVirtualHost *:80 (This tells apache to use port 80 on what ever IP address. The astricks is a wild card. So it can be any IP address for the server.
  • <VirtualHost *:80> (Tells apache to use any IP address and use port 80 for this particular website)
  • DocumentRoot /www/example1 (Tells apache where to look for the website. This can be any directory. Just make sure it really exist. Web Hosting companies usually make a directory for each client inside of the /www directory. The directory is usually called what ever the clients username is)
  • ServerName www.example1.com (This is the domain name for the site stored in the /www/example1 directory.
  • </VirtualHost> (This tells apache that their is no more info needed for this particular website)




So lets say we have client named Bob, and his user name for the server is bob123. And Bob has a website called bobsite.com. We also have client named Fred. Freds user name is freddy23. And he has website called iamfreddy.com. Then what we will need to do is first add these clients to our server. To do this we will type the following commands as a root user.


useradd bob123
useradd freddy23

Next we want to give the clients a password. To do this we will type the following as a root user.


passwd bob123

This will ask you to type a new password for bob123. Once you type the password in you will be asked to enter it again to confirm that you typed it right the first time. We will do the same for freddy23. As follows.


passwd freddy23

Now we will need to create directories for our two clients. We can have these directories located anywhere on the server. But we will have them located in the /www directory. Bob’s directory we will call bob123 and Fred’s directory we will call freddy23. So we will enter the following commands.


mkdir /www/bob123
mkdir /www/freddy23

Great now both of our clients have a place to store their websites. However they don’t have the proper permission to upload content to those locations. So we will want to give them permission. To do this we will type the following.


chown -R bob123 /www/bob123
chown -R freddy23 /www/freddy23


Note: The -R switch allows for the user to have permissions to all files and directories from inside of that directory. If we didn’t use -R and the user added another directory he wouldn’t have permissions to it. So we should always use the -R switch.

Now we need to tell apache about these two new sites for our clients. To do this we will modify the apache config file as we did before. By typing the following.


nano /etc/httpd/conf/httpd.conf

Then we will scroll all the way down to the bottom of the file and add the following.


<VirtualHost *:80>
DocumentRoot /www/bob123
ServerName www.bobsite.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/freddy23
ServerName www.iamfreddy.com
</VirtualHost>

Now that we have added the above info to the apache configuration file we will need to save it. To do this we will press CTRL + X. Nano will ask us if we want to save our changes, we do so confirm it. Next we will need to restart the Apache Server. To do this we will type the following.


systemctl restart httpd.service

Now our clients can start to upload content to the server and their sites can be view able on the web.




Of course on a small scale this may be okay. But say we have 100 clients on our server. Our httpd.conf file will be extremely long and hard to manage if we ever need to make changes to it. So to get around this we will simply create extra configuration files and tell the httpd.conf where to look for other configuration files. To do this we will first create directory to store such files. In this post we will call this directory vhosts. So we will type the following.


mkdir /etc/httpd/conf/vhosts

Now we have the vhosts directory. We can make sure by typing the following.


ls -l /etc/httpd/conf

Next we will have to tell apache where to find our extra configuration files. So we will use nano to edit the httpd.conf file. To do this we will type the following.


nano /etc/httpd/conf/httpd.conf

Next we will add the following to the configuration file.


Include conf/vhosts/*.conf

if we named our directory something like virtualhost then we would add the following to the configuration file.


Include conf/virtualhost/*.conf

The *.conf just tells the configuration file to look for any files that end with .conf. So now that we have modified the the httpd.conf file. We will need to save our changes by pressing CTRL + X. Then we will confirm our changes. Next we will restart apache by typing the following.


systemctl restart httpd.service

Now instead of constantly having to edit the httpd.conf file we can just make new ones from scratch and have them placed in the vhosts directory. For example we will make set bob123 site up by typing the following.


nano /etc/httpd/conf/vhosts/bob123.conf

Since the file doesn’t exist yet. We will get blank file. From here we will just add bob123 configuration information as we did above. Then we will save the information and restart apache again by typing the following again.


systemctl restart httpd.service