How To Set Up Apache Virtual Hosts on CentOS 7

Try it in our public cloud & Get $50 Credit
CLAIM NOW
The Apache Foundation

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.

Getting Started

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.
setenforce 0
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.
nano /etc/httpd/conf/httpd.conf
IncludeOptional sites-enabled/*.conf

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.
mkdir /etc/httpd/sites-available
mkdir /etc/httpd/sites-enabled

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.
cd /etc/httpd/sites-available/
nano globo.tech.conf

<VirtualHost *:80>
ServerAdmin webmaster@globo.tech
ServerName www.globo.tech
ServerAlias globo.tech
DocumentRoot /var/www/globo.tech/public_html
ErrorLog /var/www/globo.tech/error.log
CustomLog /var/www/globo.tech/access.log combined
</Virtualhost>

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.

Conclusion

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.