Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)

Selvakumar
Author

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Share


Our Newsletter


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tags


Twitter


Official Power Up Hosting Blog

Create Name Based Virtual Host using Apache on Ubuntu in 5 Easy Step

SelvakumarSelvakumar

Are you suffering from the resource scarcity and unable to host your domains due to the budget problems?

Then Apache virtual hosting with Name Based Virtual Host can be the best solution for you.

Don't know what virtual hosting is?

name based virtual host

Read further to get what exactly a virtual hosting is.

Virtual hosting is hosting multiple domains on a single server so that they can share the resources such as memory, RAM, and processor.

Hence the resource utilization will be established here.

Here we are going to talk about Name-based virtual hosting and IP-based virtual hosting using Apache server.

You might have a question now, what is Apache server? (if you are new)

Let me make it easier for you. Apache web server is a software which runs on a server and maintains all the process of the server regarding hosting such as hosting multiple domains, and serving the request from the client and managing the resources for the server( allocating CPU, RAM, and cache).

Note: 65% web server runs on Apache software. Especially most of the WordPress hosting using the Apache web server.

Apache is fast and secure. It also has a good name for its high reliability.

There is two type of virtual hosting is available. One is name based virtual hosting, and the other is IP-based virtual hosting.

If you have installed the LAMP on your server, then skip the last section, if not read here to know how to install the Apache.

Install Apache on Ubuntu

To install Apache on Ubuntu run the following command

sudo apt-get update

After executing the above command, you will be asked for the password of the system to change the admin. Enter it and use the below command

sudo apt-get install apache2

Once you use this command, the terminal will give you details about the requirement and ask for the permission to install it.

Hit Y and enter, that's all now the terminal takes care of the remaining installation process

The terminal executes the file and installs all the Apache server modules.

To check whether the Apache is working well, you have to input the server IP in the browser and check. If the apache server is configured perfectly, you will get a message like in the below image.

To find the IP of the server just use ifconfig

The next step is to install the MySQL. To do that

Use the command below

sudo apt-get install MySQL-server

Once again the terminal will ask for continue. Just put Y and enter.

Based on the internet speed, it will take the time to download and install the package.

You can also see the status in the terminal.

Once the package is downloaded and installed, the MySQL setup will ask to set a new password for the root. Just enter a new password and press enter. Then once again it will ask to retype the password.

Input the password again and enter. That’s all; now the MySQL is installed on the server.

Now the next step is to install PHP on the server.

Install the PHP

PHP is the open source scripting language used all over the world to build better dynamic web pages.

To install PHP on Ubuntu server use the below command

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

The terminal will ask do you want to continue?

Just Press Y and enter

Again the terminal will take some time as it has to download and parse the package to install PHP.

Now the Terminal will look like this.

Add the index.php file before index.html

Once you added Press CTRL+X and then press Y to save the changes you have made.

PHP Modules

PHP is the open source and has the lot of modules to use. To see the available modules use the code

apt-cache search php5-

The modules displayed will be

selvakumar@noisalesteam2:~$ apt-cache search php5-
php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the             php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
php5-dev - Files for PHP5 module development
php5-gd - GD module for php5
php5-gmp - GMP module for php5
php5-json - JSON module for php5
php5-ldap - LDAP module for php5
php5-mysql - MySQL module for php5
php5-odbc - ODBC module for php5
php5-pgsql - PostgreSQL module for php5
php5-pspell - pspell module for php5
php5-readline - Readline module for php5
php5-recode - recode module for php5
php5-snmp - SNMP module for php5
php5-sqlite - SQLite module for php5
php5-tidy - tidy module for php5
php5-xmlrpc - XML-RPC module for php5
php5-xsl - XSL module for php5
bluefish - advanced Gtk+ text editor for web and software development
libphp5-embed - HTML-embedded scripting language (Embedded SAPI library)
libssh2-php - transitional dummy package for php5-ssh2

The modules list will look like the above. Once you decide the module and ready to install use the following command

sudo apt-get install name of the module

Replace the name of the module with the package name.

You can also install multiple modules at command by just leaving the space between the each module name.

PHP on your server

Now the LAMP is installed on your server. Create a new PHP file to see the components online.

To create a new file use the below command

sudo nano /var/www/info.php

Add the following PHP code in the file

<?php
phpinfo();
?>

Now press CTRL+X, the terminal will ask you whether to save the file or not.

Press Y and enter. Now your file will be saved.

Now you have to restart the Apache to update and see the result of all the changes.

Here is the command to restart the apache

sudo service apache2 restart

Wondering what name-based virtual hosting is?

A single server will host multiple domain names sharing the same IP. Here you need only one IP address instead of using individual IP address for each domain.

You have to do two tasks to configure name-based virtual host

Step 1: Map the DNS of the server Install Apache on Ubuntu to redirect the request to the correct IP address.

Step 2: Configure the Apache server to identify the domains with the IP address

Configure Name-based Virtual Host

Step 1: Creating Directory

Before configuring name based virtual host, you have to create the directory for the domains under the var/www/html folder.

Here is the command for that.

The var/www/html will be the default root document for the Apache virtual configuration.

sudo mkdir -p /var/www/example1.com/
sudo mkdir -p /var/www/test1.com/

Here the two domains example1.com and test1.com will come under the directory.

Now you have to create the index file for them.

Step2: Create Index file

Use the following code to create an index file for test1.com

sudo nano /var/www/test1.com/index.html 

Now a file will open and add the following html code to that file

<html>
<head>
    <title>www.test1.com</title>
</head>
<body>
    <h1>Welcome To www.test1.com website</h1>
</body>
</html>

Same as you can create the index file for example1.com. Use the code below to open the file

Sudo nano /var/www/example1.com/index.html 

Now the file will open and input the code and save in that file

<html>
<head>
    <title>www.example1.com</title>
</head>
<body>
    <h1>Welcome To www.example1.com website</h1>
</body>
</html>

Step 3: Change the ownership of directories

Root owns the directories we have created, but the Apache runs on www-data user on Ubuntu.

You have to change the ownership root to www-data so that apache can read and write the data.

To do that you have to follow the code below

sudo chown -R www-data:www-data    /var/www/html/www.example1.com/
sudo chown -R www-data:www-data  /var/www/html/www.test1.com/

Now the ownership has been changed to www-data.

Hereafter, you have to make the apache root directory universally readable so that everyone can read files from that directory.

Use the command below for that

sudo chmod -R 755 /var/www/html

Step 4: Creating Virtual host file

By default, the Apache comes with a virtual host file ****“ 000-default.conf.” **** But you have to disable it and create the new virtual host files for the website.

To disable default virtual host file use this command

sudo a2dissite 000-default.conf

To create virtual host file “www.example1.com.conf” for www.example1.com use the command

sudo nano /etc/apache2/sites-available/www.example1.com.conf

A file will open and add this following code to that file

<VirtualHost *:80>
ServerAdmin admin@example1.com
ServerName  www.example1.com
DocumentRoot /var/www/html/www.example1.com

ErrorLog ${APACHE_LOG_DIR}/www.example1.com_error.log
CustomLog ${APACHE_LOG_DIR}/www.example1.com_access.log    combined
</VirtualHost>

Now create virtual host file “www.test1.com.conf” to test1.com

sudo nano /etc/apache2/sites-available/www.test1.com.conf

Now the file will open. Add the below code save and close

<VirtualHost *:80>
ServerAdmin admin@test1.com
ServerName  www.test1.com
DocumentRoot /var/www/html/www.test1.com

ErrorLog ${APACHE_LOG_DIR}/www.test1.com_error.log
CustomLog ${APACHE_LOG_DIR}/www.test1.com_access.log     combined
</VirtualHost>

Step 5: Enable Virtual Host files

You have created virtual host files for both sites. Now you have to enable both of them. Use the following command for that.

sudo a2ensite www.example1.com.conf
sudo a2ensite www.test1.com.conf

When executing the command, you have to reload apache service. Use the code

sudo service apache2 reload

Finally, you have to restart the apache. The command is very similar to reload

sudo service apache2 restart

You have to add the host entry on the server and local system to resolve the website using its name.

You have to edit the “/etc.website/hosts” file to do this

Add the following lines

IP address www.example1.com
IP address www.test1.com

In the above code, you have to add the IP address of the server.

Save and close the files.

Now open websites on your browser, and it will work great.

Conclusion

Name-based Virtual Hosting is good as it only requires one IP address to deploy multiple domains. All of the domains can share the same resources. It saves cost in both software and hardware wise.

Still got questions about Name Based Virtual Host?

Leave your comment below and let us know.

Selvakumar
Author

Selvakumar

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Comments