赞
踩
目录
4.3 准备 php.ini、php-fpm.conf、www.conf 配置文件
在 Docker 上搭建 LNMP 架构(Linux + Nginx + MySQL + PHP)是一种常见的做法,可以通过容器化技术轻松地部署和管理整个 Web 应用程序的运行环境
需要使用 Docker 技术在一台主机上创建LNMP服务并运行Wordpress网站平台,然后对此服务进行相关的性能调优和管理工作
宿主机 | 操作系统 | IP地址 | 主要软件 | 配置要求 |
---|---|---|---|---|
Docker | CentOS 7 | 172.16.12.15 | Docker 19.03 | 4核8G以上 |
容器 | 操作系统 | IP地址 | 主要软件 |
---|---|---|---|
nginx | CentOS 7 | 172.20.0.10 | Docker-Nginx |
mysql | CentOS 7 | 172.20.0.20 | Docker-Mysql |
php | CentOS 7 | 172.20.0.30 | Docker-php |
(1)关闭防火墙和核心防护
- systemctl disable --now firewalld
- setenforce 0
(2)自定义网络docker1
docker network create --subnet=172.20.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
(3)所有安装包下载
- wget https://cdn.mysql.com//archives/mysql-5.7/mysql-boost-5.7.20.tar.gz
- wget http://nginx.org/download/nginx-1.12.0.tar.gz
- wget https://www.php.net/distributions/php-7.1.10.tar.bz2
- wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
- mkdir -p /opt/lnmp/nginx/html
- cd /opt/lnmp/nginx
- #需在此路径上传 nginx 安装包 nginx-1.12.0.tar.gz
- cd /opt/lnmp/nginx/html
- #需在此路径上传 wordpress 服务包 wordpress-4.9.4-zh_CN.tar.gz
- cd /opt/lnmp/nginx/html/ #切换目录
- tar xf wordpress-4.9.4-zh_CN.tar.gz #解压安装包
- cd /opt/lnmp/nginx
- vim Dockerfile
-
- FROM centos:7
- MAINTAINER this is nginx image <lnmp>
- 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 -j 4 && make install
- ENV PATH /usr/local/nginx/sbin:$PATH
- ADD nginx.conf /usr/local/nginx/conf/
- #ADD html/wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html
- RUN chmod 777 -R /usr/local/nginx/html/
- EXPOSE 80
- EXPOSE 443
- VOLUME [ "/usr/local/nginx/html/" ]
- CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]
- #需上传nginx的主配置文件到/opt/lnmp/nginx目录下,再过滤查看其生效的配置
- egrep -v "^(.)*#(.)*$" nginx.conf | grep -v "^$"
-
- 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.20.0.30:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
- include fastcgi_params;
- }
- }
- }
- docker build -t nginx:lnmp .
- # -t 参数用于指定构建镜像的名称及标签
- # . 就是指示Docker在当前目录中查找名为Dockerfile的文件,并以此文件作为构建镜像的基础
-
- docker images #查看所有镜像
- docker run -d --name nginx -p 80:80 -v /opt/lnmp/nginx/html:/usr/local/nginx/html -m 500m --memory-swap 1g --net mynetwork --ip 172.20.0.10 nginx:lnmp
-
- #docker run: 运行一个新的容器
- #-d: 在后台运行容器
- #--name nginx: 为容器指定一个名称为 nginx
- #-p 80:80: 将容器的 80 端口映射到主机的 80 端口,允许通过主机的 80 端口访问容器内的服务
- #-v /opt/lnmp/nginx/html:/usr/local/nginx/html: 将主机的 /opt/lnmp/nginx/html 目录挂载到容器内的 /usr/local/nginx/html 目录,实现主机和容器之间的文件共享
- #-m 500m: 限制容器的内存使用为 500MB
- #--memory-swap 1g: 设置容器的内存加交换空间的上限为 1GB
- #--net mynetwork: 连接容器到名为 mynetwork 的网络
- #--ip 172.20.0.10: 为容器分配 IP 地址为 172.20.0.10
- #nginx:lnmp: 使用 nginx:lnmp 镜像来创建容器
-
- docker ps -a #查看所有的容器
docker inspect nginx #查看nginx容器的ip地址等信息
########如果有网络报错提示########
[Warning] IPv4 forwarding is disabled. Networking will not work.解决方法:
vim /etc/sysctl.conf
net.ipv4.ip_forward=1sysctl -p
systemctl restart network
systemctl restart docker
- cd /opt/lnmp/nginx/html/
- echo "welcome to my world" > index.html
- #编写测试网页文件
curl http://172.16.12.15:80 #浏览器访问宿主机的80端口
- mkdir -p /opt/lnmp/mysql
- cd /opt/lnmp/mysql
- #需在此路径上传 mysql 安装包mysql-boost-5.7.20.tar.gz
- vim Dockerfile
-
- FROM centos:7
- MAINTAINER this is mysql image <lnmp>
- 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 -j 4 && 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"]
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
- docker build -t mysql:lnmp .
- # -t 参数用于指定构建镜像的名称及标签
- # . 就是指示Docker在当前目录中查找名为Dockerfile的文件,并以此文件作为构建镜像的基础
-
- docker images #查看所有镜像
- docker run --name=mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql --net mynetwork --ip 172.20.0.20 mysql:lnmp
-
- #docker run: 运行一个新的容器
- #--name=mysql: 为容器指定一个名称为 mysql
- #-d: 在后台运行容器
- #--privileged: 给容器赋予特权,即容器内的进程可以获得访问主机的权限
- #--device-write-bps /dev/sda:10M: 限制容器对指定设备 /dev/sda 的写入速率为 10MB/s
- #-v /usr/local/mysql: 挂载主机的 /usr/local/mysql 目录到容器内的相同路径,实现主机和容器之间的文件共享
- #--net mynetwork: 连接容器到名为 mynetwork 的网络
- #--ip 172.20.0.20: 为容器分配 IP 地址为 172.20.0.20
- #mysql:lnmp: 使用 mysql:lnmp 镜像来创建容器
-
- docker ps -a #查看所有的容器
docker inspect mysql #查看mysql容器的ip地址等信息
- mkdir -p /opt/lnmp/php
- cd /opt/lnmp/php
- #需在此路径上传 php 安装包php-7.1.10.tar.bz2
- vim Dockerfile
-
- FROM centos:7
- MAINTAINER this is php image <lnmp>
- 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 -j 4 && 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" ]
该配置文件模板位于安装目录的 php-7.1.10/php.ini-development 位置
- vim php.ini
-
- #第939行,取消注释,修改
- date.timezone = Asia/Shanghai
- #第1170行,修改
- mysqli.default_socket = /usr/local/mysql/mysql.sock
- [root@localhost php]#egrep -v "^;" php.ini | egrep -v "^$" #过滤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
- 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]
该配置文件位于 /usr/local/php/etc/php-fpm.conf.default
- vim php-fpm.conf
-
- #第17行,删除注释符号“;”
- pid = run/php-fpm.pid
- [root@localhost php]# egrep -v "^;" php-fpm.conf | egrep -v "^$"
- #过滤php-fpm.conf配置文件中生效的配置
- [global]
- pid = run/php-fpm.pid
- include=/usr/local/php/etc/php-fpm.d/*.conf
该配置文件位于 usr/local/php/etc/php-fpm.d/www.conf.default
- vim www.conf
-
- #第23、24行,修改用户和组
- user = nginx
- group = nginx
- #第36行,修改监听IP和端口为容器IP:9000端口
- listen = 172.20.0.30:9000
- #第62行,修改被允许监听的客户端容器地址为回环地址和nginx容器地址
- listen.allowed_clients = 127.0.0.1,172.20.0.10
- [root@docker php]# egrep -v "^;" www.conf | egrep -v "^$"
- [www]
- user = nginx
- group = nginx
- listen = 172.20.0.30:9000
- listen.allowed_clients = 127.0.0.1,172.20.0.10
- pm = dynamic
- pm.max_children = 5
- pm.start_servers = 2
- pm.min_spare_servers = 1
- pm.max_spare_servers = 3
- docker build -t php:lnmp .
- # -t 参数用于指定构建镜像的名称及标签
- # . 就是指示Docker在当前目录中查找名为Dockerfile的文件,并以此文件作为构建镜像的基础
-
- docker images #查看所有镜像
- docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.20.0.30 php:lnmp
-
- #docker run: 运行一个新的容器
- #--name=php: 为容器指定一个名称为 php
- #-d: 在后台运行容器
- #-p 9000:9000: 将容器的 9000 端口映射到主机的 9000 端口,允许通过主机的 9000 端口访问容器内的服务
- #--volumes-from mysql: 将容器 mysql 的数据卷挂载到当前容器中,实现数据共享
- #--volumes-from nginx: 将容器 nginx 的数据卷挂载到当前容器中,实现数据共享
- #--net mynetwork: 连接容器到名为 mynetwork 的网络
- #--ip 172.20.0.30: 为容器分配 IP 地址为 172.20.0.30
- #php:lnmp: 使用 php:lnmp 镜像来创建容器
-
- docker ps -a #查看所有的容器
docker inspect php #查看php容器的ip地址等信息
- #进入到 php 容器中
- docker exec -it php bash
-
- ps -aux
- [root@localhost mysql]#docker exec -it mysql /bin/bash #进入到 mysql 容器内
- [root@8ef6f9c3b89d mysql-5.7.20]#mysql -u root -p
- Enter password:
- #初始密码为空,回车即可
- mysql> create database wordpress;
- #创建wordpress数据库
-
- mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
- #授予一个名为 wordpress 的数据库中的用户 wordpress 在任何主机上('%')的所有权限,并使用密码 123456 进行身份验证
- #将所有权限授予用户 wordpress,允许其从任何主机连接到 wordpress 数据库,并使用密码 123456 进行身份验证
-
- mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
- #授予用户名为 root,从任何主机连接到 MySQL 数据库服务器的用户,对所有数据库中的所有表具有全部权限,并设置密码为 abc123
- #这样的设置允许用户 root 从任何地方以密码 abc123 连接到 MySQL 数据库,并具有对所有数据库的全部权限
-
- mysql> flush privileges;
- #刷新权限
-
- mysql> exit #退出 mysql 数据库
-
- [root@8ef6f9c3b89d mysql-5.7.20]# exit #退出 mysql 容器
浏览器访问 http://172.16.12.15/wordpress/index.php
报错页面:
解决方法:
cd /opt/lnmp/nginx/html/wordpress #切换目录 cp wp-config-sample.php wp-config.php #手动创建wp-config.php文件 vim wp-config.php #第23行,将'database_name_here'改为'wordpress' define('DB_NAME', 'wordpress'); #第26行,将'username_here'改为'wordpress' define('DB_USER', 'wordpress'); #第29行,将'password_here'改为'123456' define('DB_PASSWORD', '123456'); systemctl restart docker #重启docker服务 docker start nginx;docker start mysql;docker start php #重启docker容器 docker ps #查看当前正在运行中的容器,必须得有nginx、mysql、php
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。