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

CSS3 realizes the animation effect of lotus blooming

Let’s look at the effect first: This effect looks...

Vue-pdf implements online preview of PDF files

Preface In most projects, you will encounter onli...

Detailed installation process of Jenkins on Linux

Table of contents 1. Install JDK 2. Install Jenki...

Detailed explanation of Apache SkyWalking alarm configuration guide

Apache SkyWalking Apache SkyWalking is an applica...

How to point the target link of a tag to iframe

Copy code The code is as follows: <iframe id=&...

Docker builds CMS on-demand system with player function

Table of contents text 1. Prepare the machine 2. ...

The perfect solution for MySql version problem sql_mode=only_full_group_by

1. Check sql_mode select @@sql_mode The queried v...

Can CSS be used like this? The art of whimsical gradients

In the previous article - The charm of one line o...

MySQL msi installation tutorial under windows10 with pictures and text

1. Download 1. Click the latest download from the...

JS implements Baidu search box

This article example shares the specific code of ...

Detailed explanation of mysql exists and not exists examples

Detailed explanation of mysql exists and not exis...

Before making a web page, let’s take a look at these so-called specifications

This article has compiled some so-called specific...

Example analysis of the page splitting principle of MySQL clustered index

This article uses an example to illustrate the pa...

Docker deploys net5 program to achieve cross-platform functions

Deployment environment: docker container, liunx s...