zeldor.biz

Linux, programming and more

Copyright © 2023
Log in

Apache2 and php5-fpm combination

March 26, 2011 by Igor Drobot 6 Comments

PHP-FPM (FastCGI Process Manager) is a great alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

1
vim /etc/apt/sources.list

vim /etc/apt/sources.list

Add source for php5-fpm:

1
2
# PHP-FPM
deb http://packages.dotdeb.org stable all

# PHP-FPM deb http://packages.dotdeb.org stable all

1
aptitude install apache2 apache2-mpm-worker libapache2-mod-fastcgi php5-fpm

aptitude install apache2 apache2-mpm-worker libapache2-mod-fastcgi php5-fpm

Enable apache modules after installation:

1
a2enmod actions fastcgi

a2enmod actions fastcgi

Replace your content of fastcgi with this one:

1
vim /etc/apache2/mods-enabled/fastcgi.conf

vim /etc/apache2/mods-enabled/fastcgi.conf

1
2
3
4
5
6
7
8
9
<ifmodule mod_fastcgi.c="">
  AddHandler php5-fcgi .php
  Action php5-fcgi /cgi-bin/php5.external
  <location "="" cgi-bin="" php5.external"="">
    Order Deny,Allow
    Deny from All
    Allow from env=REDIRECT_STATUS
  </location>
</ifmodule>

<ifmodule mod_fastcgi.c=""> AddHandler php5-fcgi .php Action php5-fcgi /cgi-bin/php5.external <location "="" cgi-bin="" php5.external"=""> Order Deny,Allow Deny from All Allow from env=REDIRECT_STATUS </location> </ifmodule>

Example of apache virtual host:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<virtualhost *:80="">
        ServerName dev.zeldor.biz
        ServerAdmin webmaster@zeldor.biz
        DocumentRoot /var/www/vhosts/dev.zeldor.biz/public_html/
        Options None
 
        # Fast CGI + FPM
        FastCgiExternalServer /var/www/vhosts/dev.zeldor.biz/cgi-bin/php5.external -socket /var/run/php5-fpm/dev.soc
        Alias /cgi-bin/ /var/www/vhosts/dev.zeldor.biz/cgi-bin/
 
        Alias /phpguru /usr/share/phpmyadmin
 
        <directory var="" www="" vhosts="" dev.zeldor.biz="" public_html="">
                Options SymLinksIfOwnerMatch
                AllowOverride All
                Order allow,deny
                Allow from all
        </directory>
 
        ErrorLog ${APACHE_LOG_DIR}/dev.zeldor.biz/error.log
        LogLevel warn
 
        CustomLog ${APACHE_LOG_DIR}/dev.zeldor.biz/access.log combined
</virtualhost>

<virtualhost *:80=""> ServerName dev.zeldor.biz ServerAdmin webmaster@zeldor.biz DocumentRoot /var/www/vhosts/dev.zeldor.biz/public_html/ Options None # Fast CGI + FPM FastCgiExternalServer /var/www/vhosts/dev.zeldor.biz/cgi-bin/php5.external -socket /var/run/php5-fpm/dev.soc Alias /cgi-bin/ /var/www/vhosts/dev.zeldor.biz/cgi-bin/ Alias /phpguru /usr/share/phpmyadmin <directory var="" www="" vhosts="" dev.zeldor.biz="" public_html=""> Options SymLinksIfOwnerMatch AllowOverride All Order allow,deny Allow from all </directory> ErrorLog ${APACHE_LOG_DIR}/dev.zeldor.biz/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/dev.zeldor.biz/access.log combined </virtualhost>

Now edit your php5-fpm.conf (/etc/php5/fpm/php5-fpm.conf)

Find section Pool Definitions add add this include for your separated domains.

1
include=/etc/php5/fpm/pools/*.conf

include=/etc/php5/fpm/pools/*.conf

Every modification will be made strictly in your pool config like this one.

1
vim /etc/php5/fpm/pools/dev.zeldor.biz.conf

vim /etc/php5/fpm/pools/dev.zeldor.biz.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[dev.zeldor.biz]
listen = /var/run/php5-fpm/dev.zeldor.biz.soc
 
listen.owner = www-dev
listen.group = www-dev
listen.mode = 0666
 
user = www-dev
group = www-dev
 
pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
 
php_admin_value[open_basedir]=/var/www/vhosts/dev.zeldor.biz/public_html
:/usr/share/phpmyadmin
php_admin_value[session.save_path]=/var/www/vhosts/dev.zeldor.biz/tmp
php_admin_value[upload_tmp_dir]=/var/www/vhosts/dev.zeldor.biz/tmp
php_admin_value[memory_limit] = 128M
php_admin_value[upload_max_filesize] = 100M
php_admin_value[post_max_size] = 100M

[dev.zeldor.biz] listen = /var/run/php5-fpm/dev.zeldor.biz.soc listen.owner = www-dev listen.group = www-dev listen.mode = 0666 user = www-dev group = www-dev pm = dynamic pm.max_children = 20 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 10 php_admin_value[open_basedir]=/var/www/vhosts/dev.zeldor.biz/public_html :/usr/share/phpmyadmin php_admin_value[session.save_path]=/var/www/vhosts/dev.zeldor.biz/tmp php_admin_value[upload_tmp_dir]=/var/www/vhosts/dev.zeldor.biz/tmp php_admin_value[memory_limit] = 128M php_admin_value[upload_max_filesize] = 100M php_admin_value[post_max_size] = 100M

That means each domain may have different configuration types!

Add user for each domain and disable shell access

1
adduser www-dev

adduser www-dev

Filed Under: Debian, Linux Tagged With: apache vhost, apache2, apache2 and php5-fpm, fastcgi, php5-fpm5

Categories

Archives

Tags

apache2 Apple arduino ARM Automation backup bash Cisco Cluster Corosync Database Debian Debian squeeze DIY DNS Fedora FTP Fun Icinga Ipv6 KVM Linux LVM MAC OS X Monitoring MySQL Nagios Nginx openSUSE OpenVPN PHP Proxy Python python3 qemu RAID rsync Samba security ssh Ubuntu virtualization Windows Windows 7 Wordpress

Comments

  1. Dieter says

    October 14, 2012 at 23:39

    Not working, got:

    FastCGI: server “/var/htdocs/www.test.de/cgi-bin/php5.external” stderr: Primary script unknown

    cgi-bin exists
    all rights set to www-data.www-data, chmod 777 everything

  2. Josh says

    June 3, 2012 at 16:11

    Figured out the 404. The cgi-bin directory has to exist.

  3. Josh says

    June 3, 2012 at 15:58

    I keep getting 404s.

    The requested URL /cgi-bin/php5.external/index.php was not found on this server.

  4. Web Hosting says

    January 14, 2012 at 10:17

    Thanks for the small tutorial. Just wanted to point out here that thanks for the separate PHP-FPM pool files it is finally possible to run the PHP interpreter under different user accounts.

Trackbacks

  1. Nginx and php5-fpm combination says:
    August 14, 2020 at 23:06

    […] months ago I wrote how to combine Apache2 and php5-fpm, now I want to show you how you could migrate to […]

  2. Nginx and php5-fpm combination « ID's blog says:
    May 26, 2011 at 14:50

    […] months ago I wrote how to combine Apache2 and php5-fpm, now I want to show you how you could migrate to […]

Leave a Reply

Your email address will not be published. Required fields are marked *