How To Install Lighttpd, PHP, and MariaDB on Ubuntu 20.04 LTS

Install Lighttpd, PHP, and MariaDB on Ubuntu 20.04

In this tutorial, we will show you how to install Lighttpd, PHP, and MariaDB on Ubuntu 20.04 LTS. For those of you who didn’t know, Lighttpd is a free, open-source, secure, and standards-compliant web server designed for high-performance environments. Compared to other alternative web servers, Lighttpd consumes very few resources and capable of serving large loads and when installed alongside PHP and MySQL or MariaDB it can serve millions of connections reliably. Lighty also has many modules that extend its capabilities.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you through the step-by-step installation of PrestaShop on Ubuntu 20.04 (Focal Fossa). You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.

Install Lighttpd, PHP, and MariaDB on Ubuntu 20.04 LTS Focal Fossa

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade

Step 2. Installing Lighttpd on Ubuntu 20.04.

Lighttpd is available within Ubuntu’s default software repositories. Run the following commands below to install it:

sudo apt install lighttpd

Once the Lighttpd packages have been installed, we can start and enable the service to automatically start at boot:

sudo systemctl start lighttpd
sudo systemctl enable lighttpd

Next, add a lighttpd user and group:

groupadd lighttpd
useradd -g lighttpd -d /var/www/html -s /sbin/nologin lighttpd
chown -R lighttpd:lighttpd /var/www/html/

Step 3. Installing MariaDB.

Run the following command to install MariaDB on your Ubuntu system:

sudo apt install mariadb-server

By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation script. you should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB:

mysql_secure_installation

Configure it like this:

- Set root password? [Y/n] y
- 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

Next, we will need to log in to the MariaDB console and create a database. Run the following command:

mysql -u root -p

Step 4. Installing PHP 8.

Now we add a third-party PPA to Ubuntu:

sudo add-apt-repository ppa:ondrej/php

Then, you can update the packages cache in the system and install PHP packages:

sudo apt update
sudo apt install php8.0 php8.0-fpm libapache2-mod-php8.0

After installing, you can restart Apache using the following commands:

sudo systemctl restart apache2

Once successfully installed, you can confirm the using the following command:

php -v

Next, rename the default PHP FPM Pool to align with the webserver name:

mv /etc/php/8.0/fpm/pool.d/www.conf /etc/php/8.0/fpm/pool.d/lighttpd.conf

Then, edit the Unix socket associated with the pool :

nano /etc/php/8.0/fpm/pool.d/lighttpd.conf

Change four lines:

  • Change the top line inside the brackets that sets the pool name from [www] to [lighttpd]
  • Change the line user = www-data to user = lighttpd
  • Change the line group = www-data to group = lighttpd
  • Change the line listen = /run/php/php8.0-fpm.sock to listen = /run/php/php8.0-lighttpd-fpm.sock

Next, open the FastCGI configuration file /etc/lighttpd/conf-available/15-fastcgi-php.conf using your preferred text editor:

nano /etc/lighttpd/conf-enabled/15-fastcgi-php.conf

Find the following lines:

"bin-path" => "/usr/bin/php-cgi",
"socket" => "/var/run/lighttpd/php.socket",

Then replace those values with:

"host" => "127.0.0.1",
"port" => "9000",

Once is done, now enable the FastCGI and FastCGI-PHP modules with the following commands:

lighty-enable-mod fastcgi
lighty-enable-mod fastcgi-php

Restart Lighttpd and PHP to apply all the configuration changes:

sudo systemctl restart php8.0-fpm
sudo systemctl lighttpd restart

Finaly, add a test PHP file to ensure that PHP is running:

nano /var/www/html/test.php

Add the following line:

<?php phpinfo();

To check the configuration, visit http://your-ip-address/test.php and find a PHP information page.

Step 5. Configure Firewall.

Run the following commands to allow HTTP (80) and HTPPS (443) requests through the firewall.

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

Congratulations! You have successfully installed LLMP. Thanks for using this tutorial for installing LLMP (Lighttpd, PHP, and MariaDB) on Ubuntu 20.04 LTS Focal Fossa system. For additional help or useful information, we recommend you to check the official Lighttpd website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!