How To Install (LEMP), Linux, nginx, MySQL, PHP stack on Ubuntu 15.10

By | November 21, 2015

LEMP stacks are an open source platform for web applications consisting of Linux, Nginx, MySQL or MariaDB, and PHP or another language such as Python.

L – Linux
E – Nginx (Pronounced Engine-X)
M – MySQL
P – PHP

Remember the best way to learn any new technical skill, is to play around and see how things work. Get comfortable you’re few steps in right direction. Click HERE to Order Your VPS NOW! so that you can gain the practical experience needed.

Now 50% OFF! VPS hosting solutions optimized for you: Click HERE to Order NOW!

Step 1 Log in to your Linux server with the root user. This has taken care of the ‘L’ of LEMP.

Step 2 Next we will update all the softwares to the latest versions

sibanda@gs2:~# sudo apt-get update

Step 3 Install MySQL on the server with the following command :

sibanda@gs2:~# sudo apt-get install mysql-server php5-mysql

You would be prompted to enter a password for your MySQL.

Once the installation is complete activate it using the following command :

sibanda@gs2:~# sudo mysql_install_db

Next run the MySQL set up script :

sibanda@gs2:~# sudo /usr/bin/mysql_secure_installation

You will be prompted to enter your password. Then you will be asked to change the root password, type N and move on. For the rest of the options chose ‘Y’.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...
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...

Step 4 Install PHP

sibanda@gs2:~# sudo apt-get install php5-fpm

Open www.conf file

sibanda@gs2:~# sudo vi /etc/php5/fpm/pool.d/www.conf

Find the line

;listen = /var/run/php5-fpm.sock
; listen = 127.0.0.1:9000

and change it to

listen = /var/run/php5-fpm.sock
Save the file and exit and restart php-fpm :

sibanda@gs2:~# sudo service php5-fpm restart

Step 5 Install nginx

sibanda@gs2:~# echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/nginx-stable.list
sibanda@gs2:~# sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
sibanda@gs2:~# sudo apt-get update && sudo apt-get install nginx

You need to start nginx by typing :

sibanda@gs2:~# sudo service nginx start

Step 6 Copy the default config file for Nginx

sibanda@gs2:~# sudo cp -rf /etc/nginx/sites-available/default /etc/nginx/sites-available/magwinya.co.za

I normally delete the default nginx server block to avoid “conflicting server name” error.

sibanda@gs2:~# sudo rm -rf /etc/nginx/sites-available/default

Step 7 Open the copied config file for Nginx

sibanda@gs2:~# sudo vi /etc/nginx/sites-available/magwinya.co.za

The Server block for the config file should have the following settings

# You may add here your
# server {
#       ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Server Block Configuration for www.magwinya.co.za
server {
        listen 80;
#       listen [::]:80 default_server ipv6only=on;

        root /home/mwired/public_html;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name magwinya.co.za www.magwinya.co.za;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?q=$uri&$args;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                allow ::1;
                deny all;
        }

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;
        #}

        error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht/ {
                deny all;
        }
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#       listen 8000;
#       listen somename:8080;
#       server_name somename alias another.alias;
#       root html;
#       index index.html index.htm;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}


# HTTPS server
#
#server {
#       listen 443;
#       server_name localhost;
#
#       root html;
#       index index.html index.htm;
#
#       ssl on;
#       ssl_certificate cert.pem;
#       ssl_certificate_key cert.key;
#
#       ssl_session_timeout 5m;
#
#       ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
#       ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
#       ssl_prefer_server_ciphers on;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

Step 8

sibanda@gs2:~# sudo cp /etc/nginx/sites-available/magwinya.co.za /etc/nginx/sites-enabled/

Alternatively, you can activate the host by creating a symbolic link between the sites-available directory and the sites-enabled directory.

sibanda@gs2:~# sudo ln -s /etc/nginx/sites-available/magwinya.co.za /etc/nginx/sites-enabled/magwinya.co.za

Important: Restart php-fpm and nginx so that changes can take effect.

sudo service php5-fpm restart && service nginx restart