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.

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)

Create a Demo Pages

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

Do the same thing for Virtual Host Directory 2

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/

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.

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

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

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

Copy paste bellow code.

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

Copy paste bellow code.

Check Virtual Host Configuration Syntax

To check configuration files for syntax errors:

To list all Virtual Hosts and their locations:

Enable the Newly Created Virtual Host Files

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

Setup Local Host Files

Edit file, /etc/hosts,

Add the Virtual Domain Names one by one as shown below

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.

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!