Shared Hosting on your Web Server: Multiple virtual hosts


This article tends to help you to set up multiple websites (or virtual hosts) on single web server running on Linux.

It’s particularly useful if you don’t want to spend money on multiple Virtual Private Servers (VPS), but you’d like to have ability to run and build multiple websites with minimum cost on the same server. In real world scenario, most of those sites would be based on common content management systems such as WordPress, Drupal, Joomla, etc.

The idea lies in the fact that instead of having single root directory for your website (which is /var/www/html/ in our example), you can have multiple folders located in /var/www/ where each folder will contain separate website.

What would you need to have to make it work:

  • web server which already has full LAMP stack on it. As example in this article, we use Ubuntu 14 with MySQL and Apache2 as web server.
  • root access to this web server
  • root access to MySQL database on this server

What we actually would do? When you want to add new website into your server, there are following three steps to consider:

  • pointing your domain into that folder
  • creating root folder for the website and setting up web server to recognize that folder
  • creating database and database user which would manage your WordPress or Drupal installation

Note, it doesn’t matter in what sequence you are following these three steps, as long as they all completed. And once all these steps completed, you are good to go with installation of your CMS as per its documentation.

All actions shown as performed under root account. Remember, it’s best practice to use regular account with sudo rather than root to perform any changes in your system.

Step 1: Point the domain

Point your web traffic of your  domain on to your server as per requirements of  your domain provider. Essentially it means you would need to create A record which resolves into your web server’s IP.

Step 2: Adding virtual host into web server’s configuration.

Let’s assume we want to build  website called  and related configuration files and folders in our server will be named accordingly as cutepuppies

Make separate root directory for your new website:

mkdir  /var/www/cutepuppies


Give ownership of the directory to the Apache web user (which is www-data)

chown www-data:www-data -R /var/www/cutepuppies


Also, if you action not under root account, add your username to the web group:

usermod -aG www-data YOUR_USER_NAME

Go to your web server configuration folder  /etc/apache2/sites-available

Make a copy of default configuration file 000-default.conf and name it cutepuppies.conf

cp 000-default.conf cutepuppies.conf


Open your new configuration file and change following parameters as per below:

Server Alias
DocumentRoot /var/www/cutepuppies

<Directory /var/www/cutepuppies>
    DirectoryIndex index.php
    AllowOverride All
    Order allow,deny
    Allow from all


Once your configuration file is ready, enable it with this command:

a2ensite cutepuppies.conf


Now to make it work after changes, you need to reload configuration of your web server:

service apache2 reload


Step 3: Create MySQL database and database user

Let’s assume we’ve defined the values for new database as following:

Database name: dbcutepups

Database user: dbuser

DB user’s password: password123


Log into MySQL using your MySQL root credentials:

mysql -u root -p


Provide password when prompted. After successful login, instead of BASH invitation you will see the one looking like this:



What we have logged into MySQL for is to create database, database user and make that user work with this database. It’s simply achieved by the following commands:

mysql> CREATE DATABASE dbcutepups;
mysql> CREATE USER dbuser@localhost;
mysql> SET PASSWORD FOR dbuser@localhost= PASSWORD("password123");
mysql> GRANT ALL PRIVILEGES ON dbcutepups.* TO dbuser@localhost IDENTIFIED BY 'password123';

Then, you can exit from MySQL.

Now you are ready to unpack your CMS into root directory /var/www/cutepuppies/ and to run installation. When installing CMS, you would need to use MySQL credentials that you’ve just created.

Each time you want to add new website into your server, follow described instructions again. If everything is working properly, all your websites located on the server should be accesible. The other thing to worry about after that will be your server’s security and performance what’s beyond of subject of this article.

If you have anything to add or clarify, please feel free to comment and contibute.