How to Install Lighttpd with PHP-FPM and MariaDB on CentOS 7

Try it in our public cloud & Get $50 Credit
CLAIM NOW

Nginx and Apache are popular choices for serving PHP applications, but Lighttpd is also a contender. Like Nginx, Lighttpd is a faster, less resource-intensive HTTP server that runs scripts in separate processes rather than using internal modules. If you’d like to use this worthy but less popular alternative, then read on to learn how to set up Lighttpd to serve PHP with access to MariaDB for persistence.

Getting Started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running CentOS 7.
• Root access to the server.

When we’re done, you’ll have a powerful environment for delivering PHP-based content, along with a first-class storage solution for your data.

Tutorial

We’ll begin by updating all installed packages on the server. This ensures that any currently known CentOS bugs and security issues are patched.

yum update -y && shutdown -r now

Now we’ll install the minimal set of packages required to make things work.

yum -y install nano epel-release

Next we’ll install the packages for MariaDB. MariaDB is a community-supported MySQL fork with various improvements, but which is backwards-compatible with MySQL.

This command installs MariaDB.

yum -y install mariadb mariadb-server

MariaDB is installed, but is not yet configured to launch on boot. This next step configures MariaDB to launch when your server boots up.

systemctl start mariadb.service
systemctl enable mariadb.service

There are a few post-installation steps needed to configure and harden MariaDB. We’ll now set a root password and perform a few extra security modifications.

mysql_secure_installation

mysql_secure_installation_500px

Our next step is setting up Lighttpd. Lighttpd will serve up static content, and will hand off PHP scripts to a separate interpreter for processing.

This command installs the basic Lighttpd package with its default configuration.

yum -y install lighttpd

Again, Lighttpd is installed but not configured to launch on boot. We’ll take care of that next.

systemctl start lighttpd.service
systemctl enable lighttpd.service

Unfortunately, there is a bug in Lighttpd’s default configuration. As configured, the document root is /var/www/htdocs but the installation step creates /var/www/lighttpd instead. Let’s fix that by renaming the offending directory.

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

PHP-FPM is the daemon that executes PHP scripts, returning their output to be served up by Lighttpd. Let’s set that up next.

First, we’ll install the package itself.

yum -y install php-fpm lighttpd-fastcgi

Now we need to configure PHP-FPM to run as the lighttpd user.

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

Find this block :
; 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

And change it for :

; 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 this file and exit. Once again, PHP-FPM must be configured to start at boot.

systemctl start php-fpm.service
systemctl enable php-fpm.service

PHP-FPM is now correctly configured, but Lighttpd still doesn’t know about it. Let’s tell Lighttpd how to hand off scripts for execution.

nano /etc/php.ini

Find this block :

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

And uncomment the last line :

cgi.fix_pathinfo=1

Save this file and exit. We must next edit modules.conf as shown:

nano /etc/lighttpd/modules.conf

And uncomment this line:

##
include "conf.d/fastcgi.conf"
##

fastcgi.conf must also be edited like so:

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

Add this block at the end :

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

Save and exit. To communicate with MariaDB, we must now install PHP’s MySQL module. Since these DBMSs are compatible, the MySQL module also supports communication with MariaDB.

yum install php-mysql -y

In order for the changes to take effect, we must now restart Lighttpd and PHP-FPM.

systemctl restart lighttpd.service
systemctl restart php-fpm.service

We’re almost done. To test things out, we’ll create a basic phpinfo page. This will display a host of diagnostic information about your PHP configuration and environment.

Remove the Lighttpd default page from the document root.

cd /var/www/htdocs/
rm -rf *

Replace it with a page containing the following content:

nano index.php

Save this file and exit. Access your page at http://your_main_ip.

Conclusion

You now have a lean, fast environment for executing PHP scripts. The addition of MariaDB makes this a great setup for WordPress, along with countless other PHP and MySQL tools. If this guide was helpful to you, kindly share it with others who may also be interested.