Last active
August 1, 2021 15:27
-
-
Save haproxytechblog/dd86992640295e097e016c00270f4ce9 to your computer and use it in GitHub Desktop.
Load Balancing PHP-FPM with HAProxy and FastCGI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo apt update | |
$ sudo apt install php-fpm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo mkdir -p /var/www/myapp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php phpinfo(); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd pool.d | |
$ sudo cp www.conf myapp.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; The address on which to accept FastCGI requests. | |
listen = /run/php/myapp.sock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
listen = 127.0.0.1:9000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo systemctl restart php7.2-fpm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)(/.*)?$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
backend phpservers | |
use-fcgi-app php-fpm | |
server server1 /run/php/myapp.sock proto fcgi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server server1 127.0.0.1:9000 proto fcgi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fcgi-app php-fpm | |
log-stderr global | |
docroot /var/www/myapp | |
index index.php | |
path-info ^(/.+\.php)(/.*)?$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fcgi-app php-fpm | |
log-stderr global | |
docroot / | |
index index.php | |
path-info ^(/.+\.php)(/.*)?$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http-request deny if { path_sub -i %0a %0d } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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