Host Multiple Domains on One Server/IP using Nginx

If you are on dedicated or VPS like DigitalOcean or any other server and planning to host multiple websites on a single server then here is how you can do it. Aroham Technologies can support you to set up multiple websites on Nginx

Host Multiple Domains on One Server/IP using Nginx
Want to run multiple websites on Nginx Server. It’s actually pretty straightforward, just create a new directory for each site and set up accordingly. let’s take an example of setup multiple WordPress sites This assumes you have already mapped DNS to your host. Before you implement this, to give you an idea of how it works. The concept to have multiple websites on a single instance is called Virtual Server. Virtual server configuration is defined within web server configuration and based on server/IP address, a request is getting forwarded to respective Document Root. If you still not process LAMP setup you can follow that link: LEMP: How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 16.04 Nginx server block is a feature similar to Apache2. These features allow users to host multiple websites on a single host with single IP with multiple domains. Using Nginx, one can save on the cost of additional servers or resources. When you’re ready, follow the steps below to get this working.
Create WordPress Databases And Users
Now if you setup LEMP properly, Now we need to create databases for as many sites we have we take the example of two websites. If you have more then two you can create more than 2 databases. Run the commands below to sign onto MySQL server using the password previous tutorial.
$ sudo mysql -u root -p
Then run the commands below to create a new database called arohamwp1 and arohamwp2 for WordPress site #1 & #2
mysql > CREATE DATABASE arohamwp1;
mysql > CREATE DATABASE arohamwp2;
Next, we need to create new database users called “arohamdb” and grant him access to both database, you can create different user as well
//Create User
mysql > CREATE USER 'arohamdb'@'localhost' IDENTIFIED BY '__PASSWORD__';

//Grant Access
mysql > GRANT ALL PRIVILEGES ON arohamwp1.* TO 'arohamdb'@'localhost';
mysql > GRANT ALL PRIVILEGES ON arohamwp2.* TO 'arohamdb'@'localhost';

//Flush Privileges & exit
mysql > FLUSH PRIVILEGES;
mysql > exit;
Create server blocks as many required
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 required. So, let’s create two WordPress websites called arohamexample.com and arohamexample.in from the default config file by running the commands below.
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/arohamexample.com
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/arohamexample.in
After creating the server blocks or site configuration files, open each file and make the highlighted file to match each domain.
sudo nano /etc/nginx/sites-available/arohamexample.com
Make below changes and save.
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html/arohamexample.com;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name arohamexample.com www.arohamexample.com;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}
Do the same for arohamexample.in and replace all arohamexample.com references with arohamexample.in and save the file. Run the commands below to edit arohamexample.in file.
sudo nano /etc/nginx/sites-available/arohamexample.in
Then change arohamexample.com references with arohamexample.in and save. When you’re done, run the commands below to enable both server blocks and delete the default configuration.
sudo ln -s /etc/nginx/sites-available/arohamexample.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/arohamexample.in /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-available/default
Create Multiple DocRoot
In each of the server block, there’s a root location. This is where each website content should be stored and that need to be different for all websites. Now we need to defined the root locations for all our site, go and create them below.
sudo mkdir -p /var/www/html/arohamexample.com
sudo mkdir -p /var/www/html/arohamexample.in
Setup WordPress
Now download WordPress extract into the root directory for each site. To do that run the commands below
cd /tmp/ && wget http://wordpress.org/latest.tar.gz

//extract the downloaded file.
tar -xzvf latest.tar.gz

//Copy to each root folder for each site

sudo cp -R wordpress/* /var/www/html/arohamexample.com
sudo cp -R wordpress/* /var/www/html/arohamexample.in
Next, we need 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
Next, just restart Nginx web server
sudo nginx -t
sudo systemctl restart nginx
After restarting Nginx with no errors, you should be able to access your sites through domain names and see WordPress default setup page.

Now is the time you start getting more online