From edca3040ebc6f42ceba494168f67439010a4e711 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Sun, 22 Oct 2017 20:15:00 -0400 Subject: [PATCH] split php upstream settings into its own configuration file and assign to $upstream variable --- README.md | 20 ++++++++++++++++++++ global/php-pool.conf | 12 ++++++++++++ nginx.conf | 5 ++++- sites-available/fastcgi-cache.com | 7 +++---- sites-available/multisite-subdirectory.com | 7 +++---- sites-available/multisite-subdomain.com | 7 +++---- sites-available/singlesite.com | 7 +++---- sites-available/ssl-fastcgi-cache.com | 7 +++---- sites-available/ssl.com | 13 ++++++------- upstreams/php70.conf | 4 ++++ 10 files changed, 61 insertions(+), 28 deletions(-) create mode 100644 global/php-pool.conf create mode 100644 upstreams/php70.conf diff --git a/README.md b/README.md index 1f9f80e..b4904bb 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,26 @@ Looking for a modern hosting environment provisioned using Ansible? Check out [W ## Usage +### PHP configuration + +The php-fpm pool configuration is located in `global/php-pool.conf` and defaults to PHP 7.1. It will need modified if you want the default php-fpm pool service to be a different PHP version. Additional PHP version upstream definitions can be added to the `/upstreams` folder (a PHP 7.0 sample is provided there). You can either use the default pool using `$upstream` in your nginx configurations or the specific upstream definition (i.e. php71, php70) setup by your custom upstream definitions. + +For example, currently the nginx configuration for `singlesite.com` has the following set for php requests: + +``` +fastcgi_pass $upstream +``` + +You could change that to the following to use the php 7.0 php service instead (assuming that php7.0-fpm service is running). + +``` +fastcgi_pass php70 +``` + +This effectively allows you to have different server blocks execute different versions of PHP if needed. + +### Site configuration + You can use these sample configurations as reference or directly by replacing your existing nginx directory. Follow the steps below to replace your existing nginx configuration. Backup any existing config: diff --git a/global/php-pool.conf b/global/php-pool.conf new file mode 100644 index 0000000..051e3c9 --- /dev/null +++ b/global/php-pool.conf @@ -0,0 +1,12 @@ +# Upstream to abstract backend connection(s) for PHP. +# Additional upstreams can be added to /etc/nginx/upstreams/*.conf and then you just +# change `default php71` to whatever the new upstream is (could be php70 for example). +upstream php71 { + server unix:/run/php/php7.1-fpm.sock; +} + +include /etc/nginx/upstreams/*.conf; + +map '' $upstream { + default php71; +} \ No newline at end of file diff --git a/nginx.conf b/nginx.conf index 806aa9e..8fee385 100644 --- a/nginx.conf +++ b/nginx.conf @@ -33,7 +33,7 @@ http { include global/limits.conf; # Some WP plugins that push large amounts of data via cookies - # can cause 500 HTTP erros if these values aren't increased. + # can cause 500 HTTP errors if these values aren't increased. fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; @@ -44,6 +44,9 @@ http { # Gzip include global/gzip.conf; + # exposes configured php pool on $upstream variable + include global/php-pool.conf; + # Modules include /etc/nginx/conf.d/*.conf; diff --git a/sites-available/fastcgi-cache.com b/sites-available/fastcgi-cache.com index 4644a69..01121ac 100644 --- a/sites-available/fastcgi-cache.com +++ b/sites-available/fastcgi-cache.com @@ -35,10 +35,9 @@ server { try_files $uri =404; include global/fastcgi-params.conf; - # Change socket if using PHP pools or different PHP version - fastcgi_pass unix:/run/php/php7.1-fpm.sock; - #fastcgi_pass unix:/run/php/php7.0-fpm.sock; - #fastcgi_pass unix:/var/run/php5-fpm.sock; + # Use the php pool defined in the upstream variable. + # See global/php-pool.conf for definition. + fastcgi_pass $upstream; # Skip cache based on rules in global/server/fastcgi-cache.conf. fastcgi_cache_bypass $skip_cache; diff --git a/sites-available/multisite-subdirectory.com b/sites-available/multisite-subdirectory.com index c057227..4cea13f 100644 --- a/sites-available/multisite-subdirectory.com +++ b/sites-available/multisite-subdirectory.com @@ -30,10 +30,9 @@ server { try_files $uri =404; include global/fastcgi-params.conf; - # Change socket if using PHP pools or different PHP version - fastcgi_pass unix:/run/php/php7.1-fpm.sock; - #fastcgi_pass unix:/run/php/php7.0-fpm.sock; - #fastcgi_pass unix:/var/run/php5-fpm.sock; + # Use the php pool defined in the upstream variable. + # See global/php-pool.conf for definition. + fastcgi_pass $upstream; } # Rewrite robots.txt diff --git a/sites-available/multisite-subdomain.com b/sites-available/multisite-subdomain.com index 14260e9..8dd6273 100644 --- a/sites-available/multisite-subdomain.com +++ b/sites-available/multisite-subdomain.com @@ -27,10 +27,9 @@ server { try_files $uri =404; include global/fastcgi-params.conf; - # Change socket if using PHP pools or different PHP version - fastcgi_pass unix:/run/php/php7.1-fpm.sock; - #fastcgi_pass unix:/run/php/php7.0-fpm.sock; - #fastcgi_pass unix:/var/run/php5-fpm.sock; + # Use the php pool defined in the upstream variable. + # See global/php-pool.conf for definition. + fastcgi_pass $upstream; } # Rewrite robots.txt diff --git a/sites-available/singlesite.com b/sites-available/singlesite.com index 70c7687..c954521 100644 --- a/sites-available/singlesite.com +++ b/sites-available/singlesite.com @@ -27,10 +27,9 @@ server { try_files $uri =404; include global/fastcgi-params.conf; - # Change socket if using PHP pools or different PHP version - fastcgi_pass unix:/run/php/php7.1-fpm.sock; - #fastcgi_pass unix:/run/php/php7.0-fpm.sock; - #fastcgi_pass unix:/var/run/php5-fpm.sock; + # Use the php pool defined in the upstream variable. + # See global/php-pool.conf for definition. + fastcgi_pass $upstream; } # Rewrite robots.txt diff --git a/sites-available/ssl-fastcgi-cache.com b/sites-available/ssl-fastcgi-cache.com index 60c202d..5e1b8a0 100644 --- a/sites-available/ssl-fastcgi-cache.com +++ b/sites-available/ssl-fastcgi-cache.com @@ -42,10 +42,9 @@ server { try_files $uri =404; include global/fastcgi-params.conf; - # Change socket if using PHP pools or different PHP version - fastcgi_pass unix:/run/php/php7.1-fpm.sock; - #fastcgi_pass unix:/run/php/php7.0-fpm.sock; - #fastcgi_pass unix:/var/run/php5-fpm.sock; + # Use the php pool defined in the upstream variable. + # See global/php-pool.conf for definition. + fastcgi_pass $upstream; # Skip cache based on rules in global/server/fastcgi-cache.conf. fastcgi_cache_bypass $skip_cache; diff --git a/sites-available/ssl.com b/sites-available/ssl.com index 076b377..161be4d 100644 --- a/sites-available/ssl.com +++ b/sites-available/ssl.com @@ -34,14 +34,13 @@ server { try_files $uri =404; include global/fastcgi-params.conf; - # Change socket if using PHP pools or different PHP version - fastcgi_pass unix:/run/php/php7.1-fpm.sock; - #fastcgi_pass unix:/run/php/php7.0-fpm.sock; - #fastcgi_pass unix:/var/run/php5-fpm.sock; + # Use the php pool defined in the upstream variable. + # See global/php-pool.conf for definition. + fastcgi_pass $upstream; } - # Rewrite robots.txt - rewrite ^/robots.txt$ /index.php last; + # Rewrite robots.txt + rewrite ^/robots.txt$ /index.php last; } # Redirect http to https @@ -60,4 +59,4 @@ server { server_name www.ssl.com; return 301 https://ssl.com$request_uri; -} \ No newline at end of file +} diff --git a/upstreams/php70.conf b/upstreams/php70.conf new file mode 100644 index 0000000..74e6cbc --- /dev/null +++ b/upstreams/php70.conf @@ -0,0 +1,4 @@ +# Defines the upstream for PHP 7.0 +upstream php70 { + server unix:/run/php/php7.0-fpm.sock; +} \ No newline at end of file