Install LEMP Server (Nginx, PHP, MariaDB & phpMyAdmin) on Debian 7/8

LEMP stack stands for Linux, Nginx, MySQL, MariaDB, MongoDB, PHP, Perl, Python & phpMyAdmin. LEMP is bunch of opensource software which is developed by different organization.
NGINX stands for engine-x is a free, open-source, high-performance HTTP server and reverse proxy, as well as an mail (IMAP/POP3) proxy server. Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. Currently 146 million websites use NGINX to deliver super-fast web experiences. The big companies like (WordPress, facebook, GitHub & cloudflare, etc..) using nginx.

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. For more details about PHP-FPM.

Remove Existing LAMP stack

If you are planning to install NGINX, I advise you to remove your existing LEMP stack completely to avoid conflicts. Use the below single command to purge everything.

# Remove LAMP Stack #
$ sudo apt-get purge apache2* php5* mysql* phpmyadmin*
$ sudo apt-get autoremove
$ sudo apt-get autoclean

# Remove Apache Configuraion files #
$ whereis apache2
$ sudo rm -Rf /usr/sbin/apache2 /usr/lib/apache2 /etc/apache2 /usr/share/apache2 /usr/share/man/man8/apache2.8.gz

# Remove MySQL Configuraion files #
$ sudo rm -Rf /var/lib/mysql/
$ sudo rm -Rf /etc/mysql/

Then recheck once, if anything still available. Using below commands.

$ dpkg -l | grep apache2*
$ dpkg -l | grep php5*
$ dpkg -l | grep mysql*
$ dpkg -l | grep phpmyadmin*

1) Install Nginx

By default you will get Nginx older version from Debian repository but current stable version is Nginx 1.8.1, So you need to add Nginx Repository to get latest stable version of Nginx on your system. use the below commands to do it.

$ sudo sh -c "echo 'deb http://nginx.org/packages/debian/ `lsb_release -cs` nginx' >> /etc/apt/sources.list"
$ sudo sh -c "echo 'deb-src http://nginx.org/packages/debian/ `lsb_release -cs` nginx' >> /etc/apt/sources.list"
$ curl http://nginx.org/keys/nginx_signing.key | apt-key add -
$ sudo apt-get update
$ sudo apt-get install nginx

Nginx Version checking

$ nginx -v
nginx/1.8.1

2) Check Nginx Index page

Open your web browser and navigate to http://localhost/ or http://your-server-ip-address/ or http://127.0.0.1/
install-lemp-nginx-mysql-php-on-debian-8-1

3) Nginx Configuration

Need to make lots of changes on your Nginx default config file to make it Nginx work properly.

Open /etc/nginx/nginx.conf file on your favorite text editor and change worker_processes values according your CPU count. For CPU count use lscpu command. I’m having 4 CPU’s that’s why i added 4. Also check Nginx User, it should be www-data

$ nano /etc/nginx/nginx.conf

user www-data;
worker_processes 4;                                                                        

Note : 1) When you are installing Nginx from Debian Repository, it will create default conf file under /etc/nginx/sites-available/default
2) When you are installing Nginx from Nginx Repository, it will create default conf file under /etc/nginx/conf.d/default. So, don’t get confuse
3) Nginx Defalut Document Root : /usr/share/nginx/html

Open default Virtual host configuration file /etc/nginx/conf.d/default on your favorite text editor and uncomment below lines. Also add/modify below colored lines on your file. I have removed all the unwanted lines from this file for clear explanation. Add your FQDN (server.2daygeek.com) instead of us.

$ sudo /etc/nginx/conf.d/default

# Web root location & port listening #
	server 
	{
        listen 80;
	server_name server.2daygeek.com;
        root /usr/share/nginx/html;
        index index.php index.html index.htm;

# Redirect server error pages to the static page #        
        location / 
	{
	try_files $uri $uri/ /index.php;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html 
	{
        root /usr/share/nginx/html;
        }

# Pass the PHP scripts to FastCGI server #	
	location ~ \.php$ 
	{
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
        }
	}                                                                    

Restart & Reload Necessary Services to take effect.

# For Systemd Systems Debian 8 & later #
$ sudo systemctl reload nginx.service
$ sudo systemctl restart nginx.service
$ sudo systemctl restart php5-fpm.service

# For SysVinit Systems Debian 7 & older #
$ sudo service nginx reload
$ sudo service nginx restart
$ sudo service php5-fpm restart

Run nginx configuration test

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4) MariaDB installation

MariaDB is a drop-in replacement for MySQL. MariaDB is opensource relational database management system (RDBMS) which is supporting database access. By default all the distribution included MariaDB 5.x to own repository, if you want to install, simply you can run apt-get install mariadb-server and i want to install MariaDB 10 Which is not included in distribution repository. So, i’m going to add MariaDB repo to our system.

# Add MariaDB 10.x Repo #
$ sudo apt-get install software-properties-common
$ sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
$ sudo add-apt-repository 'deb [arch=amd64,i386] http://mirrors.opencas.cn/mariadb/repo/10.1/debian '$(lsb_release -cs)' main'
$ sudo apt-get update
$ sudo apt-get -y install mariadb-server

# For Sysvinit System Debian 8 & later #
$ sudo systemctl start mariadb.service
$ sudo systemctl enable mariadb.service
$ sudo systemctl status mariadb.service

# For Sysvinit System Debian 7 & older #
$ sudo service mysqld start
$ sudo chkconfig mysqld on

While installing MariaDB server, it will ask you to set root password for MariaDB.
install-mariadb-10-1-on-ubuntu-debian-mint-1
Confirm the root password for MariaDB.
install-mariadb-10-1-on-ubuntu-debian-mint-2
MariaDB access.

$ sudo mysql -u root -p

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.11-MariaDB-1~jessie mariadb.org binary distribution

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>  

5) PHP-FPM & PHP modules installation

Use the below command to install PHP-FPM & PHP modules. PHP initially called Personal Home Page, now it is called asHypertext Preprocessor. PHP is a opensource software which is designed for web development purpose. It is used for server-side scripting language as well as general-purpose programming language.

$ sudo apt-get install php5 php5-fpm php5-mysql php5-cli php5-curl php5-gd php5-mcrypt

Configure PHP
We need to make small changes on php.ini file to make it work php-fpm properly. open /etc/php5/fpm/php.ini file on your favorite text editor and find cgi.fix_pathinfo= then uncomment it and change from 1 to 0.

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

cgi.fix_pathinfo=0                                                                        

By default PHP-FPM listens the socket on /var/run/php5-fpm.sock and its not effective one. So we need to change the listening settings on /etc/php5/fpm/pool.d/www.conf. From listen = /var/run/php5-fpm.sock to listen = 127.0.0.1:9000 to listen TCP. Just open the file /etc/php5/fpm/pool.d/www.conf and do it.

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

;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000

phpinfo file creation

$ sudo nano /usr/share/nginx/html/phpinfo.php

<?php
 phpinfo();
?>

Restart Necessary Services

# For Sysvinit System Debian 8 & later #
$ sudo systemctl restart nginx.service
$ sudo systemctl restart php5-fpm.service

# For Sysvinit System Debian 7 & older #
$ sudo service nginx restart
$ sudo service php5-fpm restart

Open your web browser and navigate to http://localhost/phpinfo.php or http://your-server-ip-address/phpinfo.php or http://127.0.0.1/phpinfo.php
install-lemp-nginx-mysql-php-on-debian-8-2

6) Install phpMyAdmin

phpMyAdmin is a free open-source web-based administration tool for managing the MySQL, MariaDB servers which will help us to manage the database easily.

$ sudo apt-get -y install phpmyadmin

Make sure your system should have database installed (MySQL, MariaDB, etc) before proceeding phpMyAdmin installation. Here choose Yes to configure database for phpmyadmin with dbconfig-common.
install-lamp-stack-apache-mariadb-php-phpmyadmin-on-debian-7-5
Enter MySQL root password to create phpmyadmin database.
install-lamp-stack-apache-mariadb-php-phpmyadmin-on-debian-7-6
Enter password for phpmyadmin database.
install-lamp-stack-apache-mariadb-php-phpmyadmin-on-debian-7-7
Confirm the password for phpmyadmin database.
install-lamp-stack-apache-mariadb-php-phpmyadmin-on-debian-7-8
Select the webserver to configure phpMyAdmin automatically but here Nginx not showing, so you can choose anything one, we will configure later.
install-lamp-stack-apache-mariadb-php-phpmyadmin-on-debian-7-9
Just create symlink from /usr/share/phpMyAdmin to /usr/share/nginx/html, this will do the trick

# Configure phpMyAdmin #
ln -s /usr/share/phpMyAdmin /usr/share/nginx/html

# Restart apache service #
$ sudo systemctl restart nginx.service    # [For Systemd Systems] #
$ sudo service nginx restart    	  # [For Systemd Systems] #

Open your web browser and navigate to http://localhost/phpMyAdmin or http://your-server-ip-address/phpMyAdmin or http://127.0.0.1/phpMyAdmin
install-lamp-stack-apache-mariadb-php-phpmyadmin-on-debian-7-10
Enter the Mysql root password to login phpmyadmin.
install-lamp-stack-apache-mariadb-php-phpmyadmin-on-debian7-11

We are preparing all articles in-depth to understand by all level/stage Linux administrators. If the article is useful for you, then please spend less than a minute to share your valuable comments in our commenting section.

Please stay tune with us…Good Luck.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

2 Comments on “Install LEMP Server (Nginx, PHP, MariaDB & phpMyAdmin) on Debian 7/8”

Leave a Reply

Your email address will not be published. Required fields are marked *