How To Install LEMP on Debian & Ubuntu (Linux, nginx, MySQL, PHP,PHPMyAdmin)

The LEMP (Linux, nginx, MySQL, PHP,PHP,MyAdmin)software stack is a group of software that can be used to serve dynamic web pages and web applications.In this guide, I will walk you through installing all of these components on Debian & Ubuntu .

This tutorial series helps sysadmins set up a new web server using the LEMP stack, 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.

You can read the following articles to know more about Cheap VPS Hosting and VPS Tutorials .

  1. How to test VPS Speed and Performance? Install and Run UnixBench & sysbench
  2. RamNode VPS Review-Cheap KVM and SSD VPS Starts at $3.50/mo
  3. host1plus VPS Review-Affordable & Quick VPS Hosting RAM 256 MB Only $2.00/mo

How to Install the Nginx Web Server

Nginx is a open source web server. Generally, Apache is installed on majority of the servers but Nginx tends to give better performance out of the box compared to Apache.

There are several ways to install Nginx.

1.Install Nginx from Repositories

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install nginx

2.Install Nginx from a Source Distribution

sudo apt-get install libpcre3-dev build-essential libssl-dev
cd /opt/
sudo wget
sudo tar -zxvf nginx*.tar.gz
cd /opt/nginx-*
sudo ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-ipv6
sudo make
sudo make install
sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx
#Create a script to run Nginx: /lib/systemd/system/nginx.service
Description=A high performance web server and a reverse proxy server
ExecStartPre=/opt/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/opt/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/opt/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /opt/nginx/logs/

sudo chmod +x /lib/systemd/system/nginx.service
sudo systemctl start nginx

3.Configure Server Blocks

Now, we need to configure nginx.Create the server block file /etc/nginx/sites-available/ with your domain:)

server {
listen   80;
access_log /var/www/html/;
error_log /var/www/html/;

location / {
    root   /var/www/html/;
    index  index.html index.htm;


4.Create the public_html and log directories.

sudo mkdir -p /var/www/html/{public_html,logs}

5.Enable the site and restart the web server.

sudo rm /etc/nginx/sites-enabled/
sudo /etc/init.d/nginx restart

After installation, we can verify nginx installation by opening the ip of server. It should display default nginx page.

Install and Configure PHP

PHP is a server-side scripting language designed for web development. The scripting role can also be filled with Python or Perl. We can do so by the running the following commands:

sudo apt-get install php5-fpm php5-mysql

Then open up php.ini:

sudo nano /etc/php5/fpm/php.ini
# Find the line cgi.fix_pathinfo=1 and change the 1 to 0.


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
sudo service php5-fpm restart

Install and Configure MySQL Database Server

MySQL is commonly used as the database for most LEMP stacks or other LAMP stacks.Open terminal and type in these commands:

sudo apt-get install mysql-server

During the installation,You should set a root password.


Create a database and grant permissions.

mysql -u root -p
create database howsvps;
grant all on howsvps.* to 'howsvpsu' identified by '1234567';
sudo /etc/init.d/php-fastcgi restart


#to generate the directory structure
sudo mysql_install_db
sudo mysql_secure_installation

Install and Configure phpMyAdmin

PhpMyAdmin is a powerful PHP based gui for MySQL. phpMyAdmin was originally developed by Tobias Ratschiller.Installing phpMyAdmin is almost as easy as installing MySQL, just a few more steps.

Install phpMyAdmin

sudo apt-get install phpmyadmin

During the installation,You could choose the web server that will automatically configured to run PhpMyadmin .

The next prompt will ask if you would like dbconfig-common to configure a database for phpmyadmin to use. Select “Yes” to continue.

Afterward, you will be asked to select and confirm a password for a new database that will hold phpMyAdmin’s own data.

You just need to create a symbolic link from the installation files to our Nginx document root directory by typing this:

sudo ln -s /usr/share/phpmyadmin /usr/share/nginx/html

#enable the mcrypt PHP module
sudo php5enmod mcrypt
sudo service php5-fpm restart

You should now have a configured and secured PHPMyAdmin instance and MySQL database ready to use. Go to your server’s domain name or public IP address followed by /phpmyadmin, in your web browser:


You should now have Linux, nginx, MySQL, PHP,PHPMyAdmin configured and ready to use on your Debian & Ubuntu server. If you want to host websites on your server running NGINX ,you can use NGINX server blocks.

Server blocks are the NGINX equivalent of Apache’s virtual hosts – they let you direct incoming HTTP traffic for a domain name to a particular directory on your server.