容器化部署wordpress個人部落格系統lnmp環境[自定義網路]
阿新 • • 發佈:2020-07-12
目錄
容器化部署個人部落格系統lnmp環境
#告警: WARNING: IPv4 forwarding is disabled. Networking will not work. 96c083a8b58112f64e84baf73810197ea8ba4b48fa292bfb9635b92e6efc4e5c 因為轉發沒開,解決如下: echo 1 >/proc/sys/net/ipv4/ip_forward sysctl -p 0. 配置源 yum install -y docker-ce rm -f /etc/docker/* sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://ajvcw8qn.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker systemctl enable docker.service 1. 自定義docker網路 network_lnmp [root@k8s-master ~]# docker network create network_lnmp 742504219a6955425beb43f5cc014376a1efea2ed5b78b939f7cb92cc8879c83 2. mysql配置 2.1. 建立掛載卷: docker volume create mysql-vol 2.2. 建立本地持久化卷 mkdir -p /var/lib/mysql/ 掛載方法: 方法1: --name=mysql-vol --mount src=mysql-vol,dst=/var/lib/mysql/ 方法2: --name=mysql-vol -v mysql-vol:/usr/share/nginx/html nginx 2.3 mysql容器建立與登入測試,並掛載到mysql-vol: 建立: mkdir -p /var/lib/mysql docker run -d --name lnmp_mysql \ --net network_lnmp \ --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 \ --character-set-server=utf8 登入測試: docker inspect lnmp_mysql|sed -nr 's#^.*Address": \"(.*)\"\,.*$#\1#gp'|egrep -o "([0-9]{1,3}\.){3}[0-9]" 172.18.0.2 <---獲取mysql容器IP 連線mysql測試: [root@docker-02 ~]# yum install -y mysql [root@docker-02 ~]# mysql -uroot -p123456 wordpress -h 172.18.0.2 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.30 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> 測試連結成功 --------------------------------------------------------------------- #建立授權wordpress資料庫: 建立相關資料庫: mysql -uroot -p123456 wordpress -h 172.18.0.2 #建立wordpress資料庫 create database wordpress; #wordpress資料庫許可權配置,只允許本地訪問 使用者名稱 wordpress 密碼 123456 grant all on wordpress.* to wordpress@'%' identified by '123456'; #重新整理許可權 flush privileges; #測試新建賬號 wordpress 連線資料庫: mysql -uwordpress -p123456 -h 172.18.0.2 [root@docker-02 _data]# mysql -uwordpress -p123456 -h 172.18.0.2 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.7.30 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> 3. 建立php容器: 規劃: 建立網站資料目錄 www-vol 啟動時指定解析該目錄 建立: docker run -d \ --name lnmp_php-1 \ --net network_lnmp \ -p 9000:9000 \ -v www-vol:/usr/share/nginx/html \ registry.cn-hangzhou.aliyuncs.com/iyunw/php:7.1 這裡是將本地/www目錄掛載到容器中的/usr/share/nginx/html 建立使用者: useradd -s /sbin/nologin -M www-data -u 1000 清理資料卷: docker volume ls --filter dangling=true 過濾出沒有使用的卷,然後使用"docker volume rm [卷名]"刪除 #--------------------------------------------------------------------------------------------------------------- 3. nginx容器 3.1 準備目錄和配置檔案: mkdir /etc/nginx/conf.d/ -p vim /etc/nginx/conf.d/nginx.conf #---nginx.conf-------------------------------------------------------------------------------------------------- user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; } #---nginx.conf-----------預設就有不用建立------------------------------------------------------------------------ 新增server: [root@docker-02 _data]# vim /root/nginx_php.conf #---------------include /root/nginx_php.conf-----------------------------# server { listen 80 default_server; listen [::]:80 default_server; server_name _; #root /usr/share/nginx/html; index index.php index.html; # include /etc/nginx/default.d/*.conf; location / { root /usr/share/nginx/html; } location ~* \.php$ { root /usr/share/nginx/html; fastcgi_pass lnmp_php-1:9000; ## php的容器名,如果容器名不存在就不會啟動容器。注意 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } #---------------include /root/nginx_php.conf-----------------------------# 5. 建立nginx容器掛載相關配置檔案和目錄: 網頁目錄卷: www-vol 5.1. 建立掛載卷: docker volume create www-vol 通過 docker inspect www-vol 可以看到這個卷所在的目錄 掛載方法: 方法2: -v www-vol:/usr/share/nginx/html -v /root/nginx_php.conf:/etc/nginx/conf.d/www.conf 5.2 起容器: docker run -d --name lnmp_nginx \ -p 80:80 --net network_lnmp \ -v www-vol:/usr/share/nginx/html nginx 5.3 容器裡建立一個使用者,用於和php互動: useradd -s /sbin/nologin -M www-data -u 1000 #注意,yum安裝的時候這個index.html可能就是個軟連線,她並不在容器中,而是在宿主機中,並且沒有被對映到容器 #此時訪問時會出現403錯誤 #只要刪除這些檔案。重新建立檔案即可 #如:echo hello >/usr/share/nginx/html/index.html [容器中的目錄] #如:echo hello >>/var/lib/docker/volumes/www-vol/_data/index.html [映射出來的目錄] ------------------------------------------------------------------------------------------------------
PHP解析測試:
echo '<?php phpinfo(); ?>' >/var/lib/docker/volumes/www-vol/_data/index.php
php-fpm.conf下載地址:
https://www.chenleilei.net/soft/php-fpm.conf
wordpress安裝配置:
下載安裝包: wget https://cn.wordpress.org/latest-zh_CN.zip unzip latest-zh_CN.zip mv wordpress/* ./ rm -fr wordpress 資料庫名:wordpress 使用者名稱: wordpress 密碼: 123456 網站目錄許可權設定: useradd -u 1000 www-data -M -s /sbin/nologin chown -R 755 /var/lib/docker/volumes/www-vol/_data chown -R www-data /var/lib/docker/volumes/www-vol/_data 1. 進入網頁資料目錄(docker inspect www-vol): cd /var/lib/docker/volumes/www-vol/_data echo '<?php phpinfo(); ?>' >./index.php 檢視測試也,如果正常,就將wordpress網站檔案放到該目錄下 2. 安裝wordpress 資料庫地址:172.18.0.2 [通過 docker inspect lnmp_mysql |grep IPA 檢視] 資料庫名: wordpress 使用者名稱:wordpress 密碼:123456
圖形頁面: