Since Debian powers a large percentage of web servers all over the world, in this article we will explain how to install the LEMP (Linux + Nginx + MariaDB + PHP-FPM) stack on Debian 9 Stretch as an alternative to LAMP (use this guide to install LAMP on Debian 9).

Additionally, we will show how to perform a minimal Nginx / PHP-FPM configuration so that even new system administrators can set up brand new web servers to set up dynamic pages.

To do this, we will leverage the recent updates to the distribution’s official repositories. It is assumed that you have installed Debian 9 on a bare metal, virtual machine, or VPS, or have upgraded from Jessie.

Installing LEMP in Debian 9 Stretch

You may be wondering why we mentioned PHP-FPM instead of PHP as part of the LEMP stack. As opposed to other web servers, Nginx does not provide native support for PHP.

For that reason, PHP-FPM (Fast Process Manager) is used to handle requests for PHP pages. You can learn more about PHP-FPM in the PHP official site.

The default version provided in the Debian repositories php7.0-fpm. As you can probably guess based on the package’s name, this version can even handle requests to pages including PHP 7 code.

NOTE: If Apache has been installed in the same box previously, make sure it is stopped and disabled before proceeding.

With that said, let’s install the components of the LEMP stack as follows:

# aptitude update # aptitude install nginx mariadb-server mariadb-client php-mysqli php7.0-fpm

When the installation is complete, let’s first make sure that Nginx and PHP-FPM are running and enabled to start on boot:

# systemctl status nginx php7.0-fpm

If indicates that one or both services are not running, then do.

# systemctl start nginx php7.0-fpm
# systemctl enable nginx php7.0-fpm

As it is the case with every MariaDB or MySQL installation, it is important to run mysql_secure_installation to perform a minimal security configuration and set the password for the database root account.

# mysql_secure_installation

If you don’t know how to do this, you can refer to step #4 in How to Install MariaDB 10 on Debian and Ubuntu.

Configuring Nginx to Use PHP-FPM on Debian 9

The Nginx main configuration file is /etc/nginx/sites-available/default, where we will need to make the following changes inside the server block:

  • Ensure the location block that handles PHP requests is enabled, with the exception of the one where the fastcgi_pass directive points to the loopback NIC.
  • Add index.php after the index directive to indicate that if found, it should be served by default before index.html or other files.
  • Add the server_name directive pointing to the IP address or hostname of your server. This will be in our case.
  • Additionally, make sure the root directive points to the location where your .php files will be stored (/var/www/html by default).

When you’re done, you can use the following command to test the configuration file for errors.

# nginx -t 

At this point, your /etc/nginx/sites-available/default should look as follows where the numbers refer to the configuration represent the above list:

# grep -Ev '#' /etc/nginx/sites-available/default

Configure Nginx for PHP-FPM

Configure Nginx for PHP-FPM

Testing Nginx and PHP-FPM on Debian 9

To make sure we are now using Nginx as our web server, let’s create a file named info.php inside /var/www/html with the following contents:


Then go to and check the top of the page where you should see this:

Test Nginx and PHP-FPM

Test Nginx and PHP-FPM

Finally, let’s point our browser to the booksandauthors.php file that we created in Install LAMP (Linux, Apache, MariaDB or MySQL and PHP) Stack on Debian 9.

As you can see in the following image, this file is now being served by Nginx:

Check PHP Pages Served by Nginx

Check PHP Pages Served by Nginx

NOTE: If you notice that Nginx serves the .php files as downloads instead of executing them, clear your browser cache or try a different browser. Particularly, if you are using Chrome you may want to test with incognito mode.


In this article we have explained how to install and configure Nginx to serve dynamic .php pages. It is important to note that after this initial setup there are settings that should be taken into account to secure the web server.

You may find a basic summary in The Ultimate Guide to Secure, Harden and Improve Performance of Nginx Web Server.

If you are looking for virtual hosting on Nginx, read How to Setup Name-based and IP-based Virtual Hosts on NGINX.

As always, don’t hesitate to let us know if you have questions or comments about this article.

Facebook Comments

More Stuff

Emblematic Group and Mozilla Team Up to Showcase Next Generation o... Everything you share on the internet is a story. You read blog posts and watch videos that make you feel connected to people across the world. Virtual...
Latest Firefox Release is Faster than Ever With the introduction of the new Firefox Quantum browser in 2017 we changed the look, feel, and performance of our core product. Since then we have la...
Competing without a Marketing Budget: When All You Have is a Produ... This is a story about the power of a good landing page, PR efforts, and marketing strategy. The refreshing twist here is that instead of explaining ho...
The Ongoing Fiasco and Mysterious Afterlife of QuadrigaCX I’d say you were a little late to the cryptocurrency party if you haven’t heard about the ongoing catastrophe that’s been QuadrigaCX these past six ...
Spread the love

Posted by News Monkey