Docker搭建LNMP+WordPress

一、服务器环境

容器 操作系统 IP地址 主要软件
nginx CentOS 7 172.18.0.10 Docker-Nginx
mysql CentOS 7 172.18.0.20 Docker-Mysql
mysql CentOS 7 172.18.0.20 Docker-Mysql

二、Linux系统基础镜像

systemctl stop firewalld setenforce 0 docker pull centos:7 #从公有仓库中下载centos7作为系统基础镜像 

三、Nginx

1、上传安装包

mkdir /opt/nginx cd /opt/nginx 上传安装包 mkdir /opt/nginx/html tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html

Docker搭建LNMP+WordPress插图

2、配置nginx.conf 文件

vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; charset utf-8; location / { root html; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root html; fastcgi_pass 172.18.0.30:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } } }

3、编写Dockerfile脚本

vim Dockerfile FROM centos:7 MAINTAINER this is nginx image <wl> RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make RUN useradd -M -s /sbin/nologin nginx ADD nginx-1.12.0.tar.gz /usr/local/src/ WORKDIR /usr/local/src/nginx-1.12.0 RUN ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module && make && make install ENV PATH /usr/local/nginx/sbin:$PATH ADD nginx.conf /usr/local/nginx/conf/ #ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/ RUN chmod 777 -R /usr/local/nginx/html/ EXPOSE 80 EXPOSE 443 ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]

4、生成镜像

docker build -t nginx:lnmp .

5、创建自定义网络

docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork 

Docker搭建LNMP+WordPress插图1

6、启动镜像容器

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 docker inspect nginx

Docker搭建LNMP+WordPress插图2

四、Mysql

1、上传安装包

mkdir /opt/mysql cd /opt/mysql 上传安装包

Docker搭建LNMP+WordPress插图3

2、配置my.cnf文件

vim my.cnf [client] port = 3306 socket = /usr/local/mysql/mysql.sock [mysql] port = 3306 socket = /usr/local/mysql/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 character_set_server=utf8 pid-file = /usr/local/mysql/mysqld.pid socket = /usr/local/mysql/mysql.sock server-id = 1 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

3、编写Dockerfile

vim Dockerfile FROM centos:7 MAINTAINER this is mysql image <wl> RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make ADD mysql-boost-5.7.20.tar.gz /usr/local/src/ WORKDIR /usr/local/src/mysql-5.7.20/ RUN cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ -DSYSCONFDIR=/etc \ -DSYSTEMD_PID_DIR=/usr/local/mysql \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DWITH_BOOST=boost \ -DWITH_SYSTEMD=1 && make && make install ADD my.cnf /etc/ ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH RUN useradd -M -s /sbin/nologin mysql RUN chown mysql:mysql /etc/my.cnf RUN chown -R mysql:mysql /usr/local/mysql/ RUN /usr/local/mysql/bin/mysqld \ --initialize-insecure \ --user=mysql \ --basedir=/usr/local/mysql \ --datadir=/usr/local/mysql/data EXPOSE 3306 CMD ["/usr/local/mysql/bin/mysqld"]

4、生成镜像

docker build -t mysql:lnmp .

5、启动镜像容器

docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp 

五、Php

1、上传安装包

mkdir /opt/php cd /opt/php 上传安装包

Docker搭建LNMP+WordPress插图4

2、配置php.ini、php-fpm.conf、www.conf文件

1)php.ini

vim php.ini #939行,取消注释,修改 date.timezone = Asia/Shanghai #1170行,修改 mysqli.default_socket = /usr/local/mysql/mysql.sock egrep -v "^;" php.ini | egrep -v "^$" [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 expose_php = On max_execution_time = 30 max_input_time = 60 memory_limit = 128M error_reporting = E_ALL display_errors = On display_startup_errors = On log_errors = On log_errors_max_len = 1024 ignore_repeated_errors = Off ignore_repeated_source = Off report_memleaks = On track_errors = On html_errors = On variables_order = "GPCS" request_order = "GP" register_argc_argv = Off auto_globals_jit = On post_max_size = 8M 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 = 2M max_file_uploads = 20 allow_url_fopen = On allow_url_include = Off default_socket_timeout = 60 [CLI Server] cli_server.color = On [Date] date.timezone = Asia/Shanghai [filter] [iconv] [intl] [sqlite3] [Pcre] [Pdo] [Pdo_mysql] pdo_mysql.cache_size = 2000 pdo_mysql.default_socket= [Phar] [mail function] SMTP = localhost smtp_port = 25 mail.add_x_header = On [SQL] sql.safe_mode = Off [ODBC] odbc.allow_persistent = On odbc.check_persistent = On odbc.max_persistent = -1 odbc.max_links = -1 odbc.defaultlrl = 4096 odbc.defaultbinmode = 1 [Interbase] ibase.allow_persistent = 1 ibase.max_persistent = -1 ibase.max_links = -1 ibase.timestampformat = "%Y-%m-%d %H:%M:%S" ibase.dateformat = "%Y-%m-%d" ibase.timeformat = "%H:%M:%S" [MySQLi] mysqli.max_persistent = -1 mysqli.allow_persistent = On mysqli.max_links = -1 mysqli.cache_size = 2000 mysqli.default_port = 3306 mysqli.default_socket = /usr/local/mysql/mysql.sock mysqli.default_host = mysqli.default_user = mysqli.default_pw = mysqli.reconnect = Off [mysqlnd] mysqlnd.collect_statistics = On mysqlnd.collect_memory_statistics = On [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.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 [mcrypt] [dba] [opcache] [curl] [openssl]

2)php-fpm.conf

vim php-fpm.conf #17行,删除注释符号“;” pid = run/php-fpm.pid egrep -v "^;" php-fpm.conf | egrep -v "^$" [global] pid = run/php-fpm.pid include=/usr/local/php/etc/php-fpm.d/*.conf

3)www.conf

vim www.conf #23、24行,修改用户和组 user = nginx group = nginx #36行,修改监听IP和端口为容器IP:9000端口 listen = 172.111.0.30:9000 egrep -v "^;" www.conf | egrep -v "^$" [www] user = nginx group = nginx listen = 172.18.0.30:9000 listen.allowed_clients = 127.0.0.1,172.18.0.10 pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3

3、编写Dockerfile脚本

vim Dockerfile FROM centos:7 MAINTAINER this is php image <wl> RUN yum -y install gd \ libjpeg libjpeg-devel \ libpng libpng-devel \ freetype freetype-devel \ libxml2 libxml2-devel \ zlib zlib-devel \ curl curl-devel \ openssl openssl-devel \ gcc gcc-c++ make pcre-devel RUN useradd -M -s /sbin/nologin nginx ADD php-7.1.10.tar.bz2 /usr/local/src/ WORKDIR /usr/local/src/php-7.1.10 RUN ./configure \ --prefix=/usr/local/php \ --with-mysql-sock=/usr/local/mysql/mysql.sock \ --with-mysqli \ --with-zlib \ --with-curl \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-openssl \ --enable-fpm \ --enable-mbstring \ --enable-xml \ --enable-session \ --enable-ftp \ --enable-pdo \ --enable-tokenizer \ --enable-zip && make && make install ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH ADD php.ini /usr/local/php/lib/ ADD php-fpm.conf /usr/local/php/etc/ ADD www.conf /usr/local/php/etc/php-fpm.d/ EXPOSE 9000 ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]

4、生成镜像

docker build -t php:lnmp .

5、启动镜像容器

docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp

六、启动wordpress服务

1、mysql授权

docker exec -it mysql /bin/bash #进入mysql容器 mysql create database wordpress; grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123123'; grant all privileges on *.* to 'root'@'%' identified by 'abc123'; flush privileges;

2、浏览器访问测试

访问 http://192.168.80.0/wordpress/index.php

Docker搭建LNMP+WordPress插图5

原文链接:https://blog.csdn.net/TTSuzuka/article/details/128779511?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168994567316800180690880%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168994567316800180690880&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-5-128779511-null-null.268%5Ev1%5Ekoosearch&utm_term=docker%E3%80%81wordpress%E3%80%81wordpress%E5%BB%BA%E7%AB%99%E3%80%81wordpress%E4%B8%BB%E9%A2%98%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享