How To Host Multiple Websites with NGINX On Ubuntu

If a server has enough resources, then there is no point to waste those resources by only hosting a single website. This guide is tested on Ubuntu 14.04 LTE. The installation and configuration would be very much similar to other Linux OS or OS versions. The commands used during this guide might vary for different OS and OS versions.

If we want to host multiple websites on a single server then we have features called Virtual Host on Apache2 and Server Blocks on NGINX.

For LEMP Installation Read: How To Install Linux, NGINX, MySQL, PHP (LEMP) stack on Ubuntu

Before we start, I would like you to inform you that I have used WordPress as an example in this tutorial. In case you want to deploy another website apart from PHP or WordPress, check the installation guide or documentation for that particular language or framework on their website. Once you get the idea, you can use this tutorial. The steps for hosting multiple websites would be the same.

Steps Required to Host Multiple Websites with Nginx on Ubuntu

1. Prerequisite

First of all, you need to sign on to Ubuntu and update your system. In order to do that run the commands below.

sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove
dist-upgrade

In addition to performing an upgrade, it also handles dependencies to be handled with new versions of packages.

Once you update, reboot your Ubuntu Server and install NGINX Web Server.

sudo apt-get install nginx

The above command installs the stable version of the NGINX.

2. Deploy PHP with FastCGI

If you are going to deploy a PHP based website like WordPress, you need to deploy PHP with FastCGI. For simple HTML based website, you can skip this step and go to the next step. So, in order to deploy PHP applications, you need to implement the “PHP-FastCGI”. This is important as it will allow NGINX to handle and serve the web applications and websites based on PHP.

sudo apt-get install php5-cli php5-cgi php5-fpm

Now create a new configuration file for each of your website. Let’s say we are deploying two websites example1.com and example2.com then how it goes the configuration for each website:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example1.com
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example2.com

Now open each configuration file by running the following command and adjust them as per your need.

For example1.com

sudo vim /etc/nginx/sites-available/example1.com

This is how your code should look like:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/example1.com/public_html;
index index.php;
# Make site accessible from http://localhost/
server_name example1.com www.example1.com *.example1.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

For example2.com

sudo vim /etc/nginx/sites-available/example2.com

This is how your code should look like:

server {
listen 80;
listen [::]:80;
root /var/www/example2.com/public_html;
index index.php;
# Make site accessible from http://localhost/
server_name example2.com www.example2.com *.example2.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

You can keep the exact copy of the above content as well. But do not forget to update the Website Directory and Server Name(server_name.

Next, we need to link each of the configuration files to the sites-enabled directory. To do this you need to run the following command:

For example1.com

sudo ln -s /etc/nginx/sites-available/example1.com /etc/nginx/sites-enabled/

For example2.com

sudo ln -s /etc/nginx/sites-available/example2.com /etc/nginx/sites-enabled/

Once you copy each configuration files into a sites-enabled directory, remove the default configuration file from the sites-enabled directory by running the following command.

sudo rm -rf /etc/nginx/sites-enabled/default

Finally, restart the NGINX service

sudo service nginx restart

3. Changes Inside HOST file

Open the host file inside the etc directory and update your domain name. It should look like something this:

hostname

 

4. Create Database

Enter the MySQL command line:

mysql -u root -p

Now Create the databases for each of your websites:

create database Your-Database-Name1;
create database Your-Database-Name2;

Now, grant user privileges for the newly-created databases, replacing user and password with the username and password you wish to use:

grant all on YourDatabaseName1.* to 'user' identified by 'password';
grant all on YourDatabaseName2.* to 'user' identified by 'password';

Now exit MySQL:

quit

5. Install WordPress

Create an src directory inside each of your website’s directory and download the latest copy of the WordPress. In my case, the root directory of my both domains are /var/www/exmaple1.com/ and /var/www/exmaple2.com/. If you don’t have these directory, create separate directories for all of your websites.

sudo mkdir /var/www/example1.com/src/
sudo mkdir /var/www/example2.com/src/

Now, navigate inside the src directory.

cd /var/www/example1.com/src/

Now set the owner of the new directory to be your web server’s user. In this instance, our web server is NGINX:

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

Now you need to download the latest version of WordPress and expand it:

sudo wget http://wordpress.org/latest.tar.gz
sudo -u www-data tar -xvf latest.tar.gz

Now you need to move latest.tar.gz by renaming it as WordPress, followed by the date to store the original condition of the source files:

sudo mv latest.tar.gz wordpress-`date "+%Y-%m-%d"`.tar.gz

Copy the WordPress files to your public_html folder and remove the empty WordPress folder in the src directory:

sudo cp -R wordpress/* ../public_html/
sudo rm -rf wordpress/

Similarly, repeat the same steps for example2.com website as well.

6. Configure WordPress

Method 1:

Now, visit your domain in the browser and follow along with the configuration process by entering Database Name, User Name, and Password. Submit the information, and you are good to go.

Method 2:

Navigate to your website directory /var/www/example.com/public_html/ and look for the wp-config-sample.php file and make a duplicate copy with the name wp-config.php inside the same directory.

sudo cp wp-config-sample.php wp-config.php

Now edit the wp-config.php file using your favourite editor and enter the database information.

WordPress MySQL Settings

 

sudo vim wp-config.php

Once you enter the database details, exit the edit mode by pressing ESC and then press, :wq! hit enter to save and exit.

Similarly enter the database details for example2.com website as well.

Now when you visit your website by entering the domain name into a browser, you see a recent default WordPress theme based website. Theme and pages can be changed later by entering into the WordPress admin panel.

Sometimes you will be asked to enter the FTP information to update WordPress, install new themes or plugins. Which is very frustrating, especially if you are new to WordPress. To get rid of this issue, configure your wp-config.php file and add FS_METHOD:

sudo vim /var/www/example.com/public_html/wp-config.php
/** Bypass FTP */
define('FS_METHOD', 'direct');

Now give your WordPress permission to add and edit files inside the public_html folder:

sudo chown -R www-data:www-data /var/www/example1.com/public_html
sudo chown -R www-data:www-data /var/www/example2.com/public_html

Congratulations! You have now successfully installed multiple websites on a single server. You can repeat the same steps if you want to add more websites.

Now you will be able to login into your new WordPress based website. Next, you can configure your website using web-based interface.

Hope you will find this article useful. If you face any difficulty throughout this process, feel free to ask me and email me at [email protected] or put your query in the comment section below. I will give my best to resolve it.

How to Host Multiple Websites with Nginx On Ubuntu

In this tutorial, I am going to show you How to host multiple websites with NGINX on Ubuntu. If a server has enough resources, then there is no point to

Send this to a friend