How to install LEMP (Linux, Nginx, MySQL, PHP) on Ubuntu 12.xx

What is Nginx ?

Nginx is an open source web server that runs fast and uses less system resources than Apache. When light load, the difference between Apache and Nginx is negligible. However, in heavy load, Nginx can be expanded to fit and operate fast without taking up too much memory resources like Apache.

How to install LEMP on Ubuntu ?

First you need to prepare a new Ubuntu server without any installation, here we use Ubuntu 12.04( 64 bit ). Verify that the hostname and host file are correct.

Command-line operations, using the ZOC Terminal  and Nano Editor for editing text file on server.

If you do not have a nano application installed then use the following command:

– CentOS:

yum install nano

– Ubuntu:

apt-get install nano

To use you need to type in the command:

nano file_name

That file_name is the file name or path to the file.

At the bottom you will find keyboard shortcuts to use when working with nano:

  • Symbol ^ instead of Ctrl
  • Ctrl + O: save the file
  • Ctrl + X: exit editor (may need to press Y / N)
  • Ctrl+W: search

1. Update Apt-Get

sudo apt-get update

2. Install MySQL

sudo apt-get install mysql-server php5-mysql

During installation you will have to install root mysql password

After installing MySQL, activate by command:

sudo mysql_install_db

Complete installation

sudo /usr/bin/mysql_secure_installation

Fill in the root password

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Then follow the steps like reset password, delete anonymous user … This is your setup

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y                                            
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

MySQL installation file path: /etc/my.cnf

3. Install nginx

sudo apt-get install nginx

Run nginx

sudo service nginx start

At this point you can directly access the IP / domain to see if nginx is active or not

4. Install PHP

sudo apt-get install php5-fpm

5. Customize PHP

Customize php.ini again

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

Find the line cgi.fix_pathinfo = 1 (press Ctrl + W in nano), remove the accent; at the beginning and replace 1 = 0


Save (Ctrl + O, Enter) and exit (Ctrl + X).

Custom php5-fpm

sudo nano /etc/php5/fpm/pool.d/www.conf

Find the line listen = and replace with /var/run/php5-fpm.sock.

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

Save and exit.

Restart php-fpm

sudo service php5-fpm restart

6. Custom nginx

Open the virtual host file by default

sudo nano /etc/nginx/sites-available/default

Find and change the settings as shown below. Note that newer versions of Ubuntu use ‘htm’ instead of ‘www’ so you need to adjust the settings accordingly.

server {
        listen   80;

        root /usr/share/nginx/www;
        index index.php index.html index.htm;


        location / {
                try_files $uri $uri/ /index.php?$args;

        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
        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;



Some changes:

  • Add index.php
  • Change server_name to domain
  • Edit the path to suit WP’s permalink
  • Modify the content in the “location ~ \ .php $ {” settings.

Save and exit.

7. Create php info file

Create file

sudo nano /usr/share/nginx/www/info.php

Add content


Save and exit

Resume nginx

sudo service nginx restart

Finished, now you can see the settings of nginx and php-fpm by visiting: http: //youripaddress/info.php

8. Install phpMyAdmin

sudo apt-get install phpmyadmin

When phpmyadmin asks you to select the server (apache or lighttpd), choose which one.

Create symbolic link

sudo ln -s /usr/share/phpmyadmin/ /usr/share/nginx/www

The mcrypt extension is missing when running phpMyAdmin

Open the php.ini file

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

Find the Dynamic Extensions section and add the following line to the end

Restart php5-fpm

sudo service php5-fpm restart

Restart nginx

sudo service nginx restart

You can now access phpMyAdmin via http://your-ip-address/phpmyadmin

Add Comment