How To Install Nginx, MariaDB, PHP (LEMP stack) on Debian 9

Install MariaDB Server

Update the system repository index

apt-get update

output:

[email protected]:~#  apt-get update
Hit:1 http://nginx.org/packages/debian stretch InRelease
Hit:2 http://security.debian.org/debian-security stretch/updates InRelease 
Ign:3 http://ftp.us.debian.org/debian stretch InRelease                        
Get:4 http://ftp.us.debian.org/debian stretch-updates InRelease [91.0 kB]      
Hit:5 https://download.docker.com/linux/debian stretch InRelease
Hit:6 http://ftp.us.debian.org/debian stretch Release          
Fetched 91.0 kB in 1s (61.6 kB/s)

Install the MariaDB server by issuing the following command.

apt-get -y install mariadb-server

output:

[email protected]:~# apt-get -y install mariadb-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done

Start MariaDB server.

systemctl start mariadb.service

output:

[email protected]:~# systemctl start mariadb.service

Auto-start the MariaDB service during every boot.

systemctl enable mariadb.service

output:

[email protected]:~#  systemctl enable mariadb.service

Install Nginx:

Download the Nginx repository signing key

wget http://nginx.org/keys/nginx_signing.key

output:

[email protected]:~# wget http://nginx.org/keys/nginx_signing.key
--2019-01-08 11:55:41--  http://nginx.org/keys/nginx_signing.key
Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1561 (1.5K) [text/plain]
Saving to: ‘nginx_signing.key.1’

Add the Nginx public key to the system.

apt-key add nginx_signing.key

output:

[email protected]:~# apt-key add nginx_signing.key
key ABF5BD827BD9BF62:
3 signatures not checked due to missing keys
OK

Append Nginx repository to /etc/apt/sources.list file.

echo "deb http://nginx.org/packages/debian/ stretch nginx" | sudo tee -a /etc/apt/sources.list

output:

[email protected]:~# echo "deb http://nginx.org/packages/debian/ stretch nginx" | sudo tee -a /etc/apt/sources.list
deb http://nginx.org/packages/debian/ stretch nginx

Install Nginx package using the following command.

apt-get update; apt-get -y install nginx

output:

[email protected]:~# apt-get update; apt-get -y install nginx
Hit:1 http://nginx.org/packages/debian stretch InRelease
Hit:2 http://security.debian.org/debian-security stretch/updates InRelease                                           
Hit:3 https://download.docker.com/linux/debian stretch InRelease                                         
Ign:4 http://ftp.us.debian.org/debian stretch InRelease                            
Hit:5 http://ftp.us.debian.org/debian stretch-updates InRelease
Hit:6 http://ftp.us.debian.org/debian stretch Release
Reading package lists... Done

Start the Nginx service after the installation.

systemctl start nginx.service

output:

[email protected]:~# systemctl start nginx.service

Auto-start Nginx at system startup.

systemctl enable nginx.service

output:

[email protected]:~# systemctl enable nginx.service

Install PHP5-FPM

Install PHP through PHP-FPM by issuing the following command.

apt-get -y install php-fpm php-mysql php-cli

output:

[email protected]:~# apt-get -y install php-fpm php-mysql php-cli
Reading package lists... Done
Building dependency tree       
Reading state information... Done
php-cli is already the newest version (1:7.0+49).
php-fpm is already the newest version (1:7.0+49).

Edit /etc/php.ini.

vi /etc/php/7.0/fpm/php.ini

output:

; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

PHP-FPM listens on the socket /var/run/php7-fpm.sock by default. To make PHP-FPM use a TCP connection, open the file /etc/php/7.0/fpm/pool.d/www.conf.

FROM:

listen = /run/php/php7.0-fpm.sock

TO:

listen = 127.0.0.1:9000

output:

; Note: This value is mandatory.
listen = 127.0.0.1:9000

Restart PHP-FPM service.

systemctl restart php7.0-fpm.service

output:

[email protected]:~# systemctl restart php7.0-fpm.service

Enable PHP-FPM Support on Virtual Host

Server Name : server.test.com
Document Root : /usr/share/nginx/html/test.com

Create a virtual host configuration file /etc/nginx/conf.d/virtual.conf.

vi /etc/nginx/conf.d/virtual.conf

output:

server {
server_name server.test.com;
root /usr/share/nginx/html/test.com;

location / {
    index index.html index.htm index.php;
}

location ~ \.php$ {
   include /etc/nginx/fastcgi_params;
   fastcgi_pass 127.0.0.0:9000;
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME /usr/share/ngnix/html/test.com$fastcgi_script_name;
   }
 }

Create a host entry for your domain (server.itzgeek.local) in the /etc/hosts file.

vi /etc/hosts

output:

127.0.0.1               localhost    server.test.com

Testing PHP-FPM support on the Virtual Host

Create the root directory for the virtual host.

mkdir /usr/share/nginx/html/test.com

For testing the PHP, place a PHP file onto the document root of the created virtual host.

vi /usr/share/nginx/html/test.com/index.php

Copy/Paste the below line into the index.php file.

<?php phpinfo(); ?>

Restart the services and enable them to start automatically on system startup.

systemctl restart nginx.service

output:

[email protected]:~# systemctl restart nginx.service

systemctl restart php7.0-fpm.service

output:

[email protected]:~# systemctl restart php7.0-fpm.service

systemctl enable php7.0-fpm.service

output:

[email protected]:~# systemctl enable php7.0-fpm.service
Synchronizing state of php7.0-fpm.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable php7.0-fpm

Last Modified: January 08 2019 20:26:06