• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Install LEMP Stack on Debian 9

Written by Admin, Updated On May 7, 2019
debian, lemp, mariadb, nginx, php
How To Install LEMP Stack on Debian 9

LEMP stack on Debian 9 is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. LEMP stands for Linux OS, with the Nginx (pronounced like “Engine-X”) web server, Data store in a MySQL or MariaDB database, and dynamic content is processed by PHP.

Install LEMP Stack on Debian 9#

In this tutorial you will learn step by step how to install LEMP stack on Debian 9 server.

Prerequisites#

Firstly, you need Debian 9 server with a non-root sudo enabled user account.

Step 1 – Installing Nginx#

Nginx is a modern and efficient web server now a days. This will be used to show web pages to our site users.

First you need to update your software packages and then install Nginx, an open source, fast and high-performance web server and Nginx is available in default Debian repositories. So execute following commands to update packages and install Nginx web server :

sudo apt update
sudo apt install nginx

Once complete installation, Nginx service should start automatically and will be enabled to start at boot time. Also, You can check status by executing below command :

sudo systemctl status nginx

If you have enabled firewall then we need to allow connections to Nginx. By default Nginx registers itself with ufw upon installation. You also can enable by below command :

sudo ufw allow 'Nginx HTTP'

You also can verify by execute command :

sudo ufw status

It will show you following output :

Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)

Alternate option is, you can directly allow default ports by following commands:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

After that, we will test nginx is installed properly and working or not. So open your web browser and type below url and it will open nginx default page as given below.

http://YOUR_SERVER_IP/
Debian 9 Nginx Default Page

Step 2 – Installing MariaDB#

With the release of Debian 9, MySQL was replaced with MariaDB as the default database system.

Execute following command to install mysql server.

sudo apt install mariadb-server

By default, MariaDB service will start automatically. You can verify it by typing:

sudo systemctl status mariadb

After that, now run MySQL pre-installed simple security script. Which will remove defaults and lock down access to your database system. Execute given command :

sudo mysql_secure_installation

It will prompt you to configure password validation policy and more settings for MySQL. It will also ask to remove unnecessary users, disallow remote root login and test databases so go throughout it.

Finally, your database set up is completed and we will go ahead to install last component of the LEMP stack.

Step 3 – Installing PHP and Configuring Nginx#

PHP component will process code to display dynamic content. Since Nginx does not contain native PHP processing like some other web servers, we will need to install fpm. We will also install an additional helper package that will allow PHP to communicate with our MySQL database backend.

Execute below command with helper packages so it can communicate with Nginx and can make connection with MariaDB database.

sudo apt install php-fpm php-opcache php-cli php-gd php-curl php-mysql

After complete this installation you need to do few configuration changes in order to tell Nginx to use the PHP processor for dynamic content.

We will make change in Nginx server blocks at server block level. Create a new file server block configuration file at /etc/nginx/sites-available/ directory. Give file as your domain name like yourdomain.com

sudo nano /etc/nginx/sites-available/yourdomain.com

Add the following content to this file :

server {
	listen 80;
	root /var/www/html;
	index index.php index.html index.htm index.nginx-debian.html;
	server_name yourdomain.com;

	location / {
			try_files $uri $uri/ =404;
	}

	location ~ \.php$ {
			include snippets/fastcgi-php.conf;
			fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
	}

	location ~ /\.ht {
			deny all;
	}
}

Save and close the file. Now, you have to create a symbolic link from your new server block configuration file to enable your new server block. It will be stored at /etc/nginx/sites-available/ directory. Execute below command :

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

At last, run below command to test new configuration file for syntax errors :

sudo nginx -t

If everything is okay then it will show output as following :

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

You must need to reload Nginx in order to take changes in effect by below command :

sudo systemctl reload nginx

Step 4 – Testing Configuration#

Now your LEMP stack completely set up. So we will check it by creating a .php file. Create a test PHP file called info.php in your document root:

sudo nano /var/www/html/info.php

Add following code lines to this file :

<?php
phpinfo();
?>

Save and close this file. Now visit this page using your server’s public ip address like below :

http://YOUR_SERVER_IP_OR_DOMAIN/info.php

You should see a web page that has been generated by PHP with information about your server:

How to Install LEMP Stack on Debian 9

Finally, you finished it.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install LAMP Stack on Debian 9
Next Article   How to Install Apache on Debian 9 Server

Related Posts

  • How to Install Php 8 on Debian 10

    How to Install PHP 8 on Debian 10

    January 2, 2021
  • How to Install Php 8 on Ubuntu 20.04

    How to Install PHP 8 on Ubuntu 20.04

    December 28, 2020
  • How to Install GIMP on Debian 10

    How to Install GIMP 2.10 on Debian 10

    December 27, 2020

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on Debian 10
    How to Install Python 3.9 on Debian 10 December 25, 2020
  • How to Install Php 8 on Debian 10
    How to Install PHP 8 on Debian 10 January 2, 2021
  • How to Install Python 3.9 on CentOS 8
    How to Install Python 3.9 on CentOS 8 December 31, 2020
  • How to Install Php 8 on Ubuntu 20.04
    How to Install PHP 8 on Ubuntu 20.04 December 28, 2020
  • How to Install GIMP on Debian 10
    How to Install GIMP 2.10 on Debian 10 December 27, 2020
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook