Are you looking to cut your server cost and host multiple websites on Ubuntu server with NGINX?
This tutorial is for you.
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, 16.04, 18.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.
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 to Host Multiple Websites 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 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
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.
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 by typing:
sudo vim /etc/hosts
Now update your domain name inside this host file.
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 WordPress. In my case, the root directory of both domains are /var/www/exmaple1.com/
and /var/www/exmaple2.com/
. If you don’t have this 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 like 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.
You can also follow How To Install WordPress On Ubuntu 18.04 LTE – LEMP Stack for the in-depth installation process.
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.
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 a web-based interface.
For more help on installing WordPress on Ubuntu, you can check the official documentation of Ubuntu.
Hope you will find this tutorial on how to host multiple websites on Ubuntu server useful.
If you face any difficulty throughout this process, feel free to ask me and email me at rajesh.rai@websitevidya.com or put your query in the comment section below. I will give my best to resolve it.