How to Install Lighttpd with PHP-FPM on CentOS 7

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month. Get a Cloud Server

Lighttpd is a secure, fast and very flexible open source web server. It is optimised for a high performance environment and uses very low memory as compared to other web servers. It supports many advanced features for example FastCGI, CGI, Auth, Output-Compression, URL-Rewriting and many more. It is a popular web server for the Catalyst and Ruby on Rails web frameworks. Lighttpd is used by many high traffic websites like Bloglines, WikiMedia etc.

Some the most notable features of Lighttpd are

It supports SCGI, HTTP proxy and FastCGI for load balancing.

It supports web server event mechanism performance also provides also support for more efficient event notification schemes.

It supports chroot that changes the apparent root directory for the current running process and its children.

It supports conditional URL rewriting (mod_rewrite), TLS/SSL with SNI support, via OpenSSL and authentication against an LDAP server.

It supports HTTP compression using mod_deflate.

Requirements

Lighttpd does not require any special hardware requirement but to follow this guide you will need a server with CentOS 7 installed. You will also need sudo or root access to the server. If you are logged in as a non-root but sudo user, run sudo su to switch to root user.

Install Lighttpd

You can install Lighttpd either from the available packages or installing from source. It is recommended to update your system and available repositories before we install any packages. Run the following command to do so.

yum -y update

Before installing Lighttpd we need to make sure that Apache or nginx is not installed on your server. Run the following commands to remove these packages.

yum -y erase httpd nginx

If they are not installed in your server, the command will simply show you

No Match for argument: httpd No Match for argument: nginx No Packages marked for removal

Lighttpd is not available on the default CentOS YUM repository hence you will need to add EPEL repository to your system. Install EPEL repository in your system using the following command.

yum -y install epel-release yum -y update

Now you can install Lighttpd using the following command.

yum -y install lighttpd

Once the packages are installed, you can run the Lighttpd server also enable it to automatically start at boot time using the following commands:

systemctl start lighttpd systemctl enable lighttpd

You can see the version of Lighttpd installed in your system using the following command:

lighttpd -v

You will see following output:

lighttpd/1.4.39 (ssl) - a light and fast webserver Build-Date: Mar 1 2016 15:43:12

Now you will need to adjust your firewall to allow the http and https traffic to pass. Run the following commands to add new firewall rules. If you do not have firewalld installed, no need to run these commands.

firewall-cmd --permanent --zone=public --add-service=http firewall-cmd --permanent --zone=public --add-service=https firewall-cmd --reload

Now you can browse the following URL using your favorite browser to see your web server working.

http://Your-IP-addr

You will see a not found message as shown below:

HP_NO_IMG/data/uploads/users/d0888044-e701-4d69-a760-fddd1b09f3f2/531033426.png" alt="" />

This due to the default document root directory of Lighttpd is /var/www/htdocs but the startup promo files for Lighttpd is saved in /var/www/lighttpd .

Now you have two options to correct this issue. You can either rename /var/www/lighttpd to /var/www/htdocs or you can change the configuration files to make /var/www/lighttpd directory as the default document root directory. To rename /var/www/lighttpd to /var/www/htdocs run the following command.

mv /var/www/lighttpd /var/www/htdocs

You can now check the web front of your server, by going to http://Your-IP-addr and you will see the following page.

HP_NO_IMG/data/uploads/users/d0888044-e701-4d69-a760-fddd1b09f3f2/1300012439.png" alt="" />

You can change the default web root directory by editing the default configuration file of Lighttpd, which is /etc/lighttpd/lighttpd.conf . Use your favorite editor to edit the files. In this tutorials we will be using nano , if you do not have nano installed, you can run following command to install yum -y install nano .

nano /etc/lighttpd/lighttpd.conf

Scroll down to find the following lines:

## ## Document root ## server.document-root = server_root + "/htdocs"

Change htdocs to lighttpd to make it look like the following code.

## ## Document root ## server.document-root = server_root + "/lighttpd"

Now save the file and you should see your server running. If you renamed your directory to /var/www/htdocs then your document root directory is /var/www/htdocs and if you have changed your configuration file then your default document root is /var/www/lighttpd .

Install PHP-FPM

You can install PHP to work with Lighttpd using PHP-FPM. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

yum -y install php-fpm lighttpd-fastcgi

Now we will need configure PHP-FPM to run a FastCGI server on port 9000 . Edit /etc/php-fpm.d/www.conf file using your favorite editor.

nano /etc/php-fpm.d/www.conf

Scroll down to see the following code:

; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache Choosed to be able to access some dir as httpd user = apache ; RPM: Keep a group allowed to write in log dir. group = apache

Change the value of user and group to lighttpd and make it look like shown below.

; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache Choosed to be able to access some dir as httpd user = lighttpd ; RPM: Keep a group allowed to write in log dir. group = lighttpd

Save and exit the file. Now start PHP-FPM and enable it to automatically start at boot time using the following command:

systemctl start php-fpm systemctl enable php-fpm

This should run PHP-FPM on your server successfully.

Configuring Lighttpd to Work With PHP

To enable PHP to work with Lighttpd web server, we will need to make few configuration changes. Open your /etc/php.ini file in your favorite editor:

nano /etc/php.ini

Look for the following lines in the configuration:

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. $ ; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not $ ; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Se$ ; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A sett$ ; of zero causes PHP to behave as before. Default is 1. You should fix your s$ ; to use SCRIPT_FILENAME rather than PATH_TRANSLATED. ; http://php.net/cgi.fix-pathinfo ;cgi.fix_pathinfo=1

Uncomment the line ;cgi.fix_pathinfo=1 to make it cgi.fix_pathinfo=1 . Save the file and exit the editor.

Now open another file /etc/lighttpd/conf.d/fastcgi.conf using your favorite editor.

nano /etc/lighttpd/conf.d/fastcgi.conf

Now look for the following lines in the file:

## server.modules += ( "mod_fastcgi" )

Add the following lines just below the above line:

fastcgi.server += ( ".php" => (( "host" => "127.0.0.1", "port" => "9000", "broken-scriptfilename" => "enable" )) )

Save the file and exit from editor:

Now open /etc/lighttpd/modules.conf file using your favorite editor.

nano /etc/lighttpd/modules.conf

Look for the following lines in the file:

## ## FastCGI (mod_fastcgi) ## #include "conf.d/fastcgi.conf"

Uncomment #include "conf.d/fastcgi.conf" to make it look line include "conf.d/fastcgi.conf" . Save the file and exit from editor.

Now restart PHP-FPM and Lighttpd using the following command.

systemctl restart php-fpm systemctl restart lighttpd

Now to verify if Lighttpd is configured to use PHP-FPM, you will need to view your php information. Create a new file in your document root directory which may be /var/www/htdocs or /var/www/lighttpd according how you have configured it before.

nano /var/www/lighttpd/phpinfo.php

Now add the following php code into the file.





Now browse the following file through frontend using your favorite web browser. Go to the following URL.

http://your-IP-addr/phpinfo.php

You will see following page, which will show you all your php configuration and information.

HP_NO_IMG/data/uploads/users/d0888044-e701-4d69-a760-fddd1b09f3f2/1140049052.png" alt="" />

You will find that your server API is FPM/FastCGI. This shows that you have a working Lighttpd web server with PHP-FPM.

Conclusion

In this tutorial we have learnt to install Lighttpd web server, which is known for its ability to handle more than thousands concurrent connections in parallel without using much resources. We also learnt to install and configure PHP-FPM to use with Lighttpd web server. You can install MySQL or MariaDB to make it full LLMP stack.