Okay, going slightly crazy trying to figure this out. (I have read hundreds of questions/answers, and google articles, but none have answered it)
I have just changed from using mod_php
to using PHP through FastCGI and fpm, using the method described in this question, purely because I was under the impression it was 'easy' to specify php.ini files for individual vhosts using this set-up.
What I'm pulling my hair out over, is how can I specify a custom PHP ini file each vhost uses?
Luckily, It's only on my test rig so far ... But I am hoping to do the same on my production server if I can ever figure this out
I thought I may as-well post the whole process I took to configure fpm with pools, as @ChristianM mentioned, because I've not yet found a full explanation on how to do it.
The first part of this is mostly a copy of an AskUbuntu post: https://askubuntu.com/questions/378734/how-to-configure-apache-to-run-php-as-fastcgi-on-ubuntu-12-04-via-terminal/527227#comment905702_527227
The last part is how to configure pools, and get the vhost to use the relevent pool settings
Here it goes:
Install the apache mpm worker (Explanation of prefork/wroker and event at http://www.vps.net/blog/2013/04/08/apache-mpms-prefork-worker-and-event/):
sudo apt-get install apache2-mpm-worker
Install fastcgi and php5-fpm:
sudo apt-get install libapache2-mod-fastcgi php5-fpm
Now enable mods you need, and disable those you don't:
sudo a2dismod php5 mpm_prefork
sudo a2enmod actions fastcgi alias mpm_worker
Create the php5.fcgi file and give the webserver permission to use it.
sudo touch /usr/lib/cgi-bin/php5.fcgi
sudo chown -R www-data:www-data /usr/lib/cgi-bin
Create a global config for php5-fpm
sudo nano /etc/apache2/conf-available/php5-fpm.conf
paste in the following (we'll use a socket instead of IP address)
<IfModule mod_fastcgi.c>
AddHandler php5.fcgi .php
Action php5.fcgi /php5.fcgi
Alias /php5.fcgi /usr/lib/cgi-bin/php5.fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5.fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization -idle-timeout 3600
<Directory /usr/lib/cgi-bin>
Require all granted
</Directory>
</IfModule>
Note: Ensure all configs follow the same new 'Require all granted'/'Require all denied' syntax ... Otherwise you'll feel the pain after restarting ...
Enable the php5-fpm conf
sudo a2enconf php5-fpm
Restart apache and fpm
sudo service apache2 restart && sudo service php5-fpm restart
This setup essentially creates a global fastcgi configuration for php, which uses the file /etc/php5/fpm/php.ini file.
If you have multiple vhosts, that are going to need different php configurations, continue with the example below
First, within the /etc/php5/fpm/pool.d dir, you will find the default www.conf file. Copy this, naming it something relevent:
sudo cp /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/domain2.conf
Edit this file, changing the pool name:
[...]
[domain2]
[...]
And change name of the listen socket to something relevent:
[...]
listen = /var/run/php5-fpm-domain2.sock
[...]
Then copy the /usr/lib/cgi-bin/php5.fcgi file, again naming it something relevent:
cp /usr/lib/cgi-bin/php5.fcgi /usr/lib/cgi-bin/php5-domain2.fcgi
Now you're ready to add the mod_fastcgi module to the domain2 vhost. It's almost the same as the one described above, but notice the changes for 'Alias','FastCgiServer' and '-socket'
<VirtualHost *:80>
ServerName domain2.com
[...]
<IfModule mod_fastcgi.c>
AddHandler php5.fcgi .php
Action php5.fcgi /php5.fcgi
Alias /php5.fcgi /usr/lib/cgi-bin/php5-domain2.fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-domain2.fcgi -socket /var/run/php5-fpm-domain2.sock -pass-header Authorization -idle-timeout 3600
<Directory /usr/lib/cgi-bin>
Require all granted
</Directory>
</IfModule>
[...]
</VirtualHost>
Restart apache and fpm
sudo service apache2 restart && sudo service php5-fpm restart
Now to test changes.
In your new /etc/php5/fpm/pool.d/domain2.conf file, add a php value change (I've chosen the session.name value):
[...]
php_admin_value[session.name] = 'DOMAIN2'
[...]
Now test the configuration before restarting fpm:
sudo php5-fpm -t
It will tell you if the configuration fails, but more importantly will tell you if your configuration is fine. Then you can go ahead and restart fpm:
sudo service php5-fpm restart
And finally, if you want to be super sure the php value has been set, create info.php within your site, and just add:
<?php
phpinfo();
?>
Unfortunately it is not possible to set a php.ini file per vhost. What you can do is configure different php-fpm pools and give each a set of php configuration values that set/override something from the (shared) default config. See fpm configuration on how to do that.
Example config with different pools
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With