Solved Apache & php

Ooh yes, php-fpm , is the current way to go. One moment i'm on Linux mint now. See if i can mount zfs/ufs from my freebsd. Come back in a second.
 
rc.conf :
php_fpm_enable="YES"
edit /usr/local/etc/php-fpm.d/ as needed ...
------------------------------------------------------------------------
I could be wrong !!!, but this is what A.I. says.

In FreeBSD, you need to manually enable the required modules in the Apache configuration file and point it to the PHP-FPM service.


1. Enable Required Apache Modules
Open your main Apache configuration file, usually located at /usr/local/etc/apache24/httpd.conf. Find and uncomment (remove the #) the following lines:
  • LoadModule proxy_module libexec/apache24/mod_proxy.so
  • LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so
  • LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so
Note: You should comment out mod_mpm_prefork and mod_php if they are enabled, as they are not used with PHP-FPM.

2. Configure PHP-FPM Socket or Port
FreeBSD's PHP-FPM can listen on either a Unix socket or a network port. Check your PHP-FPM pool configuration (usually /usr/local/etc/php-fpm.d/www.conf) to see the listen directive.
  • If using a Unix Socket (common for performance):
    apache
    <span>&lt;FilesMatch "\.php$"&gt;<br> SetHandler "proxy:unix:/var/run/php-fpm.sock|fcgi://localhost/"<br>&lt;/FilesMatch&gt;<br></span>
    Use code with caution.
  • If using a TCP Port (default is often 127.0.0.1:9000):
    apache
    <span>&lt;FilesMatch "\.php$"&gt;<br> SetHandler "proxy:fcgi://127.0.0.1:9000"<br>&lt;/FilesMatch&gt;<br></span>
    Use code with caution.

3. Enable and Restart Services
Ensure both services are enabled in /etc/rc.conf:

bash
<span>sudo sysrc apache24_enable=</span><span>"YES"</span><span><br>sudo sysrc php_fpm_enable=</span><span>"YES"</span><span><br></span>
Use code with caution.
Then restart them:

bash
<span>sudo service php-fpm restart<br>sudo service apache24 restart<br></span>
Use code with caution.

4. Verification
You can verify the modules are correctly loaded by running:

bash
<span>apachectl -M | grep -E </span><span>'proxy|mpm'</span><span><br></span>
Use code with caution.
Follow-up: Would you like a sample configuration file to place in your Includes/ directory to keep your main httpd.conf clean?

----------------------------------------------------------------------------------------
To keep your
httpd.conf clean, it is best practice on FreeBSD to create a separate configuration file in the Includes directory.

1. Create the Config File
Create a new file named php-fpm.conf:

bash
<span>sudo vi /usr/local/etc/apache24/Includes/php-fpm.conf<br></span>
Use code with caution.

2. Add the Configuration
Paste the following block. This setup handles the proxying and ensures index files work correctly:

apache
<span># Enable the PHP-FPM handler<br>&lt;FilesMatch "\.php$"&gt;<br> # Change this to match your 'listen' setting in php-fpm.d/www.conf<br> # Example for Socket: unix:/var/run/php-fpm.sock|fcgi://localhost/<br> # Example for TCP: fcgi://127.0.0.1:9000<br> SetHandler "proxy:fcgi://127.0.0.1:9000"<br>&lt;/FilesMatch&gt;<br><br># Allow index.php to be a directory index<br>&lt;IfModule dir_module&gt;<br> DirectoryIndex index.php index.html<br>&lt;/IfModule&gt;<br><br># Security: Block access to .user.ini files<br>&lt;Files ".user.ini"&gt;<br> Require all denied<br>&lt;/Files&gt;<br></span>
Use code with caution.

3. Verify your PHP-FPM Listener
Before restarting, double-check where PHP-FPM is actually listening so the SetHandler line above is accurate:

bash
<span>grep </span><span>"^listen ="</span><span> /usr/local/etc/php-fpm.d/www.conf<br></span>
Use code with caution.
  • If it says 127.0.0.1:9000, use the TCP example above.
  • If it shows a path like /var/run/php-fpm.sock, use the Unix example.

4. Test and Restart
Run the config test to make sure you didn't miss any modules in httpd.conf:

bash
<span># Check for syntax errors</span><span><br>apachectl configtest<br><br></span><span># If "Syntax OK", restart both</span><span><br>sudo service php-fpm restart<br>sudo service apache24 restart<br></span>
 
To be honest , i know little about apache configuration. I find it "complex". That's why i use nginx , fastcgi connecting to my php-fpm services running on port 9000.
 
service php-fpm start:-

php-fpm does not exist in /etc/rc.d or the local startup

even though it does exist in /usr/local/sbin/php-fpm.

Since it exists the only pkg that could have installed it is nextcloud-php85 as that is the only pkg I've installed apart from apache24.
 
Now i see,
pkg install phpXXX
----------------------------------------------------------------------------------
And it is
service php_fpm start
or
service php-fpm start

Note - vs _
------------------------------------------------------------------------------------
What i also learned,
1. Edit the Configuration
The default pool configuration on FreeBSD is typically located at:
/usr/local/etc/php-fpm.d/www.conf
  1. Open the file with your preferred editor (e.g., vi or nano).
  2. Find the listen directive. It will likely look like this if currently using a socket:
    listen = /var/run/php-fpm.sock
  3. Change it to an IP address and port (usually 9000 is the default):
    listen = 127.0.0.1:9000
 
Now i see,
pkg install phpXXX
----------------------------------------------------------------------------------
And it is
service php_fpm start
or
service php-fpm start

Note - vs _
OMG a typo! Many thanks for noticing.

Just in case anyone else is using php-fpm it is set up with

sysrc php_fpm_enable=YES
service php_fpm start


and this starts php-fpm.

Confusing or what?
 
Without all the AI stuff. From the top of my head.

Modify httpd.conf, disable mpm_prefork_module and enable mpm_event_module.
Code:
LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so
#LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so

Create a /usr/local/etc/apache24/modules.d/001_php-fpm.conf:
Code:
LoadModule proxy_module libexec/apache24/mod_proxy.so
LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so

<IfModule proxy_fcgi_module>
  <IfModule dir_module>
     DirectoryIndex index.php
  </IfModule>
  <FilesMatch "\.(php|phtml|inc)$">
     SetHandler "proxy:fcgi://127.0.0.1:9000"
  </FilesMatch>
</IfModule>

Enable sysrc php_fpm_enable="YES"

That should be it. But maybe I forgot something.
 
Note that not all PHP web applications like PHP-FPM. That number is quite low, but you can run into it. You can always fallback to mod_php.
 
Without all the AI stuff. From the top of my head.

Modify httpd.conf, disable mpm_prefork_module and enable mpm_event_module.
Code:
LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so
#LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so

Create a /usr/local/etc/apache24/modules.d/001_php-fpm.conf:
Code:
LoadModule proxy_module libexec/apache24/mod_proxy.so
LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so

<IfModule proxy_fcgi_module>
  <IfModule dir_module>
     DirectoryIndex index.php
  </IfModule>
  <FilesMatch "\.(php|phtml|inc)$">
     SetHandler "proxy:fcgi://127.0.0.1:9000"
  </FilesMatch>
</IfModule>

Enable sysrc php_fpm_enable="YES"

That should be it. But maybe I forgot something.
This is very little. Must try myself. But one thing sometimes , php-fpm takes socket and not url.
 
php-fpm requires mod_mpm_event, mod_proxy, mod_proxy_fcgi

/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 proxy_module libexec/apache24/mod_proxy.so
LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so
LoadModule socache_shmcb_module libexec/apache24/mod_socache_shmcb.so
LoadModule ssl_module libexec/apache24/mod_ssl.so
#LoadModule cgi_module libexec/apache24/mod_cgi.so

/usr/local/etc/php-fpm.d/www.conf
listen = /var/run/php-fpm.sock
listen.owner = www
listen.group = www

/usr/local/etc/apache24/Includes/php-fpm.conf
<FilesMatch \.php$>
SetHandler proxy:unix:/var/run/php-fpm.sock|fcgi://localhost/
# use the following line instead if you didn't set PHP-FPM to listen on a Unix socket
#SetHandler proxy:fcgi://127.0.0.1:9000
</FilesMatch>

------OR------

/usr/local/etc/apache24/modules.d/030_php-fpm.conf

Code:
<IfModule proxy_fcgi_module>
    <IfModule dir_module>
        DirectoryIndex index.php
    </IfModule>
    <FilesMatch "\.(php|phtml|inc)$">
        SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</IfModule>
If you want to use socket
Code:
<IfModule proxy_fcgi_module>
    <IfModule dir_module>
        DirectoryIndex index.php
    </IfModule>
    <FilesMatch \.php$>
        SetHandler proxy:unix:/var/run/php-fpm.sock|fcgi://localhost/
    </FilesMatch>
</IfModule>


For JAIL usage

security.jail.allow_raw_sockets=1

/usr/local/etc/php-fpm.d/www.conf

listen = /var/run/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660
 
I don't know if I want to use socket.

Looks like the only difference is in SetHandler statement which I would set in

/usr/local/etc/apache24/modules.d/030_php-fpm.conf


PS why 030_php-fpm ?
 
modules.d is directory for third party module configuration files. The files in this directory are automatically included in the httpd config if they name begins with a 3 digit number followed by "_" underscore and ending in ".conf"
So if you have different config files for example 010_ratelimit.conf 020_cloudflare.conf 030_php-fpm.conf etc.

Using sockets is faster than using IP
 
php-fpm requires mod_mpm_event, mod_proxy, mod_proxy_fcgi

/usr/local/etc/apache24/httpd.conf

php-fpm requires mod_mpm_event, mod_proxy, mod_proxy_fcgi

/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 proxy_module libexec/apache24/mod_proxy.so
LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so
LoadModule socache_shmcb_module libexec/apache24/mod_socache_shmcb.so
LoadModule ssl_module libexec/apache24/mod_ssl.so
#LoadModule cgi_module libexec/apache24/mod_cgi.so
I hoping to script this.

Can anyone check that I have this right?

sh:
cat <<EOF > apache.scr
/ mpm_event_module /s/^#//g
/ mpm_prefork_module /s/^/#/g
/ proxy_module /s/^#//g
/ proxy_fcgi_module /s/^#//g
/ socache_shmcb_module /s/^#//g
/ ssl_module /s/^#//g
EOF

sed -f apache.scr /zroot/iocage/jails/FAMP/root/usr/local/etc/apache24/httpd.conf.sample > /zroot/iocage/jails/FAMP/root/usr/local/etc/apache24/httpd.conf

This is in my FAMP jail.
 
I have no idea what apache.src or where it is used or placed. Even never heard of it.
Maybe httpd.conf (or subdirectory) | grep -i module ?
Sorry now i see your httpd.conf in jail grep module would be nice.
 
modules.d is directory for third party module configuration files. The files in this directory are automatically included in the httpd config if they name begins with a 3 digit number followed by "_" underscore and ending in ".conf"
So if you have different config files for example 010_ratelimit.conf 020_cloudflare.conf 030_php-fpm.conf etc.

Using sockets is faster than using IP
Hope can I tell if I have set up sockets correctly?

ls -l /var/run/php-fpm.sock does not find anything. Should it get created when you start fpm?
 
Note,
Code:
lsof | grep -i sock | grep unix | grep -v dbus | grep -v xdg

Code:
mariadbd   2839      mysql   25u     unix    0xfffff80043700400         0          /var/run/mysql/mysql.sock
git       53852          x    3u     unix    0xfffff80043843000         0          /home/x/.cache/git/credential/socket
cupsd     55948       root    7u     unix    0xfffff80043839800         0          /var/run/cups/cups.sock
avahi-dae 87900      avahi    9u     unix    0xfffff80043702000         0          /var/run/avahi-daemon/socket
mongod    88954    mongodb   10u     unix    0xfffff80043701000         0          /tmp/mongodb-27017.sock
 
Back
Top