How To Install Linux, Nginx, MySQL, PHP (LEMP) Stack on Debian 7

Published on October 31, 2013
Default avatar

By Adam LaGreca

How To Install Linux, Nginx, MySQL, PHP (LEMP) Stack on Debian 7

Lemp Stack: Basic Info

LEMP stack is a group of open source software to get a VPS up and running. The acronym includes the Linux (L), MySQL (M), and PHP (P) of the more traditional LAMP stack, but switches out Apache for Nginx (pronounced engine x).

This tutorial explains how to install nginx/MySQL/PHP-fpm, as well as all of the required repos to download nginx and php-fpm. It will also review how to configure php and nginx, as well as how to create a php info page.

1) Update Apt-Get

The apt-get update command is used to re-synchronize the package index files from their sources. If used in combination with the apt-get upgrade command, they install the newest versions of all packages currently available.

At the moment, we only need to do a thorough update:

sudo apt-get update

2) Install MySQL on your VPS

MySQL is a powerful database management system used for organizing and retrieving data

To install MySQL, open terminal and type in these commands:

sudo apt-get install mysql-server

During the installation, MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell.


Once you have installed MySQL, we should activate it with this command:

sudo mysql_install_db

Finish up by running the MySQL set up script:

sudo /usr/bin/mysql_secure_installation

The prompt will ask you for your current root password. Type it in.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

The prompt will ask if you want to change the root password. Go ahead and choose N for this option, as the root password should aready be set; however, for the rest of the questions you can simply reply Y to all-- unless there is a reason for you to do otherwise.

Now let's install nginx!

3) Install and Configure Nginx on your VPS


Initial installation is simple with the apt-get command.

sudo apt-get install nginx

nginx needs a command to begin running:

sudo service nginx start

Now if you point your browser to your IP address, it should confirm that nginx was successfully installed on your cloud server.

*Run the following command to reveal your VPS's IP address.

ifconfig eth0 | grep inet | awk '{ print $2 }'


Open up the default virtual host file with this command:

sudo nano /etc/nginx/sites-available/default

The configuration should include the changes below (the details of the changes are under the config information):

UPDATE: Newer Ubuntu versions create a directory called 'html' instead of 'www' by default. If /usr/share/nginx/www does not exist, it's probably called html. Make sure you update your configuration appropriately.

server {
        listen   80;

        root /usr/share/nginx/www;
        index index.php index.html index.htm;

        server_name example.com;

        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;

        # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
        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;


Here are the details of the changes:

  • Add index.php to the index line.
  • Change the server_name from local host to your domain name or IP address (replace the example.com in the configuration)
  • Change the correct lines in “location ~ \.php$ {“ section

Save and Exit

4) Install and Configure PHP


You probably guessed it! We will use the apt-get command to install PHP-FPM:

sudo apt-get install php5-fpm php5-mysql


We need to make one small change in the php configuration. Open up php.ini:

 sudo nano /etc/php5/fpm/php.ini

Find the line cgi.fix_pathinfo=1 and change the 1 to 0.


If this number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path— a much safer alternative.

Save and Exit.

We need to make another small change in the php5-fpm configuration. Open up www.conf:

 sudo nano /etc/php5/fpm/pool.d/www.conf

Find the line, listen =, and change the to /var/run/php5-fpm.sock.

listen = /var/run/php5-fpm.sock

Save and Exit.

Restart php-fpm:

sudo service php5-fpm restart

5) Create a Php Info Page

We can quickly see all of the details of the new php configuration.

To set this up, first create a new file:

sudo nano /usr/share/nginx/www/info.php

Add in the following line:


Then Save and Exit.

Finishing Up

Restart nginx:

sudo service nginx restart

You can see the nginx and php-fpm configuration details by visiting http://youripaddress/info.php

Your LEMP stack is now set up and configured on your virtual private server :)

By Adam LaGreca

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us

About the authors
Default avatar
Adam LaGreca


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I think it is safe to include a section that checks/removes apache if its already on your server. Great guide!

Hi !

I got a problem, I need nginx with rtmp module so I need to compile it but after the path configuration are different. The biggest problem is with php, I don’t know how nginx configuration to setup the right php path.

I try several things but it doesn’t work.

If you have a tutorial “How to install nginx with rtmp module and PHP” something like that, I will appreciate.

Regards, Misteralex0007

Edit: Everyone ignore my comment – I should have been using the tutorial for Debian 8 and not 7! https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-debian-8 in case anyone is looking.

I must assume that it’s because it’s the latest php5 version – but I’m following this tutorial for Debian 8.6 x64.

The “listen = /var/run/php5-fpm.sock” line is already set correctly, so this step seems unnecessary now.

I don’t know if php.ini is generated from the PHP install or from the Debian install, so I can’t confirm whether this is because I’m on Debian 8.6 or because PHP5’s installation is newer now than it was when this article was written.

Also, some of the settings in the /default file were a bit different, but I just un-commented out the ones that seemed to make sense.

One final thing – sudo nano /usr/share/nginx/www/info.php was now under /html/info.php (the newer nginx seems to use /html instead of /www perhaps?

I also seem to not be able to view anything (on the web) that I’m adding to that /html folder – except that I can always (still) see the index.html ‘welcome to debian nginx’ page. Kind of strange. Thanks for the tutorial though!

This comment has been deleted

    This comment has been deleted

      Installing LEMP directly on Debian 7 requires sysadmin experience and use of CLI. Unless you know how to do that, you will have difficulty doing that. In such cases, I’d recommend you to use a managed hosting, like Cloudways, for quickly and easily installing LEMP on Debian.

      This comment has been deleted

        Hello. Thanks for the very detailed tutorials! while I type: mysql_install_db /// show error: command not found <<<<<<<< root@mainguiglo:/home/lanotec# mysql_install_db Installing MySQL system tables… 150806 11:42:20 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead. 150806 11:42:20 [Note] /usr/sbin/mysqld (mysqld 5.5.44-0+deb8u1) starting as process 28007 … OK Filling help tables… 150806 11:42:20 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead. 150806 11:42:20 [Note] /usr/sbin/mysqld (mysqld 5.5.44-0+deb8u1) starting as process 28013 … OK

        To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system

        PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands:

        /usr/bin/mysqladmin -u root password ‘new-password’ /usr/bin/mysqladmin -u root -h mainguiglo password ‘new-password’

        Alternatively you can run: /usr/bin/mysql_secure_installation

        which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers.

        See the manual for more instructions.

        You can start the MySQL daemon with: cd /usr ; /usr/bin/mysqld_safe &

        You can test the MySQL daemon with mysql-test-run.pl cd /usr/mysql-test ; perl mysql-test-run.pl

        Please report any problems at http://bugs.mysql.com/

        Thanks for beautiful job here! this line does not exist `listen =```na current version of php .. because already used the search feature of the nano .. ctrl + we nothing ... only this line: `; listen.allowed_clients =``` only this line contains the IP address www.conf

        there is: listen =

        ``; listen.allowed_clients =``` it comes on the same line you to move to /var/run/php5-fpm.sock ??? please help me!

        Recalling that the wizard asked me several questions in my server installation, and here not hinted at these questions after: apt-get install mysql-server

        O wizard me fez as seguintes seguntas e responde das seguintes formas: root@mainguiglo:/home/lanotec# /usr/bin/mysql_secure_installation


        In order to log into MySQL to secure it, we’ll need the current password for the root user. If you’ve just installed MySQL, and you haven’t set the root password yet, the password will be blank, so you should just press enter here.

        Enter current password for root (enter for none): ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES) Enter current password for root (enter for none): ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES) Enter current password for root (enter for none): ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES) Enter current password for root (enter for none): OK, successfully used password, moving on…

        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… /// pleass help me – already created several dynamic website with drupal, joomla, wordpress in my loca server, and always stayed in terrible hosting services and HG BH and others with shared ip, I want to experience the digital ocean so praised, but to following the step by step and to having errors. the strangest was not found after typing command: mysql_install_db

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Get our biweekly newsletter

        Sign up for Infrastructure as a Newsletter.

        Hollie's Hub for Good

        Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

        Become a contributor

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        Welcome to the developer cloud

        DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

        Learn more
        DigitalOcean Cloud Control Panel