July 06, 2017

PHP-FPM: Multiple Resource Pools

Split PHP-FPM into multiple resource pools to separate applications and enhance system security!

Split PHP-FPM into multiple resource pools to separate applications and enhance system security!### Setup

Here's what was installed to test out PHP-FPM in this video.

sudo apt-get update
sudo apt-get install -y vim tmux curl wget unzip software-properties-common

sudo add-apt-repository -y ppa:nginx/stable
sudo add-apt-repository -y ppa:ondrej/php5-5.6

sudo apt-get update
sudo apt-get install -y nginx php5-fpm php5-cli php5-curl \
                              php5-mcrypt php5-mysql php5-gd
<?php
phpinfo();
?>

PHP-FPM

sudo cp /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/app2.conf

Changes to the new configuration:

# Change "www" to "app2"
[app2]

# Must listen on unique socket (file or tcp)
listen = /var/run/php5-fpm-app.sock;
listen = 127.0.0.1:9001;

Nginx

Pool per site.

server {
    listen       80 default_server;
    server_name  localhost;
    root         /var/www/phpsite;

    access_log /var/log/nginx/phpsite.com-access.log;
    error_log  /var/log/nginx/phpsite.com-error.log error;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    charset utf-8;
    index index.html index.htm index.php;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        # fastcgi_pass 127.0.0.1:9000;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}

Update php block:

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    # fastcgi_pass 127.0.0.1:9001;
    fastcgi_pass unix:/var/run/php5-fpm-app.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
}

Test and reload Nginx:

sudo service nginx configtest
sudo service nginx reload

All Topics