Skip to main content

Quick Setup LEMP

This is a quick guide on how to setup a LEMP stack. The following assumes you are using Debian 11 and have a user with sudo privileges.


Installing Packages


First update the repos and upgrade any packages to ensure everything is in sync:

sudo apt update
sudo apt upgrade

Then we move onto installing all the necessary packages required for the LEMP Stack.

sudo apt install php php-fpm nginx mariadb-server
Different PHP version requirements on Debian

As of writing, the Debian repos only have version 7.4. Verify what version you need before installation. If you require a newer (8.0 - 8.1) or earlier (5.6-7.3) version of PHP, you can add the Sury Debian repo using the following commands:

sudo apt-get update
sudo apt-get -y install apt-transport-https lsb-release ca-certificates curl
sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt-get update

To install the version of your choice, append the version number you require to php e.g. php8.1, php7.3. It is also the same for installing extensions e.g. php8.1-xml, php7.3-xml

sudo apt install php8.1 php8.1-xml

MariaDB is an Open Source drop in replacement for MySQL. Most commands that work on MySQL will work on MariaDB. Unless you have a particular reason to use MySQL then it's preferred to install MariaDB.

Setting up NGINX


NGINX should be enabled and active upon installation. No further action is required to setup NGINX.

To verify it is working, enter your server's IP address into your favourite browser.

What is the local network IP Address

You can check the server's local network IP address by typing:

ip a

This will give you a list of interfaces, typically the IP address will be under the interface called enp3s0 or something similar. In the output below, the IPv4 address is 192.168.32.32

2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether d8:cb:8a:e7:7a:81 brd ff:ff:ff:ff:ff:ff
inet 192.168.32.32/24 brd 192.168.32.255 scope global dynamic enp2s0
valid_lft 31149sec preferred_lft 31149sec
inet6 fe80::dacb:8aff:fee7:7a81/64 scope link
valid_lft forever preferred_lft forever
What is the Public IP Address

The server may also have a Public IP address assigned to it's interface which can be checked in the above method.

You can tell if it's a Public IP address if it isn't in the RFC1918 private address ranges:
10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255

Another easy method to figure out the Public IP is to type in the following command:

curl -4 icanhazip.com

 This will reply back with your Public IPv4 address.

 9_03_22_12_17_12.png

If you see the above page appear in your browser, then NGINX has been successfully installed.

Setting up MariaDB


Your MariaDB server is ready to go. A recommended procedure is to secure your MariaDB instance which can be invoked with:

sudo mysql_secure_installation

It will then take you through a small script to secure your MariaDB server for production. Adjust accordingly if you have different needs.

Enter current password for root (enter for none): [ENTER]
Switch to unix_socket authentication [Y/n] n
Change the root password? [Y/n] n
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

To verify MariaDB is running, type in the following:

sudo mysql -u root

Then you should be brought to the MariaDB prompt, MariaDB [(none)]> will show at the cursor. If so, then you have successfully setup MariaDB.

Setting up PHP


Once the packages are installed for PHP, all that's left to do is to verify that PHP Processing is working.

First create a PHP file in the /var/www/html/ directory to display information about the current PHP configuration.

Using your favourite editor make a file called test.php and enter the following contents:

<?php
phpinfo();

From there, you will need to edit your server block in NGINX to process PHP files. Add the following code snippet into the default server block located at /etc/nginx/sites-available/default:

server {
# ...

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

}

Reload the NGINX service with sudo service nginx reload to apply the changes.

Then go to http://<IP-of-server>/test.php and it should display the your PHP configuration as shown below.

If configuration page doesn't display, ensure the PHP version for the socket is correct in the NGINX server block and double check that the php-fpm.service is enabled and is active.

PHP_Info.png

Don't forget to delete the test.php file once you've confirmed that it is working.

If you see this page, then you know that PHP-FPM has been setup correctly.

Conclusion


You have successfully setup a LEMP stack, which is the basis of many web services out there. All that's left to do is to start hosting your own services or create something new.