1

I have some php scripts that need to run as another user than the apache user. In order to this, the scripts need to be run using FastCGI

My plan is to use FastCGI in only a single virtual host. Is it possible to setup FasCGI only under a single virtual host, while all other php scripts are executed with mod_php?

If so any insight or resources you can provide into setting this up would be extremely helpful.

Jeffrey Ray
  • 1,244
  • 2
  • 9
  • 20

1 Answers1

0

I'll assume:

  • SO: Debian GNU/Linux
  • localhost.holomodor: host that will use PHP-FPM. Located in /var/www/holomodor/
  • version of PHP: 8.1
  • version of Apache: 2.4
  • PHP-FPM/Apache proxy method (unix socket or TCP socket): TCP socket
  1. Install both, mod_php and php-fpm:
user@debian:~$ sudo apt install -y libapache2-mod-php8.1 php8.1-fpm
  1. Create a virtualhost called localhost.holomodor in Apache just creating a new file on virtualhost configurations files directory of Apache (default: /etc/apache2/sites-available/):
<VirtualHost localhost.holomodor:80>
  ServerAdmin user@email
  ServerName localhost.holomodor
  ServerAlias www.localhost.holomodor
  DocumentRoot /var/www/holomodor

  ErrorLog /var/www/holomodor/error.log # plz change this
  CustomLog /var/www/holomodor/access.log combined # plz change this

  <Directory /var/www/holomodor>
      Options Indexes MultiViews
      AllowOverride None
      Require all granted

      # Begin FPM config
      <FilesMatch \.php$>
          # TCP socket example (port):
          # SetHandler proxy:fcgi://127.0.0.1:9000
          # or, unix socket example (file):
          # SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost.holomodor/"
          <If "-f %{REQUEST_FILENAME}">
          SetHandler proxy:fcgi://127.0.0.1:9000
          </If>
      </FilesMatch>
      # end FPM config
  </Directory>
</VirtualHost>
  1. Create a FPM pool for localhost.holomodor on PHP-FPM pool configuration directory (default: /etc/php/8.1/fpm/pool.d/):
# rename default config
# remove default (unused) localhost.conf pool
user@debian:~$ sudo cp /etc/php/8.1/fpm/pool.d/localhost.conf /etc/php/8.1/fpm/pool.d/localhost-holomodor.conf \
sudo rm /etc/php/8.1/fpm/pool.d/localhost.conf
  1. Changes on localhost.holomodor PHP-FPM pool /etc/php/8.1/fpm/pool.d/localhost-holomodor.conf

4.1 Set the TCP socket address for requests (and problably customize your pool with other things):

# ...
listen = 127.0.0.1:9000
# ...

4.2 Change the pool name ([localhost]) to localhost.holomodor on this same file:

# ...
[localhost.holomodor]
# ...
  1. Enable localhost.holomodor virtualhost; Enable Apache modules: mod_php, mod_proxy and mod_proxy_fcgi; restart Apache and PHP-FPM services:
user@debian:~$ sudo a2ensite localhost.holomodor \
sudo systemctl restart apache2 php8.1-fpm

VoilĂ  http://localhost.holomodor:80