How To Install LEMP (nginx, MySQL or MariaDB, PHP) Stack On Debian 8

by

Please shareShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditDigg thisShare on StumbleUponShare on VKBuffer this page

LEMP is a combination of the operating system and open-source software stack. The acronym LEMP came from the first letters of Linux, Nginx(engine-x) HTTP Server, MySQL/MariaDB database, and PHP/Perl/Python.

In this tutorial, let us see how to install lemp stack on Debian 8, and its previous versions such as Debian 7 and Debian 6 etc.

Install LEMP Stack On Debian

1. Install Nginx

Nginx (pronounced as engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server written by Igor Sysoev.

Let us begin to install nginx.

First, switch to root user using command:

su

Note: If another web server like apache2 was installed in your system, remove it first to avoid conflicts. To uninstall apache, run the following commands:

service apache2 stop
apt-get remove --purge apache2 apache2-utils apache2.2-bin apache2-common
apt-get autoremove
apt-get autoclean

Find the apache2 configuration directories and files using command:

whereis apache2

Then, permanently delete them with command:

rm -Rf /etc/apache2 /usr/lib/apache2 /usr/include/apache2

Now, install nginx using command:

apt-get install nginx

Start Nginx service using the following command.

On Debian 8:

systemctl start nginx

On Debian 7 and previous versions:

service nginx start

To check whether nginx service is started, run:

systemctl status nginx

Or

ervice nginx status

Sample output:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
   Active: active (running) since Wed 2015-06-24 15:37:16 IST; 3min 7s ago
 Main PID: 2923 (nginx)
   CGroup: /system.slice/nginx.service
           ├─2923 nginx: master process /usr/sbin/nginx -g daemon on; master_...
           ├─2924 nginx: worker process
           ├─2925 nginx: worker process
           ├─2926 nginx: worker process
           └─2927 nginx: worker process

2. Test nginx

Open up your web browser and navigate to http://ip-address/ or http://localhost/. You will see a screen something like below.

Welcome to nginx on Debian! - Mozilla Firefox_001

3. Install MySQL

MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases, though SQLite probably has more total embedded deployments

apt-get install mysql-server mysql-client

During installation, you’ll be asked to setup the MySQL “root” user password. Enter the password and click Ok.

sk@debian: ~_002

Re-enter the password.

sk@debian: ~_003

MySQL is installed now.

You can verify the MySQL server status using command:

On Debian 8:

systemctl status mysql

On Debian 7 and previous versions:

service mysql status

Sample output:

● mysql.service - LSB: Start and stop the mysql database server daemon
   Loaded: loaded (/etc/init.d/mysql)
   Active: active (running) since Wed 2015-06-24 16:03:20 IST; 19s ago
   CGroup: /system.slice/mysql.service
           ├─5047 /bin/sh /usr/bin/mysqld_safe
           └─5394 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid...

Jun 24 16:03:21 debian /etc/mysql/debian-start[5453]: mysql.help_keyword                                 OK
Jun 24 16:03:21 debian /etc/mysql/debian-start[5453]: mysql.help_relation                                OK
Jun 24 16:03:21 debian /etc/mysql/debian-start[5453]: mysql.help_topic                                   OK
Jun 24 16:03:21 debian /etc/mysql/debian-start[5453]: mysql.host                                         OK
Jun 24 16:03:21 debian /etc/mysql/debian-start[5453]: mysql.ndb_binlog_index                             OK
Jun 24 16:03:21 debian /etc/mysql/debian-start[5453]: mysql.plugin                                       OK
Jun 24 16:03:21 debian /etc/mysql/debian-start[5453]: mysql.proc                                         OK
Jun 24 16:03:21 debian /etc/mysql/debian-start[5453]: mysql.procs_priv                                   OK
Jun 24 16:03:21 debian /etc/mysql/debian-start[5515]: Checking for insecure root accounts.
Jun 24 16:03:21 debian /etc/mysql/debian-start[5520]: Triggering myisam-recover for all MyISAM tables

4. Install MariaDB

In case you want to use MariaDB instead of MySQL community edition, follow the steps given below.

MariaDB is a drop in replacement for MySQL. It is a robust, scalable and reliable SQL server that comes rich set of enhancements.

First you have to remove existing MySQL packages if any. To completely uninstall MySQL along with its configuration files, enter the following commands one by one:

systemctl stop mysql

Or,

service mysql stop
apt-get remove --purge mysql-server mysql-client mysql-common
apt-get autoremove 
apt-get autoclean
rm -rf /var/lib/mysql/
rm -rf /etc/mysql/

After removing MySQL, run the following command to install MariaDB.

apt-get install mariadb-server

Alternatively, you can install it using MariaDB repository if you want to try most recent version of MariaDB. Run the following commands to add PPA.

apt-get install python-software-properties
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
add-apt-repository 'deb http://kartolo.sby.datautama.net.id/mariadb/repo/10.0/debian jessie main'

Update the software sources list and install MariaDB using following commands:

apt-get update
apt-get install mariadb-server

During installation you will be asked to set MariaDB ‘root’ user password.

Enter the password twice, and complete the installation.

sk@debian: ~_004

Re-enter password:

sk@debian: ~_005

Check if mariadb is running or not, using the following command:

On Debian 8:

systemctl status mysql

On Debian 7 and previous versions:

service mysql status

Sample output:

● mysql.service - LSB: Start and stop the mysql database server daemon
   Loaded: loaded (/etc/init.d/mysql)
   Active: active (running) since Wed 2015-06-24 16:06:25 IST; 10s ago
   CGroup: /system.slice/mysql.service
           ├─8276 /bin/bash /usr/bin/mysqld_safe
           ├─8277 logger -p daemon.err -t /etc/init.d/mysql -i
           └─8418 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid...

Jun 24 16:06:27 debian /etc/mysql/debian-start[8469]: Phase 2/5: Running 'mysql_fix_privilege_tables'...
Jun 24 16:06:27 debian /etc/mysql/debian-start[8469]: Phase 3/5: Fixing table and database names
Jun 24 16:06:27 debian /etc/mysql/debian-start[8469]: Processing databases
Jun 24 16:06:27 debian /etc/mysql/debian-start[8469]: information_schema
Jun 24 16:06:27 debian /etc/mysql/debian-start[8469]: mysql
Jun 24 16:06:27 debian /etc/mysql/debian-start[8469]: performance_schema
Jun 24 16:06:27 debian /etc/mysql/debian-start[8469]: Phase 4/5: Checking and upgrading tables
Jun 24 16:06:27 debian /etc/mysql/debian-start[8469]: Processing databases
Jun 24 16:06:27 debian /etc/mysql/debian-start[8469]: information_schema
Jun 24 16:06:27 debian /etc/mysql/debian-start[8542]: Triggering myisam-recover for all MyISAM tables

5. Install PHP

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely used open-source general purpose scripting language that is especially suited for web development and can be embedded into HTML.

Install PHP with following command:

apt-get install php5 php5-fpm php5-mysql

6. Configure Nginx

Open the file /etc/nginx/nginx.conf in any editor:

nano /etc/nginx/nginx.conf

Set the worker_processes (i.e No. of CPU’s in your system). To see the no. of CPU’s, use the command “lscpu”. In my case it’s “1″. So I set this as ’1′.

worker_processes 1;

The default vhost(server block) is defined in the /etc/nginx/sites-available/default file.

Make a backup of the original nginx configuration file.

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

Create a new file /etc/nginx/sites-available/default.

nano /etc/nginx/sites-available/default

Add the following lines:

server {
        listen       80;
        server_name  debian.unixmen.local;
        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;
        }
}

Here,

  • root /var/www/html; –> document root directory.
  • server_name debian.unixmen.local; –> Server FQDN.

Save and exit the file.

Restart Nginx service:

systemctl restart nginx

Or

service nginx restart

Restart php5-fpm service:

systemctl restart php5-fpm

Or

service php5-fpm restart

7. Test nginx configuration

Test the nginx configuration for any syntax errors using command:

nginx -t

Sample output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

8. Configure PHP

Open php.ini file in any editor:

nano /etc/php5/fpm/php.ini

Find the line ‘cgi.fix_pathinfo=1′, uncomment it and change the value 1 to 0.

cgi.fix_pathinfo=0

Now restart php-fpm service.

systemctl restart php5-fpm

Or

service php5-fpm restart

To check whether ‘php5-fpm’ is running or not using command:

systemctl status php5-fpm

Or

service php5-fpm status

Sample output:

● php5-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php5-fpm.service; enabled)
   Active: active (running) since Wed 2015-06-24 16:08:21 IST; 9s ago
  Process: 11204 ExecStartPre=/usr/lib/php5/php5-fpm-checkconf (code=exited, status=0/SUCCESS)
 Main PID: 11210 (php5-fpm)
   Status: "Ready to handle connections"
   CGroup: /system.slice/php5-fpm.service
           ├─11210 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
           ├─11214 php-fpm: pool www
           └─11215 php-fpm: pool www

9. Test PHP

Create a sample “testphp.php” file in nginx document root folder.

nano /var/www/html/testphp.php

Add the following lines in it.

<?php
 phpinfo();
?>

Save and exit the file.

Navigate to  http://server-ip-address/testphp.php. It will display all the details about php such as version, build date and commands etc.

phpinfo() - Mozilla Firefox_002

PHP-FPM listens on the socket /var/run/php5-fpm.sock by default. If you want to make PHP-FPM use a TCP connection, open the file /etc/php5/fpm/pool.d/www.conf,

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

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

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

and modify it to listen = 127.0.0.1:9000.

listen = 127.0.0.1:9000

Save and exit the file. Restart php5-fpm service.

systemctl restart php5-fpm

Or

service php5-fpm restart

Now open the nginx configuration file:

nano /etc/nginx/sites-available/default

Find the line fastcgi_pass unix:/var/run/php5-fpm.sock; and change it to fastcgi_pass 127.0.0.1:9000; as shown below.

[...]
location ~ \.php$ {
                try_files $uri =404;            
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;         }
[...]

Save and exit the file. Finally restart nginx service.

systemctl restart nginx

Or

service nginx restart

10. Manage MySQL Databases Using phpMyAdmin (Optional)

phpMyAdmin is a free open-source web interface tool used to manage your MySQL databases.

It is available in the Official Debian repositories. So install it with command:

apt-get install phpmyadmin

Select the web server that should be automatically configured to run phpMyAdmin.

By default, nginx will not be displayed here. So, select apache or lighttpd, and we will configure phpmyadmin to work with nginx webserver later.

sk@debian: ~_004

Select Yes to configure database for phpmyadmin with dbconfig-common.

sk@debian: ~_005

Enter password of the database’s administrative user.

sk@debian: ~_006

Enter MySQL application password for phpmyadmin.

sk@debian: ~_007

Re-enter the password.

sk@debian: ~_008

The phpMyAdmin installation has been completed.

Create a symbolic link between phpMyAdmin and the website root directory. Here our website root document directory is /var/www/html/.

ln -s /usr/share/phpmyadmin/ /var/www/html/

Restart nginx server.

systemctl restart nginx

Or

service nginx restart

11. Access phpMyAdmin Web Console

Now you can access the phpmyadmin console by navigating to http://server-ip-address/phpmyadmin/ from your browser.

Enter your MySQL username and password which you have given in previous steps. In my case its “root” and “debian”.

phpMyAdmin - Mozilla Firefox_009

You will be redirected to PhpMyAdmin main web interface. This is how my phpMyAdmin dashboard looks.

192.168.1.150 - localhost | phpMyAdmin 4.2.12deb2 - Mozilla Firefox_010

Now you can manage your MySQL databases from phpMyAdmin web interface.

That’s it. Your LEMP server is ready to use.

Cheers!