How to Setup Apache Virtual Host on Ubuntu (1)

Steps to Setup Apache Virtual Host on Ubuntu

Apache web server is a free and open-source cross-platform designed to create web servers that have the ability to host or more HTTP-based websites. It allows the website owners to serve the content on the webserver. 

Apache uses Virtual Host to describe any individual site or domain. These virtual hosts allow the users to host multiple domains or sites with a single server or IP using directory matching. Each domain that is configured will direct to the specific directory holding that site’s information. 

Prerequisites

1. Apache: If Apache is not installed on your server, install it using the following commands:

sudo apt-get update

sudo apt-get install apache2

2. Directory: A directory/website for which the host is to be created. Add it in the /var/www/html with the directory permission as 755. This directory will be the entry for your DocumentRoot directive.

Creating New Virtual Host Files

Copy the default configuration file provided by Apache using the command:

sudo cp /etc/apache2/sites-avaliable/000-default.conf /etc/apache2/sites-available/example-zehncloud.com.conf

Step1: Open the file in your editor or terminal with root privileges.

sudo nano /etc/apache2/sites-available/example-zehncloud.com.conf

The default file looks like:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost 
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

This Virtual host section matches any request made on port 80, the default HTTP port.

The ServerAdmin directive includes the email that the site administrator can receive emails through.

ServerAdmin admin@example-zehncloud.com

Add the two directives after ServerAdmin ServerName, and ServerAlias. The ServerName refers to the base domain that should match the virtual host definition and the ServerAlias defines the other names that should match as if they were the base names.

ServerName example-zehncloud.com
SeverAlias www.example-zehncloud.com

The directive DocumentRoot refers to the location of the document root for the domain.

DocumentRoot /var/www/html/example_zehncloudcom

After doing the above changes the virtual host file looks like:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example-zehncloud.com
    ServerAlias www.example-zehncloud.com
    DocumentRoot /var/www/html/example_zehncloudcom
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 2: After doing all the required changes to the virtual host file close and save the changes, enable the new virtual host files, and restart the Apache webserver.

sudo a2ensite example-zehncloud.com.conf
sudo service apache2 restart

Example to Host Two Applications From the Same Server

The example includes two applications/websites app1 and app2. The directory structure of app1 and app2 are /var/www/html/app1 and /var/www/html/app2 respectively. The two domains can be hosted in two ways either by having different domains for each like app1.zehncloud.net and app2.zehncloud.net or it can be app.zehncloud /app1 and app.zehncloud.net/app2.

The default permissions given to the directories are [ 755 ]:

sudo chmod -R 755 /var/www

Accessing Different Website With The Same Domain

Step 1: Open the default Apache virtual host file with root privileges in /etc/apache2/sites-available directory.

sudo nano /etc/apache2/sites-avaliable/000-default.conf

The default file looks like:

VirtualHost *:80>
    ServerAdmin webmaster@localhost 
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 2: Add the ServerName in the configuration file so that when the domain name hits on the browser then the apache default page opens.

VirtualHost *:80>
    ServerAdmin webmaster@localhost 
    ServerName app.zehncloud.net
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 3: After doing all the required changes to the virtual host file close and save the changes. To enable the new virtual host files and restart the Apache webserver.

sudo a2ensite 000-default.conf
sudo service apache2 restart

Go to the domain name that you configured in your web browser:

http://app.zehncloud.net

You will see the default Apache web page.

Step 4: To access your websites in folders /var/www/html/app1 and /var/www/html/app2 hit the URL as:

http://app.zehncloud.net/app1
http://app.zehncloud.net/app2

With the above URL’s you can access your application1 [ app1 ] and application2 [ app2 ] respectively.

This is how you can configure Apache Virtual Host to use the same domain to access different websites in your server.

Accessing Different Website With Different Domain

Step 1: Copy the default Apache virtual host file in /etc/apache2/sites-available directory as app1.zehncloud.net.conf and app2.zehncloud.net.conf respectively.

sudo cp /etc/apache2/sites-avaliable/000-default.conf /etc/apache2/sites-available/app1.zehncloud.net.conf
sudo cp /etc/apache2/sites-avaliable/000-default.conf /etc/apache2/sites-available/app2.zehncloud.net.conf

Step 2: Edit the files app1.zehncloud.net.conf with root privileges

sudo nano /etc/apache2/sites-available/app1.zehncloud.net.conf

The default file looks like:

VirtualHost *:80>
    ServerAdmin webmaster@localhost 
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 3: Add the ServerName as app1.zehncloud.net and change the Document root to /var/www/html/app1. After the changes the files look like:

VirtualHost *:80>
    ServerAdmin webmaster@localhost 
    ServerName app1.zehncloud.net
    DocumentRoot /var/www/html/app1
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 4: Similarly edit the app2.zehncloud.net file by adding ServerName and changing the DocumentRoot. The file app2.zehncloud.net after the changes looks like:

VirtualHost *:80>
    ServerAdmin webmaster@localhost 
    ServerName app2.zehncloud.net
    DocumentRoot /var/www/html/app2
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 5: After doing all the required changes to the virtual host file to enable both the new virtual host files and restart the Apache webserver.

sudo a2ensite app1.zehncloud.net
sudo a2ensite app2.zehncloud.net
sudo service apache2 restart

Step 6: To access your websites in folders /var/www/html/app1 and /var/www/html/app2 hit the URL as:

http://app1.zehncloud.net
http://app2.zehncloud.net

With the above URL’s you can access your application1 [ app1 ] and application2 [ app2 ] respectively.

This is how you can have different domains for the applications/websites on the same server.

Conclusion

If you followed the article, then you will have a single server handling different domains or you can also have the same domain but accessing different applications/websites (app1 and app2). The above article hosts only two domains but you can have any as many as you want, it depends on server handling capacity about how many websites/domains it can handle.

ELK stack setup on centos
AWS
Chanchal Khatri

ELK Stack Setup on CentOS

Contents Introduction Elasticsearch Kibana Logstash Introduction ELK stack is formerly known as Elastic Stack. It consists of Elasticsearch, Logstash, and Kibana. It helps you to store all your logs in one place and analyze the issues

Read More »
amazonsqs
AWS
Anuj Mandloi

Why Amazon SQS?

Contents Introduction Difference between SQS and MQ Benefits of SQS Getting started with AWS SQS Conclusion Introduction Amazon Web Services provides many services to make life easier. “Amazon SQS” is another service provided by Amazon

Read More »