docker使用lnmp搭建wordpress访问php报错

发布于 2022-09-12 22:57:25 字数 4208 浏览 53 评论 0

环境

[root@docker opt]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
···
···

[root@docker opt]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core) 

[root@docker opt]# firewall-cmd --state
not running

[root@docker opt]# getenforce 
Permissive

配置文件:

php容器的镜像vphp:7.3-fpm是使用php官方的php:7.3-fpm镜像仅添加了一个nginx的用户和组制作的

docker-compose:
[root@docker lnmp]# cat docker-compose.yml 
version: "3.0"

services:
  web01:
    container_name: nginx01
    depends_on:
      - "db01"
      - "php01"
    image: nginx:stable
    ports:
      - "8080:80"
    volumes:
      - /opt/conf/nginx/:/etc/nginx
      - /opt/html/:/usr/share/nginx/html
    #restart: always
    networks:
      - lnmp

  db01:
    container_name: mysql01
    image: mysql:5.7
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql
    #environment:
    #  - MYSQL_ROOT_PASSWORD=123.com
    env_file:
      - env.mysql
    #restart: always
    networks:
      - lnmp

  php01:
    container_name: php-fpm01
    image: vphp:7.3-fpm
    ports:
      - "9000:9000"
    volumes:
      - /opt/conf/php-fpm/php-fpm.d:/usr/local/etc/php-fpm.d
      - /opt/html/:/var/www/html
    networks:
      - lnmp
    privileged: true

#  dashboard:
#    container_name: "portainer"
#    image: "portainer/portainer-ce:2.1.1"
#    restart: "always"
#    ports:
#      - "8000:8000"
#      - "9001:9000"
#    volumes:
#      - "/var/run/docker.sock:/var/run/docker.sock"
#      - "portainer_data:/data"
#    networks:
#      - "lnmp"

volumes:
  db_data:
#    external:
#      false
  portainer_data:

networks:
  lnmp:
#    external:
#     false
nginx:
[root@docker lnmp]# grep -Ev "^$|#" /opt/conf/nginx/nginx.conf
worker_processes  1;
user nginx nginx;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    include    conf.d/*.conf;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
}

[root@docker opt]# grep -Ev '^$|#' conf/nginx/conf.d/default.conf 
server {
        listen       80;
        server_name  localhost;
        location / {
            root   /usr/share/nginx/html/wordpress;
            index  index.php index.html;
        }
        location ~\.php$ {
            root /usr/share/nginx/html/;
            fastcgi_pass php-fpm01:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        include fastcgi_params;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
}
php-fpm:
[root@docker opt]# grep -Ev '^$|;' conf/php-fpm/php-fpm.d/www.conf
[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000
listen.owner = nginx
listen.group = nginx
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
security.limit_extensions = .php .php3 .php4 .php5 .php7
目录结构

宿主机:
wordpress:/opt/html/wordpress html文件夹权限777

报错详情:

nginx:

2021/03/09 13:17:34 [error] 28#28: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.0.117, server: localhost, request: "GET /wp-admin/install.php HTTP/1.1", upstream: "fastcgi://172.22.0.3:9000", host: "192.168.0.179:8080"

php-fpm:

172.22.0.4 -  09/Mar/2021:13:13:56 +0000 "GET /index.php" 404
172.22.0.4 -  09/Mar/2021:13:17:34 +0000 "GET /wp-admin/install.php" 404

访问http://宿主机IP:8080/wp-admin/install.php,网页报错File not found.
访问http://宿主机IP:8080/index.html,正常显示提前准备的html

基本上能百度/Google到的方法都试了,每次都是这个报错,但是访问html文件没问题

求助大牛!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不必了 2022-09-19 22:57:25

docker-compose 里挂载卷的时候,nginx里

    - /opt/html/:/usr/share/nginx/html

php里

    - /opt/html/:/var/www/html

猜测是不是同一个目录挂载了两次,你可以把nginx的这一行去掉再试试

夜雨飘雪 2022-09-19 22:57:25

这一段里面的 目录是不是写错了?和 root指令不一致。

 location ~.php$ {
        root `/usr/share/nginx/html/`;
        fastcgi_pass php-fpm01:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        include fastcgi_params;
故事灯 2022-09-19 22:57:25

先去 php-fpm01 容器中看目录是否映射到里面

心房的律动 2022-09-19 22:57:25

问题已经解决了,两个点:
1.nginx里配置php的那一段配置

fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;

要改为

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

2.php配置文件www.conf中

listen = 127.0.0.1:9000

改为

listen = 9000

我已经将其详细搭建过程写成了一篇博客,想详细了解的朋友可以去看看:
https://blog.csdn.net/milkbro...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文