How to Set Up Apache Virtual Hosts on CentOS 7
This post covers the steps involve in setting up apache virtual host on CentOS 7. Virtual hosting is a method for hosting multiple domain names (with separate handling of each name) on a single server (or pool of servers). This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same hostname.
1. Install Apache
If Apache is not yet installed, use the command below to install it.
sudo yum -y install httpd
2. Enable Apache as CentOS Service
Use the command below to enable Apache as a CentOS service so that it will automatically start after a reboot.
sudo systemctl enable httpd.service
3. Create Directory Structure
For this tutorial, I would use these two fictitious websites as examples i.e divostar1.com and divostar2.com. Create the directories like below:
sudo mkdir -p /var/www/divostar1.com/public_html sudo mkdir -p /var/www/divostar2.com/public_html
4. Grant Permissions
Use the command below to make your regular user the owner of these directory structures. This way, you can modify files in the directories. The files are currently owned by root user.
sudo chown -R $USER:$USER /var/www/divostar1.com/public_html sudo chown -R $USER:$USER /var/www/divostar2.com/public_html
Grant read access to the general web directory, and all files and folders inside it using the command below:
sudo chmod -R 755 /var/www
5. Create Demo Page
In this file, create a simple HTML document that indicates the site that the page is connected to. For this tutorial, the file for our first domain will look like this:
<html> <head> <title>Welcome to divostar1.com!</title> </head> <body> <h1>Success! The divostar1.com virtual host is working!</h1> </body> </html>
Save and close the file when you are finished.
We can copy this file to use as the template for our second site’s index.php by typing:
cp /var/www/divostar1.com/public_html/index.php /var/www/divostar2.com/public_html/index.php
Open the file and modify its content like below:
<html> <head> <title>Welcome to divostar2.com!</title> </head> <body> <h1>Success! The divostar2.com virtual host is working!</h1> </body> </html>
6. Set Up Directory to Store Virtual Hosts
To begin, we will need to set up the directory that our virtual hosts will be stored in, as well as the directory that tells Apache that a virtual host is ready to serve to visitors. The
sites-available directory will keep all of our virtual host files, while the
sites-enabled directory will hold symbolic links to virtual hosts that we want to publish. We can make both directories by typing:
sudo mkdir /etc/httpd/sites-available sudo mkdir /etc/httpd/sites-enabled
7. Tell Apache to Look for Virtual hosts in the Sites-enabled Directory
sudo nano /etc/httpd/conf/httpd.conf
Add this line to the end of the file:
Save and close the file when you are done adding that line. We are now ready to create our first virtual host file.
8. Create New Virtual Host Files
For divostar1.com, do the following:
sudo nano /etc/httpd/sites-available/divostar1.com.conf
ServerName www.divostar1.com ServerAlias divostar1.com DocumentRoot /var/www/divostar1.com/public_html ErrorLog /var/www/divostar1.com/error.log CustomLog /var/www/divostar1.com/requests.log combined AllowOverride All
Similarly, for divostar2.com, do the following:
sudo nano /etc/httpd/sites-available/divostar2.com.conf
ServerName www.divostar2.com ServerAlias divostar2.com DocumentRoot /var/www/divostar2.com/public_html ErrorLog /var/www/divostar2.com/error.log CustomLog /var/www/divostar2.com/requests.log combined AllowOverride All
9. Enable the New Virtual Host Files
Create a symbolic link for each virtual host in the sites-enabled directory:
sudo ln -s /etc/httpd/sites-available/divostar1.com.conf /etc/httpd/sites-enabled/divostar1.com.conf sudo ln -s /etc/httpd/sites-available/divostar2.com.conf /etc/httpd/sites-enabled/divostar2.com.conf
When you are finished, restart Apache to make these changes take effect:
sudo apachectl restart
10. Test your Set Up
If you have been using the fictitious domain names divostar1.com and divostar2.com, you can test your
setup by editing your /etc/hosts file like below:
Linux or Mac System
127.0.0.1 localhost.localdomain localhost 127.0.1.1 divosays-pc server_ip_address divostar1.com server_ip_address divostar2.com
Read here for instructions on altering your hosts file.
On your web browser, enter
You should see a page that looks like this:
Success! The divostar1.com virtual host is working!
Likewise, if you visit your other domains, you will see the files that you created for them.
Note: If you are using real domain names, no need to edit /etc/hosts files.