How To Install Linux, Nginx, MySQL & PHP (LEMP) on Debian 8

As always we start off with

apt-get update
apt-get upgrade

Install MySQL Server

apt-get install mysql-server

You will be prompted to input a password, put anything “secure”

Now let’s secure our database.


This will ask you a few questions

– Enter “secure” password set previously
– Change Password : n
– Remove Anonymous Users : Y
– Disallow root login remotely : Y
– Remove test database : Y
– Reload privilege tables : Y


Install and Configure PHP (+optional modules)

Install php and a few important modules with the following command

apt-get install php5-fpm php5-mysqlnd php5-mcrypt

Now in /etc/php5/fpm/php.ini change the following lines (you need to search for each one)

upload_max_filesize = 2M
post_max_size = 8M


upload_max_filesize = 12M
post_max_size = 16M

Install and Configure NGINX

apt-get install nginx

Nginx is now installed and if you go to your IP address you should see the welcome page

To configure nginx to work with php, edit /etc/nginx/sites-availiable/default with the following changes. (lines with edits are highlighted)

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;

        # pass the PHP scripts to FastCGI server listening on
        location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #       # With php5-cgi alone:
        #       fastcgi_pass;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                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;

Lets not forget to restart nginx

service nginx restart


Finishing off

Create a test page and try it out at http://my.ip.address/info.php

echo "<?php phpinfo(); ?>" > /var/www/html/info.php