How to create Multiple Virtual Host in CentOS/RHEL 5/6/7
By Anuket Jain On 13 October 2015 In Home
Using Apache Virtual Host, you can run several websites on the same server. In this article, we are going to talk about one more feature of Apache which permits us to have more than one site on a single Linux machine.
See Also:
Step #1: Create DocumentRoot
Before creating an virtual host, you need to create document root where you will keep the new website’s information.
# mkdir /home/techoism/virtual-one # mkdir /home/techoism/virtual-two
Step #2:Create the Page
Create a sample index file for testing purpose in document root.
# cd /home/techoism/virtual-one # vim index.html
<html> <head> <title>www.techoism.com</title> </head> <body> <h1>You have set up a Virtual Host successfully</h1> </body> </html>
# cd /home/techoism/virtual-two # vim index.html
<html> <head> <title>www.techoism.com</title> </head> <body> <h1>You have set up a New Virtual Host successfully</h1> </body> </html>
Step #3: Turn on Virtual Host
Now Edit the apache configuration file and make the below changes.
# vim /etc/httpd/conf/httpd.conf
Add the below lines at the end of the page:
<VirtualHost *:80> ServerAdmin dennis.r@techoism.com DocumentRoot /home/techoism/virtual-one ServerName virtual.techoism.com ErrorLog logs/virtual.techoism.com-error_log CustomLog logs/virtual.techoism.com-access_log common <Directory "/home/techoism/public_html"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerAdmin dennis.r@techoism.com DocumentRoot /home/techoism/virtual-two ServerName virtualnew.techoism.com ErrorLog logs/virtual.techoism.com-error_log CustomLog logs/virtual.techoism.com-access_log common <Directory "/home/techoism/public_html"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all Require all granted </Directory> </VirtualHost>
Step #4: Restart Apache WebServer
Now restrat apache webservice.
For CentOS/RHEL 5/6: # /etc/init.d/httpd restart For CentOS/RHEL 7: # systemctl restart httpd
Step #5: Verify the Configuration
Now you can verify the configuration on your browser by running below domain:
http://virtual.techoism.com
http://virtualnew.techoism.com
Enjoy it!