How to Install Lighttpd with PHP-FPM and MySQL on Ubuntu 20.04 LTS

Lighttpd is an open-source web server that focused on simplicity and high performance. It's a light webserver with small and low memory consumption but still remaining standard-compliance, security, and flexibility. The Lighttpd web server is a part of the LLMP Stack, which stands for Linux, Lighttpd, MySQL/MariaDB, and PHP/PHP-FPM.

In this tutorial, we will show you how to install and configure the LLMP Stack on the Ubuntu 20.04 Server. We will install the Lighttpd web server with the MariaDB database server and the PHP-FPM on the latest version of the Ubuntu 20.04 server.

Prerequisites

  • Ubuntu 20.04 server
  • Root privileges
  • Understanding the basic of Ubuntu/Debian Server

What will we do?

  • Install Lighttpd Web Server
  • Install and Configure MariaDB Database Server
  • Install and Configure PHP-FPM
  • Setup Lighttpd and PHP-FPM
  • Testing

Step 1 - Install Lighttpd

First, we will install the Lighttpd webserver to our Ubuntu 20.04 Server. The Lighttpd packages are available by default on the Ubuntu repository, update all available repositories, and install Lighttpd using the apt command below.

sudo apt update
sudo apt install lighttpd

Once all installation is completed, start the Lighttpd service and add it to the system boot.

systemctl start lighttpd
systemctl enable lighttpd

The Lighttpd service is up and running, check it using the following command.

systemctl status lighttpd

Below is the result you will get.

Start Lighttpd Webserver Service

Next, add the HTTP, HTTPS, and SSH services to the ufw firewall.

sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https

Enable the ufw firewall service using the command below.

sudo ufw enable

Type 'y' to enable the ufw firewall, and as a result, you've enabled the firewall on the Ubuntu system.

ufw firewall open http and https ports

Now open your web browser and type the server IP address on the address bar.

http://10.3.3.40/

And you will get the Lighttpd default index.html page.

Lighttpd default Index.hmtl

As a result, the Lighttpd web server has been installed on the Ubuntu 20.04 System.

Step 2 - Install and Configure MySQL Server

In this step, we will install the MySQL database server and set up the default root user for MySQL.

Install MySQL Server using the apt command below.

sudo apt install mysql-server mysql-client

Once all installation is completed, start the MySQL service and add it to the system boot.

systemctl start mysql
systemctl enable mysql

Now check the MySQL service using the command below.

systemctl status mysql

Below is the result you will get.

Start MySQL Service

As a result, the MySQL database server is up and running on the Ubuntu system.

Next, run the 'mysql_secure_installation' command below to set up the MySQL root password.

mysql_secure_installation

Press the 'Enter' button to continue, type your new password for the MySQL root user, and type 'Y' for all options.

Press y|Y for Yes, any other key for No: 
Please set the password for root here.

New password:
Re-enter new password:

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

As a result, the MySQL root password has been configured.

Next, log in to the MySQL shell using the root user as below.

mysql -u root -p

Now check all available users on MySQL using the following query.

select User,Host from mysql.user;

And you will get list default users on the MySQL Server. Now type 'exit' to log out from the MySQL shell.

MySQL Check users

As a result, the installation and configuration of MySQL Server for the LLMP Stack has been completed successfully.

Step 3 - Install and Configure PHP-FPM

In this step, we will install PHP-FPM packages and configure them to make it works with the Lighttpd web server.

Install PHP-FPM packages to the Ubuntu system using the apt command below.

sudo apt install php-fpm php-cgi php-mysql

The command will automatically install the latest PHP-FPM 7.4.

Once all installation is completed, go to the '/etc/php/7.4/fpm' directory and edit the configuration 'php.ini' using vim editor.

cd /etc/php/7.4/fpm/
vim php.ini

Uncomment the following line to enable FastCGI support.

cgi.fix_pathinfo=1

Save and close.

Next, start the PHP-FPM service and add it to the system boot.

systemctl start php7.4-fpm
systemctl enable php7.4-fpm

Configure PHP-FPM

Now check the PHP-FPM service using the following command.

ss -pl | grep php
systemctl status php7.4-fpm

Below is the result you will get.

Check PHP-FPM Service

As can be seen, the PHP-FPM service is up and running on the Ubuntu 20.04 system. And by default, it's running under the sock file '/var/run/php/php7.4-fpm.sock'.

And the installation of PHP-FPM on ubuntu 20.04 has been completed.

Step 4 - Configure Lighttpd and PHP-FPM

For this step, we will configure the Lighttpd with PHP-FPM.

Now go to the '/etc/lighttpd/conf-available' directory, and you will get some configurations.

cd /etc/lighttpd/conf-available/

Copy the default PHP FastCGI configuration '15-fastcgi-php.conf' and edit using the vim editor.

cp 15-fastcgi-php.conf 15-fastcgi-php.conf.orig
vim 15-fastcgi-php.conf

Now you will get the default PHP FastCGI configuration as below.

fastcgi.server += ( ".php" => 
    ((
        "bin-path" => "/usr/bin/php-cgi",
        "socket" => "/var/run/lighttpd/php.socket",
        "max-procs" => 1,
        "bin-environment" => (
            "PHP_FCGI_CHILDREN" => "4",
            "PHP_FCGI_MAX_REQUESTS" => "10000"
        ),
        "bin-copy-environment" => (
            "PATH", "SHELL", "USER"
        ),
        "broken-scriptfilename" => "enable"
    ))
)

The default configuration will make the Lighttpd spawn the PHP-CGI on itself.

To make the Lighttpd works with the PHP-FPM service, replace the default PHP-CGI configuration using the following configuration.

fastcgi.server += ( ".php" =>
        ((
                "socket" => "/var/run/php/php7.4-fpm.sock",
                "broken-scriptfilename" => "enable"
        ))
)

Save and close.

Next, enable the Lighttpd module fastcgi and fastcgi-php using the command below.

lighttpd-enable-mod fastcgi
lighttpd-enable-mod fastcgi-php

The command will make a symlink of modules configuration to the '/etc/lighttpd/conf-enabled' directory.

You can check the '/etc/lighttpd/conf-enabled' directory using the following command.

ls -lah /etc/lighttpd/conf-enabled/

And you will get both fastcgi and fastcgi-php configurations on the 'conf-enabled' directory.

ENable FastCGI and FastCGI PHP Lighttpd

Next, to apply the new setup and configuration, restart the Lighttpd service.

systemctl restart lighttpd

Make sure there is no error.

And as a result, the configuration of Lighttpd with PHP-FPM has been completed successfully.

Step 5 - Testing

To test our LLMP Stack installation, we will create a new PHP info script file on the default document root directory '/var/www/html'.

Go to the '/var/www/html' directory and create a new php file 'info.php' using vim editor.

cd /var/www/html/
vim info.php

Paste the following script.

<?php
phpinfo();
?>

Save and close.

Next, open your web browser and type the server IP address with the path '/info.php' as below.

http://10.10.10.30/info.php

And you will get the result as below.

phpinfo

As can be seen, you can see all info about your Lighttpd and PHP-FPM installation.

And as a result, the installation of LLMP Stack on Ubuntu 20.04 has been completed successfully.

Share this page:

Suggested articles

0 Comment(s)

Add comment