Virtual hosting is a method for hosting multiple domain names on a single server. The are two types of virtual hosting: “Name-based virtual hosting” and “IP-based virtual hosting.” With the name-based virtual hosting, you can host multiple websites on a single machine with a single IP address.
Here, we are going to host two websites – namely “www.virtualhost1.com” and “www.virtualhost2.com” – on a single IP “192.168.1.227” on the Apache web serve, the most popular web server in the world.
Note: while Ubuntu 14.04 server is used for this tutorial, the instructions will be similar for most Linux distros.
Getting Started: Installing Apache
First, you need to update your system and then install Apache.
To do this, run the following command:
sudo apt-get update sudo apt-get install apache2
To verify whether the Web server is working or not, open the web browser and navigate to the URL http://192.168.1.227/ (assuming that your server IP address is 192.168.1.227).
You should see that the Apache web server is working.
Create Virtual Directories
First, you need to make a directory structure that will hold the website data that we will be serving to clients.
Create two directories for the websites “www.virtualhost1.com” and “www.virtualhost2.com.”
sudo mkdir -p /var/www/html/www.virtualhost1.com sudo mkdir -p /var/www/html/www.virtualhost2.com
Note: the default folder for hosting your files is “/var/www/html.” For those who are using an alternative file path, you will have to change the above code accordingly.
Now you need to create an “index.html” file for both websites. This will be served by the Apache web server.
Create an index.html file for “www.virtualhost1.com” virtual host:
sudo nano /var/www/html/www.virtualhost1.com/index.html
Add the following content:
<html> <head> <title>www.virtualhost1.com</title> </head> <body> <h1>Welcome To www.virtualhost1.com website</h1> </body> </html>
Save and close the file.
Similarly, create an index.html file for “www.virtualhost2.com” virtual host:
sudo nano /var/www/html/www.virtualhost2.com/index.html
Add the following content:
<html> <head> <title>www.virtualhost2.com</title> </head> <body> <h1>Welcome To www.virtualhost2.com website</h1> </body> </html>
Save and close the file.
Setting Up Ownership and Permissions
By default, Apache service runs as a “www-data” user in Ubuntu. Both virtual directories that we created earlier are owned by root. You must change the ownership of these two virtual directories to “www-data,” so that Apache can read and write data.
To do this, run
sudo chown -R www-data:www-data /var/www/html/www.virtualhost1.com/ sudo chown -R www-data:www-data /var/www/html/www.virtualhost2.com/
Also, you need to make the Apache web root (/var/www/html) directory world readable so that everyone can read files from that directory.
sudo chmod -R 755 /var/www/html
Create Virtual Host Files:
By default, Apache comes with a default virtual host file called “000-default.conf.” You need to disable this virtualhost file first.
To do this, run the following command:
sudo a2dissite 000-default.conf
Now, create a virtual host file “www.virtualhost1.com.conf” for the virtual host “www.virtualhost1.com.”
sudo nano /etc/apache2/sites-available/www.virtualhost1.com.conf
Add the following content:
<VirtualHost *:80> ServerAdmin admin@virtualhost1.com ServerName www.virtualhost1.com DocumentRoot /var/www/html/www.virtualhost1.com ErrorLog ${APACHE_LOG_DIR}/www.virtualhost1.com_error.log CustomLog ${APACHE_LOG_DIR}/www.virtualhost1.com_access.log combined </VirtualHost>
Save and close the file.
Similarly, create a virtual host file “www.virtualhost2.com.conf” for the virtual host “www.virtualhost2.com.”
sudo nano /etc/apache2/sites-available/www.virtualhost2.com.conf
Add the following content:
<VirtualHost *:80> ServerAdmin admin@virtualhost2.com ServerName www.virtualhost2.com DocumentRoot /var/www/html/www.virtualhost2.com ErrorLog ${APACHE_LOG_DIR}/www.virtualhost2.com_error.log CustomLog ${APACHE_LOG_DIR}/www.virtualhost2.com_access.log combined </VirtualHost>
Save and close the file.
After creating the virtual host files, you need to enable a new virtual host.
You can do this by running
sudo a2ensite www.virtualhost1.com.conf sudo a2ensite www.virtualhost2.com.conf
Finally, restart the Apache service.
sudo /etc/init.d/apache2 restart
Testing Virtual Hosts:
You need to add a host entry on each and every remote or local system to resolve the website by name.
You can do this by editing the “/etc/hosts” file.
sudo nano /etc/hosts
Add the following lines:
192.168.1.227 www.virtualhost1.com 192.168.1.227 www.virtualhost2.com
Save and close the file.
Open your web browser and navigate to the URLs “http://www.virtualhost1.com” and “http://www.virtualhost2.com.”
You should see the sample demo pages which we created earlier.
www.virtualhost1.com demo page:
www.virtualhost2.com demo page:
Conclusion
I hope this post will help you to set up virtual host in Ubuntu. There is no limit on the number of virtualhost Apache can handle, so you can create and host as many websites you want.
Over 5 years of experience as IT system administrator for IT company in India. My skills include a deep knowledge of Rehat/Centos, Ubuntu nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, squied proxy, NFS, FTP, DNS, Samba, ldap, Openvpn, Haproxy, Amazon web services, WHMCS, Openstack Cloud, Postfix Mail Server, Security etc.
Subscribe to our newsletter!
Our latest tutorials delivered straight to your inbox
Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe