Lighttpd is a free, open-source, and high-performance web server designed for speed-critical environments. It is lightweight, secure, fast, and consumes very few resources compared to other web servers.Â Lighttpd also supports FastCGI, CGI, Output-Compression, Auth and URL-Rewriting, which makes it the perfect web server. Lighttpd can handle up to 10000 connections in parallel on a single server.
If you are looking for a fast, efficient, and secure web server, then Lighttpd is the best choice for you.
In this tutorial, we will explain how to install Lighttpd web server with PHP and PHP-FPM support on Ubuntu 18.04.
In This Article
Step 1 – Installing Lighttpd
By default, Lighttpd is available in the Ubuntu 18.04 default repository. You can install it by just running the following command:
apt-get install lighttpd -y
Once the installation is completed, check the status of Lighttpd with the following command:
systemctl status lighttpd
If everything goes fine, you should get the following output:
â— lighttpd.service - Lighttpd Daemon Â Loaded: loaded (/lib/systemd/system/lighttpd.service; enabled; vendor preset: enabled) Â Active: active (running) since Sat 2019-05-25 09:18:47 UTC; 20s ago Main PID: 1860 (lighttpd) Â Â Tasks: 1 (limit: 1114) Â CGroup: /system.slice/lighttpd.service Â Â Â Â Â Â Â Â Â â””â”€1860 /usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf May 25 09:18:47 ubuntu1804 systemd: Starting Lighttpd Daemon... May 25 09:18:47 ubuntu1804 systemd: Started Lighttpd Daemon.
You can check the version of the Lighttpd installed on your server with the following command:
You should see the following output:
lighttpd/1.4.45 (ssl) - a light and fast webserver Build-Date: FebÂ 6 2018 12:41:51
You can also check Lighttpd by visiting the URL http://your-server-ip on your web browser. You should see the Lighttpd default page per the following screen:
The main configuration file for Lighttpd is located at /etc/lighttpd/lighttpd.conf.
This is the main configuration file from where you can change default document root, port, and index file, and enable/disable any modules per your requirements.
You can also see all the configuration files using the tree command. First, install the tree package with the following command:
apt-get install tree -y
Once installed, you can see all the configuration files as shown below:
You should see the following output:
/etc/lighttpd/ â”œâ”€â”€ conf-available â”‚Â Â â”œâ”€â”€ 05-auth.conf â”‚Â Â â”œâ”€â”€ 10-accesslog.conf â”‚Â Â â”œâ”€â”€ 10-cgi.conf â”‚Â Â â”œâ”€â”€ 10-dir-listing.conf â”‚Â Â â”œâ”€â”€ 10-evasive.conf â”‚Â Â â”œâ”€â”€ 10-evhost.conf â”‚Â Â â”œâ”€â”€ 10-expire.conf â”‚Â Â â”œâ”€â”€ 10-fastcgi.conf â”‚Â Â â”œâ”€â”€ 10-flv-streaming.conf â”‚Â Â â”œâ”€â”€ 10-no-www.conf â”‚Â Â â”œâ”€â”€ 10-proxy.conf â”‚Â Â â”œâ”€â”€ 10-rewrite.conf â”‚Â Â â”œâ”€â”€ 10-rrdtool.conf â”‚Â Â â”œâ”€â”€ 10-simple-vhost.conf â”‚Â Â â”œâ”€â”€ 10-ssi.conf â”‚Â Â â”œâ”€â”€ 10-ssl.conf â”‚Â Â â”œâ”€â”€ 10-status.conf â”‚Â Â â”œâ”€â”€ 10-userdir.conf â”‚Â Â â”œâ”€â”€ 10-usertrack.conf â”‚Â Â â”œâ”€â”€ 11-extforward.conf â”‚Â Â â”œâ”€â”€ 15-fastcgi-php.conf â”‚Â Â â”œâ”€â”€ 90-debian-doc.conf â”‚Â Â â””â”€â”€ README â”œâ”€â”€ conf-enabled â””â”€â”€ lighttpd.conf
Once you have finished, you can proceed to the next step.
Step 2 – Enable PHP and PHP-FPM Support
By default, Lighttpd does not support PHP. You will need to install PHP with PHP-FPM to work with Lighttpd. PHP-FPM stands for “FastCGI Process Manager” and is an alternative PHP FastCGI implementation with some additional features that can be used for sites of any size.
You can install them by simply running the following command:
apt-get install php php-cgi php-fpm -y
Note: In Ubuntu 18.04, the default PHP version is 7.2. You can replace php7.2 with the PHP version installed on your operating system.
Once the installation is completed,Â the Apache web server also installed automatically along with PHP. So, you will need to remove the Apache package from your system.
First, stop the Apache service with the following command:
systemctl stop apache2 systemctl disable apache2
Next, remove the Apache package with the following command:
apt-get remove apache2Â -y
Next, you will need to configure PHP-FPM to run a FastCGI server on port 9000. By default, PHP listens on the UNIX socket /run/php/php7.2-fpm.sock. Edit the file /etc/php/7.2/fpm/pool.d/www.conf as shown below:
Find the following line:
listen = /run/php/php7.2-fpm.sock
And replace it with the following line:
listen = 127.0.0.1:9000
Save the file then restart PHP-FPM service to apply the changes:
systemctl restart php7.2-fpm
Next, you will also need to configure PHP to work with Lighttpd. You can do it by editing the php.ini file:
Find and uncomment the following line:
Save the file when you are finished.
Next, configure PHP-FPM to run a FastCGI server on port 9000. You can do this by editing the file 15-fastcgi-php.conf:
Find the following lines:
"bin-path" => "/usr/bin/php-cgi", "socket" => "/var/run/lighttpd/php.socket",
Replace them with the following lines:
"host" => "127.0.0.1", "port" => "9000",
Save the file. Then, enable FastCGI and FastCGI-PHP modules using the following command:
lighty-enable-mod fastcgi lighty-enable-mod fastcgi-php
Next, reload the Lighttpd service the enable the changes:
service lighttpd force-reload
Step 3 – Testing Lighttpd
At this point, Lighttpd is configured to work with PHP and PHP-FPM. It’s time to test it.
First, create a sample info.php file in the Lighttpd default document root directory:
Add the following lines:
Save and close the file. Then, change the ownership of the info.php file to www-data:
chown www-data:www-data /var/www/html/info.php
Now, open your web browser and visit the URL http://your-server-ip/info.php. You will be redirected to the following page:
That means PHP is working well with Lighttpd.
Note : Don’t forget to remove the file /var/www/html/info.php after testing.
rm -rf /var/www/html/info.php
Congratulations! You have successfully installed and configured Lighttpd on an Ubuntu 18.04 server. I hope you have now enough knowledge to deploy PHP applications with Lighttpd and PHP-FPM. Get started with Lighttpd today with a VPS hosting plan from Atlantic.Net.