Home > Enterprise >  Running wordpress on docker-compose,nginx, mysql and php
Running wordpress on docker-compose,nginx, mysql and php

Time:12-23

I need to run wordpress on docker-compose, nginx, php and mysql. After I run it all I see is "Welcome to nginx" and I don't see my site.

I'm not sure if I have to configure something outside on my OS (I'm on Ubuntu 20.04).

I see the docker containers, there is no error and yet there is no website. I have no idea why.

This is my config: 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:

This is my dockerfile

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

This is my 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
}

CodePudding user response:

You didn't specify how you are trying to reach your site, but your error clearly shows that you're being routed to the default virtual host, instead of the one you configured.

There are 2 solutions to this:

  1. You can add engine.local, as it is configured in your site.conf to your /etc/hosts, for example like this:

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

    In this case you will be able to access http://engine.local in your browser.

  2. You can change engine.local to localhost in site.conf. Please remember to restart your nginx container for the change to become visible. You will then be able to access the site through http://localhost from your browser.

  3. You can map the nginx configuration file site.conf to the default nginx virtualhost config, change this line in docker-compose.yml:

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

    and then change the server_name like this:

       server_name _;
    

    This is a catch-all virtual host and should respond to any name you type in the browser.

  • Related