我以前在这里见过类似的问题和解决方案,但它们不是解决方案。我想使用 docker 在本地运行一个 WordPress 网站,但它不起作用。我得到“localhost没有发送任何数据ERR_EMPTY_RESPONSE”。我根据评论编辑了代码。现在我收到 502 Bad Gateway 错误。如果您能帮助我解决问题,我将不胜感激。您可以在本文末尾找到我的两个相关代码文件。
当我输入 docker-compose up -d 时,我得到输出:
当我输入 docker-compose ps 时,我得到输出:
码头工人:
nginx.conf
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass wordpress:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
docker-compose.yml
services:
db:
image: mysql:latest
restart: always
environment:
MYSQL_DATABASE: wordpress_db
MYSQL_USER: wp_user
MYSQL_PASSWORD: wp_password
MYSQL_ROOT_PASSWORD: '1'
volumes:
- db_data:/var/lib/mysql
ports:
- "3306:3306"
wordpress:
image: wordpress:latest
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wordpress_db
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: wp_password
volumes:
- wp_data:/var/www/html
depends_on:
- db
ports:
- "9000:9000"
nginx:
image: nginx:latest
ports:
- "80:80"
- "443:443"
volumes:
- wp_data:/var/www/html
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- wordpress
volumes:
db_data:
wp_data:
此设置将使用 Docker Compose 来编排容器。这是设置 WordPress 环境的综合指南:
docker-compose.yml
services:
wordpress:
image: wordpress:latest
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress_user
WORDPRESS_DB_PASSWORD: wordpress_pass
WORDPRESS_DB_NAME: wordpress_db
volumes:
- wordpress_data:/var/www/html
depends_on:
- db
db:
image: mysql:latest
restart: always
environment:
MYSQL_DATABASE: wordpress_db
MYSQL_USER: wordpress_user
MYSQL_PASSWORD: wordpress_pass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db_data:/var/lib/mysql
nginx:
image: nginx:latest
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
- wordpress_data:/var/www/html
depends_on:
- wordpress
volumes:
wordpress_data:
db_data:
nginx.conf
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass wordpress:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
docker-compose up -d
http://localhost