How To Set Up Apache Virtual Hosts on CentOS 7
An Apache Virtual Hosts allows multiple websites to run on the same server. If you only have one CentOS 7 server or VPS, this is an efficient way to host multiple sites or domains that are cleanly separated from each other. There is no limit to the number of virtual hosts for the Apache server however you must keep your hardware limitations in mind.
To complete this walkthrough successfully the following are required:
• A node (Dedicated or Cloud Server)running CentOS 7
• All commands must be entered as root
• A complete LAMP implementation
Tutorial – Apache Virtual Hosts
For the purpose of this article, we will set up a virtual host for globo.tech on our instance of Apache.
We will start with disabling SELinux. This will make testing the setup easier and can be re-enabled later with the proper context to handle the new virtual host.
sed -i 's/enforcing/disabled/' /etc/sysconfig/selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
Each virtual host must have its own top-level directory under /var/www. Use the mkdir command to create the directory for globo.tech and the public_html subdirectory.
mkdir -p /var/www/globo.tech/public_html/
Since this directory is owned by the root user, we will need to specifically grant access rights to Apache.
chown -R apache. /var/www/
chmod -R 755 /var/www/globo.tech/
Apache will also need to know where to look for additional *.conf files as each virtual host will have one. Open the main configuration file for Apache at /etc/httpd/httpd.conf and add this line at the end.
Now we will create two additional folders enable virtual hosts on the system. One directory will hold all the virtual host files while the other will hold symbolic links for each virtual host published by Apache.
Once that is done we can create the *.conf file for the virtual host. In this case, we will create the globo.tech.conf file in /etc/httpd/site-available directory. Remember that we’ve already instructed Apache to look for additional config files at this location.
CustomLog /var/www/globo.tech/access.log combined
Then we will need to create the symbolic link for globo.tech in the /etc/httpd/sites-enabled directory.
ln -s /etc/httpd/sites-available/globo.tech.conf /etc/httpd/sites-enabled/globo.tech.conf
After all the above steps have been completed, it is now time to restart Apache. Use this command in order for the server to publish the globo.tech virtual host.
systemctl restart httpd.service
If the instructions have been followed completely, you can place your website files in the root document directory of globo.tech at /var/www/globo.tech/public_html. All you need to do is point your browser to the domain name at the server IP to see your virtual host.
We hope you have enjoyed learning about setting up an Apache virtual host on CentOS 7. You can always experiment further by adding more sites to your server. If you have found this article useful, please consider sharing it.