Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| gentoo:separated-php [2024/03/12 08:15] – created willy | gentoo:separated-php [2025/03/13 13:27] (current) – [Using independent PHP for different services] willy | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Using independent PHP for different services ====== | + | ====== |
| Ideally, you want to run each PHP-based service within the boundaries of it's own PHP instance. The advantages of such a solution would be that each PHP instance can run as the service user which guarantee service isolation and more protection in data access. | Ideally, you want to run each PHP-based service within the boundaries of it's own PHP instance. The advantages of such a solution would be that each PHP instance can run as the service user which guarantee service isolation and more protection in data access. | ||
| Line 16: | Line 16: | ||
| </ | </ | ||
| - | the script will assume you want to run PHP-FPM with a version of PHP called // | + | the script will assume you want to run PHP-FPM with a version of PHP called // |
| So, copy (don't link, as you will need to edit that per service) the configs: | So, copy (don't link, as you will need to edit that per service) the configs: | ||
| <code bash> | <code bash> | ||
| cp -a / | cp -a / | ||
| + | rm / | ||
| </ | </ | ||
| Choose your preferred (or required) PHP version for service1. | Choose your preferred (or required) PHP version for service1. | ||
| + | Your PHP FPM will still use the **php.ini** from the original setup folder, so delete the copied one or you will get confused! | ||
| - | Then, link the libraries (i know, this step is ugly and bad, but i tihnk it's better than modifying | + | Then, you need the following edit to your **/ |
| - | < | + | < |
| - | ln -s /usr/lib64/php8.2 | + | command=" |
| </ | </ | ||
| - | Here you need to symlink it, you don't really want two copies of php. | ||
| - | Now, assuming | + | and replace it with the following lines: |
| + | < | ||
| + | if [ -e "/etc/php/fpm-${PHP_SLOT}/php-version" | ||
| + | PHP_SLOT_EXEC=" | ||
| + | else | ||
| + | PHP_SLOT_EXEC=" | ||
| + | fi | ||
| + | command="/ | ||
| + | </ | ||
| + | |||
| + | ===== PHP configuration for specific service ===== | ||
| + | |||
| + | Ok, it's time to adapt the PHP configuration for our service. You need to configure three things: | ||
| + | * Setup the desired PHP version manually | ||
| + | * Change username | ||
| + | * change the port used by PHP FPM | ||
| + | * Replace the include path for the confiiguration | ||
| + | |||
| + | First of all, create | ||
| + | <file - php-version> | ||
| + | php8.2 | ||
| + | </ | ||
| + | |||
| + | Then edit the file **/etc/php/fpm-service1/ | ||
| + | < | ||
| + | include=/ | ||
| + | </ | ||
| + | |||
| + | Last, edit the file | ||
| < | < | ||
| user = service1 | user = service1 | ||
| Line 36: | Line 65: | ||
| listen = 127.0.0.1: | listen = 127.0.0.1: | ||
| </ | </ | ||
| + | |||
| + | Remeber the port number as you will need to use it in NGINX configuration. | ||
| and then you can start it and add to boot: | and then you can start it and add to boot: | ||
| Line 45: | Line 76: | ||
| done! | done! | ||
| - | ==== Updating php ==== | + | ==== NGINX configuration |
| + | See [[selfhost: | ||
| + | A generic NGINX configuration for PHP-FPM is the following: | ||
| + | < | ||
| + | location ~ /.*\.php$ { | ||
| + | try_files $uri =404; | ||
| + | fastcgi_split_path_info ^(.+\.php)(/ | ||
| + | include fastcgi_params; | ||
| + | fastcgi_param SCRIPT_FILENAME $request_filename; | ||
| + | fastcgi_pass 127.0.0.1: | ||
| + | } | ||
| + | </ | ||
| + | where you need to adapt the port (9000) and the location, of course. | ||
| + | |||
| + | ==== Updating php ==== | ||
| + | When you perform emerge updates PHP will be updated sooner or later. | ||
| + | You can, and should, migrate to the newer version when possible. In this case all you need to do it to update the value into **php-version** and probably also update the specific //php.ini// values. | ||
| - | This approach will load configuration files from **/ | ||