An example of building a host Nginx + Docker WordPress Mysql
This article mainly introduces the host Nginx + Docker WordPress Mysql building example, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.
Environment
Linux 3.10.0-693.el7.x86_64
Docker version 18.09.0
Docker-compose version 1.17.0
Installation
Dependencies required to install docker
$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.ymlversion: '3'services: db: image: mysql:5.7 volumes: # data volume, map local folder-db_data:/var/lib/mysql ports: # if port mapping is not written The mysql cannot be connected externally (no problem with the lower WordPress connection)-"3306 MYSQL_ROOT_PASSWORD 3306" restart: always environment: MYSQL_ROOT_PASSWORD: xxx MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: # depends on the top db service-db image: wordpress:latest volumes:-wp_site:/var/www/html ports:-"8000 db image 80" restart: always privileged : true environment: # you can directly use the above service name + port to obtain the link WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress # define the domain name WORDPRESS_CONFIG_EXTRA: "define ('WP_HOME' 'https://blog.amoyiki.com'); Define ('WP_SITEURL',' https://blog.amoyiki.com');"volumes: db_data: wp_site:
Run the container
# start $sudo docker-compose up-d # close $sudo docker-compose down
It can now be accessed 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 33060/tcpblog_wordpress_1 docker-entrypoint.sh apach 3306-> 3306/tcp, 33060/tcpblog_wordpress_1 docker-entrypoint.sh apach... Up 0.0.0.0 WordPress 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 $service-f-- tail=10 db
Using host Nginx to reverse docker wordpress
The configuration file for nginx is as follows (omitting irrelevant content)
User root; # this is because the user of the WordPress file in the docker container is www-dataserver {listen 80; server_name * .amoyiki.com; return 301 https://$host$request_uri;} upstream wordpress-workhorse {server 127.0.0.1 fail_timeout=10s; 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; # host WordPress data volume location 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.htl; location = / 50x.html {root html;}}
Erroneous 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 uses the default nobody that cannot read the www-data user group data of the wp in docker, so I directly refer the nginx user to root, which can be optimized later.
Thank you for reading this article carefully. I hope the article "examples of building host Nginx + Docker WordPress Mysql" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!