Setup Lighttpd With PHP FPM mod Fastcgi On FreeBSD

PHP-FPM is a FastCGI implementation for PHP that improves the performance and scalability of PHP web applications. In this article we will review in more depth what PHP-FPM is and how to install Lighttpd on a FreeBSD server with PHP support (via PHP-FPM).

PHP-FPM is an abbreviation for PHP FastCGI Process Manager, which is a process manager for running PHP applications via the FastCGI protocol. PHP-FPM can help improve the performance of PHP applications by running the PHP process as a separate daemon from the web server. In a standard configuration, PHP-FPM manages PHP processes separately from a web server such as Lighttpd or Nginx, allowing more flexibility in managing PHP processes and increasing application scalability.

PHP-FPM has several features, such as better process management, better process isolation, better resource management, the ability to limit the number of connections, and support for enabling PHP opcode caching. PHP-FPM also allows administrators to manage PHP applications more easily, including monitoring PHP processes and stopping problematic PHP processes.

Meanwhile, Lighttpd is a web server that is reliable, fast, secure and meets modern web server standards. Lighttpd web servers are designed to work at high speeds in mission-critical environments. Additionally, lighttpd has support for on-the-fly content compression, HTTP authentication, URL rewriting, SSL, and automatic load balancing (load can be automatically distributed across multiple running lighttpd servers). Lighttpd web server also supports CGI, SCGI, FastCGI interfaces.


1. How to Install PHP-FPM on FreeBSD

In Lighttpd, PHP-FPM runs its PHP scripts independently outside the Lighttpd environment, so it can work efficiently. Therefore, when a user requests a PHP page, the Lighttpd web server will forward the request to the PHP-FPM service using FastCGI.

It works like this:




The way to install PHP-FPM on FreeBSD is almost the same as other PHP mods. What's different is that PHP-FPM has a special configuration file located at "/usr/local/etc/php-fpm.d". Follow the guide below to install PHP-FPM.
root@ns5:~ # cd /usr/ports/lang/php82
root@ns5:/usr/ports/lang/php82 # make install clean
root@ns5:/usr/ports/lang/php82 # cd /usr/ports/www/mod_php82
root@ns5:/usr/ports/www/mod_php82 # make install clean
root@ns5:/usr/ports/www/mod_php82 # cd /usr/ports/databases/php82-mysqli
root@ns5:/usr/ports/databases/php82-mysqli # make install clean
We suggest that before you continue this article, you should read the previous article about how to install Lighttpd on FreeBSD.


Let's assume, you have read the article above and Lighttpd is already running on the FreeBSD server. The next step is to edit the "/usr/local/etc/php.ini" file, and type the script below in the file.

cgi.fix_pathinfo=0

PHP-FPM's main configuration file is "/usr/local/etc/php-fpm.d/www.conf", open the file and enter the script below.

user = www
group = www
listen = /var/run/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660

To activate PHP-FPM on FreeBSD, you simply enter the script below in the "/etc/rc.conf" file.

php_fpm_enable="YES"

Now try running PHP-FPM, with the following command.
root@ns5:~ # service php-fpm restart
Performing sanity check on php-fpm configuration:
[16-Jan-2024 11:09:16] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Stopping php_fpm.
Waiting for PIDS: 38677.
Performing sanity check on php-fpm configuration:
[16-Jan-2024 11:09:16] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Starting php_fpm.
With the command above, you have activated PHP-FPM. The next step is to connect Lighttpd with PHP-FPM.


2. Lighttpd configuration

You don't need to write a script to connect Lighttpd with PHP-FPM. To do this work, Lighttpd has provided a fastcgi module, you only need to activate the fastcgi module.

Open the "/usr/local/etc/lighttpd/modules.conf" file and in the server.modules script, type the fastcgi script, as below.

server.modules = (
#  "mod_rewrite",
"mod_access",
#  "mod_evasive",
#  "mod_auth",
#  "mod_authn_file",
#  "mod_redirect",
#  "mod_setenv",
#  "mod_alias",
"mod_fastcgi"
)

By removing the "#" sign in the "mod_fastcgi" script, it means you have activated the PHP-FPM module in Lighttpd. Now we will create a PHP script to connect Lighttpd with PHP-FPM.

Open the "/usr/local/etc/lighttpd/conf.d/fastcgi.conf" file, and insert the script below into the file.

fastcgi.server = ( ".php" => ((
"socket" => "/var/run/php-fpm.sock"
)))

If you have activated everything, restart PHP-FPM and Lighttpd.
root@ns5:~ # service php-fpm restart
root@ns5:~ # service lighttpd restart


3. Test Lighttpd PHP-FPM

Needless to say, now you have reached stage three. In this final stage, we will test whether Lighttpd and PHP-FPM can communicate.

To carry out this test, we will create a php file called "/usr/local/www/data/info.php", in this file you type the following script.


<?php phpinfo(); ?>

This file will read all the PHP information installed on the FreeBSD and Lighttpd Server.

Now you open the Google Chrome web browser, in the address bar menu, type the following command.

http://192.168.5.2/info.php

If there is nothing wrong with all the configurations above, it will appear as shown in the image below.



Now on your FreeBSD server. has running Lighttpd web server. You can host web pages, documents, images and other files on your web server. To make your web server more secure by adding SSL configuration and other security features.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post