├── LICENSE.md ├── README.md └── nginx-for-php-fpm ├── Dockerfile └── default.conf /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2019, Yoshi Walsh 3 | 4 | Permission to use, copy, modify, and/or distribute this software for any 5 | purpose with or without fee is hereby granted, provided that the above 6 | copyright notice and this permission notice appear in all copies. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nginx-for-php-fpm 2 | 3 | [![](https://img.shields.io/docker/cloud/build/yoshiwalsh/nginx-for-php-fpm.svg)](https://hub.docker.com/r/yoshiwalsh/nginx-for-php-fpm) 4 | 5 | This Docker image allows for serving php-fpm apps with nginx. It uses Alpine Linux, so it's lean and mean. Designed to be easily used within Docker Swarm. 6 | 7 | ## Usage instructions 8 | 9 | This image is designed for use within a Compose file. To use it, you should: 10 | 11 | 1. Create a service in your docker-compose.yml file using this image (yoshiwalsh/nginx-for-php-fpm) 12 | 2. Create a service in your docker-compose.yml file which runs your PHP application with PHP-FPM 13 | 1. The PHP-FPM image can contain the application code anywhere in its filesystem (`/var/www/html` is commonly used) 14 | 2. The php-fpm.conf file should contain a `chdir` directive which points to the directory that you want served by nginx (e.g. for a Laravel app, you might wish to serve `/var/www/html/public/`) 15 | 3. The PHP-FPM service should either be named `php-fpm`, or should be aliased as `php-fpm` on a network that the nginx service has access to 16 | 4. The PHP-FPM service should listen on port 9000 17 | 3. Create a [named volume](https://docs.docker.com/compose/compose-file/#volume-configuration-reference) to store the content that you want nginx to serve 18 | 4. Add this volume to the php-fpm service so it maps to the location from step 2b 19 | 5. Add this volume to the nginx service so it maps to `/var/www/html` 20 | 6. Make it so that the nginx service `depends_on` the php-fpm service 21 | 7. If you want to apply any customisations to the nginx configuration, (e.g. redirect or rewrite rules) you can use a [config](https://docs.docker.com/compose/compose-file/#configs) with the target path set to /etc/nginx/conf.d/custom.conf or you can share it from your php-fpm image via a named volume 22 | -------------------------------------------------------------------------------- /nginx-for-php-fpm/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:stable-alpine 2 | COPY default.conf /etc/nginx/conf.d/default.conf -------------------------------------------------------------------------------- /nginx-for-php-fpm/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | server_name localhost; 4 | root /var/www/html; 5 | index index.html index.htm index.php; 6 | 7 | charset utf-8; 8 | 9 | location = /favicon.ico { access_log off; log_not_found off; } 10 | location = /robots.txt { access_log off; log_not_found off; } 11 | 12 | access_log off; 13 | error_log stderr error; 14 | 15 | # I'm being fairly generous max_body_size and very generous client_boidy_timeout. 16 | # I highly recommend you configure PHP with lower values. 17 | client_max_body_size 200m; 18 | client_body_timeout 600s; 19 | 20 | sendfile on; 21 | 22 | location / { 23 | try_files $uri $uri/ /index.php?$query_string; 24 | } 25 | 26 | location ~ \.php$ { 27 | fastcgi_split_path_info ^(.+\.php)(/.+)$; 28 | if (!-f $document_root$fastcgi_script_name) { 29 | return 404; 30 | } 31 | 32 | fastcgi_pass php-fpm:9000; 33 | fastcgi_index index.php; 34 | 35 | include fastcgi_params; 36 | 37 | # Mitigate https://httpoxy.org/ vulnerabilities 38 | fastcgi_param HTTP_PROXY ""; 39 | fastcgi_intercept_errors off; 40 | fastcgi_connect_timeout 30s; 41 | fastcgi_send_timeout 300s; 42 | fastcgi_read_timeout 600s; 43 | 44 | # Removing the leading slash from $fastcgi_script_name allows it to be interpreted relative to php-fpm.conf's `chdir` directive 45 | set $filename "index.php"; 46 | if ( $fastcgi_script_name ~ "^/+(.*)$" ) { 47 | set $filename $1; 48 | } 49 | fastcgi_param SCRIPT_FILENAME $filename; 50 | fastcgi_param PATH_INFO $fastcgi_path_info; 51 | fastcgi_param PATH_TRANSLATED $fastcgi_path_info; 52 | } 53 | } --------------------------------------------------------------------------------