在 docker-compose、nginx、mysql 和 php 上运行 wordpress

Posted

技术标签:

【中文标题】在 docker-compose、nginx、mysql 和 php 上运行 wordpress【英文标题】:Running wordpress on docker-compose,nginx, mysql and php 【发布时间】:2022-01-24 00:32:27 【问题描述】:

我需要在 docker-compose、nginxphpmysql 上运行 wordpress。 运行它后,我看到的只是“欢迎使用 nginx”,但看不到我的网站。

我不确定我是否必须在我的操作系统上进行外部配置(我在 Ubuntu 20.04 上)。

我看到 docker 容器,没有错误,但没有网站。我不知道为什么。

这是我的配置: docker-compose.yml

version: "3"

services:
  nginx:
    image: nginx
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - ./src:/src
      - ./site.conf:/etc/nginx/conf.d/site.conf
    links:
      - php
  db:
    image: mysql
    container_name: mysql
    ports:
      - 3306:3306
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: toor
    volumes:
      - db-data:/var/lib/mysql
  php:
    build:
      context: .
      dockerfile: ./php/Dockerfile
    image: php:7-fpm
    container_name: php
    volumes:
      - ./src:/src

volumes:
  db-data:

这是我的 dockerfile

# syntax=docker/dockerfile:1
FROM php:7-fpm
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

这是我的 site.conf

server 
    index index.php index.html;
    server_name engine.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /src;
    location / 
        # This is cool because no php is touched for static content.
        # include the "?$args" part so non-default permalinks doesn't break when using query string
        try_files $uri $uri/ /index.php?$args;
    
    location ~ \.php$ 
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    

    #stolen from the nginx.conf
    # BEGIN W3TC Minify cache
    location ~ /wp-content/cache/minify/.*js_gzip$ 
        gzip off;
        types 
        default_type application/x-javascript;
        add_header Content-Encoding gzip;
        expires 31536000s;
        etag on;
        if_modified_since exact;
        add_header Referrer-Policy "no-referrer-when-downgrade";
        add_header Vary "Accept-Encoding";
    
    location ~ /wp-content/cache/minify/.*css_gzip$ 
        gzip off;
        types 
        default_type text/css;
        add_header Content-Encoding gzip;
        expires 31536000s;
        etag on;
        if_modified_since exact;
        add_header Referrer-Policy "no-referrer-when-downgrade";
        add_header Vary "Accept-Encoding";
    
    # END W3TC Minify cache
    # BEGIN W3TC Page Cache cache
    location ~ /wp-content/cache/page_enhanced.*gzip$ 
        gzip off;
        types 
        default_type text/html;
        add_header Content-Encoding gzip;
        etag on;
        if_modified_since exact;
        add_header Referrer-Policy "no-referrer-when-downgrade";
    
    # END W3TC Page Cache cache
    # BEGIN W3TC Browser Cache
    gzip on;
    gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon application/json application/vnd.ms-access video/webm application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint image/svg+xml application/x-shockwave-flash image/tiff application/x-font-ttf audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel;
    location ~ \.(css|htc|less|js|js2|js3|js4)$ 
        expires 31536000s;
        etag on;
        if_modified_since exact;
        try_files $uri $uri/ /index.php?$args;
    
    location ~ \.(html|htm|rtf|rtx|txt|xsd|xsl|xml)$ 
        etag on;
        if_modified_since exact;
        try_files $uri $uri/ /index.php?$args;
    
    location ~ \.(asf|asx|wax|wmv|wmx|avi|avif|avifs|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|webp|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|webm|mpp|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|_ttf|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ 
        expires 31536000s;
        etag on;
        if_modified_since exact;
        try_files $uri $uri/ /index.php?$args;
    
    add_header Referrer-Policy "no-referrer-when-downgrade";
    # END W3TC Browser Cache
    # BEGIN W3TC Minify core
    set $w3tc_enc "";
    if ($http_accept_encoding ~ gzip) 
        set $w3tc_enc _gzip;
    
    if (-f $request_filename$w3tc_enc) 
        rewrite (.*) $1$w3tc_enc break;
    
    rewrite ^/wp-content/cache/minify/ /index.php last;
    # END W3TC Minify core
    # BEGIN W3TC Page Cache core
    set $w3tc_rewrite 1;
    if ($request_method = POST) 
        set $w3tc_rewrite 0;
    
    if ($query_string != "") 
        set $w3tc_rewrite 0;
    
    if ($request_uri !~ \/$) 
        set $w3tc_rewrite 0;
    
    if ($http_cookie ~* "(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)") 
        set $w3tc_rewrite 0;
    
    set $w3tc_preview "";
    if ($http_cookie ~* "(w3tc_preview)") 
        set $w3tc_preview _preview;
    
    set $w3tc_ssl "";
    if ($scheme = https) 
        set $w3tc_ssl _ssl;
    
    if ($http_x_forwarded_proto = 'https') 
        set $w3tc_ssl _ssl;
    
    set $w3tc_enc "";
    if ($http_accept_encoding ~ gzip) 
        set $w3tc_enc _gzip;
    
    if (!-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_ssl$w3tc_preview.html$w3tc_enc") 
      set $w3tc_rewrite 0;
    
    if ($w3tc_rewrite = 1) 
        rewrite .* "/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_ssl$w3tc_preview.html$w3tc_enc" last;
    
    # END W3TC Page Cache core

    #end stolen

【问题讨论】:

【参考方案1】:

您没有指定尝试访问网站的方式,但您的错误清楚地表明您被路由到默认虚拟主机,而不是您配置的虚拟主机。

有两种解决方案:

    您可以添加engine.local,因为它在您的site.conf 中配置到您的/etc/hosts,例如这样:

    echo '127.0.0.1 engine.local' | sudo tee -a /etc/hosts
    

    在这种情况下,您将能够在浏览器中访问http://engine.local。

    您可以在site.conf 中将engine.local 更改为localhost。请记住重新启动您的 nginx 容器以使更改可见。然后,您就可以从浏览器通过 http://localhost 访问该站点。

    可以将nginx配置文件site.conf映射到默认的nginx虚拟主机配置,在docker-compose.yml改这一行:

      - ./site.conf:/etc/nginx/conf.d/default.conf
    

    然后像这样更改server_name

       server_name _;
    

    这是一个包罗万象的虚拟主机,应该响应您在浏览器中键入的任何名称。

【讨论】:

它仍然无法正常工作。不知道为什么。我安装了 docker,docker-compose。你可以看到下载的文件。尝试了engine.local并将其添加为localhost。我仍然得到相同的默认页面。我不得不将 SQL 端口更改为 3307:3306,因为 3006 已经在使用中。 1.您在浏览器中输入了什么 - localhost 或 engine.local? 2. 你能显示docker ps 的输出吗?让我们仔细检查一下你的nginx 容器是否已经启动。 3. 如果一切都失败了 - 你可以检查选项 3 - 我将它添加到我的答案中。 第三个选项有效。非常感谢。

以上是关于在 docker-compose、nginx、mysql 和 php 上运行 wordpress的主要内容,如果未能解决你的问题,请参考以下文章

docker-compose 服务的集中配置

docker-compose快速启动nginx

Nginx 从 docker-compose 运行返回“在上游找不到主机”

docker-compos

在 docker-compose、nginx、mysql 和 php 上运行 wordpress

Docker Compose 一键部署Nginx代理Tomcat集群