How To Setup a NGINX Virtual Host – Easy Guide

In this tutorial we are going to learn how to install and configuration of virtual hosts “Server Blocks” Nginx on your Linux server. 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 wpcademy.com/{public_html,logs,stats}
mkdir -p wpcademy.com/{public_html,logs,stats}
Create vhost conf file
#nano /etc/nginx/conf.d/wpcademy.com.conf

server {
   listen  80;
   server_name  wpcademy.com www.wpcademy.com;
 
   access_log  /var/www/wpcademy.com/logs/access.log ;
   error_log    /var/www/wpcademy.com/logs/error.log ;
 
   location / {
       root   /var/www/wpcademy.com/public_html;
       index  index.php index.html index.htm;
 
   }
 
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   /var/www/wpcademy.com/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/wpcademy.com/public_html;
fastcgi_param  SCRIPT_FILENAME  /var/www/wpcademy.com/public_html$fastcgi_script_name;
include fastcgi_params;
}
 
 
   location ~ /.ht {
       deny  all;
   }
}
# nano /etc/nginx/conf.d/wpcademy.com.conf
server {
   listen  80;
   server_name  wpcademy.com www.wpcademy.com;
 
   access_log  /var/www/wpcademy.com/logs/access.log ;
   error_log    /var/www/wpcademy.com/logs/error.log ;
 
   location / {
       root   /var/www/wpcademy.com/public_html;
       index  index.php index.html index.htm;
 
   }
 
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   /var/www/wpcademy.com/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/wpcademy.com/public_html;
fastcgi_param  SCRIPT_FILENAME  /var/www/wpcademy.com/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

Leave a Reply