{"id":52,"date":"2020-02-26T15:44:20","date_gmt":"2020-02-26T07:44:20","guid":{"rendered":"https:\/\/www.346pro.club\/?p=52"},"modified":"2020-02-26T15:56:10","modified_gmt":"2020-02-26T07:56:10","slug":"%e8%ae%b0%e8%bf%b0%e4%bd%bf%e7%94%a8docker%e5%ae%89%e8%a3%85wordpress%e7%9a%84%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.346pro.club\/?p=52","title":{"rendered":"\u8bb0\u8ff0\u4f7f\u7528Docker\u5b89\u88c5wordpress\u7684\u8bb0\u5f55"},"content":{"rendered":"<p><em>\u5199\u7ed9\u81ea\u5df1\u770b\u7684\u5907\u4efd\u6587\u6863<\/em><br \/>\n\u4f7f\u7528Docker\u7684\u597d\u5904\u5f88\u591a\uff0c\u5173\u952e\u70b9\u5728\u4e8e\u65b9\u4fbf\u8fc1\u79fb\uff0c\u4e0d\u5f71\u54cd\u4e3b\u673a\u7b49\u7b49\u3002\u672cblog\u5c31\u4f7f\u7528Docker\u5b89\u88c5\uff0c\u73b0\u5c06\u8fc7\u7a0b\u8bb0\u5f55\u5982\u4e0b\u3002<br \/>\n\u9996\u5148\uff0c\u4f60\u9700\u8981\u6709\u4e00\u53f0\u4e3b\u673a\uff0c\u8fd9\u662f\u5e9f\u8bdd\u3002<br \/>\n\u5b89\u88c5docker-ce\u4e0edocker-compose\uff0c\u63a8\u8350\u6309\u7167\u5b98\u7f51\u7684\u5b98\u65b9\u6b65\u9aa4\u5b89\u88c5\uff0c\u5728\u8fd9\u91cc\u4e0d\u591a\u8bf4\u3002<br \/>\n\u57df\u540d\u4e0eDNS\u89e3\u6790\uff0c\u8fd9\u4e2a\u5e94\u8be5\u4e5f\u4e0d\u9700\u8981\u6211\u6765\u63d0\u3002<\/p>\n<h4>\u7b2c\u4e00\u3001\u4f60\u9700\u8981\u5efa\u7acb\u76ee\u5f55\u6765\u653e\u7f6enginx\u914d\u7f6e\u6587\u4ef6<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">mkdir wordpress &amp;&amp; cd wordpress\nmkdir nginx-conf\nvim nginx-conf\/nginx.conf<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-title=\"~\/wordpress\/nginx-conf\/nginx.conf\">server {\n        listen 80;\n        listen [::]:80;\n\n        server_name 346pro.club www.346pro.club;\n\n        index index.php index.html index.htm;\n\n        root \/var\/www\/html;\n\n        location ~ \/.well-known\/acme-challenge {\n                allow all;\n                root \/var\/www\/html;\n        }\n\n        location \/ {\n                try_files $uri $uri\/ \/index.php$is_args$args;\n        }\n\n        location ~ \\.php$ {\n                try_files $uri =404;\n                fastcgi_split_path_info ^(.+\\.php)(\/.+)$;\n                fastcgi_pass wordpress:9000;\n                fastcgi_index index.php;\n                include fastcgi_params;\n                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n                fastcgi_param PATH_INFO $fastcgi_path_info;\n        }\n\n        location ~ \/\\.ht {\n                deny all;\n        }\n\n        location = \/favicon.ico { \n                log_not_found off; access_log off; \n        }\n        location = \/robots.txt { \n                log_not_found off; access_log off; allow all; \n        }\n        location ~* \\.(css|gif|ico|jpeg|jpg|js|png)$ {\n                expires max;\n                log_not_found off;\n        }\n}<\/pre>\n<p>\u8bf7\u5c06server_name\u7684\u57df\u540d\u66f4\u6362\u4e3a\u81ea\u5df1\u7684\u57df\u540d<\/p>\n<p>\u6211\u6682\u65f6\u4e0d\u5bf9\u914d\u7f6e\u6587\u4ef6\u505a\u8be6\u7ec6\u7684\u5206\u6790\uff0c\u5982\u6709\u5174\u8da3\u53ef\u4ee5\u81ea\u5df1\u770bnginx\u6587\u6863<\/p>\n<h4>\u4e8c\u3001\u5efa\u7acb\u73af\u5883\uff0c\u8bbe\u7f6esql\u5bc6\u7801<\/h4>\n<p>\u5728\u4e4b\u524d\u7684\u76ee\u5f55\u4e2d\uff0c\u521b\u7acb.env\u73af\u5883\u6587\u4ef6<code class=\"language-shell\">vim .env<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">MYSQL_ROOT_PASSWORD=your_root_password\nMYSQL_USER=your_wordpress_database_user\nMYSQL_PASSWORD=your_wordpress_database_password<\/pre>\n<p>\u8bbe\u7f6e\u4f60\u81ea\u5df1\u60f3\u8bbe\u7f6e\u7684\u6570\u636e\u5e93\u5bc6\u7801<\/p>\n<p>\u518d\u5efa\u7acbdocker\u5ffd\u7565\u76ee\u5f55\uff0c\u8fd9\u6837\u4fdd\u8bc1\u5bc6\u7801\u6587\u4ef6\u4e0d\u4f1a\u51fa\u73b0\u5728docker\u91cc\u9762<\/p>\n<h4>\u4e09\u3001\u521b\u5efadocker-compose.yml\u6587\u4ef6<\/h4>\n<p>docker-compose.yml\u6587\u4ef6\u5305\u542b\u4e86\u4f7f\u7528docker\u7684\u955c\u50cf\u3001\u7248\u672c\u4ee5\u53ca\u8fd0\u884c\u65b9\u5f0f\uff0c\u5176\u4f5c\u4e3a\u672c\u6b21\u642d\u5efa\u7684\u6700\u4e3b\u8981\u90e8\u5206\uff0c\u521b\u5efadocker-compose.yml\u6587\u4ef6<code class=\"language-shell\">vim docker-compose.yml<\/code>\u6211\u4f7f\u7528\u4e86\u5982\u4e0b\u914d\u7f6e<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">version: '3.3'\n\nservices:\n  db:\n    image: mysql:8\n    container_name: db\n    restart: unless-stopped\n    env_file: .env\n    environment:\n      - MYSQL_DATABASE=wordpress\n    volumes: \n      - dbdata:\/var\/lib\/mysql\n    command: '--default-authentication-plugin=mysql_native_password'\n    networks:\n      - app-network\n\n  wordpress:\n    depends_on: \n      - db\n    image: wordpress:fpm-alpine\n    container_name: wordpress\n    restart: unless-stopped\n    env_file: .env\n    environment:\n      - WORDPRESS_DB_HOST=db:3306\n      - WORDPRESS_DB_USER=$MYSQL_USER\n      - WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD\n      - WORDPRESS_DB_NAME=wordpress\n    volumes:\n      - wordpress:\/var\/www\/html\n    networks:\n      - app-network\n\n  webserver:\n    depends_on:\n      - wordpress\n    image: nginx:alpine\n    container_name: webserver\n    restart: unless-stopped\n    ports:\n      - \"80:80\"\n    volumes:\n      - wordpress:\/var\/www\/html\n      - .\/nginx-conf:\/etc\/nginx\/conf.d\n      - certbot-etc:\/etc\/letsencrypt\n    networks:\n      - app-network\n\n  certbot:\n    depends_on:\n      - webserver\n    image: certbot\/certbot\n    container_name: certbot\n    volumes:\n      - certbot-etc:\/etc\/letsencrypt\n      - wordpress:\/var\/www\/html\n    command: certonly --webroot --webroot-path=\/var\/www\/html --email hoshinokun@346pro.club --agree-tos --no-eff-email --force-renewal -d 346pro.club -d www.346pro.club\n\nvolumes:\n  certbot-etc:\n  wordpress:\n  dbdata:\n\nnetworks:\n  app-network:\n    driver: bridge\n<\/pre>\n<p>\u5728\u8fd9\u4e00\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u89c4\u5b9a\u4e86docker\u955c\u50cf\u6240\u62c9\u53d6\u7684\u7248\u672c\u4ee5\u53ca\u7aef\u53e3\u53f7\u7684\u6620\u5c04\u65b9\u5f0f\uff0c\u5176\u7ed3\u6784\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206\uff1a<br \/>\ndb\uff1amysql\u6570\u636e\u5e93\u955c\u50cf<br \/>\nwordpress\uff1awordpress\u7248\u672c\u955c\u50cf<br \/>\nweb-server\uff1a\u7f51\u7ad9\u670d\u52a1\u5668nginx\u955c\u50cf<br \/>\ncertbot\uff1a\u81ea\u52a8\u8bc1\u4e66\u955c\u50cf<br \/>\n\u4f7f\u7528\u6865\u63a5\u7f51\u7edc\uff0c\u955c\u50cf\u5171\u4eab\u6570\u636e<\/p>\n<p>\u914d\u7f6e\u5b8c\u6210\u4ee5\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8fd0\u884c\u955c\u50cf\u4e86<\/p>\n<h4>\u56db\u3001\u8fd0\u884cdocker-compose<\/h4>\n<p>\u6211\u4eec\u4f7f\u7528<code>docker-compose up -d<\/code>\u6765\u8fd0\u884cdocker-compose\uff0c\u5728\u542f\u7528\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u955c\u50cf\u7684\u62c9\u53d6\u8fc7\u7a0b\u4e0e\u8fd0\u884c\u7ed3\u679c\uff0c\u4e4b\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>docker-compose ps<\/code>\u6765\u67e5\u770b\u8fd0\u884c\u7684\u7ed3\u679c\uff0c\u82e5\u7ed3\u679c\u4e0d\u4e3a<\/p>\n<pre><code>  Name                 Command               State           Ports       \n-------------------------------------------------------------------------\ncertbot     certbot certonly --webroot ...   Exit 0                      \ndb          docker-entrypoint.sh --def ...   Up       3306\/tcp, 33060\/tcp\nwebserver   nginx -g daemon off;             Up       0.0.0.0:80-&gt;80\/tcp \nwordpress   docker-entrypoint.sh php-fpm     Up       9000\/tcp           <\/code><\/pre>\n<p>\u5219\u9700\u8981\u4f7f\u7528<code>docker-compose logs service_name<\/code>\u547d\u4ee4\u6765\u67e5\u770b\u51fa\u95ee\u9898\u7684\u670d\u52a1\uff0c\u5e76\u4f7f\u7528<code>docker-compose up -d --force-recreate --no-deps service_name<\/code>\u91cd\u542f\u670d\u52a1<\/p>\n<h4>\u4e94\u3001\u914d\u7f6e\u8bc1\u4e66\u670d\u52a1<\/h4>\n<p>\u5982\u679c\u4e0a\u8ff0\u670d\u52a1\u542f\u52a8\u5b8c\u6210\uff0c\u6211\u4eec\u5219\u9700\u8981\u4fee\u6539nginx\u914d\u7f6e\u6587\u4ef6\u4f7f\u5176\u53ef\u4ee5\u8bbf\u95ee443\u7aef\u53e3\u670d\u52a1\uff0c\u5c06\u6587\u4ef6\u5185\u5bb9\u66ff\u6362\u4e3a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">server {\n        listen 80;\n        listen [::]:80;\n\n        server_name 346pro.club www.346pro.club;\n\n        location ~ \/.well-known\/acme-challenge {\n                allow all;\n                root \/var\/www\/html;\n        }\n\n        location \/ {\n                rewrite ^ https:\/\/$host$request_uri? permanent;\n        }\n}\n\nserver {\n        listen 443 ssl http2;\n        listen [::]:443 ssl http2;\n        server_name 346pro.club www.346pro.club;\n\n        index index.php index.html index.htm;\n\n        root \/var\/www\/html;\n\n        server_tokens off;\n\n        ssl_certificate \/etc\/letsencrypt\/live\/346pro.club\/fullchain.pem;\n        ssl_certificate_key \/etc\/letsencrypt\/live\/346pro.club\/privkey.pem;\n\n        include \/etc\/nginx\/conf.d\/options-ssl-nginx.conf;\n\n        add_header X-Frame-Options \"SAMEORIGIN\" always;\n        add_header X-XSS-Protection \"1; mode=block\" always;\n        add_header X-Content-Type-Options \"nosniff\" always;\n        add_header Referrer-Policy \"no-referrer-when-downgrade\" always;\n        add_header Content-Security-Policy \"default-src * data: 'unsafe-eval' 'unsafe-inline'\" always;\n        # add_header Strict-Transport-Security \"max-age=31536000; includeSubDomains; preload\" always;\n        # enable strict transport security only if you understand the implications\n\n        location \/ {\n                try_files $uri $uri\/ \/index.php$is_args$args;\n        }\n\n        location ~ \\.php$ {\n                try_files $uri =404;\n                fastcgi_split_path_info ^(.+\\.php)(\/.+)$;\n                fastcgi_pass wordpress:9000;\n                fastcgi_index index.php;\n                include fastcgi_params;\n                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n                fastcgi_param PATH_INFO $fastcgi_path_info;\n        }\n\n        location ~ \/\\.ht {\n                deny all;\n        }\n\n        location = \/favicon.ico { \n                log_not_found off; access_log off; \n        }\n        location = \/robots.txt { \n                log_not_found off; access_log off; allow all; \n        }\n        location ~* \\.(css|gif|ico|jpeg|jpg|js|png)$ {\n                expires max;\n                log_not_found off;\n        }\n}\n<\/pre>\n<p>\u5e76\u5728docker-compose.yml\u6587\u4ef6\u4e2d\u52a0\u5165443\u7aef\u53e3<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">webserver:\n  depends_on:\n    - wordpress\n  image: nginx:alpine\n  container_name: webserver\n  restart: unless-stopped\n  ports:\n    - \"80:80\"\n    - \"443:443\"\n  volumes:\n    - wordpress:\/var\/www\/html\n    - .\/nginx-conf:\/etc\/nginx\/conf.d\n    - certbot-etc:\/etc\/letsencrypt\n  networks:\n    - app-network<\/pre>\n<p>\u4e4b\u540e\u91cd\u542fwebserver\u670d\u52a1<code>docker-compose up -d --force-recreate --no-deps webserver<\/code>\u3002\u4e4b\u540e\u53ef\u4ee5\u7528<code>docker-compose ps<\/code>\u6765\u67e5\u770b\u8fd0\u884c\u7684\u7ed3\u679c\uff0c\u5e94\u8be5\u662f<\/p>\n<pre><code>  Name                 Command               State                     Ports \n----------------------------------------------------------------------------------------------\ncertbot     certbot certonly --webroot ...   Exit 0                          \ndb          docker-entrypoint.sh --def ...   Up       3306\/tcp, 33060\/tcp   \nwebserver   nginx -g daemon off;             Up       0.0.0.0:443-&gt;443\/tcp, 0.0.0.0:80-&gt;80\/tcp\nwordpress   docker-entrypoint.sh php-fpm     Up       9000\/tcp    <\/code><\/pre>\n<h4>\u516d\u3001\u914d\u7f6e\u8ba1\u5212\u4efb\u52a1\uff0c\u81ea\u52a8\u91cd\u7b7e\u8bc1\u4e66<\/h4>\n<p>\u521b\u5efa\u6587\u4ef6ssl_renew.sh<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">#!\/bin\/bash\n\nCOMPOSE=\"\/usr\/local\/bin\/docker-compose --no-ansi\"\nDOCKER=\"\/usr\/bin\/docker\"\n\ncd \/home\/sammy\/wordpress\/\n$COMPOSE run certbot renew --dry-run &amp;&amp; $COMPOSE kill -s SIGHUP webserver\n$DOCKER system prune -af<\/pre>\n<p>\u7ed9\u4e88\u5176\u6267\u884c\u6743\u9650<code>chmod +x ssl_renew.sh<\/code>\u5e76\u4f7f\u7528<code>sudo crontab -e <\/code>\u5c06\u5176\u52a0\u5165\u5230\u8ba1\u5212\u4efb\u52a1\u4e2d\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u81ea\u52a8\u6267\u884c\u8bc1\u4e66\u91cd\u7b7e\u7684\u5de5\u4f5c\u4e86\u3002<\/p>\n<h4>PS.<\/h4>\n<p>\u6211\u5728\u4f7f\u7528cf\u7684cdn\u670d\u52a1\u65f6\uff0c\u66fe\u7ecf\u9047\u5230\u4e86ssh\u8bc1\u4e66\u91cd\u5b9a\u5411\u8fc7\u591a\u7684\u95ee\u9898\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ea\u9700\u8981\u5c06cf\u7684\u8bc1\u4e66\u52a0\u5bc6\u9009\u9879\uff0c\u7531\u7075\u6d3b\u6539\u4e3afull\u5c31\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u4e86\uff0c\u8fd9\u5728cf\u7684\u6587\u6863\u4e2d\u4e5f\u6709\u8bb0\u8f7d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5199\u7ed9\u81ea\u5df1\u770b\u7684\u5907\u4efd\u6587\u6863 \u4f7f\u7528Docker\u7684\u597d\u5904\u5f88\u591a\uff0c&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/www.346pro.club\/?p=52\">Continue reading<span class=\"screen-reader-text\">\u8bb0\u8ff0\u4f7f\u7528Docker\u5b89\u88c5wordpress\u7684\u8bb0\u5f55<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-technology","entry"],"_links":{"self":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=52"}],"version-history":[{"count":0,"href":"https:\/\/www.346pro.club\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.346pro.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}