How To Install LEMP (Linux, Nginx, MySQL, PHP) On Ubuntu 14.04, Ubuntu 16.04

About Nginx

Nginx is an open source web server that runs fast and uses fewer system resources than Apache. In 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.

Previous blogs ran on the LAMP platform (Linux, Apache, MySQL, PHP), but since switching to LEMP, they’ve seen a lot fewer resources than before.

Install Lemp On Ubuntu

Command-line operations, using the ZOC Terminal tool or Putty and the nano tool.

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 the command:

sudo mysql_install_db

Complete installation

sudo /usr/bin/mysql_secure_installation

Enter the root password

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

Then the actions such as 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] and
 ... 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

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

cgi.fix_pathinfo=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 = 127.0.0.1:9000 and replace 127.0.0.1:9000 with /var/run/php5-fpm.sock.

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

Save and exit.

Restart php-fpm

sudo service php5-fpm restart

6. Customize Nginx

Open the default virtual host file

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

Find and change the settings as shown below. Note that newer Ubuntu versions 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;

        server_name example.com;

        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 127.0.0.1:9000
        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 the permalink of the WP
  • Resize the content in the “location ~ \ .php $ {“

Save and exit.

7. Create PHP info file

Create php.info file

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

Add content

<?php
phpinfo();
?>

Save and exit

Resume Nginx

sudo service nginx restart

Finished, you can now view the Nginx and PHP-fpm settings 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), select any one.

Create symbolic link

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

Fix the issue with The mcrypt extension is missing when you run 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

extension=mcrypt.so

Restart php5-fpm

sudo service php5-fpm restart

Restart Nginx

sudo service nginx restart

How To Install LEMP (Linux, Nginx, MySQL, PHP) On Ubuntu 14.04, Ubuntu 16.04

Rate this post



Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.