Installation LNMP ( Nginx+MySQL+PHP ) on Ubuntu

Enabling SPDY and HSTS on Nginx


$ sudo apt-get update

Installing Nginx

$ sudo apt-get install nginx

After installation, the file structure like this:
All configuration files are in /etc/nginx, and each virtual host has been arranged in the /etc/nginx/sites-available;

Program files in the /usr/sbin/nginx;
Log files in /var/log/nginx;

And has been in the /etc/init.d/nginx create a startup script;
The default virtual host directory is set in the /var/www/nginx-default;

Start nginx (Make sure that no other services in 80 ports used)

$ sudo /etc/init.d/nginx start


$ sudo service nginx start

Test the Nginx configuration file syntax

$ sudo nginx -t

Restart Nginx

$ sudo service nginx restart

Open your browser and visit to http://localhost or, see if prompt Welcome to Nginx!, If the display has already proved successful launch.

Install latest version of Nginx on Ubuntu

In general, you should deploy the Nginx mainline branch at all times. You may wish to use stable if you are concerned about possible impacts of new features, such as incompatibility with third-party modules or the introduction of bugs in new features.

First, to avoid missing PGP key during installation and upgrades, install the Nginx team’s package signing key:

$ sudo curl | sudo apt-key add -

Add the repo to your apt sources:

$ sudo echo -e "deb `lsb_release -cs` nginx\ndeb-src `lsb_release -cs` nginx" > /etc/apt/sources.list.d/nginx.list

Resynchronize the package index files from their sources:

$ sudo apt-get update

If installing Nginx:

$ sudo apt-get install nginx

or if upgrading:

$ sudo apt-get dist-upgrade


Installing PHP

$ sudo apt-get install php5-fpm


  • Configuration PHP

We need to make one small change in the php configuration. Open up /etc/php5/fpm/php.ini, Find the line, cgi.fix_pathinfo=1, and change the 1 to 0.


If this number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative. Save and Exit. We need to make another small change in the php5-fpm configuration.Open up www.conf:

$ sudo vim /etc/php5/fpm/pool.d/www.conf

Find the line, listen =, and change the to /var/run/php5-fpm.sock.

listen = /var/run/php5-fpm.sock

Save and Exit.

Restart php-fpm:

$ sudo service php5-fpm restart
  • Configuration Nginx

Add the following line to your nginx fastcgi config file /etc/nginx/fastcgi_params:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  • Open up the default virtual host file
$ sudo vim /etc/nginx/sites-available/default

The configuration should include the changes below (the details of the changes are under the config information):

server {
        listen   80;
        root /usr/share/nginx/www;
        index index.php index.html index.htm;


        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;

        # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;


Here are the details of the changes:

Add index.php to the index line.
Change the server_name from local host to your domain name or IP address (replace the in the configuration)
Change the correct lines in location ~ \.php$ { section
Save and Exit

Installing MySQL

$ sudo apt-get install mysql-server mysql-client

Create a PHP Info Page
In /var/www/ directory to create a file info.php, written the following code in the info.php file, save.

<?php phpinfo();

If you see the nginx and php-fpm configuration details by visiting http://localhost/info.php, LNMP (Nginx, MySQL, PHP) has been built successfully.

See more Enabling SPDY and HSTS on Nginx

Installation LAMP ( Apache+MySQL+PHP ) on Ubuntu

Installation LNMP ( Nginx+MySQL+PHP ) on Ubuntu
2 votes, 5.00 avg. rating (98% score)