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

Lighttpd is a secure, fast, standards-compliant web server designed for speed in mission-critical environments. This tutorial shows how you can install Lighttpd on a Centos 7 server with PHP support (via PHP-FPM) and MySQL support. PHP-FPM (FastCGI Process Manager) is an alternative implementation of PHP FastCGI with some additional features useful for sites of any size, especially in busy areas. I am using PHP-FPM in this tutorial instead of Lighttpd’s spawn-fcgi.

Table of contents

  • Preliminary remark
  • Installing MariaDB as MySQL drop replacement
  • Installing Lighttpd
  • Install PHP

1 Preliminary note

In this tutorial, I am using the hostname server1.example.com with the IP address 192.168.1.110. These parameters may differ for you, so you should replace them if necessary.

2 Installing MariaDB as a MySQL replacement

First, we install MySQL as follows:

yum -y install mariadb mariadb-server

Then we create links to start the MySQL system (so that MySQL starts automatically whenever the system boots) and start the MySQL server:

systemctl enable  mariadb.service
systemctl start  mariadb.service

Set passwords for the MariaDB root account:

mysql_secure_installation

[[email protected] ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation:
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): <-- press enter
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] <-- y
New password: <-- enter new password
Re-enter new password: <-- enter new password
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] <-- y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] <-- y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] <-- y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] <-- y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

3 Installing Lighttpd

Since Lighttpd and PHP-FPM are not available from the official CentOS repositories, we need to enable the Epel repository:

yum -y install epel-release

EPEL GPG-key import:

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

and then run the following command:

yum update

After that, we can install Lighttpd as follows:

yum -y install lighttpd

Then we create links to start the Lighttpd system (so that Lighttpd automatically starts every time the system boots) and start it:

systemctl enable  lighttpd.service
systemctl start  lighttpd.service

If Lighttpd won’t start with an error message …

(network.c.203) socket failed: Address family not supported by protocol

… Open /etc/lighttpd/lighttpd.conf …

nano /etc/lighttpd/lighttpd.conf

… And change server.use-ipv6 from enable to disable:

[...]
##
## Use IPv6?
##
server.use-ipv6 = "disable"
[...]

Then try starting Lighttpd again – it should now work without any problems:

systemctl start  lighttpd.service

Lighttpd is rooted at / var / www / htdocs (base / var / www plus htdocs as a subdirectory according to lighttpd.conf file), but it installs files by default to / var / www / lighttpd. This is inconsistent, so we must rename the directory.

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

Now point your browser to http://192.168.1.110 and you should see the following page:

The default LightTPD root is / var / www / htdocs / on CentOS 7, and the configuration file is /etc/lighttpd/lighttpd.conf.

4 Install PHP

We can get PHP to work in Lighttpd via PHP-FPM, which we install like this:

yum -y install php-fpm lighttpd-fastcgi

PHP-FPM is a daemon process that runs a FastCGI server on port 9000.

Open /etc/php-fpm.d/www.conf…

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

… and set user and group to lighttpd:

[...]
; 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
[...]

Let’s create a link to start the system for PHP-FPM and launch it:

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

Further

Lighttpd with PHP-FPM and MariaDB on CentOS 7

Sidebar