Configuring PHP FPM and Apache24 on FreeBSD

The Apache web server has evolved over the years to serve various needs and tasks related to the HTTP/HTTPS protocol. Like other web servers, Apache must solve one important problem, namely how to serve various processes to serve HTTP/HTTPS protocol requests. This work includes opening sockets, handling requests, keeping connections open for a certain period of time, handling new events that occur over those connections, and returning content generated by programs written in a particular language (such as PHP, Perl or Python). These tasks are executed and controlled by the Multi-Processing Module (MPM).




1. PGP-FPM Apache

On the Apache Web server there are 3 MPM:
  1. mpm_prefork: A new process is created for each incoming connection that reaches the server. Each process is isolated from the others, so there is no shared memory between them, even if they make identical calls at some point in their execution. This method is a safe way to run applications that are linked to libraries that do not support multithreading, usually older applications or libraries.
  2. mpm_worker: The parent process is responsible for starting a collection of child processes, some of which listen for new incoming connections, while others serve requested content. Each process is multi-threaded (each thread serves one connection), so one process can handle multiple requests at once. This method of handling connections promotes better resource usage while maintaining stability. This is a result of the pool of available processes, which often have free threads ready to serve new connections immediately.
  3. mpm_event: While these processes depend on their workers, this MPM goes a step further by optimizing the way the parent process schedules tasks for child processes and the threads associated with them. By default, the connection remains open for 5 seconds and is closed if no new activity occurs. This process is the default value of the keep-alive directive, which keeps the stream associated with it. MPM events allow a process to manage threads so that some threads are free to process new incoming connections while others remain tied to direct connections. This process will allow the redistribution of assigned tasks to threads, thereby improving resource usage and performance.
On FreeBSD systems by default Apache24 uses the MPM (Multi-Processing Module) prefork. In some cases these modules are very good and offer the most compatibility, MPM provides better performance on most systems. However, because PHP is not thread-safe, the regular mod_php method (PHP as an Apache module) is only compatible with preforks.

As a solution PHP-FPM (FastCGI Process Manager) is an alternative that provides a CGI daemon that can be used by any version of Apache and offers its own performance benefits over regular mod_php (PHP as an Apache module).

PHP-FPM is a FastCGI process manager for PHP. The FastCGI protocol is based on the Common Gateway Interface (CGI), a protocol created between applications and web servers such as Apache HTTP. This allows developers to write applications separately from the web server behavior. The program carries out its processes independently and transfers its products to the web server using this protocol. Each new connection that the application needs to handle will create a new process.

By combining the Apache HTTP MPM module with PHP's FastCGI, process Manager (PHP-FPM), websites can load faster and handle more concurrent connections using fewer resources.


2. System Requirements

OS: FreeBSD 13.2
Web Server: Apache24
Modul: PHP82 dan mod_php

Before you continue reading this article, it is recommended that you read the previous article which discusses the configuration and installation of apache24, PHP82 and the mod_php module.




3. Apache24 PHP-FPM Configuration

The main PHP-FPM configuration file is located in the /usr/local/etc/php-fpm.d/www.conf folder, edit the file as in the example below.

root@ns1:~ # ee /usr/local/etc/php-fpm.d/www.conf
user = www
group = www
listen = 192.168.5.2:9000
listen.owner = www
listen.group = www
listen.mode = 0660

Remove the "#" sign in the script above, listen = 192.168.5.2 is the FreeBSD Server IP Address. After the main PHP-PFM configuration file has been edited, continue by creating the file /usr/local/etc/apache24/Includes/php-fpm.conf and type the script below in that file.

root@ns1:~ # ee /usr/local/etc/apache24/Includes/php-fpm.conf
<IfModule proxy_fcgi_module>
<IfModule dir_module>
DirectoryIndex index.php
</IfModule>
<FilesMatch "\.(php|phtml|inc)$">
SetHandler "proxy:fcgi://192.168.5.2:9000"
</FilesMatch>
</IfModule>

The script above is to activate the PHP-FPM module. What you need to pay attention to when creating the script above is that the IP Address in SetHandler must be the same as the IP Address File /usr/local/etc/php-fpm.d/www.conf.

The core of the PHP-FPM configuration lies in the /usr/local/etc/apache24/httpd.conf file. Edit several scripts in the file by removing the "#" sign in front of the script file /usr/local/etc/apache24/httpd.conf. Follow the example below to enable the PHP-FPM module on apache24.

root@ns1:~ # ee /usr/local/etc/apache24/httpd.conf
#LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so
LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so
#LoadModule mpm_worker_module libexec/apache24/mod_mpm_worker.so
LoadModule authnz_fcgi_module libexec/apache24/mod_authnz_fcgi.so
LoadModule proxy_module libexec/apache24/mod_proxy.so
LoadModule proxy_http_module libexec/apache24/mod_proxy_http.so
LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so
LoadModule proxy_scgi_module libexec/apache24/mod_proxy_scgi.so

According to the explanation above in this article, there are 3 MPM modules, you can choose one according to your needs. In this article, the mpm_event module is used. After that, continue by editing the /etc/make.conf file and enter the script below in the /etc/make.conf file.

DELETE the script below in the /usr/local/etc/apache24/httpd.conf file

<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>

root@ns1:~ # ee /etc/make.conf
WITH_MPM=event


4. Test PHP-FPM Apache24

After all the configurations above are complete, it's time for us to do the test. Create a test file called php.info and enter the script below in the file /usr/local/www/apache24/data/info.php.

root@ns1:~ # ee /usr/local/www/apache24/data/info.php

<?php phpinfo(); ?>

Give ownership rights to the file and also file permissions.

root@ns1:~ # chown -R www:www /usr/local/www/apache24/data/info.php
root@ns1:~ #
chmod +x /usr/local/www/apache24/data/info.php

The next step is to activate the PHP-FPM module so that it can run automatically. Edit the /etc/rc.conf file and enter the script below.

root@ns1:~ # ee /etc/rc.conf
php_fpm_enable="YES"

Before we do the test, restart or reboot the apache24 and PHP-FPM web server.

root@ns1:~ # service apache24 restart
Performing sanity check on apache24 configuration:
Syntax OK
Stopping apache24.
Waiting for PIDS: 2437.
Performing sanity check on apache24 configuration:
Syntax OK
Starting apache24.

root@ns1:~ # service php-fpm restart
Performing sanity check on php-fpm configuration:
[20-Aug-2023 13:20:07] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Stopping php_fpm.
Waiting for PIDS: 2105.
Performing sanity check on php-fpm configuration:
[20-Aug-2023 13:20:07] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Starting php_fpm.

The way to do the PHP-FPM test is to type the IP Address of the FreeBSD Server, because in this article the FreeBSD server uses IP 192.168.5.2, type the IP Address in a Web Browser such as Yandex or Google Chrome, and the results should appear as shown in the image below.



5. Apache24 PHP-FPM With Unix Socket

The method above is to connect Apache to the PHP-FPM server using the TCP/IP protocol. In this section we will learn how to connect an Apache server to a PHP-FPM server using a UNIX socket.

Open the file /usr/local/etc/php-fpm.d/www.conf, and change the script "listen = 192.168.5.2:9000" to "listen = /tmp/php-fpm.sock".

listen = 192.168.5.2:9000

change with

listen = /tmp/php-fpm.sock

Then in the file /usr/local/etc/apache24/Includes/php-fpm.conf, type the script below.

<IfModule proxy_fcgi_module>
   <IfModule dir_module>
       DirectoryIndex index.php
   </IfModule>

   <FilesMatch "\.(php|phtml|inc)$">
SetHandler proxy:unix:/tmp/php-fpm.sock|fcgi://localhost/
   </FilesMatch>
</IfModule>

Restart the apache server and PHP-FPM server

root@ns:~ # service apache24 restart
Performing sanity check on apache24 configuration:
Syntax OK
Stopping apache24.
Waiting for PIDS: 3258.
Performing sanity check on apache24 configuration:
Syntax OK
Starting apache24.


root@ns:~ # service php-fpm restart
Performing sanity check on php-fpm configuration:
[21-Nov-2023 20:57:00] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Stopping php_fpm.
Waiting for PIDS: 3211.
Performing sanity check on php-fpm configuration:
[21-Nov-2023 20:57:01] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Starting php_fpm.


Pay attention to line 5 where it says Server API FPM/FastCGI, that means the PHP-FPM module is RUNNING or running on the apache24 web server.

PHP-FPM is an efficient method on how to minimize memory consumption and improve performance for websites with heavy traffic. This is significantly faster than traditional CGI-based methods in multi-user PHP environments. If your main goal is to host web applications and to achieve optimal performance and security, then PHP-FPM is the solution.
Iwan Setiawan

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

Post a Comment

Previous Post Next Post