How to Install Lighttpd with PHP-FPM 7 and MySQL 5.7 on Ubuntu 18.04 LTS
This tutorial exists for these OS versions
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 18.04 (Bionic Beaver)
- Ubuntu 16.04 (Xenial Xerus)
- Ubuntu 14.04 LTS (Trusty Tahr)
- Ubuntu 12.04 LTS (Precise Pangolin)
On this page
Lighttpd (pron. as lighty) is an open-source web server optimized for high performance. It's secure, fast, standard-compliant, and very flexible webserver designed for a high-performance environment. It was very low memory footprints (compared to other webservers) and effective management of CPU-load.
The Lighttpd web server is one of the perfect solutions for every server that is suffering load problems. It comes with advanced feature-set such as FastCGI, CGI, Auth, Output-Compression, URL Rewriting, and many more.
In this tutorial, we will show you how to install the Lighttpd with PHP-FPM 7.2 and MySQL Server 5.7 on the latest Ubuntu 18.04. Also, we will show you the installation and configuration of phpMyAdmin with Lighttpd webserver.
Prerequisite
For this guide, we will install the Lighttpd Stack on the Ubuntu 18.04 Server with 2GB of RAM, 25GB free disk space, and 2CPUs. Also, you must have the root privileges for modifying the system.
Run the sudo command below to get the root privileges.
sudo -s
What we will do?
- Install MySQL Server 5.7
- Install Lighttpd Webserver
- Install PHP-FPM 7.2
- Setup PHP-FPM with Lighttpd
- Add MySQL Support to PHP-FPM
- Install phpMyAdmin
Step 1 - Install MySQL Server 5.7
First, we will install the MySQL Server to the Ubuntu system. It's available by default on the Ubuntu repository, you can install the MySQL packages using the apt command below.
apt install mysql-server mysql-client -y
Once the installation is complete, start the MySQL service and add it to the system boot.
systemctl start mysql
systemctl enable mysql
The MySQL service is up and running.
Next, we will set up the password for the root user on MySQL. Run the 'mysql_secure_installation' command below.
mysql_secure_installation
Now you will be asked for some questions about the MySQL deployments.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of the password
and allows the users to set only those passwords which are
secure enough. Would you like to set up a VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: --> Press Enter Here
New password: --> Type Your Password
Re-enter new password: --> Repeat Your 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.
Step 2 - Install Lighttpd Webserver
In this step, we will install the Lighttpd from the official Ubuntu repository.
Install the Lighttpd server using the apt command below.
apt install lighttpd -y
Once the installation is complete, check the Lighttpd service status using the command below.
systemctl is-enabled lighttpd
systemctl status lighttpd
The Lighttpd service is up and running, it's automatically added to the system boot during the installation.
Now open your web browser and type the server IP address on the address bar.
And you will get the default index.html page of Lighttpd webserver.
As can be seen, you got some information related to the Lighttpd installation.
- The document root is on the '/var/www/html' directory.
- The Lighttpd configuration is located at 'etc/lighttpd/lighttpd.conf'.
- All available module for Lighttpd located at the '/etc/lighttpd/conf-available' directory.
- And all enabeld module is located at the '/etc/lighttpd/conf-enabled' directory.
Step 3 - Install PHP-FPM 7.2
In this step, we will install the PHP-FPM 7.2 to the Ubuntu system. It will be used as a backend processing language with Lighttpd.
Install PHP-FPM 7.2 using the apt command below.
apt install php-fpm
Once the installation is complete, edit the 'php.ini' configuration for PHP-FPM using the vim editor.
vim /etc/php/7.2/fpm/php.ini
Uncomment the 'cgi.fix_pathinfo' option and change the value to '0'.
cgi.fix_pathinfo=0
Save and close.
Next, restart the PHP-FPM service and add it to the system boot.
systemctl restart php7.2-fpm
systemctl enable php7.2-fpm
The PHP-FPM is up and running on the Ubuntu system, check it using the following command.
netstat -pl | grep php
systemctl status php7.2-fpm
As a result, the PHP-FPM is running under the system sock file '/run/php/php7.2-fpm.sock'.
Step 4 - Setup Lighttpd with PHP-FPM
After installing the PHP-FPM packages, we will configure the PHP-FPM with the Lighttpd webserver. We will enable the PHP-FastCGI module for the Lighttpd web server and edit the default configuration for the PHP-FPM backend.
Go to the Lightttpd configuration directory '/etc/lighttpd/conf-available/'.
cd /etc/lighttpd/conf-available/
Copy the default FastCGI configuration for backup and edit the real file using vim editor.
cp 15-fastcgi-php.conf 15-fastcgi-php.conf.orig
vim 15-fastcgi-php.conf
Now change the configuration as below.
# -*- depends: fastcgi -*-
# /usr/share/doc/lighttpd/fastcgi.txt.gz
# http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi
## Start an FastCGI server for php (needs the php5-cgi package)
fastcgi.server += ( ".php" =>
((
"socket" => "/run/php/php7.2-fpm.sock",
"broken-scriptfilename" => "enable"
))
)
Save and close.
Next, enable the FastCGI and FastCGI-PHP modules for Lighttpd using the following command
lighttpd-enable-mod fastcgi
lighttpd-enable-mod fastcgi-php
The 'lighttpd-enable-mod' command will create a symlink configuration files to the '/etc/lighttpd/conf-enabled/' directory. Check it using the command below.
ls -lah /etc/lighttpd/conf-enabled/
And the FastCGI and FastCGI-PHP module for Lighttpd has been enabled, now restart the Lighttpd service to apply the new configuration.
systemctl force-reload lighttpd
As a result, the configuration of Lighttpd with PHP-FPM has been completed.
Step 5 - Add MySQL Support in PHP-FPM
In this step, we will install additional PHP packages to the Ubuntu system. We will add the MySQL support to the PHP-FPM.
Install additional PHP packages using the apt command below.
apt install php-apcu php-mysql php-curl php-gd php-intl php-pear php-imagick php-imap php-memcache php-pspell php-recode php-sqlite3 php-tidy php-xmlrpc php-xml php-mbstring php-gettext -y
After that, restart the PHP-FPM and Lighttpd services using the systemctl command below.
systemctl restart php7.2-fpm
systemctl restart lighttpd
As a result, the Additional PHP package has been installed, and the MySQL support for PHP has been added.
Step 6 - Testing with phpinfo
Now create a new PHP file on the document root directory '/var/www/html/info.php' using vim editor.
vim /var/www/html/info.php
Paste the phpinfo script below.
<?php
phpinfo();
?>
Save and close.
Next, open your web browser and type the server IP address following by the 'info.php' file as below.
Now you will get information about your PHP installation.
Scroll down the page and you will get the MySQL and MySQLnd support section as below.
The installation of Lighttpd with PHP-FPM and MySQL Server has been completed successfully.
Step 7 - Install phpMyAdmin with lighttpd
In this step, we will install the phpMyAdmin alongside the Lighttpd web server.
The phpMyAdmin packages are available by default on the Ubuntu repository, install using the apt command below.
apt install phpmyadmin
During the phpMyAdmin installation, you will be asked for some configuration.
For the web server configuration, select the 'lighttpd' and choose 'OK'.
Now configure the phpMyAdmin with the dbconfig-common, choose 'YES'.
Type the password for 'phpmyadmin' MySQL user and choose 'OK' again.
Repeat the password and choose 'OK' to complete.
Once the installation is complete, you will get an error message as below.
To solve the phpMyAdmin installation error, run the command below.
systemctl force-reload lighttpd
apt install phpmyadmin -y
The phpMyAdmin will be reinstalled without any error.
Next, grant all privileges of MySQL databases to the 'phpmyadmin' user.
Login to the MySQL shell with the root user as below.
mysql -u root -p
Allow all access of the 'phpmyadmin' user to the MySQL system using the following query.
grant all privileges on *.* to 'phpmyadmin'@'localhost' with grant option;
flush privileges;
Now type 'exit' to log out from the MySQL shell.
Next, open your web browser and type the server IP address following by the '/phpmyadmin' path as below.
You will get the phpMyAdmin login page as below.
Type the 'phpmyadmin' user and your password, then click the 'Go' button to log in.
Now you will get the phpMyAdmin dashboard as below.
As a result, the phpMyAdmin installation and configuration with Lighttpd webserver, PHP-FPM, and MySQL server on Ubuntu 18.04 has been completed successfully.