How to setup nginx (with directory structure, virtual hosts, port forwarding), mysql, php, fastcgi

In this article you will learn how to setup NGINX along with is directory structure. You’ll also learn how to setup virtual hosts for NGINX, port forwarding also known as reverse proxy and make NGINX with PHP-FastCGI, PHP and MySQL.


This is also known is LEMP, L=Linux, E=Enginex (NGINX), M=MySQL and P=PHP. Sometime people also refer this as LNMP stack.


LEMP Stack has Linux installed along with NGINX web server with PHP and MySQL as the database fully configured and ready to run. Since we have already installed this under Ubuntu 12.04 LTS our step for installing Linux is not needed.


Note: We will be using Ubuntu 12.04 LTS to install our LEMP / LNMP stack. The instructions below work under Ubuntu 10.04 LTS, Ubuntu 13, Debian 6 and Debian 7.


We assume that you have already up and running Ubuntu based machine up and running with your hostname and network configure.


If you do not, or you don’t know how to set your hostname for Linux or Ubuntu, please refer to one of our previous articles on how to set your hostname under Ubuntu or Linux.


First update your repositories and upgrade any existing packages that may have installed.

sudo apt-get update
sudo apt-get upgrade

When all the repositories and upgrade process is completed, you can now install MySQL server and client.


sudo apt-get install mysql-server mysql-client


During the installation of MySQL it will ask you for the root password. Please enter the root password for the root user of the MySQL server and remember it as you may need it later when creating new databases, users etc.


Now, our MySQL server is up and running and you can install NGINX web server.


sudo apt-get install nginx


When you get “Completed” message it means NGINX is installed. You need to start the service for NGINX as by default it does not start.

You can start service for NGINX by using below command.

sudo /etc/init.d/nginx start
sudo service nginx start


When the service is started you can find your ip address or if you have domain configured and DNS is pointed to the machine you are installing NGINX on, you can try opening it to see the test default page.


In our case, we find our ip address using below command

$ sudo ifconfig


We get result “”


Open up any web browser and point URL to and you should get a default test page of NGINX. This means NGINX is installed and working properly.


Now, we are going to install PHP-FPM/FastCGI on NGINX; we will configure PHP-FPM/FastCGI later with NGINX.


sudo apt-get install php5-fpm php5-mysql


We need to make a very small change now for possible security risk to process the exact file defined.


Open php.ini in /etc/php5/fpm directory.


sudo vim /etc/php5/fpm/php.ini


Find and change the below line






Save and exit. Then, restart the PHP5-FPM service.

sudo /etc/init.d/php5-fpm restart

sudo service php5-fpm restart


Now it’s time to make NGINX understand that PHP5 with FPM/FastCGI is installed so that it uses it to serve our PHP scripts.


We edit the file called “default”. This is the default virtual host file located under /etc/nginx/sites-available. This directory has all the virtual hosts in it. If you want other websites to run under same ip which is also known as shared hosting or shared ip hosting you need create files in this directory.

We will cover on how to create virtual hosts for NGINX later.

The following is the content of the default virtual host and we need to edit as follows: please add the files which you don’t find in your default virtual host file. You can also use below and it will work fine.

Please read the comments as we have explained things in comments.


server {

        listen   80; # port 80 default


        root /usr/share/nginx/www; # default directory where the files will be stored and served from

        index index.php index.html index.htm; # index defined to be served under directory


        server_name; #name of the virtual host or domain


        location / {

                try_files $uri $uri/ /index.html;



        error_page 404 /404.html;


        error_page 500 502 503 504 /50x.html;

        location = /50x.html {

              root /usr/share/nginx/www;



        # Serve PHP scripts to FastCGI server our php-fpm server listening on

        location ~ \.php$ {


                # With php5-fpm:

                fastcgi_pass unix:/var/run/php5-fpm.sock;

                fastcgi_index index.php;

                include fastcgi_params;




Default location of the nginx DocumentRoot is /usr/share/nginx/www


We now create a file called “phpinfo.php” to test if PHP is working.


$ sudo vim /usr/share/nginx/www/phpinfo.php


Content for above file:




Now, save the file, exit and restart the NGINX web server for the changes to take effect.


sudo /etc/init.d/nginx restart


sudo service nginx restart


You will be able to see the PHP info page along with FPM/FastCGI as your handler. You should now have a fully configured LEMP / LNMP stack along with PHP-FPM/FastCGI and MySQL


Configure virtual host in NGINX


Now, you can configure virtual host in NGINX.

We are going to use “” domain as our virtual host from now on.


For example, we would like to use /var/www directory as the directory for all virtual hosts.

First, we create all parent and child directories.


$ sudo mkdir -p /var/www/


If you are using an actual domain you will need the DNS to point to the IP of the server. If you just want to test we will describe how to check if virtual hosting is working.

Grant permission and ownership to the newly created directory.

sudo chown -R www:www /var/www/

sudo chmod -R 755 /var/www


Within the directory “/var/www/” create a file called “index.html” and put some text in it to see if things are working.





Welcome to our first Virtual Host in NGINX –





If you have pointed your domain name you have used above to the server in DNS you can skip this step.

vim /etc/hosts


Edit the file called “/etc/hosts”

Below is the sample file if you want to use it you can use it as well.

# Host Database


# localhost is used to configure the loopback interface

# when the system is booting.  Do not change this entry.

##       localhost.localdomain


#Virtual Hosts


You can see we have pointed domain to our webserver ip.

This file is also called “Local DNS resolver”. Default behavior of Linux/ UNIX is to check this file first then move onwards to the DNS server.

Virtual host for NGINX continued…

Now, let’s come back to virtual host for NGINX. Copy the existing configuration file that comes with NGINX so that you can make changes into the file and you are good to go for your new virtual host.

$ sudo cd /etc/nginx/sites-enabled

$ sudo cp default


Now edit the file you just recently copied.

$ sudo vim /etc/nginx/sites-enabled/


Change only below lines to set your virtual host.

                listen   80; # port 80 default


        root /var/www/; # default directory where the files will be stored and served from


        index index.php index.html index.htm; # index defined to be served under directory


        server_name; #name of the virtual host or domain



If you get server_name conflicts you may delete “default” virtual host file.

Finally, restart your NGINX server for the above changes to take effect.

sudo /etc/init.d/nginx restart


View from the browser and see the index.html page that we set up. You may also create a “phpinfo.php” to test if PHP is working.

To create further virtual host just repeat above process from Virtual Host and you can add as many Virtual Hosts as you want.

This article has been provided by Codero Hosting, the leading provider of reliable dedicated, managed and cloud hosting services. Need more information on this topic or to learn more about Codero’s hosting services please visit 
www.codero.comchat with us online or give us a call at 866-2-CODERO.

Posted in
Last update:
2015-12-03 20:39
Average rating:0 (0 Votes)