Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Last active August 1, 2021 15:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save haproxytechblog/dd86992640295e097e016c00270f4ce9 to your computer and use it in GitHub Desktop.
Save haproxytechblog/dd86992640295e097e016c00270f4ce9 to your computer and use it in GitHub Desktop.
Load Balancing PHP-FPM with HAProxy and FastCGI
$ sudo apt update
$ sudo apt install php-fpm
$ php --version
PHP 7.2.24-0ubuntu0.18.04.1 (cli) (built: Oct 28 2019 12:07:07) (NTS)
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
$ sudo systemctl status php7.2-fpm.service
php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-01-07 15:38:28 UTC; 49min ago
$ sudo mkdir -p /var/www/myapp
<?php phpinfo(); ?>
$ cd pool.d
$ sudo cp www.conf myapp.conf
; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[myapp]
; The address on which to accept FastCGI requests.
listen = /run/php/myapp.sock
listen = 127.0.0.1:9000
$ sudo systemctl restart php7.2-fpm
global
log /dev/log local0
user haproxy
group www-data
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5s
timeout client 50s
timeout server 50s
frontend myproxy
bind :80
default_backend phpservers
backend phpservers
use-fcgi-app php-fpm
server server1 /run/php/myapp.sock proto fcgi
fcgi-app php-fpm
log-stderr global
docroot /var/www/myapp
index index.php
path-info ^(/.+\.php)(/.*)?$
backend phpservers
use-fcgi-app php-fpm
server server1 /run/php/myapp.sock proto fcgi
server server1 127.0.0.1:9000 proto fcgi
fcgi-app php-fpm
log-stderr global
docroot /var/www/myapp
index index.php
path-info ^(/.+\.php)(/.*)?$
; Chroot to this directory at the start. This value must be defined as an
; absolute path. When this value is not set, chroot is not used.
chroot = /var/www/myapp
fcgi-app php-fpm
log-stderr global
docroot /
index index.php
path-info ^(/.+\.php)(/.*)?$
http-request deny if { path_sub -i %0a %0d }
SecRule REQUEST_URI "@rx %0(a|d)" "id:1,phase:1,t:lowercase,deny"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment