Docker分离构建lnmp部署wordpress

作者:袖梨 2022-11-14

I. os:

[root@LinuxEA lnmp]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
mariadb-10.0.27-linux
nginx1.10:1
php5.6.26
II. 须知

在Docker单机上分离LNMP
在dockerfile之前已经将/mydata/data的数据目录放置在宿主机上存放,在run起mysql时挂载/mydata/data到容器内,以及其他日志文件和配置文件,在配置文件中需要指定datadir目录

III. mysql Dockerfile

[root@LinuxEA mysql]# cat Dockerfile
FROM centos
MAINTAINER wwww.111com.net and mark make
RUN yum makecache
&& yum install libaio -y
&& curl -sO http://mirrors.ds.com/tar%E5%8C%85/mariadb-10.0.27-linux-x86_64.tar.gz
&& mv mariadb-10.0.27-linux-x86_64.tar.gz /usr/local
&& yum clean all
&& tar xf /usr/local/mariadb-10.0.27-linux-x86_64.tar.gz -C /usr/local/
&& rm -rf /usr/local/mariadb-10.0.27-linux-x86_64.tar.gz
&& groupadd -r -g 306 mysql
&& useradd -r -g 306 -u 306 mysql
&& mkdir -p /mydata/data
&& chown -R mysql.mysql /mydata
&& chown -R root.mysql /usr/local/mariadb-10.0.27-linux-x86_64
&& ln -s /usr/local/mariadb-10.0.27-linux-x86_64 /usr/local/mysql
WORKDIR /usr/local/mysql
RUN scripts/mysql_install_db --user=mysql --datadir=/mydata/data
&& rm -rf /mydata
ENV PATH /usr/local/mysql/bin:$PATH
EXPOSE 3306
ENTRYPOINT mysqld_safe
IV. nginx Dockerfile

nginx容器在前几章中还是有提到,dockerfile如下:
仍然是将配置,日志文件和网站文件存放在宿主机而后挂载到容器中

[root@LinuxEA nginx]# cat Dockerfile
FROM marksugar/alpine
MAINTAINER wwww.111com.net
RUN addgroup -g 499 -S nginx
&& adduser -HDu 499 -s /sbin/nologin -g 'web server' -G nginx nginx
&& cd /usr/local
&& curl -sO http://nginx.org/download/nginx-1.10.1.tar.gz
&& tar xf nginx-1.10.1.tar.gz
&& rm -rf nginx-1.10.1.tar.gz
&& apk --update --no-cache add geoip geoip-dev pcre libxslt gd openssl-dev
pcre-dev zlib-dev build-base linux-headers libxslt-dev gd-dev openssl-dev
libstdc++ libgcc patch logrotate supervisor inotify-tools
WORKDIR /usr/local/nginx-1.10.1
RUN ./configure
--prefix=/usr/local/nginx
--conf-path=/etc/nginx/nginx.conf
--user=nginx
--group=nginx
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock
--with-http_ssl_module
--with-http_stub_status_module
--with-http_gzip_static_module
--with-http_flv_module
--with-http_mp4_module
--http-client-body-temp-path=/var/tmp/nginx/client
--http-proxy-temp-path=/var/tmp/nginx/proxy
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
&& make && make install
&& mkdir -p /var/tmp/nginx/{client,fastcgi,proxy,uwsgi}
&& echo "daemon off;" >> /etc/nginx/nginx.conf
&& apk del wget
&& rm -rf /usr/local/nginx-1.10.1
&& rm -rf /var/cache/apk/*
&& rm -rf /etc/nginx/nginx.conf
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
ENTRYPOINT /usr/local/nginx/sbin/nginx
V. nginx配置文件:

[root@LinuxEA nginx]# cat nginx.conf
user nginx nginx;
error_log /data/nginx/wwwlog/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
use epoll;
worker_connections 51200;
}

http {

include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;

# fastcgi_connect_timeout 300;
# fastcgi_send_timeout 300;
# fastcgi_read_timeout 300;
# fastcgi_buffer_size 64k;
# fastcgi_buffers 4 64k;
# fastcgi_busy_buffers_size 128k;
# fastcgi_temp_file_write_size 128k;

# gzip on;
# gzip_buffers 16 8k;
# gzip_comp_level 6;
# gzip_http_version 1.1;
# gzip_min_length 256;
# gzip_proxied any;
# gzip_vary on;
# gzip_types
# text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
# text/javascript application/javascript application/x-javascript
# text/x-json application/json application/x-web-app-manifest+json
# text/css text/plain text/x-component
# font/opentype application/x-font-ttf application/vnd.ms-fontobject
# image/x-icon;
# gzip_disable "msie6";


# open_file_cache max=1000 inactive=20s;
# open_file_cache_valid 30s;
# open_file_cache_min_uses 2;
# open_file_cache_errors on;

# log_format access '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" $http_x_forwarded_for';

# log_format upstream2 '$proxy_add_x_forwarded_for $remote_user [$time_local] "$request" $http_host'
# '$body_bytes_sent "$http_referer" "$http_user_agent" $ssl_protocol $ssl_cipher'
# '$request_time [$status] [$upstream_status] [$upstream_response_time] "$upstream_addr"';
###########################################################################################
server {

listen 80;
server_name localhost;
location / {
root /data/nginx/wwwroot;
index index.php index.html index.htm;
}
# location /nginx_status {
# stub_status on;
# access_log off;
# allow 127.0.0.1;
# #deny all;
# }
location ~ .php$ {
root /data/nginx/wwwroot;
fastcgi_pass phpfpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/nginx/wwwroot$fastcgi_script_name;
include fastcgi.conf;
}
access_log /data/nginx/wwwlog/www.com.log;
}
include vhost/*.conf;
}
daemon off;
[root@LinuxEA nginx]#
VI. php-fpm Dockerfile

[root@LinuxEA php-fpm]# cat Dockerfile
FROM centos
MAINTAINER wwww.111com.net for mark
RUN curl -sO http://ftp.ntu.edu.tw/php/distributions/php-5.5.26.tar.gz
&& tar xf php-5.5.26.tar.gz -C /usr/local/
&& mv /usr/local/php-5.5.26 /usr/local/php
&& rm -rf php-5.5.26.tar.gz
WORKDIR /usr/local/php
RUN groupadd -g 499 -r nginx
&& useradd -u 499 -g nginx -r nginx -s /sbin/nologin
&& yum install epel* -y
&& yum install -y gcc automake autoconf libtool make
libxml2-devel
openssl
openssl-devel
bzip2
bzip2-devel
libpng
libpng-devel
freetype
freetype-devel
libcurl-devel
libcurl libjpeg
libjpeg-devel
libpng
libpng-devel
freetype
freetype-devel
libmcrypt-devel
libmcrypt
libtool-ltdl-devel
libxslt-devel
mhash
mhash-devel
&& ./configure --prefix=/usr/local/php
--disable-pdo
--disable-debug
--disable-rpath
--enable-inline-optimization
--enable-sockets
--enable-sysvsem
--enable-sysvshm
--enable-pcntl
--enable-mbregex
--enable-xml
--enable-zip
--enable-fpm
--enable-mbstring
--with-pcre-regex
--with-mysql
--with-mysqli
--with-gd
--with-jpeg-dir
--with-bz2
--with-zlib
--with-mhash
--with-curl
--with-mcrypt
--with-jpeg-dir
--with-png-dir
&& make
&& make install
&& yum remove make -y
&& yum clean all
#ENV PATH /usr/local/php/sbin:$PATH
#EXPOSE 9000
ENTRYPOINT /usr/local/php/sbin/php-fpm --nodaemonize
VII. php-fpm.conf

[root@LinuxEA php-fpm]# cat php-fpm.conf
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = warning

emergency_restart_threshold = 30
emergency_restart_interval = 60s
process_control_timeout = 5s
daemonize = no

;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;

[php]
;listen = /dev/shm/php-cgi.sock
listen = 9000
listen.backlog = -1
;listen.allowed_clients = 127.0.0.1
listen.owner = nginx
listen.group = nginx
listen.mode = 0777
user = nginx
group = nginx
;Come from www.111com.net for mark
pm = dynamic
pm.max_children = 12
pm.start_servers = 8
pm.min_spare_servers = 6
pm.max_spare_servers = 12
pm.max_requests = 2048
pm.process_idle_timeout = 10s
request_terminate_timeout = 120
request_slowlog_timeout = 0

pm.status_path = /php-fpm_status
slowlog = log/slow.log
rlimit_files = 51200
rlimit_core = 0

catch_workers_output = yes
;env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
[root@LinuxEA php-fpm]#
我们在run php是需要将nginx网站根目录也挂载进来

版权属于:www.111com.net
转载时须注明出处及本声明

VIII. 目录结构:

[root@LinuxEA lnmp]# tree -L 4
.
├── docker-compose.yml
├── log
│ ├── nginx
│ │ ├── wwwlog
│ │ └── wwwroot
│ │ └── index.php
│ ├── php-fpm.log
│ ├── php-fpm.log.slow
│ └── www.111com.net
├── mysql
│ ├── Dockerfile
│ ├── mariadb-10.0.27-linux-x86_64.tar.gz
│ ├── my.cnf
│ └── mydata
│ ├── data
│ │ ├── 1033805cad2d.pid
│ │ ├── 1639a5882354.pid
│ │ ├── 2619917b9ae6.pid
│ │ ├── 5c05812ed420.pid
│ │ ├── 909d6ef97f04.pid
│ │ ├── aria_log.00000001
│ │ ├── aria_log_control
│ │ ├── b83f3b6a702f.pid
│ │ ├── dff35cf18443.pid
│ │ ├── ibdata1
│ │ ├── ib_logfile0
│ │ ├── ib_logfile1
│ │ ├── linuxea
│ │ ├── multi-master.info
│ │ ├── mysql
│ │ ├── mysql-bin.000001
│ │ ├── mysql-bin.000002
│ │ ├── mysql-bin.000003
│ │ ├── mysql-bin.000004
│ │ ├── mysql-bin.000005
│ │ ├── mysql-bin.000006
│ │ ├── mysql-bin.000007
│ │ ├── mysql-bin.000008
│ │ ├── mysql-bin.000009
│ │ ├── mysql-bin.000010
│ │ ├── mysql-bin.index
│ │ ├── performance_schema
│ │ └── test
│ └── log
│ └── mysql-error.log
├── nginx
│ ├── Dockerfile
│ ├── log
│ │ ├── error_nginx.log
│ │ └── www.com.log
│ ├── nginx.conf
│ ├── www.conf
│ └── wwwroot
│ ├── helo.php
│ ├── index.html
│ ├── index.php
│ └── mysql.php
└── php-fpm
├── Dockerfile
├── log
├── php-fpm.conf
├── php-fpm.conf.bak
├── php-fpm.conf.default
└── php.ini

17 directories, 47 files
[root@LinuxEA lnmp]#
挂载相应的配置文件和数据目录

IX. mariadb启动

启动顺序mysql->php->nginx

[root@LinuxEA lnmp] docker run --name mysql1
-v /root/lnmp/mysql/my.cnf:/etc/mysql/my.cnf
-v /root/lnmp/mysql/mydata/data:/mydata/data
-v /root/lnmp/mysql/mydata/log:/mydata/log
-e MYSQL_ROOT_PASSWORD=linuxea
-d -p 3306:3306 mariadb:10.0.27
X. php-fpm启动

[root@LinuxEA lnmp] docker run --name phpfpm
--link mysql1:mysql1
-v /root/lnmp/php-fpm/php-fpm.conf:/usr/local/php/etc/php-fpm.conf:rw
-v /root/lnmp/log:/data -v /root/lnmp/nginx/wwwroot:/data/nginx/wwwroot
-d php-fpm:5.6.24
如果php.ini需要挂载,位置:/usr/local/php/lib下

XI. nginx启动

[root@LinuxEA lnmp] docker run --name nginx
--link phpfpm:phpfpm
-v /root/lnmp/nginx/nginx.conf:/etc/nginx/nginx.conf
-v /root/lnmp/nginx/log:/data/nginx/wwwlog
-v /root/lnmp/nginx/wwwroot:/data/nginx/wwwroot
--volumes-from phpfpm
-d -p 80:80 nginx1.10:1
XII. 查看启动状态

[root@LinuxEA lnmp]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2d2a8e11454 nginx1.10:1 "/bin/sh -c /usr/loca" 42 hours ago Up 2 seconds 0.0.0.0:80->80/tcp nginx
c89e724b0973 php-fpm:2 "/bin/sh -c '/usr/loc" 42 hours ago Up 3 seconds phpfpm
1639a5882354 mariadb:10.0.27 "/bin/sh -c mysqld_sa" 42 hours ago Up 25 seconds 0.0.0.0:3306->3306/tcp mysql1
[root@LinuxEA lnmp]#
XIII. 验证phpinfo

[root@LinuxEA wwwroot]# cat index.php
phpinfo();
?>
[root@LinuxEA wwwroot]#


XIV. 验证mysql链接

授权一个账户

[root@1639a5882354 /]# /usr/local/mysql/bin/mysql -uroot -p -h127.0.0.1
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 8
Server version: 10.0.27-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE linuxea CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> GRANT ALL ON linuxea.* TO 'linuxea'@'%' IDENTIFIED BY 'linuxea';
Query OK, 0 rows affected (0.00 sec)
创建一个mysql链接php文件

[root@LinuxEA wwwroot]# cat mysql.php
$link=mysql_connect("mysql1","linuxea","linuxea");
if(!$link) echo "

FAILD!nlinks www.111com.net error username and passwd

";
else echo "

OK!
www.111com.net links normal

";
?>
[root@LinuxEA wwwroot]# curl 10.10.239.194/mysql.php

Hello Docker!
www.111com.net links normal


[root@LinuxEA wwwroot]#


XV. 安装wordpress


这里的ip可能需要手动指定,如果嫌麻烦可以直接使用hosts网络模式

XVI. docker-compost

[root@LinuxEA ~]# cat lnmp/docker-compose.yml
version: '2'
services:

mysql:
container_name: mysql-1
image: mariadb:10.0.27
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=linuxea
volumes:
- /root/lnmp/mysql/my.cnf:/etc/mysql/my.cnf
- /root/lnmp/mysql/mydata/data:/mydata/data
- /root/lnmp/mysql/mydata/log:/mydata/log
phpfpm:
container_name: phpfpm-1
image: php-fpm:2
ports:
- "9000:9000"
links:
- "mysql:mysql"
volumes:
- /root/lnmp/php-fpm/php-fpm.conf:/usr/local/php/etc/php-fpm.conf
- /root/lnmp/log:/data/phplog
- /root/lnmp/nginx/wwwroot:/data/nginx/wwwroot
nginx:
container_name: nginx-1
image: nginx1.10:1
ports:
- "80:80"
links:
- "phpfpm:php"
volumes_from:
- "phpfpm"
volumes:
- /root/lnmp/nginx/nginx.conf:/etc/nginx/nginx.conf
- /root/lnmp/nginx/log:/data/nginx/wwwlog
- /root/lnmp/nginx/wwwroot:/data/nginx/wwwroot
[root@LinuxEA ~]#

相关文章

精彩推荐