How to Setup LEMP (Linux, NGINX, MySQL, PHP) on Ubuntu 14

Try it in our public cloud & Get $50 Credit
CLAIM NOW

The LEMP stack is a variant on the well-known LAMP stack, which stands for Linux, Apache, MySQL and PHP. With LEMP, Nginx (pronounced Engine-X) replaces Apache as the server component. Nginx is a lightweight, minimalist proxy server that routinely beats Apache in speed tests.

Nginx has some advantages, but also some disadvantages when compared with Apache. Be sure to do your research into both to understand if Nginx is right for your particular situation.

Getting started

Confirm that you have the following before you follow this guide:
• 1 Node (Cloud Server or Dedicated Server) running Ubuntu 14.
• Root access to the node.

Tutorial

These instructions will serve as a useful guide on how to setup a LEMP server. We’ll be installing Nginx, PHP-FPM and MySQL on Ubuntu 14.04.

First, make sure that your server is fully up to date and reboot at the end of the update.

apt-get update && apt-get upgrade -y
reboot

Now, we’ll install MySQL as the first component of our LEMP stack.

apt-get -y install mysql-server mysql-client

During the installation, you will be asked to setup your MySQL root password. Make sure to remember this for later.

After the installation is complete, the next step is to start and secure the database server.

service mysql start
mysql_secure_installation

Be sure to enable the daemon to autostart on server boot.

update-rc.d mysql default

The Nginx installation will follow. To install Nginx, type the following command:

apt-get install nginx -y

After that’s done, you can start the nginx daemon.

service nginx start
update-rc.d nginx default

Finally, we’ll install the packages for PHP-FPM.

apt-get install php5-fpm php5-mysql

Now to configure Nginx. We need to open the default file of virtualhosts in a text editor. Here’s the modifications that you should make to this file.

nano /etc/nginx/sites-available/default

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm; # Make site accessible from http://localhost/
server_name 173.209.44.190;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht { deny all; }
}
[...]

Save the file and exit.

Next, reload nginx so that the daemon recognizes the changes that you made.

service nginx reload

Again in your text editor, open the file php.ini. We’ll only be modifying one line.

nano /etc/php5/fpm/php.ini

You’ll need to find the following block:

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
; cgi.fix_pathinfo=1

And change the last line to:

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

Save and close the file.

Now, we’ll reload PHP-FPM so that the changes you made to php.ini goes through. As with the nginx daemon, you’ll need to enable the php daemon to start on boot.

service php5-fpm reload
update-rc.d php5-fpm default

The last step of our installation is to create a phpinfo page in the Document root directory.

cd /usr/share/nginx/html/
nano info.php

<?php
phpinfo();
?>

Access your phpinfo web page to see how it looks at this URL:
http://your_main_IP