微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

再Docker中架设完整的WordPress站点全攻略

1. 安装 Docker

在我们真正开始之前,我们需要确保在我们的 Linux 机器上已经安装了 Docker。我们使用的主机是 CentOS 7,因此我们用下面的命令使用 yum 管理器安装 docker。

rush:bash;"> # yum install docker

rush:bash;"> # systemctl restart docker.service

2. 创建 wordpress 的 Dockerfile

我们需要创建用于自动安装 wordpress 以及其前置需求的 Dockerfile。这个 Dockerfile 将用于构建 wordpress 的安装镜像。这个 wordpress Dockerfile 会从 Docker Registry Hub 获取 CentOS 7 镜像并用最新的可用更新升级系统。然后它会安装必要的软件,例如 Nginx Web 服务器、PHP、MariaDB、Open SSH 服务器,以及其它保证 Docker 容器正常运行不可缺少的组件。最后它会执行一个初始化 wordpress 安装的脚本。

rush:bash;"> # nano Dockerfile

然后,我们需要将下面的配置行添加到 Dockerfile中。

<div class="jb51code">
<pre class="brush:plain;">
FROM centos:centos7
MAINTAINER The CentOS Project cloud-ops@centos.org
RUN yum -y update; yum clean all
RUN yum -y install epel-release; yum clean all
RUN yum -y install mariadb mariadb-server mariadb-client Nginx PHP-fpm PHP-cli PHP-MysqL PHP-gd PHP-imap PHP-ldap PHP-odbc PHP-pear PHP-xml PHP-xmlrpc PHP-magickwand PHP-magpieRSS PHP-mbstring PHP-mcrypt PHP-mssql PHP-shout PHP-snmp PHP-soap PHP-tidy PHP-apc pwgen python-setuptools curl git tar; yum clean all
ADD ./start.sh /start.sh
ADD ./Nginx-site.conf /Nginx.conf
RUN mv /Nginx.conf /etc/Nginx/Nginx.conf
RUN rm -rf /usr/share/Nginx/html/
RUN /usr/bin/easy_install supervisor
RUN /usr/bin/easy_install supervisor-stdout
ADD ./supervisord.conf /etc/supervisord.conf
RUN echo %sudo ALL=nopASSWD: ALL >> /etc/sudoers
ADD http://wordpress.org/latest.tar.gz /wordpress.tar.gz
RUN tar xvzf /wordpress.tar.gz
RUN mv /wordpress/
/usr/share/Nginx/html/.
RUN chown -R apache:apache /usr/share/Nginx/
RUN chmod 755 /start.sh
RUN mkdir /var/run/sshd
EXPOSE 80
EXPOSE 22
CMD ["/bin/bash","/start.sh"]

3. 创建启动脚本

我们创建了 Dockerfile 之后,我们需要创建用于运行和配置 wordpress 安装的脚本,名称为 start.sh。它会为 wordpress 创建并配置数据库和密码。用我们喜欢的文本编辑器打开 start.sh。

rush:bash;"> # nano start.sh

打开 start.sh 之后,我们要添加下面的配置行到文件中。

<div class="jb51code">
<pre class="brush:bash;">

!/bin/bash

check() {
if [ -f /usr/share/nginx/html/wp-config.php ]; then
exit
fi
}
create_user() {

创建用于 SSH 登录的用户

SSH_USERPASS=pwgen -c -n -1 8
useradd -G wheel user
echo user:$SSH_USERPASS | chpasswd
echo ssh user password: $SSH_USERPASS
}
__mysql_config() {

启用并运行 MySQL

yum -y erase mariadb mariadb-server
rm -rf /var/lib/mysql/ /etc/my.cnf
yum -y install mariadb mariadb-server
mysql_install_db
chown -R mysql:mysql /var/lib/mysql
/usr/bin/mysqld_safe &
sleep 10
}
__handle_passwords() {

在这里我们生成随机密码(多亏了 pwgen)。前面两个用于 mysql 用户,最后一个用于 wp-config.php 的随机密钥。

WORDPRESS_DB="wordpress"
MYSQL_PASSWORD=pwgen -c -n -1 12
WORDPRESS_PASSWORD=pwgen -c -n -1 12

这是在日志中显示的密码。

echo mysql root password: $MYSQL_PASSWORD
echo wordpress password: $WORDPRESS_PASSWORD
echo $MYSQL_PASSWORD > /MysqL-root-pw.txt
echo $wordpress_PASSWORD > /wordpress-db-pw.txt

这里原来是一个包括 sed、cat、pipe 和 stuff 的很长的行,但多亏了

@djfiander 的 https://gist.github.com/djfiander/6141138

现在没有了

sed -e "s/database_name_here/$wordpress_DB/
s/username_here/$wordpress_DB/
s/password_here/$wordpress_PASSWORD/
/'AUTH_KEY'/s/put your unique phrase here/pwgen -c -n -1 65/
/'SECURE_AUTH_KEY'/s/put your unique phrase here/pwgen -c -n -1 65/
/'LOGGED_IN_KEY'/s/put your unique phrase here/pwgen -c -n -1 65/
/'NONCE_KEY'/s/put your unique phrase here/pwgen -c -n -1 65/
/'AUTH_SALT'/s/put your unique phrase here/pwgen -c -n -1 65/
/'SECURE_AUTH_SALT'/s/put your unique phrase here/pwgen -c -n -1 65/
/'LOGGED_IN_SALT'/s/put your unique phrase here/pwgen -c -n -1 65/
/'NONCE_SALT'/s/put your unique phrase here/pwgen -c -n -1 65/" /usr/share/Nginx/html/wp-config-sample.PHP > /usr/share/Nginx/html/wp-config.php
}
__httpd_perms() {
chown apache:apache /usr/share/Nginx/html/wp-config.php
}
_startMysqL() {

systemctl 启动 MysqLd 服务

MysqLadmin -u root password $MysqL_PASSWORD
MysqL -uroot -p$MysqL_PASSWORD -e "CREATE DATABASE wordpress; GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost' IDENTIFIED BY '$wordpress_PASSWORD'; FLUSH PRIVILEGES;"
killall MysqLd
sleep 10
}
__run_supervisor() {
supervisord -n
}

调用所有函数

check
create_user
MysqL_config
__handle_passwords
httpd_perms
start_MysqL
run_supervisor

增加完上面的配置之后,保存并关闭文件

4. 创建配置文件

现在,我们需要创建 Nginx Web 服务器的配置文件,命名为 Nginx-site.conf。

rush:bash;"> # nano Nginx-site.conf

然后,增加下面的配置信息到配置文件

<div class="jb51code">
<pre class="brush:plain;">
user Nginx;
worker_processes 1;
error_log /var/log/Nginx/error.log;

error_log /var/log/Nginx/error.log notice;

error_log /var/log/Nginx/error.log info;

pid /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 0;

keepalive_timeout 65;

gzip on;

index index.html index.htm index.PHP;

Load modular configuration files from the /etc/Nginx/conf.d directory.

See http://Nginx.org/en/docs/ngx_core_module.html#include

for more information.

include /etc/Nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;

charset koi8-r;

access_log logs/host.access.log main;

root /usr/share/Nginx/html;

error_page 404 /404.html;

redirect server error pages to the static page /50x.html

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

proxy the PHP scripts to Apache listening on 127.0.0.1:80

location ~ .PHP$ {

proxy_pass http://127.0.0.1;

}

pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ .PHP$ {
root /usr/share/Nginx/html;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.PHP;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

deny access to .htaccess files,if Apache's document root

concurs with Nginx's one

location ~ /.ht {

deny all;

}

}
}

现在,创建 supervisor.conf 文件添加下面的行。

rush:bash;"> # nano supervisord.conf

然后,添加以下行。

rush:plain;"> [unix_http_server] file=/tmp/supervisor.sock ; (the path to the socket file) [supervisord] logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log) logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) logfile_backups=10 ; (num of main logfile rotation backups;default 10) loglevel=info ; (log level;default info; others: debug,warn,trace) pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid) nodaemon=false ; (start in foreground if true;default false) minfds=1024 ; (min. avail startup file descriptors;default 1024) minprocs=200 ; (min. avail process descriptors;default 200) ; the below section must remain in the config file for RPC ; (supervisorctl/web interface) to work,additional interfaces may be ; added by defining them in separate rpcinterface: sections [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket [program:PHP-fpm] command=/usr/sbin/PHP-fpm -c /etc/PHP/fpm stdout_events_enabled=true stderr_events_enabled=true [program:PHP-fpm-log] command=tail -f /var/log/PHP-fpm/PHP-fpm.log stdout_events_enabled=true stderr_events_enabled=true [program:MysqL] command=/usr/bin/MysqL --basedir=/usr --datadir=/var/lib/MysqL --plugin-dir=/usr/lib/MysqL/plugin --user=MysqL --log-error=/var/log/MysqL/error.log --pid-file=/var/run/MysqLd/MysqLd.pid --socket=/var/run/MysqLd/MysqLd.sock --port=3306 stdout_events_enabled=true stderr_events_enabled=true [program:Nginx] command=/usr/sbin/Nginx stdout_events_enabled=true stderr_events_enabled=true [eventlistener:stdout] command = supervisor_stdout buffer_size = 100 events = PROCESS_LOG result_handler = supervisor_stdout:event_handler

添加完后,保存并关闭文件

5. 构建 wordpress 容器

现在,完成了创建配置文件和脚本之后,我们终于要使用 Dockerfile 来创建安装最新的 wordpress CMS(译者注:Content Management System内容管理系统)所需要的容器,并根据配置文件进行配置。做到这点,我们需要在对应的目录中运行以下命令。

rush:bash;"> # docker build --rm -t wordpress:centos7 .

6. 运行 wordpress 容器

现在,执行以下命令运行新构建的容器,并为 Nginx Web 服务器和 SSH 访问打开88 和 22号相应端口 。

rush:bash;"> # CID=$(docker run -d -p 80:80 wordpress:centos7)

运行以下命令检查进程以及容器内部执行的命令。

<div class="jb51code">
<pre class="brush:bash;">

echo "$(docker logs $CID )"

运行以下命令检查端口映射是否正确。

rush:bash;"> # docker ps

7. Web 界面

最后如果一切正常的话,当我们用浏览器打开 http://ip-address/ 或者 http://mywebsite.com/ 的时候会看到 wordpress 的欢迎界面。

现在,我们将通过 Web 界面为 wordpress 面板设置 wordpress 的配置、用户名和密码。

然后,用上面用户名和密码输入到 wordpress 登录界面。

总结

我们已经成功地在以 CentOS 7 作为 docker OS 的 LEMP 栈上构建并运行了 wordpress CMS。从安全层面来说,在容器中运行 wordpress 对于宿主系统更加安全可靠。这篇文章介绍了在 Docker 容器中运行的 Nginx Web 服务器上使用 wordpress 的完整配置。如果你有任何问题、建议、反馈,请在下面的评论框中写下来,让我们可以改进和更新我们的内容。非常感谢!Enjoy :-)

原文地址:https://www.jb51.cc/php/21411.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐