Build the host Nginx + Docker WordPress Mysql

Keywords: Linux Docker sudo Nginx SSL

Environmental Science

  • Linux 3.10.0-693.el7.x86_64
  • Docker version 18.09.0
  • docker-compose version 1.17.0

install

  1. Required dependencies for installing docker

    $ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
  2. Install docker CE

      $ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
      $ sudo yum install docker-ce
      $ sudo systemctl start docker
      $ docker --version
      $ sudo docker run hello-world
  3. Install docker compose

    $ sudo curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/bin/docker-compose
    $ sudo chmod +x /usr/bin/docker-compose
    $ docker-compose --version

Write docker-compose.yml

$ mkdir ./blog
$ cd blog
$ sudo vim docker-compose.yml
version: '3'

services:
   db:
     image: mysql:5.7
     volumes: # Data volumes, mapping local folders
       - db_data:/var/lib/mysql
     ports: # If the port mapping is not written, the external cannot connect to the mysql (WordPress connection below is OK)
       - "3306:3306"                                                                                          
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: xxx
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on: # Depend on the db service above
       - db
     image: wordpress:latest
     volumes:
        - wp_site:/var/www/html
     ports:
        - "8000:80"
     restart: always
     privileged: true
     environment:
       # You can directly use the above service name + port to get the link 
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       # Define domain name
       WORDPRESS_CONFIG_EXTRA: "define('WP_HOME','https://blog.amoyiki.com'); define('WP_SITEURL','https://blog.amoyiki.com');"

volumes:
    db_data:
    wp_site:

Running container

# start-up
$ sudo docker-compose up -d
# Close 
$ sudo docker-compose down

You can now access it using http://ip:port. If you find that you cannot access it, you can check the container log for errors

$ sudo docker-compose ps
 Name              Command                       State                               Ports              

blog_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp
blog_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8000->80/tcp   
# The main WordPress is the name of the service, which is different from the container name used by docker to view logs
$ sudo docker-compose logs -f --tail=10 wordpress  
$ sudo docker-compose logs -f --tail=10 db

Using the host Nginx to reverse the generation of docker wordpress

The configuration file of nginx is as follows (irrelevant content is omitted)

user root;  # This is because the WordPress file user in the docker container is www data

server {
    listen 80;
    server_name *.amoyiki.com;
    return 301 https://$host$request_uri;                                                              
}
upstream wordpress-workhorse {
    server 127.0.0.1:8000 fail_timeout=10s;
}
server {  
    server_name blog.amoyiki.com;
    listen 443 ssl;
    ssl on;
    ssl_certificate /ssl/path/fullchain.pem;
    ssl_certificate_key /ssl/path/privkey.pem;
    access_log /var/log/nginx/access.log main;
   # Location of WordPress data volume in the host
    root /var/lib/docker/volumes/blog_wp_site/_data;


    location / {
        proxy_pass http://wordpress-workhorse;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
    
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 7d;
        access_log off;                                                    
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
} 

Error set

nginx error Permission denied

"/var/lib/docker/volumes/myblog_wp_site/_data/wp-includes/css/dashicons.min.css" failed (13: Permission denied)

Previously, I used the default nobody to read the WWW data user group data of wp in docker, so I mentioned the nginx user to root directly,
This can be optimized later

Reference reference

The difference between docker links and depends on
[nginx installation] ()
[generate SSL certificate using letsencrypt] ()

Posted by thenewguy on Mon, 09 Dec 2019 06:21:46 -0800