Introduction to Virtual Host
Virtual hosting is a way to host multiple domain on single server. While working on multiple projects using Apache server, creating Virtual Host is convenient option for a developer. So, creating Virtual host is, sharing the same resources like processor, memory etc. to host multiple domains or sites on single server without impacting each other.
Prerequisite
Before creating virtual host for any website, It is important to have some prerequisites.
- Updation of apt-get package
$ sudo apt-get update
- Apache should be installed. If not then, you can use below command to install Apache on ubuntu
$ sudo apt-get install apache2
How to start
Now let’s suppose, We need to host two websites on our local webserver. i.e., abc.dev and def.dev.
- Let’s start with creating different target point for them. Means let’s create different directories inside ‘/var/www/html’ which will be used by these host names.
$ cd /var/www/html
$ sudo mkdir -p abc.dev/public_html
$ sudo mkdir -p def.dev/public_html
- Giving permission to them is very important. I usually give the ownership as <username>:www-data (www-data for apache group and username is current username). Also, give 755 permission to the html folder.
sudo chown -Rf <username>:www-data /var/www/html/abc.dev/public_html
sudo chown -Rf <username>:www-data /var/www/html/abc.dev/public_html
sudo chmod -R 755 /var/www/html
- Now, we need to create Virtual hosts files for both the domains. The default virtual host file is 000-default.conf inside /etc/apache2/sites-available directory. You can also use locate command to locate the sites-available directory path.
$ locate sites-available
- Copy the default Virtual Host to the <new domain>.conf file and edit the settings. As like below
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/abc.dev.conf
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/def.dev.conf
- You need to edit the file to update the settingsin respoctive files. Use proper value for ‘ServerAdmin’, ‘ServerName’, ‘ServerAlias’ and ‘DocumentRoot’. DocumentRoot is the path of directory where project files are hosted.
$ sudo vi /etc/apache2/sites-available/abc.dev.conf
<VirtualHost *:80>
ServerAdmin abc@abc.dev
ServerName abc.dev
ServerAlias abc.dev
DocumentRoot /var/www/html/abc.dev/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
$ sudo vi /etc/apache2/sites-available/def.dev.conf
<VirtualHost *:80>
ServerAdmin abc@def.dev
ServerName def.dev
ServerAlias def.dev
DocumentRoot /var/www/html/def.dev/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- Enable the hosted sites as below
$ sudo a2ensite abc.dev.conf
$ sudo a2ensite def.dev.conf
- Restart the apache using
$ sudo service apache2 restart
- Edit the /etc/hosts file and update as below
$ sudo vi /etc/hosts
127.0.0.1 abc.dev
127.0.1.1 def.dev
- Create separate index.php files in respective directories and open the URL (abc.dev and def.dev) in browser. Your hosts are ready.
Cheers!