Detailed steps for setting up host Nginx + Docker WordPress Mysql

Detailed steps for setting up host Nginx + Docker WordPress Mysql

environment

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

Install

Install Docker dependencies

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

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

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 volume, mapping local folder - db_data:/var/lib/mysql
  ports: # If you don't write port mapping, the outside world cannot connect to the mysql (WordPress connection below is fine)
  - "3306:3306"                       
  restart: always
  environment:
  MYSQL_ROOT_PASSWORD: xxx
  MYSQL_DATABASE: wordpress
  MYSQL_USER: wordpress
  MYSQL_PASSWORD: wordpress

 wordpress:
  depends_on: #Depends 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 the container

# Start $ sudo docker-compose up -d
# Shutdown$ sudo docker-compose down

Now you can access it using http://ip:port. If you find that you cannot access it, you can check the container log to find 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 WordPress at the end is the name of the service. This is different from the container name used to view logs in Docker. $ sudo docker-compose logs -f --tail=10 wordpress 
$ sudo docker-compose logs -f --tail=10 db

Use host Nginx to reverse docker wordpress

The configuration file of nginx is as follows (irrelevant content 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;
 # WordPress data volume location 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 Collection

nginx error Permission denied

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

Previously, my nginx.conf user used the default nobody, which could not read the user group data of www-data in wp in docker, so I directly promoted the nginx user to root, which can be optimized later.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Six steps to teach you how to build a personal blog based on WordPress from scratch
  • Detailed explanation of the whole process of building a personal blog with nginx+WordPress
  • Detailed explanation of WordPress multi-site configuration under Nginx environment
  • Example of how to set WordPress pseudo-static in Nginx
  • How to configure wordpress with nginx
  • Detailed graphic tutorial on how to solve the slow access problem of WordPress website

<<:  How to use type enhancement without typingscript

>>:  mysql 5.7.11 winx64 initial password change

Recommend

A brief discussion on the solution to excessive data in ElementUI el-select

Table of contents 1. Scenario Description 2. Solu...

SQL Practice Exercise: Online Mall Database Product Category Data Operation

Online shopping mall database-product category da...

Method for comparing the size of varchar type numbers in MySQL database

Create a test table -- --------------------------...

Provides helpful suggestions for improving website design

<br />Scientifically Design Your Website: 23...

Detailed explanation of the use cases of Vue listeners

The first one is to use jQuery's ajax to send...

Detailed explanation of JS ES6 coding standards

Table of contents 1. Block scope 1.1. let replace...

The practical process of login status management in the vuex project

Table of contents tool: Login scenario: practice:...

How to rename the table in MySQL and what to pay attention to

Table of contents 1. Rename table method 2. Notes...

Solve the black screen problem after VMware installs Linux system and starts

1. Installation environment 1. HUAWEI mate x cpu ...

Use of filter() array filter in JS

Table of contents 1. Introduction 2. Introduction...

How to encapsulate timer components in Vue3

background When you open the product details on s...

Use render function to encapsulate highly scalable components

need: In background management, there are often d...

Detailed process of installing and deploying onlyoffice in docker

0. System requirements CPU I5-10400F or above Mem...