This guide will show you how to install Nginx, MariaDB, and PHP7.3 (LEMP stack) on Debian 10. A software stack is a collection of software tools that have been packaged together. LEMP stands for Linux, Nginx, MariaDB/MySQL, and PHP, which are all free to use and open source. It’s a popular software stack for creating dynamic websites and web apps. The operating system is Linux, the webserver is Nginx, the database server is MariaDB/MySQL, and PHP is the server-side programming language that generates dynamic web pages.
All four components are open-source and free. We’ll use MariaDB instead of MySQL because MySQL is now controlled by Oracle, and there’s a danger Oracle will transform it into a closed-source product.
Prerequisites of Installing LEMP Stack on Debian 10
You’ll need a Debian 10 OS operating on your local computer or a remote server to follow along with this guide.
If you’re looking for a VPS (Virtual Private Server), use this exclusive link to sign up for a Vultr account and get $50 in free credit (for new users only). If you require a domain name to set up a LEMP stack, I recommend NameCheap because their prices are reasonable and they provide free who is privacy protection for life.
Please keep in mind that when installing software on Debian, you must have root privileges. To switch to the root user, add sudo to the beginning of a command or use the su – command.
Step 1: Update Software Packages
It’s a good idea to update the repository and software packages before installing the LEMP stack. On your Debian 10 operating system, run the following command.
# sudo apt update # sudo apt upgrade
Step 2: Install Nginx Web Server on Debian 10
Nginx is a popular high-performance web server these days. It can also serve as a reverse proxy and a cache server. To install the Nginx Web server, run the command below.
# sudo apt install nginx
Nginx should start automatically after installation. Systemctl can be used to check its status.
# systemctl status nginx
Sample output:
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en Active: active (running) since Sat 2019-08-10 06:20:26 UTC; 54s ago Docs: man:nginx(8) Main PID: 19713 (nginx) Tasks: 2 (limit: 1149) Memory: 4.6M CGroup: /system.slice/nginx.service ├─19713 nginx: master process /usr/sbin/nginx -g daemon on; master_pr └─19714 nginx: worker process
Use systemctl to start it if it isn’t already running.
# sudo systemctl start nginx # sudo systemctl enable nginx
Check Nginx version:
# sudo nginx -v
Output:
nginx version: nginx/1.14.2
In the browser address bar, type in the public IP address of your Debian 10 server. The default “Welcome to nginx” Web page should appear, indicating that the Nginx Web server is up and operating. If you’re installing LEMP on a local Debian 10 computer, type 127.0.0.1 or localhost into the address bar of your browser.
A firewall may be blocking inbound requests to TCP port 80 if the connection is denied or fails to complete. To open TCP port 80 using the iptables firewall, perform the following command.
# sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
If you are using UFW firewall, then run this command to open TCP port 80.
# sudo ufw allow http
Now we need to make www-data (the Nginx user) the document root owner (also known as web root). It is owned by the root user by default. (Note that Nginx uses /usr/share/nginx/html/ as the web root by default, whereas Apache uses /var/www/html/ as web root.)
# sudo chown www-data:www-data /usr/share/nginx/html/ -R
Step 3: Install MariaDB Database Server on Debian 10
MariaDB is a MySQL drop-in replacement. To install it on Debian 10, run the command below.
# sudo apt install mariadb-server mariadb-client
MariaDB server should start automatically after installation. To verify its status, use systemctl.
# systemctl status mariadb
Output:
● mariadb.service - MariaDB 10.3.15 database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: Active: active (running) since Sat 2019-08-10 06:38:58 UTC; 13s ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Main PID: 20669 (mysqld) Status: "Taking your SQL requests now..." Tasks: 31 (limit: 1149) Memory: 77.7M CGroup: /system.slice/mariadb.service └─20669 /usr/sbin/mysqld
If it isn’t already running, perform the following command:
# sudo systemctl start mariadb
Run the following command to make MariaDB start automatically when your computer boots.
# sudo systemctl enable mariadb
Now run the security script that comes with the installation.
# sudo mysql_secure_installation
When prompted for the MariaDB root password, press Enter because the root password has not yet been configured. Then type y to set the MariaDB server’s root password.
After that, all you have to do is press Enter to answer the remaining questions. The anonymous user will be removed, as well as remote root login and the test database. This is a necessary step for MariaDB database security. (The letter Y is capitalised, indicating that it is the default answer.)
By default, the MariaDB package on Debian utilises unix socket to authenticate user login, which means you can log into MariaDB console using the OS’s username and password. As a result, you can login using the following command without entering the MariaDB root password.
# sudo mariadb -u root
or
# sudo mysql -u root
To exit, run
# exit;
Check MariaDB server version information.
# mariadb --version
Output:
mariadb Ver 15.1 Distrib 10.3.15-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Step 4: Install PHP7.3 on Debian 10
PHP7.3 is the most recent stable version of PHP at the time of writing, and it offers slight performance improvements over prior versions. To install PHP7.3 and some standard PHP extensions from the Debian 10 repository, run the following command.
# sudo apt install php7.3 php7.3-fpm php7.3-mysql php-common php7.3-cli php7.3-common php7.3-json php7.3-opcache php7.3-readline
Check the PHP version number.
# php --version
Output:
PHP 7.3.4-2 (cli) (built: Apr 13 2019 19:05:48) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.4, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.3.4-2, Copyright (c) 1999-2018, by Zend Technologies
Now start php7.3-fpm.
# sudo systemctl start php7.3-fpm
Enable auto-start at boot time.
# sudo systemctl enable php7.3-fpm
Check status:
# systemctl status php7.3-fpm
Step 5: Create a Nginx Server Block
A virtual host in Apache is analogous to a Nginx server block. We won’t use the default server block because it’s insufficient for running PHP code, and modifying it makes things worse. So, use the following command to remove the default symlink in the sites-enabled directory. (/etc/nginx/sites-available/default is still available.)
# sudo rm /etc/nginx/sites-enabled/default
Then, using a command line text editor like Nano, create a new server block file under the /etc/nginx/conf.d/ directory.
# sudo nano /etc/nginx/conf.d/default.conf
Copy and paste the following content into the document. Nginx will listen on IPv4 port 80 and IPv6 port 80 with a catch-all server name if you use the following snippet.
server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html/; index index.php index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { fastcgi_pass unix:/run/php/php7.3-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; include snippets/fastcgi-php.conf; } # A long browser cache lifetime can speed up repeat visits to your page location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ { access_log off; log_not_found off; expires 360d; } # disable access to hidden files location ~ /\.ht { access_log off; log_not_found off; deny all; } }
The file should be saved and closed. (Press Ctrl+O to save a file in Nano text editor, then Enter to confirm.) Press Ctrl+X to exit.) Then put Nginx setups to the test.
# sudo nginx -t
Reload Nginx if the test is successful.
# sudo systemctl reload nginx
Step 6: Test PHP
We need to build an info.php file in the Web root directory to test PHP scripts with the Nginx server.
# sudo nano /usr/share/nginx/html/info.php
In the file, paste the PHP code below.
<?php phpinfo(); ?>
The file should be saved and closed.
Now type server-ip-address/info.php into the address bar of your browser. Substitute your actual IP address for sever-ip-address. If you’re doing this on your own computer, type 127.0.0.1/info.php or localhost/info.php.
The PHP information on your server should be visible. This means that PHP programmes will work with the Nginx web server. Zend OPcache is activated, as you can see.
Nginx Automatic Restart
If your Nginx process is killed for whatever reason, you must perform the following command to restart it.
# sudo systemctl restart nginx
Rather than executing this command manually, we can make Nginx restart automatically by modifying the nginx.service systemd service unit. We build a new directory to modify the normal systemd service settings.
# sudo mkdir -p /etc/systemd/system/nginx.service.d/
Then, in this directory, make a file.
# sudo nano /etc/systemd/system/nginx.service.d/restart.conf
In the file, add the following lines, which will cause Nginx to restart 5 seconds after a fault is detected.
[Service] Restart=always RestartSec=5s
The file should be saved and closed. Then reload systemd.
# sudo systemctl daemon-reload
To see if this works, kill Nginx using:
# sudo pkill nginx
Then look at Nginx’s status. Nginx will be automatically restarted.
# systemctl status nginx
Wrapping Up
Congrats! On Debian 10 Buster, you’ve successfully installed Nginx, MariaDB, and PHP7.3. You should delete the info.php file right now to protect your server from prying eyes.
# sudo rm /usr/share/nginx/html/info.php
Thats it :).