There is a new version of this tutorial available for CentOS 8.

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

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

 

1 Preliminary Note

In this tutorial, I use the hostname server1.example.com with the IP address 192.168.1.100. These settings might differ for you, so you have to replace them where appropriate.

 

2 Installing MariaDB as MySQL drop in replacement

First, we install MySQL like this:

yum -y install mariadb mariadb-server

Then we create the system startup links for MySQL (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 MarisDB root account:

mysql_secure_installation
[root@server1 ~]# 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

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

yum -y install epel-release

Import the EPEL GPG-key:

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

and then run:

yum update

Afterwards, we can install Lighttpd like this:

yum -y install lighttpd

Then we create the system startup links for Lighttpd (so that Lighttpd starts automatically whenever the system boots) and start it:

systemctl enable  lighttpd.service
systemctl start  lighttpd.service

If Lighttpd fails to start with the following 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 to start Lighttpd again - it should now work without any problem:

systemctl start  lighttpd.service

Lighttpd has its document root in /var/www/htdocs (base directory /var/www plus htdocs as subdirectory according to lighttpd.conf file) but it installs the default files to /var/www/lighttpd. That's inconsistent so we have to rename the directory like this.

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

Now direct your browser to http://192.168.1.100, and you should see the following page:

lighttpd start page

Lighttpd's default document root is /var/www/htdocs/ on CentOS 7, and the configuration file is /etc/lighttpd/lighttpd.conf.

 

4 Installing PHP

We can make PHP work in Lighttpd through 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
[...]

Create the system startup links for PHP-FPM and start it:

systemctl enable  php-fpm.service
systemctl start  php-fpm.service
Share this page:

0 Comment(s)