Install Nginx MySQL PHP (LEMP Stack) on Ubuntu 16.04

LEMP (Linux, Nginx, MySQL and PHP ) Stack is the most popular environment in PHP website development and hosting. Linux is the operating system, Apache is the popular web server developed by Apache Foundation. MySQL is relational database management system used for storing data and PHP is an development language.

Step 1 – Install PHP
PHP 7 is the default available packages in Ubuntu 16.04 repositories. Simply use the following commands to update apt cache and install PHP packages on your system.
$ sudo apt update
$ sudo apt install php php-fpm
Verify installed PHP version using following command.
root@hackthesec:~$ php -v

PHP 7.0.4-7ubuntu2 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
Step 2 – Install NGINX
Use the following commands to install Nginx web server on your Ubuntu 16.04 LTS server.
$ sudo apt install nginx
Step 3 : Install MySQL
Install mysql-server packages for MySQL database and install php-mysql package to use MySQL support using php. 
$ sudo apt install mysql-server php-mysql
Installer will prompt for root password, This password will work for your MySQL root user. After installing MySQL execute following command for initial settings of MySQL server. You will she that script will prompt about more settings than earlier mysql versions like password validation policy etc.
$ sudo mysql_secure_installation
Step 4 : Configure PHP-FPM
PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features.
$ sudo nano /etc/php/7.0/fpm/php.ini
un-comment cgi.fix_pathinfo=1 line and set value to 0.
Now set the listen parameter in /etc/php/7.0/fpm/pool.d/www.conf configuration file. Here you can use php5-fpm socket to work or start php5-fpm server on any port as service. We are going to use it as service.
$ sudo nano /etc/php/7.0/fpm/pool.d/www.conf
Now make changes in configuration file as below. Commend listen with socket file and enable it as service
; listen = /run/php/php7.0-fpm.sock
listen =
Step 5 — Configure Nginx VirtualHost
Configuration of Nginx virtualhost. For this example we are editing default configuration file.
server {
        listen   80;

        root /var/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;

        location ~ .php$ {
                try_files $uri =404;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
You have to do the same changes in all VirtualHosts configured.
Step 6 – Restart Services
After installing all services on your system, start all required services.
$ sudo systemctl restart nginx.service
$ sudo systemctl restart php7.0-fpm.service
Step 7 – Open Access in Firewall
If you are using iptables, Use following commands to open port 80 for public access of webserver.
$ sudo iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
Or If you are Using UFW then command bellow.
$ sudo ufw allow 80/tcp
Step 8 – Test Setup
After completing all setup. Let's create a info.php file website document root with following content.

About Author:

I am a Linux Administrator and Security Expert with this site i can help lot's of people about linux knowladge and as per security expert i also intersted about hacking related news.TwitterFacebook

Newer Post
Older Post


Post a Comment