Setup Apache Virtual Hosts on Ubuntu OS
What is Apache Virtual Host
Virtual Hosting is a method of hosting multiple domain names on a single server( or a 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 host name. There are two types of virtual hosting in Apache, namely IP-based virtual hosting and name-based virtual hosting. With IP-based virtual hosting, you can host multiple websites or domains on the same system, but each website/domain has different IP address. With name-based virtual hosting, you can host multiple websites/domains on the same IP address.
How to setup virtual host on Ubuntu OS
In order to work this out first you need to access the following:
- A sudo user logged in
- An Apache web server which you can install by running command in terminal
sudo apt install apache2
After you have accessed above we will follow the following steps:
Step 1 – Create Directory
Firstly we need to create a directory to keep our website data. For this you can use your domain names. I’ll be using dummy domain names to provide an example . You can create directories by using following commands in terminal.
$ sudo mkdir -p /var/www/codingcafe1.com/public_html
$ sudo mkdir -p /var/www/codingcafe2.com/public_html
We should now change the permissions for non-root user to modify files in the directory. This can be done by following terminal commands.
$ sudo chown -R $USER:$USER /var/www/codingcafe1.com/public_html
$ sudo chown -R $USER:$USER /var/www/codingcafe2.com/public_html
Additionally we also write a command for access to file and folders.
$ sudo chmod -R 755 /var/www
Step 2 – Create Demo Pages for each virtual host
Create an index.html in your directory craeted above
$ nano /var/www/codingcafe1.com/public_html/index.html
$ nano /var/www/codingcafe2.com/public_html/index.html
Step 3 – Create Virtual Host Files
Apache comes with a default virtual host file 000.default.conf
file. We’ll copy it over to create a virtual host file for each of our domains. We will create virtual host file by using command
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/codingcafe1.com.conf
Now edit this newly created file in your editor
$ sudo nano /etc/apache2/sites-available/codingcafe1.com.conf
You can also use vim editor and command will be $ sudo /etc/apache2/sites-available# vim codingcafe1.com.conf
We will modify this file for our domain
<VirtualHost *:80>
ServerAdmin admin@codingcafe1.com
ServerName codingcafe1.com
ServerAlias www.codingcafe1.com
DocumentRoot /var/www/codingcafe1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
After changes save and close the file.
Copy First Virtual Host and Customize for Second Domain
Now we have first virtual host and we will create virtual host for second domain by copying that file and adjusting as needed. We will copy by using command
$ sudo cp /etc/apache2/sites-available/codingcafe1.com.conf /etc/apache2/sites-available/codingcafe2.com.conf
Open this file throughr editor and make changes accordingly
<VirtualHost *:80>
ServerAdmin admin@codingcafe2.com
ServerName codingcafe2.com
ServerAlias www.codingcafe2.com
DocumentRoot /var/www/codingcafe2.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file.
Step 5 – Enable the New Virtual Host Files
Now as our virtual host files have been created , next task is to enable them. For enabling we use command
$ sudo a2ensite codingcafe1.com.conf
$ sudo a2ensite codingcafe2.com.conf
Next, disable the default site defined in 000-default.conf
:
$ sudo a2dissite 000-default.conf
Now after above is done , we need to restart Apache server . We will use command
$ sudo systemctl restart apache2
Your server should now be set up to serve above two websites.
Step 5 – Set Up Hosts File
If you are working on a local server then you have to register your domain names in /etc/hosts
file. For this you have to edit hosts file by editor or through Vim editor command $ sudo nano /etc# vim hosts
This will look like
127.0.0.1 localhost
127.0.1.1 guest-desktop
your_server_IP codingcafe1.com
your_server_IP codingcafe2.com
Save and close the file. This will direct any requests for codingcafe1.com
and codingcafe2.com
on our computer and send them to our server.
Now our process has been completed and your domain names are ready to be hit on broswer.