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

Web page custom selection box Select

Everyone may be familiar with the select drop-dow...

Two methods to implement MySQL group counting and range aggregation

The first one: normal operation SELECT SUM(ddd) A...

Common array operations in JavaScript

Table of contents 1. concat() 2. join() 3. push()...

WeChat applet implements user login module server construction

I chose node.js to build the server. Friends who ...

jQuery plugin to implement floating menu

Learn a jQuery plugin every day - floating menu, ...

Deployment and Chinese translation of the docker visualization tool Portainer

#docker search #docker pull portainer 1. Download...

Functions in TypeScript

Table of contents 1. Function definition 1.1 Func...

How to operate MySQL database with ORM model framework

What is ORM? ORM stands for Object Relational Map...

HTML Basics Must-Read - Comprehensive Understanding of CSS Style Sheets

CSS (Cascading Style Sheet) is used to beautify H...

Summary of xhtml block level tags

* address - address * blockquote - block quote * c...

What are the differences between xHTML and HTML tags?

All tags must be lowercase In XHTML, all tags must...

MySQL exposes Riddle vulnerability that can cause username and password leakage

The Riddle vulnerability targeting MySQL versions...

Vue implements countdown between specified dates

This article example shares the specific code of ...

Detailed explanation of Tomcat's Server Options

1. Configuration By default, the first two are no...