Overview
The LEMP stack consists of Linux, Nginx, MySQL, and PHP. It is similar to the LAMP stack, but instead of using the Apache web server, it uses Nginx instead. There are many benefits to using Nginx including performance, and security improvements.
Requirements
1. Install the Packages
Install Nginx, MariaDB, PHP, and the PHP package for MySQL.
$ sudo apt install nginx mariadb-server php-fpm php-mysql python3-certbot-nginx
2. Configure Nginx
Open the configuration file. Replace example.com
with your domain name.
$ sudo nano /etc/nginx/sites-available/example.com
Add the following configuration. Save, then exit. Replace example.com
with your domain name.
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
}
Enable the configuration.
$ sudo ln -sf /etc/nginx/sites-{available,enabled}/example.com
Remove the default website configuration.
$ sudo rm /etc/nginx/sites-enabled/default
Reload Nginx so it will see the new configuration.
$ sudo systemctl reload nginx
Obtain an SSL certificate through Certbot, this will enable HTTPS on the domain. Replace example.com
with your domain name, and user@example.com
with your email address.
$ sudo certbot -d example.com -m user@example.com --agree-tos --noninteractive --nginx --redirect
3. Configure MariaDB
Run the MariaDB installation script.
$ mysql_secure_installation
Press enter when prompted for the current root password.
Enter current password for root (enter for none):
Specify "n" to not set a root password on the database.
Set root password? [Y/n] n
Press enter to disable anonymous users.
Remove anonymous users? [Y/n]
Press enter to disable root logins from remote machines.
Disallow root login remotely? [Y/n]
Press enter to remove the test database.
Remove test database and access to it? [Y/n]
Press enter to reload the privilege tables that store the database's users and permissions.
Reload privilege tables now? [Y/n]
4. Test the Server
Create a PHP test script.
$ sudo nano /var/www/html/test.php
Save the following code and exit.
<?php
phpinfo();
?>
Open https://example.com/test.php
in your browser. Replace example.com with your domain name.
You should see a screen listing the version of PHP and other information. If so, you can remove the test script.
$ sudo rm -f /var/www/html/test.php