How To Setup a NGINX Virtual Host

Nginx-Logo

In this tutorial we will show you how to Setup a 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 Nginx web server. This guide assumes that you’ve been following along from the previous tutorial: How to Install and Configure a NGINX Server.

Setup a NGINX Virtual Host

Create a New Directory

cd /var/www
mkdir -p idroot.org/{public_html,logs,stats}
mkdir -p idroot.us/{public_html,logs,stats}
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 servers ip address, if not you will not able able to check if your new configuration works or not.

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

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 a best deal!

Tags: