Blog.083 Docker 部署 LNMP + Wordpress
阿新 • • 發佈:2021-10-18
本章目錄
1. 部署步驟與流程
1.1 關閉防火牆
1.2 安裝 Docker
1.3 Docker 部署 Nginx
1.4 Docker 部署 MySQL
1.5 Docker 部署 PHP
1.6 測試訪問網頁
2. 備份恢復
2.1 操作步驟
- 環境準備:
CentOS 7:核心最好在 4 核以上;記憶體在 4G 以上;硬碟 60G。
否則很容易在建立映象的過程中出錯,提示記憶體不足。
1 systemctl stop firewalld 2 systemctl disable firewalld3 setenforce 0
詳見之前的部落格:Blog.079 Docker 安裝、基本管理與網路模式
(容器IP 為 172.18.0.10)
1 docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork #自定義一個網絡卡,網絡卡名為 docker1 ,網路模式 mynetwork, ip地址池172.18.0.0
可以用 ifconfig docker1 檢視是否建立成功。
1 mkdir /opt/nginx 2 cd /opt/nginx/ 3 4 ##上傳 nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz 到 /opt/nginx/ 目錄中 5 mkdir /opt/nginx/html 6 tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
1 ##在 /opt/nginx 目錄下解壓軟體包 2 3 tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
- 在 /opt/nginx 目錄下建立
1 vim Dockerfile 2 3 FROM centos:7 4 MAINTAINER this is nginx image <abc> 5 RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make 6 RUN useradd -M -s /sbin/nologin nginx 7 ADD nginx-1.12.0.tar.gz /usr/local/src/ 8 WORKDIR /usr/local/src/nginx-1.12.0 9 RUN ./configure \ 10 --prefix=/usr/local/nginx \ 11 --user=nginx \ 12 --group=nginx \ 13 --with-http_stub_status_module && make && make install 14 ENV PATH /usr/local/nginx/sbin:$PATH 15 ADD nginx.conf /usr/local/nginx/conf/ 16 ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/ 17 RUN chmod 777 -R /usr/local/nginx/html/ 18 EXPOSE 80 19 EXPOSE 443 20 ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
- 在 /opt/nginx 目錄下,傳入其他已經裝好的 nginx.conf 檔案,並修改如下部分:
1 worker_processes 1; 2 events { 3 worker_connections 1024; 4 } 5 http { 6 include mime.types; 7 default_type application/octet-stream; 8 sendfile on; 9 keepalive_timeout 65; 10 server { 11 listen 80; 12 server_name localhost; 13 14 ##配置網頁的字符集編碼為utf8 15 16 charset utf8; 17 location / { 18 root html; 19 20 ##修改,配合首頁檔案有 index.php 21 22 index index.html index.php; 23 } 24 error_page 500 502 503 504 /50x.html; 25 location = /50x.html { 26 root html; 27 } 28 29 ##配置nginx支援php 30 31 location ~ \.php$ { 32 root html; 33 ##fastcgi_pass 配置php所在容器的ip以及埠 34 35 fastcgi_pass 172.18.0.30:9000; 36 fastcgi_index index.php; 37 38 ##配置傳遞指令碼檔案請求路徑。 39 $fastcgi_script_name 前配置的是nginx的網頁檔案。 40 41 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; 42 include fastcgi_params; 43 } 44 } 45 }
- 構建映象,success 成功後再建立容器
1 docker build -t nginx:lnmp . 2 3 docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.18.0.10 nginx:lnmp
(容器IP 為 172.18.0.20)
mkdir /opt/mysqld cd /opt/mysqld 上傳 mysql-boost-5.7.20.tar.gz 到 /opt/mysqld 目錄中
- 在 /opt/mysqld 目錄下
1 vim Dockerfile 2 3 FROM centos:7 4 MAINTAINER this is mysql image <lv> 5 RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make 6 RUN useradd -M -s /sbin/nologin mysql 7 ADD mysql-boost-5.7.20.tar.gz /usr/local/src/ 8 WORKDIR /usr/local/src/mysql-5.7.20/ 9 RUN cmake \ 10 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ 11 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ 12 -DSYSCONFDIR=/etc \ 13 -DSYSTEMD_PID_DIR=/usr/local/mysql \ 14 -DDEFAULT_CHARSET=utf8 \ 15 -DDEFAULT_COLLATION=utf8_general_ci \ 16 -DWITH_INNOBASE_STORAGE_ENGINE=1 \ 17 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ 18 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ 19 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ 20 -DMYSQL_DATADIR=/usr/local/mysql/data \ 21 -DWITH_BOOST=boost \ 22 -DWITH_SYSTEMD=1 && make -j4 && make install 23 RUN chown -R mysql:mysql /usr/local/mysql/ 24 ADD my.cnf /etc/my.cnf 25 RUN chown mysql:mysql /etc/my.cnf 26 ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH 27 WORKDIR /usr/local/mysql/ 28 RUN bin/mysqld \ 29 --initialize-insecure \ 30 --user=mysql \ 31 --basedir=/usr/local/mysql \ 32 --datadir=/usr/local/mysql/data 33 RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ 34 EXPOSE 3306 35 RUN systemctl enable mysqld 36 VOLUME [ "/usr/local/mysql" ] 37 CMD /usr/sbin/init
- 在 /opt/mysqld 目錄下
1 vim my.cnf 2 [client] 3 port = 3306 4 socket = /usr/local/mysql/mysql.sock 5 6 [mysql] 7 port = 3306 8 socket = /usr/local/mysql/mysql.sock 9 10 [mysqld] 11 user = mysql 12 basedir = /usr/local/mysql 13 datadir = /usr/local/mysql/data 14 port = 3306 15 character_set_server=utf8 16 pid-file = /usr/local/mysql/mysqld.pid 17 socket = /usr/local/mysql/mysql.sock 18 server-id = 1 19 20 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
- 在 /opt/mysqld 目錄下
1 vim run.sh 2 #!/bin/bash 3 /usr/local/mysql/bin/mysqld 4 systemctl enable mysqld
- 構建映象,完成後建立容器
1 docker build -t mysql:lnmp . 2 3 docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp /usr/sbin/init
(容器IP 為 172.18.0.30)
1 mkdir /opt/php 2 cd /opt/php 3 4 上傳 php-7.1.10.tar.bz2 到 /opt/php 目錄中
- 在 /opt/php 目錄下,修改好的 php.ini、php-fpm.conf 和 www.conf 也傳入到此目錄下
1 vim Dockerfile 2 3 FROM centos:7 4 MAINTAINER this is php image <wl> 5 RUN yum -y install gd \ 6 libjpeg libjpeg-devel \ 7 libpng libpng-devel \ 8 freetype freetype-devel \ 9 libxml2 libxml2-devel \ 10 zlib zlib-devel \ 11 curl curl-devel \ 12 openssl openssl-devel \ 13 gcc gcc-c++ make pcre-devel 14 RUN useradd -M -s /sbin/nologin nginx 15 ADD php-7.1.10.tar.bz2 /usr/local/src/ 16 WORKDIR /usr/local/src/php-7.1.10 17 RUN ./configure \ 18 --prefix=/usr/local/php \ 19 --with-mysql-sock=/usr/local/mysql/mysql.sock \ 20 --with-mysqli \ 21 --with-zlib \ 22 --with-curl \ 23 --with-gd \ 24 --with-jpeg-dir \ 25 --with-png-dir \ 26 --with-freetype-dir \ 27 --with-openssl \ 28 --enable-fpm \ 29 --enable-mbstring \ 30 --enable-xml \ 31 --enable-session \ 32 --enable-ftp \ 33 --enable-pdo \ 34 --enable-tokenizer \ 35 --enable-zip && make && make install 36 ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH 37 ADD php.ini /usr/local/php/lib/ 38 ADD php-fpm.conf /usr/local/php/etc/ 39 ADD www.conf /usr/local/php/etc/php-fpm.d/ 40 EXPOSE 9000 41 ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
- 構建映象,完成後建立容器
1 docker build -t php:lnmp . 2 docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp
1 ##啟動容器,需要從nginx容器和mysql容器中獲取資料 2 3 docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp 4 5 ##進入容器,進行使用者授權 6 7 docker exec -it mysql /bin/bash 8 mysql -u root -p mysql 9 密碼直接回車 10 11 ##進入 MySQL 12 13 mysql> create database wordpress; 14 Query OK, 1 row affected (0.00 sec) 15 16 mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456'; 17 Query OK, 0 rows affected, 1 warning (0.00 sec) 18 19 mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123'; 20 Query OK, 0 rows affected, 1 warning (0.00 sec) 21 22 mysql> flush privileges; 23 Query OK, 0 rows affected (0.01 sec)
1 ##使用主機地址測試 2 3 firefox http://192.168.23.103/wordpress/index.php
- 將所有容器快照
- 將映象儲存成 tar 包
- 刪除容器和映象
- 恢復
-