How to install LEMP (Linux, Nginx, MySQL & PHP-FPM) on Debian 8

In this article, we will walk you through installing LEMP (Linux, Nginx, MySQL and PHP-FPM) on Debian 8. The LEMP stack is synonymous with LEMP server or LEMP web server. It includes Linux, Nginx, MySQL (MariaDB) and PHP.

Update the system

Make sure your server is completely up to date with:

apt-get update && apt-get upgrade

Install NGINX

To install Nginx on your Debian 8 server, you need to run the following command:

apt-get install nginx

Once the installation is complete, you can start with Nginx:

systemctl start nginx

Enable Nginx to start at boot:

systemctl enable nginx

Possible problems:

If you encounter errors like these while installing Nginx:

dpkg: error processing package nginx (--configure):
dependency problems - leaving unconfigured
Processing triggers for systemd (215-17+deb8u1) ...
Errors were encountered while processing:
nginx-full
nginx
E: Sub-process /usr/bin/dpkg returned an error code (1)

then, you can fix by opening the default Nginx config file and commenting out the line listen [::]: 80 default_server;… Enter the following command:

vim /etc/nginx/sites-available/default

Find the line listen [::]: 80 default_server; and comment it out by adding a # sign in front of the line. Restart Nginx for the changes to take effect and run the command to install the Nginx package manager:

systemctl restart nginx

apt-get install nginx

Verify that Nginx is running by opening a web browser and visiting your server IP (http: // server_ip). You should get the Nginx welcome page as shown below:

Installing MYSQL

Now let’s install MySQL. Type the following:

apt-get install mysql-server

During installation, you will be prompted for the password for the MySQL root user. Don’t enter a simple password that can be cracked. It must be at least 8 characters mixed with upper and lower case.

Now that MySQL is installed, we recommend that you securely configure MySQL with the command:

mysql_secure_installation

Enter your root password and answer with ‘n’ when asked to change the MySQL root password. Below is the entire procedure that you can follow:

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
... skipping.

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...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed!  Not critical, keep moving...
- 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...


All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Enable MySQL to start at boot:

systemctl enable mysql

Install PHP-FPM

Install PHP-FPM by running the following command:

apt-get install php5-fpm php5-mysql

The next step to take is to modify the Nginx configuration file. Move the default file and create a new Nginx file. The following command will do exactly that:

mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old

vim /etc/nginx/sites-available/default

Now that you have a new default file open, paste in the following content:

server {
        listen       80;
        server_name  your_website_name.ru;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        location / {
                try_files $uri $uri/ =404;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /var/www/html;
        }
        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;
        }
}

Save and close the file.

Now, let’s create a simple PHP page test. Create a PHP info page so you can check your PHP version, active modules, etc …

Create a file, let’s name it info.php in the catalog / var / www / html:

vim /var/www/html/info.php

Paste the following code into the file:

<?php
phpinfo();
?>

Restart Nginx for the changes to take effect:

systemctl restart nginx

Now open your web browser and go to http: //your_server_ip_address/info.php. We are pleased to welcome you to a web page similar to the one below:

infophp

That’s all. You have successfully installed the stack LEMP on Debian 8.

Sidebar