How To Setup a NGINX Virtual Host

Setup a NGINX Virtual Host

In this tutorial, we will show you how to setup an NGINX Virtual Host. Virtual hosts such as Nginx are used for running two or more domains or websites using just one server which you can learn more about in this hosting fundamentals course. Here’s a brief tutorial that shows you how to create a virtual host or server block on the Nginx web server.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step setup of Nginx vhost on your Linux server.

Setup an NGINX Virtual Host

First, we create a New Directory

cd /var/www
mkdir -p idroot.org/{public_html,logs,stats}
mkdir -p idroot.us/{public_html,logs,stats}
Next, create vhost conf file:
#nano /etc/nginx/conf.d/idroot.us.conf

server {
   listen  80;
   server_name  idroot.org www.idroot.org;
 
   access_log  /var/www/idroot.org/logs/access.log ;
   error_log    /var/www/idroot.org/logs/error.log ;
 
   location / {
       root   /var/www/idroot.org/public_html;
       index  index.php index.html index.htm;
 
   }
 
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   /var/www/idroot.org/public_html;
   }
 
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ .php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
root    /var/www/idroot.org/public_html;
fastcgi_param  SCRIPT_FILENAME  /var/www/idroot.org/public_html$fastcgi_script_name;
include fastcgi_params;
}
 
 
   location ~ /.ht {
       deny  all;
   }
}
# nano /etc/nginx/conf.d/idroot.us.conf
server {
   listen  80;
   server_name  idroot.us www.idroot.us;
 
   access_log  /var/www/idroot.us/logs/access.log ;
   error_log    /var/www/idroot.us/logs/error.log ;
 
   location / {
       root   /var/www/idroot.us/public_html;
       index  index.php index.html index.htm;
 
   }
 
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   /var/www/idroot.us/public_html;
   }
 
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ .php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
root    /var/www/idroot.us/public_html;
fastcgi_param  SCRIPT_FILENAME  /var/www/idroot.us/public_html$fastcgi_script_name;
include fastcgi_params;
}
 
   location ~ /.ht {
       deny  all;
   }
}

Add vhost on nginx.conf

# nano /etc/nginx/nginx.conf
### add line like this on http section:
include /etc/nginx/conf.d/*.conf;

Restarting Nginx and PHP-fpm

# /etc/init.d/nginx restart
# /etc/init.d php-fpm restart

Note: Please make sure that all the domain names are propagated and are properly directed to your server’s IP address, if not you will not able able to check if your new configuration works or not.

Congratulations! You have successfully installed server blocks Nginx. Thanks for using this tutorial for installing Virtual hosts Nginx on the Linux system. For additional help or useful information, we recommend you check the official Nginx website.

Nginx Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “Nginx Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

Tags: