ObiHill
ObiHill

Reputation: 11886

Does each PHP-FPM Worker individually load php.ini?

I installed PHP 5.3.8 from source on Ubuntu Natty. I also enabled FPM during configuration.

However, I've been having issues installing PHP extensions. When I add the extension to my php.ini file (e.g. extension=apc.so) and restart PHP (i.e. /etc/init.d/php-fpm restart), I get an error like the one below:

Starting php-fpm PHP Warning:  Module 'apc' already loaded in Unknown on line 0
<br />
<b>Warning</b>:  Module 'apc' already loaded in <b>Unknown</b> on line <b>0</b><br />
PHP Warning:  Module 'geoip' already loaded in Unknown on line 0
<br />
<b>Warning</b>:  Module 'geoip' already loaded in <b>Unknown</b> on line <b>0</b><br />
PHP Warning:  Module 'imagick' already loaded in Unknown on line 0
<br />
<b>Warning</b>:  Module 'imagick' already loaded in <b>Unknown</b> on line <b>0</b><br />
PHP Warning:  Module 'memcache' already loaded in Unknown on line 0
<br />

My PHP-FPM conf is has the setting pm.start_servers = 5, so I have about 5 PHP-FPM workers active. Are they all trying to load the php.ini file when I restart PHP (hence the message that the module has already been loaded)?! If they are, is there a way I can prevent that to stop the errors as it the errors become fatal when I load Zend Loader?

Thanks in advance.

Upvotes: 2

Views: 11934

Answers (3)

ObiHill
ObiHill

Reputation: 11886

After a couple of hours I managed to find a way to remedy this. I guess this would be more beneficial to those compiling and installing PHP with FPM from source (All you apt-get people probably don't have anything to do with this)

Basically, it involves using your php-fpm.conf to load all your PHP extensions, instead of php.ini. Here's what you do:

  1. Open your php-fpm.conf file (I installed my php from source so I have mine at /usr/local/php/etc/php-fpm.conf based on how I setup my --prefix and --with-config-file-path, but you can use find / -name "php-fpm.conf" from the bash shell to find where yours is).

  2. Add your extension load directives (ideally at the end of the file). You need to be careful here as the format is a little different. In your php.ini file it would be like this extension=apc.so, but in your php-fpm.conf file it should be defined like this php_admin_value[extension]=apc.so.

NOTE: Make sure your extension_dir (where you kept all the .so files for your extensions) value is defined in your php.ini file. Then restart php-fpm.

I did the above and have no more errors, and all my extension show up in phpinfo().

I hope this helps someone, it practically drove me crazy.

Cheers.

EDIT

After further testing I found that the issue was actually a little more complicated. It was being caused by PHP loading the php.ini twice as I had specified the same location for --with-config-file-path and --with-config-file-scan-dir at compile time.

So you would want to do the following as a better alternative to what I specified above (which also works but has a limitation as you will not be able to load Zend extensions e.g. IonCube Loader):

  1. Compile PHP with the following option --with-config-file-scan-dir=/usr/local/php/etc/conf.d. Make it is a different directory from --with-config-file-path.
  2. Create a separate file for loading your PHP extensions (you can call it phpext.ini) and save it at the location defined above i.e. /usr/local/php/etc/conf.d. In this file, define all the extensions you want to load e.g. extension=apc.so. Your extensions should be saved in the /usr/lib/php5/20090626/ directory, which should be the default directory I suppose. Make sure you don't define your extensions in the main php.ini file.
  3. Restart PHP

Everything will work fine now and you will be able to load Zend Extensions (as they must be defined in your php.ini file and can't be in the php-fpm.conf).

Cheers.

Upvotes: 8

Marc B
Marc B

Reputation: 360782

Check that there aren't any geoip/apc sub-ini's in /etc/php5/conf.d. Ubuntu tends to load external drivers by sticking their .ini into that conf.d directory, and leaves the main .ini alone.

Upvotes: 0

hakre
hakre

Reputation: 198203

Ubuntu uses multiple confiugration files for PHP, often one per extension. If you install the extension via the system software management (e.g. apt), you don't need to manually enable the extension in the php.ini file.

If you remove the files you've added into php.ini this should solve your issue.

Additionally: PHP reads all ini files once per active process.

Upvotes: 0

Related Questions