How To Set Up Apache Virtual Hosts on Ubuntu

Ubuntu

Nicesnippets

248

09-06-2020


Hello Friends,

Today,I would like to share with you how to setup apache virtual hosts in ubuntu.The Apache web server is a popular method for serving websites on the internet.It provides many powerful features including dynamically loadable modules, robust media support, and extensive integration with other popular software.

The Apache HTTP Web server is a powerful, free and open source web server that has been, and remains the leading web server platform ahead of others.

Here I will give you full example for setup apache virtual hosts on ubuntu So let's see the bellow step by step. In this example I will create two domain.

Step 1 : Create the Directory Structure

In this step, we have to make a directory structure with actual domain name like dev.nicesnippets.com. You can create directory using bellow command

sudo mkdir -p /var/www/dev.nicesnippets.com/public_html

sudo mkdir -p /var/www/demo.nicesnippets.com/public_html

Step 2 : Give Permissions

In this step, You will create directory after give the permission to directory using bellow command

sudo chown -R $USER:$USER /var/www/dev.nicesnippets.com/public_html

sudo chown -R $USER:$USER /var/www/demo.nicesnippets.com/public_html

Step 3 : Create demo page for each Virtual Hosts

In this step, We have to make index.html page for each site using bellow command.

nano /var/www/dev.nicesnippets.com/public_html/index.html

Successfully run this command then after open index.html and put the bellow command.

<html>

<head>

<title>How To Set Up Apache Virtual Hosts on Ubuntu - NiceSnippets.com</title>

</head>

<body>

<h1>Welcome to Dev.NiceSnippets.Com</h1>

<p>Success! The dev.NiceSnippets.com virtual host is working!</p>

</body>

</html>

Put the above code after save and close file, then copy this file to use as the basis for our second site:

nano /var/www/demo.nicesnippets.com/public_html/index.html

Successfully run this command then after open index.html and put the bellow command.

<html>

<head>

<title>How To Set Up Apache Virtual Hosts on Ubuntu - NiceSnippets.com!</title>

</head>

<body>

<h1>Welcome to Demo.NiceSnippets.Com</h1>

<p>Success! The demo.NiceSnippets.com virtual host is working!</p>

</body>

</html>

Put the above code after save and close file as well.

Step 4 : Create New Virtual Host Files

In this step, We have to create new virtual host files. Apache comes with a default virtual host files called 000-default.conf that we will use as a template.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/dev.nicesnippets.com.conf

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/demo.nicesnippets.com.conf

Open the new file in your terminal using nano command:

sudo nano /etc/apache2/sites-available/dev.nicesnippets.com.conf

Run this Command then after setup this conf file using fill some information as bellow:

ServerAdmin admin@example.com

ServerName dev.nicesnippets.com

ServerAlias www.dev.nicesnippets.com

DocumentRoot /var/www/dev.nicesnippets.com/public_html

For the Second Virtual host files setup configuration in second domain.

sudo nano /etc/apache2/sites-available/demo.nicesnippets.com.conf

Run this Command then after setup this conf file using fill some information as bellow:

ServerAdmin admin@example.com

ServerName demo.nicesnippets.com

ServerAlias www.demo.nicesnippets.com

DocumentRoot /var/www/demo.nicesnippets.com/public_html

Step 5 : Enable the New Virtual Host Files

In this step, We will enables the virtual host files. We’ll be using the a2ensite tool to achieve this goal.

sudo a2ensite dev.nicesnippets.com.conf

sudo a2ensite demo.nicesnippets.com.conf

When you are finished, you need to restart Apache to make these changes take effect and use systemctl status to verify the success of the restart.

sudo systemctl restart apache2

Your server should now be set up to serve two websites.

Step 6 : Set Up Local Hosts File

In this step, You will setup local host file server ip and host domain name using bellow command

sudo nano /etc/hosts

Run above command thenafter setup the localhost file using domain name.

127.0.0.1 localhost

127.0.1.1 guest-desktop

your_server_IP dev.nicesnippets.com

your_server_IP demo.nicesnippets.com

Step 7 : Run Project

In this step, Lets open the browser and run the bellow url.

dev.nicesnippets.com

Lets open the browser and run the bellow url.

demo.nicesnippets.com

It will help you...


Recommended Posts