How To Set Up Nginx Server Blocks for Multiple Sites

By | August 28, 2018

How To Set Up Nginx Server Blocks for Multiple Sites

Set Up Nginx Server Blocks : Hey!! Want to run multiple websites on a single Nginx web server ? Just like our previous tutorial that shows you how to host multiple websites on Apache2, this brief tutorial will show you how to host multiple sites on nginx server

Nginx server block is a feature similar to Apache2 virtualhost. This features allow users to host multiple independent websites on a single host computer with single IP But separate domain names.

Using server blocks or virtualhosts, one can save on cost of additional servers and resources.

Let’s, follow the steps below to implement Setup Multiple Sites on Nginx Server.

STEP 1: SETUP UBUNTU LINUX

We’re using Ubuntu Linux because it’s easiest to use and manage, especially for new users. Other Linux server will let you do the same, but I found Ubuntu to be much more user friendly then other Linux distributions.

So, setup Ubuntu server and run the commands below to update it.

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

STEP 2: INSTALL NGINX WEB SERVER

A site requires a web server to function. There are many web servers such as Apache , light speed etc that will work, however, since this post is about Nginx, So we’re going to be installing Nginx. To do that, run the commands below.

sudo apt-get install nginx

STEP 3: INSTALL MYSQL DATABASE SERVER

After installing Nginx web server, run the commands below to install MySQL database server. To Store Data For your Dynamic Site. To install MySQL, run the commands below

sudo apt-get install mysql-server mysql-client

During the installation of MySQL server, you’ll be prompted to create a new password and confirm it for MySQL root user. So just Simply set a Password.

 

STEP 4: INSTALL PHP AND OTHER PHP MODULES

After installing MySQL server, run the commands below to install PHP and other PHP based modules. To do that just pass the command below to install them

sudo apt-get install php php-mysql php-fpm php-curl php-gd php-pear php-imagick php-imap php-mcrypt php-recode php-tidy php-xmlrpc

Now comes the task to SETUP MULTIPLE SITES ON NGINX SERVER

STEP 5: CREATE AS MANY SERVER BLOCKS AS POSSIBLE

Server blocks are just individual site configuration file. Each site will have its own server block file. So the trick is to copy the default server block config and create as many as blocks from it. So, let’s create two Sites called example.com and example.net from the default config file by running the commands below.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.net

After copying , Now after creating the server blocks or site configuration files, open each file and make the highlighted file to match each domain.

edit server config file for site.

sudo nano /etc/nginx/sites-available/example.com

 

Now Just make below changes and save.

 

# Default server configuration
#
server {
        listen 80;
        listen [::]:80;  

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;
         root /var/www/html/example.com; 
        # Add index.php to the list if you are using PHP
        index  index.php  index.html index.htm index.nginx-debian.html;
        server_name example.com www.example.com;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf; 
        #
        #       # With php7.0-cgi alone:
        #       # fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

Do the same for example.net and replace all example.com references with example.net and save the file.

You can run the commands below to edit example.net file.

sudo nano /etc/nginx/sites-available/example.net

Then simply just make the  changes to  example.com references with example.net and save it.

When you’re done, run the commands below to enable both server blocks and delete the default configuration file.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-available/default

 

STEP 6: CREATE MULTIPLE DOCUMENT ROOT DIRECTORIES

In each of the server block or site configuration file, there’s a root location. This is where each website content should be stored.. and should be different for all websites. Now that we’ve defined the root locations for all our site, go and create them below

sudo mkdir -p /var/www/html/example.com sudo mkdir -p /var/www/html/example.net

Next, run the commands below to change the directory permissions so WordPress can function properly.

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

After that, restart Nginx web server

sudo nginx -t
sudo systemctl restart nginx

After restarting Nginx with no errors, you would be able to access your sites using the domain names specified by you and you can upload your site at /var/www/html/example.com.

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *