0

I configured php7.0-fpm to work with apache on Ubuntu 16.04 but not sure if it is correctly configured and working.

As I understand, after installation and configuration, the output of php info file should display "Server API --> FPM/FastCGI" but it is still showing "Server API --> Apache 2.0 Handler".

This is how I installed and configured php7.0-fpm:

sudo apt-get install libapache2-mod-fastcgi php7.0-fpm

sudo a2enmod actions fastcgi alias

sudo a2enconf php7.0-fpm

sudo service php7.0-fpm restart

sudo systemctl status php7.0-fpm

sudo systemctl restart apache2.service

Below is the output of sudo systemctl status php7.0-fpm:

php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2017-11-25 11:57:33 UTC; 23h ago
Process: 1149 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, status=0/SUCCESS)
Main PID: 1336 (php-fpm7.0)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3
Memory: 37.8M
CPU: 3.089s
CGroup: /system.slice/php7.0-fpm.service
├─1336 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
├─1340 php-fpm: pool www
└─1341 php-fpm: pool www

Nov 25 11:57:33 ip-10-0-0-126 systemd[1]: Starting The PHP 7.0 FastCGI Process Manager...
Nov 25 11:57:33 ip-10-0-0-126 systemd[1]: Started The PHP 7.0 FastCGI Process Manager.

What am I doing wrong? Would appreciate some help and advice?

Below is the contents of my /etc/apache2/conf-enabled/php7.0-fpm.conf file:

<Directory /usr/lib/cgi-bin>
    Require all granted
</Directory>
<IfModule mod_fastcgi.c>
    AddHandler php7-fcgi .php 
    Action php7-fcgi /php7-fcgi
    Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
    FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization
</IfModule>
  • You installed libapache2-mod-fastcgi and php7.0-fpm which does not make any sense really, that is two different things. – Orphans Dec 14 '17 at 10:06
  • Hello Ipor Sircer. I followed the instructions from an article. Any suggestions, how can i solve this issue? Thanks for help. – Ash Dec 16 '17 at 10:55
  • Can you add a link to the instructions you followed? – Keith Dec 22 '17 at 5:19
  • @Keith I followed this article [link] howtoforge.com/tutorial/apache-with-php-fpm-on-ubuntu-16-04/… [link] Thanks for your help. – Ash Dec 24 '17 at 11:04
1

You need to enable the proxy_fcgi module, and disable mod_php7, for the stock php7.0-fpm conf file to be effective.

If you look at /etc/apache2/conf-enabled/php7.0-fpm.conf, you'll see this at the top:

<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
1

I managed to get this working without too many problems by following the directions at Koen Reiniers' dev blog. I followed the directions almost exactly, and with one exception, noted in the blog post comments, it worked perfectly. You will need mod_fastcgi, as well as several other modules. This recipe will enable you to set up multiple vhosts, each of which uses an independent php7 fpm configuration. Note that unless phpinfo() is showing a server API of "FPM/FastCGI" then you've missed the mark. This is the acid test that you have the stack working. As a check, I also executed "<?php touch('/tmp/fpmtest');" to check that processes started for my vhost were executing as the specified user/group (they were).

This recipe is copied, with a few modifications, from Koen's blog, and credit is due to him for his clear and effective directions.

With the possible exception of the security-related stanzas in it, you do not want to enable the Apache conf for php7.0-fpm if you use this recipe. The only code that needs to be globally executed is:

<Directory "/usr/lib/cgi-bin">
  Require all granted
</Directory>

If your Apache2 configuration enables mod_cgi.c then this has probably already been taken care of in the default conf file for the module. Otherwise, if this module (or mod_cgid.c) isn't already enabled, this can be added to any conf file which is executed at Apache2 startup, although, depending on your server environment, it might be more secure to add it only to each <VirtualHost> for which PHP-FPM is enabled. Security-related code from php7.0-fpm.conf could also be copied to the same location, or enabled globally.

This guide assumes you have PHP 7.0.* running, as well as Apache (with virtual hosts), on a Ubuntu server. Also, please do not try this on a production environment without having proper backups in place.

First make sure php7.0-fpm is installed on your system:

$ sudo apt-get install php7.0-fpm

After it has been installed, start the service if it hasn’t automatically, and verify that it's running:

$ sudo service php7.0-fpm start && ps aux | grep php-fpm

To use PHP-FPM for each and every one of your virtual hosts you will have to split it into multiple resource pools, one for every separate <VirtualHost> user, which means that if you have multiple hosts assigned to one user you will only have to create one resource pool for that user.

The default location for the pool configuration for PHP-FPM is:

/etc/php/7.0/fpm/pool.d/

To create a new configuration for one of your users, simply create a new .conf file. I used the user’s username as the name of the config file:

$ sudo vim /etc/php/7.0/fpm/pool.d/username.conf

And use this as your configuration (replace "username" with the actual name of your user):

[username]
    user = username
    group = username
    listen = /run/php/php7.0-fpm.username.sock
    listen.owner = username
    listen.group = username
    listen.mode = 0666

    pm = dynamic
    pm.max_children = 5
    pm.start_servers = 2
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3

The first seven lines are the most important. Make sure that they are the same as the Apache virtualhost user. The last five lines are the default settings for the process manager. Leave them as is for the sake of simplicity, but you can change them to your liking. You can read more about these settings in the default config file located at:

/etc/php/7.0/fpm/pool.d/www.conf

The only thing remaining is telling Apache to use PHP-FPM instead of mod_php. First, make sure you have both mod_actions and mod_fastcgi enabled:

$ sudo a2enmod actions fastcgi
$ sudo service apache2 restart

Add the PHP-FPM fastcgi handler to Apache. You will have to do this for every user that you want to be able to user PHP-FPM. Edit the per-vhost file for username, typically at:

/etc/apache2/sites-available/username.conf

Add the following at the top:

<IfModule mod_fastcgi.c>
  AddHandler php7-fcgi-username .php
  Action php7-fcgi-username /php7-fcgi-username
  Alias /php7-fcgi-username /usr/lib/cgi-bin/php7-fcgi-username
  FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi-username -socket /run/php/php7.0-fpm.username.sock -pass-header Authorization
</IfModule>

Again, replace "username" with the actual name of the vhost’s user.

You must tell your vhost to actually use it for .php files, so in the same file, but in the <VirtualHost> directive, add the following lines:

<IfModule mod_fastcgi.c>
  <FilesMatch ".+\.ph(p[3457]?|t|tml)$">
    SetHandler php7-fcgi-username
  </FilesMatch>
</IfModule>

Replace “username” with the name of the actual vhost user again and save the file. It's very important that the SetHandler directive be wrppped with a subset of of the Apache conditional filesystem container tags, <Location>, <LocationMatch>, <Files> or <FilesMatch>. Ommitting this will cause SetHandler to be ignored. The <Directory> tag apparently doesn't cut it.

If you want to take advantage of PHP-FPM's "status" and "ping" capabilities, and have them turned on in your per-vhost pool file in the /etc/php/7.0/fpm/pool.d directory (see /etc/php/7.0/fpm/pool.d/www.conf for details) you'll need to make two additional modifications to your Apache2 setup. The Action directive at the top of the per-vhost .conf file must terminate with "virtual", thus:

Action php7-fcgi-username /php7-fcgi-username virtual

Additionally, your SetHandler wrapper must specify a regular expression which includes these virtual files. I use the following:

<LocationMatch ".+\.ph(p[3457]?|t|tml)|/(ping|status)$">
    SetHandler ....
</LocationMatch>

Now all that is left is to let Apache reload its config files:

$ sudo service apache2 reload

To see if your changes have taken effect, create a phpinfo.php file in your website’s public directory and visit it in your browser:

&lt;?php phpinfo();

The Server API shown near the top of the page should show "FPM/FastCGI".

To complete the process, you should repeat the steps for each of your virtual hosts. When you are entirely sure mod_php is not being used anymore you can disable it through:

$ sudo a2dismod php7.0

Until you’ve done this, Apache will still include a PHP process for every request, meaning the memory usage will stay the same and possibly be even higher.

  • After more research it looks as if the solution at wiki.apache.org/httpd/PHP-FPM which uses mod_proxy and mod_proxy_fcgi is preferred these days over the above solution using mod_fastcgi. This is another approach entirely, and in my experience it's easier to set up and get running. It's also less resource-hungry, according to people who've looked into the matter. – Lindsay Haisley Feb 28 '18 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.