15 49.0138 8.38624 1 0 4000 1 https://www.techpulsetoday.com 300
Create Apache Virtual Host

How To Create Apache Virtual Host in Ubuntu

Install the Apache Web Server

Before Create Apache Virtual Host files, we need to check in your system whether Apache Server installed or not. If it is installed no need to anything. If it is not installed type the following commands.

sudo apt-get update
sudo apt-get install apache2

After installing Apache Server, Let’s test whether the web server is working properly or not by navigating to the URL http://ip-address/ or http://localhost/. It will open Apache Ubuntu Default Page.

Create  a Virtual Directories

Create a directory in root folder (ie. public_html folder)

cd /var/www/html/
sudo mkdir dev1.techpulsetoday.com
sudo mkdir dev2.techpulsetoday.com

Create a Demo Pages

Create an index.html file in nano or vim editor by typing,

sudo nano dev1.techpulsetoday.com/index.html
<h1>Good! dev1.techpulsetoday.com Virtual Host is Working fine!</h1>

Do the same thing for Virtual Host Directory 2

sudo nano dev2.techpulsetoday.com/index.html
<h1>Good! dev2.techpulsetoday.com Virtual Host is Working fine!</h1>

Give the Proper Ownership and Permissions

Navigate to corresponding Virtual Host Directory and then run the below commands.

ie. cd /var/www/html/dev1.techpulsetoday.com/ and cd /var/www/html/dev2.techpulsetoday.com/

sudo chown www-data:www-data -R *
sudo find . -type d -exec chmod 755 {} \;
sudo find . -type f -exec chmod 644 {} \;

Create Virtual Host Files

By default, Apache comes with a default virtual host file called 000-default.conf. We will copy the 000-default.conf file contents or Create a new Virtual Host files.

vijayan@vijayan-VPCCA35FA:/var/www/html/dev1.techpulsetoday.com$ cd /etc/apache2/sites-available/
vijayan@vijayan-VPCCA35FA:/etc/apache2/sites-available$ pwd
/etc/apache2/sites-available
vijayan@vijayan-VPCCA35FA:/etc/apache2/sites-available$ ls
000-default.conf
vijayan@vijayan-VPCCA35FA:/etc/apache2/sites-available$

Now you are in this location -> /etc/apache2/sites-available

sudo touch dev1.techpulsetoday.com.conf
sudo touch dev2.techpulsetoday.com.conf

Make sure the Virtual Host files contains .conf extension at the end.

Now open dev1.techpulsetoday.com.conf, nano or vim editor by typing

sudo nano dev1.techpulsetoday.com.conf

Copy paste bellow code.

<VirtualHost *:80>
        ServerName dev1.techpulsetoday.com
        ServerAlias www.dev1.techpulsetoday.com
        ServerAdmin vijayan@techpulsetoday.com
        DocumentRoot /var/www/html/dev1.techpulsetoday.com
<Directory "/var/www/html/dev1.techpulsetoday.com">
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Likewise, open dev2.techpulsetoday.com.conf, nano or vim editor by typing

sudo nano dev2.techpulsetoday.com.conf

Copy paste bellow code.

<VirtualHost *:80>
        ServerName dev2.techpulsetoday.com
        ServerAlias www.dev2.techpulsetoday.com
        ServerAdmin vijayan@techpulsetoday.com
        DocumentRoot /var/www/html/dev2.techpulsetoday.com
<Directory "/var/www/html/dev2.techpulsetoday.com">
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Check Virtual Host Configuration Syntax

To check configuration files for syntax errors:

# Fedora, RHEL, CentOS, OSX
httpd -t

# Debian, Ubuntu
apache2ctl -t

# MacOS
apachectl -t

To list all Virtual Hosts and their locations:

# Fedora, RHEL, CentOS, OSX
httpd -S

# Debian, Ubuntu
apache2ctl -S

# MacOS
apachectl -S

Enable the Newly Created Virtual Host Files

sudo a2ensite dev1.techpulsetoday.com.conf
sudo a2ensite dev2.techpulsetoday.com.conf

After Enabling Virtual Host, you need to restart Apache Server.

# In Ubuntu 15.10/15.04:
sudo systemctl restart apache2

# In Ubuntu 14.10 and earlier versions:
sudo service apache2 restart

Setup Local Host Files

Edit file, /etc/hosts,

sudo nano /etc/hosts

Add the Virtual Domain Names one by one as shown below

#####################################
# Virtual Host
#####################################
127.0.1.1       dev1.techpulsetoday.com
127.0.1.1       dev2.techpulsetoday.com

restart your apache if require.

Test your Results

Open your browser and type URL http://dev1.techpulsetoday.com/ and http://dev2.techpulsetoday.com/

dev1.techpulsetoday.com Page

dev1.techpulsetoday.com

dev2.techpulsetoday.com Page

dev2.techpulsetoday.com

If both of these sites work, you’ve successfully configured two Virtual Hosts on the same Server.

Share:
Category:Linux
PREVIOUS POST
The Top 10 List of Popular Programming Languages
NEXT POST
How to enable Gzip compression
ABOUT
Vijayan J

Thanks for stopping by guys! I'm Vijayan and Techpulse is my beloved brainchild. Currently working as a PHP developer in a digital marketing start-up, I'm overly passionate about not just learning new things but also putting those into practice. I swear by a quote I once came across... 'What separates successful people from unsuccessful people is the former's ability to execute'. Feel free to reach out to me if you have any questions, suggestions or feedback. Hoping to see more of you here!

0 Comment

LEAVE A REPLY

This site uses Akismet to reduce spam. Learn how your comment data is processed.