Введение
Я хочу создать сайт, работающий на php-fpm
, nginx
и docker
, и я хочу иметь php-fpm
и nginx
в отдельных контейнерах. Чтобы сделать это, я следовал этому руководству , но сейчас застрял при доступе к моему веб-серверу под localhost:8000
. Мой браузер отображает 502, а мой /var/log/nginx/project_error.log
заполнен
[error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: app, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:8000"
найденные ответы
Я уже немного искал эту тему и нашел следующие темы:
Однако никто из них не решил мои проблемы.
Файлы
Я создал GitHub-репозиторий , содержащий все файлы, так что любой может просто проверить его там (я думаю, это лучше, чем сбросить все файлы здесь). Для этого просто сделайте
git clone git@github.com:tzfrs/dockertest.git
mkdir -p citynavigator/public
touch citynavigator/public/index.php
В дополнение к этому, вот код
генеральный
docker-compose.yml
version: "2"
services:
db:
image: mysql:5.6
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=citynavigatorrootpass
- MYSQL_DATABASE=citynavigator
- MYSQL_USER=citynavigator
- MYSQL_PASSWORD=citynavigatorpass
volumes:
- /var/lib/mysql
nginx:
image: nginx:latest
ports:
- "8000:80"
volumes:
- ../citynavigator:/var/www/app
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/app.conf:/etc/nginx/conf.d/default.conf
- ./nginx/fastcgi_params:/etc/nginx/fastcgi_params
links:
- php
networks:
- code-network
php:
build: fpm/
volumes:
- ../citynavigator:/var/www/app
networks:
- code-network
networks:
code-network:
driver: bridge
fpm/Dockerfile
FROM php:7.2-fpm
LABEL maintainer="Theo Tzaferis<t.tzfrs@gmail.com>"
USER root
RUN rm /usr/local/etc/php-fpm.d/docker.conf \
&& rm /usr/local/etc/php-fpm.d/www.conf \
&& rm /usr/local/etc/php-fpm.d/www.conf.default \
&& rm /usr/local/etc/php-fpm.d/zz-docker.conf \
&& rm -rf /var/www/html
ADD app.pool.conf /usr/local/etc/php-fpm.d/app.pool.conf
fpm/app.pool.conf
[app]
user = www-data
group = www-data
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 20
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
catch_workers_output = yes
nginx/app.conf
upstream php-upstream {
server 127.0.0.1:9000;
}
server {
server_name app;
root /var/www/app/public;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass php-upstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# optionally set the value of the environment variables used in the application
# fastcgi_param APP_ENV prod;
# fastcgi_param APP_SECRET <app-secret-id>;
# fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name";
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/index.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
nginx/fastcgi_params
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param HTTPS $https;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
# fastcgi_param REDIRECT_STATUS 200;
# Allow 10 minute script execution time
fastcgi_read_timeout 600s;
nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log off;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;
include /etc/nginx/conf.d/*.conf;
open_file_cache max=100;
client_max_body_size 30m;
}