Docker-day5-yum搭建php+nginx+wordpress

Dockerfile 搭建 PHP+Nginx+Wordpress

  • 安装流程规划

    • wordpress装在宿主机,相当于一台NFS
    • php装在一台容器内
    • nginx装在一个容器内
    • 将容器挂载到wordpress站点目录以实现访问
  • 目录最终结构,对应放置

[root@docker1 ~] mkdir /code
[root@docker1 ~] cd /code
[root@docker1 code] ll /code/
total 4
drwxr-xr-x 5 www  www  4096 Mar 27 18:03 wordpress
drwxr-xr-x 2 root root   75 Mar 27 18:32 php
drwxr-xr-x 2 root root   67 Mar 27 18:16 nginx
drwxr-xr-x 2 root root   80 Mar 26 22:14 mysql
  • 下载、解压wordpress
[root@docker1 code] wget https://cn.wordpress.org/latest-zh_CN.tar.gz
[root@docker1 code] tar xf *gz

一、准备php文件+构建为镜像

  • Dockerfile
  • php-fpm.conf
  • php.ini
  • www.conf

1.Dockerfile

FROM centos:7
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN yum -y install wget \
 && wget https://www.php.net/distributions/php-7.4.16.tar.gz
RUN yum -y install gcc \
gcc-c++ \
make \
libxml2-devel \
openssl-devel \
curl-devel \
libjpeg-devel \
libpng-devel \
libicu-devel \
sqlite-devel \
freetype-devel \
openldap-devel \
openldap \
openldap-devel

RUN tar -zxvf php-7.4.16.tar.gz
WORKDIR /php-7.4.16
RUN ./configure --prefix=/usr/local/php \
       --with-config-file-scan-dir=/usr/local/php/etc/ \
       --with-mhash --with-pdo-mysql \
       --with-openssl --with-mysqli \
       --with-iconv --with-zlib \
       --enable-inline-optimization \
       --disable-debug --disable-rpath \
       --enable-shared --enable-xml \
       --enable-bcmath --enable-shmop \
       --enable-sysvsem --enable-sysvshm --enable-mbregex \
       --enable-ftp \
       --enable-pcntl --enable-sockets \
       --with-xmlrpc --enable-soap \
       --without-pear --with-gettext \
       --enable-session --with-curl \
       --enable-opcache --enable-fpm \
       --without-gdbm --enable-fast-install \
       --disable-fileinfo
RUN make && make install
RUN groupadd www -g 666 && \
    useradd www -u 666 -g 666 -s /sbin/nologin -M
ADD www.conf /usr/local/php/etc/php-fpm.d/
ADD php.ini /usr/local/php/etc/
ADD php-fpm.conf /usr/local/php/etc/
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm","-F"]
EOF

2.php-fpm.conf

[root@docker1 code]# cat php/php-fpm.conf
[global]
daemonize = no
include=/usr/local/php/etc/php-fpm.d/*.conf

3.php.ini

[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
zend.exception_ignore_args = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 200M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 200M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[imap]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.cookie_samesite =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = -1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[dba]
[opcache]
[curl]
[openssl]
[ffi]

4.www.conf

[www]
user = www
group = www
listen = 9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
request_terminate_timeout = 0

5.构建为镜像

[root@docker1 php] docker build -t lnmp-php:v1 .

二、准备Nginx文件+构建为镜像

  • Dockerfile
  • nginx.conf
  • linux.wp.com.conf

1.Dockerfile

FROM nginx
RUN groupadd www -g 666 && \
    useradd www -u 666 -g 666 -s /sbin/nologin -M
ADD linux.wp.com.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx/
RUN mkdir /code/wordpress/ -p
RUN rm -rf /etc/nginx/conf.d/default.conf
EXPOSE 80
WORKDIR /root
CMD ["nginx","-g","daemon off;"]

2.nginx.conf

user  www;
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;
}

3.linux.wp.com.conf

server {
    listen 80;
    server_name linux.wp.com;
    
    location / {
        root /code/wordpress;
        index index.php;
    }
    
    location ~* \.php$ {
        fastcgi_pass php:9000;
        fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
        include fastcgi_params;
    }
}

4.构建为镜像

[root@docker1 nginx] docker build -t lnmp-nginx:v1 .

三、准备mysql文件+构建为镜像

  • Dockerfile
  • privileges.sql
  • schema.sql
  • setup.sh

1.Dockerfile

FROM mysql:5.7

#设置免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes

#添加所需文件
ADD setup.sh /mysql/setup.sh
ADD schema.sql /mysql/schema.sql
ADD privileges.sql /mysql/privileges.sql

#设置容器启动时执行的命令
CMD ["sh", "/mysql/setup.sh"]

2.privileges.sql

use mysql;
grant all on wordpress.* to wp@'172.18.0.%' identified by '123';
SET PASSWORD=PASSWORD('123456');
flush privileges;

3.schema.sql

create database wordpress;

4.setup.sh

#!/bin/bash
set -e

#查看mysql服务的状态,方便调试,这条语句可以删除
echo `service mysql status`
 
echo '1.启动mysql....'
#启动mysql
service mysql start
sleep 3
echo `service mysql status`
 
echo '2.开始导入数据....'
#导入数据
mysql < /mysql/schema.sql
echo '3.导入数据完毕....'
 
sleep 3
echo `service mysql status`
 
#重新设置mysql密码
echo '4.开始修改密码....'
mysql < /mysql/privileges.sql
echo '5.修改密码完毕....'
 
#sleep 3
echo `service mysql status`
echo `mysql容器启动完毕,且数据导入成功`
 
tail -f /dev/null

5.构建为镜像

[root@docker1 mysql] docker build -t lnmp-mysql:v1 .

6.查看构建镜像

[root@docker1 ~] docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
lnmp-php     v1        0f5aa301288b   21 hours ago   1.23GB
lnmp-nginx   v1        de9f843b40ab   22 hours ago   133MB
lnmp-mysql   v1        95be68fa0d85   22 hours ago   449MB

五、启动容器

1.创建lnmp网络

docker network create lnmp

2.启动PHP

  • 挂载目录与nginx指定目录一致,若没有会自动创建
docker run -d -v /code/wordpress/:/code/wordpress/ --name php --net=lnmp lnmp-php:v1

3.启动Nginx

docker run -d -p 8080:80 -v /code/wordpress/:/code/wordpress/ --name nginx --net=lnmp lnmp-nginx:v1

5.启动mysql

[root@docker1 nginx] docker run -d --network=lnmp --name mysql lnmp-mysql:v1

5.查看启动状态

[root@docker1 ~] docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED       STATUS       PORTS                  NAMES
7310d595f462   lnmp-mysql:v1   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   3306/tcp, 33060/tcp    mysql

e7c6d82d1d30   de9f843b40ab    "/docker-entrypoint.…"   2 hours ago   Up 2 hours   0.0.0.0:8080->80/tcp   nginx

4297b934a1ef   0f5aa301288b    "/usr/local/php/sbin…"   2 hours ago   Up 2 hours   9000/tcp               php

5.访问网站测试

  • wordpress注册登录页面
192.168.12.30:8080

六、上传镜像到GitHub

1.登录GitHub

[root@docker1 ~] docker login
···
Login Succeeded

2.打标签

[root@docker1 php] docker tag 0f5aa301288b 18954354671/php:lnmp-php-v1
[root@docker1 php] docker tag de9f843b40ab 18954354671/php:lnmp-nginx-v1
[root@docker1 php] docker tag 95be68fa0d85 18954354671/php:lnmp-mysql-v1

3.上传到GitHub

[root@docker1 php] docker push 18954354671/php:lnmp-php-v1
[root@docker1 php] docker push 18954354671/php:lnmp-nginx-v1
[root@docker1 php] docker push 18954354671/php:lnmp-mysql-v1

4.上传报错解决

  • 若上传过程出现如下报错,更改DNS地址重新上传即可
报错大致内容:
dial tcp: lookup auth.docker.io on 223.5.5.5:53: server misbehaving

方式一:无需重启
echo "nameserver 114.114.114.114" >> /etc/resolv.conf

方式二:需重启机器
sed -i 's#223.5.5.5#114.114.114.114#g' /etc/sysconfig/network-scripts/ifcfg-eth0
posted @ 2022-06-09 18:18  秋风お亦冷  阅读(56)  评论(0编辑  收藏  举报