如何在Docker容器中使用Nginx安装WordPress

时间:2020-03-05 15:28:18  来源:igfitidea点击:

Docker是一个开源项目,它提供了一个开放平台,可作为轻量级容器打包,运送和运行任何应用程序。

它不受语言支持,框架或者打包系统的限制,并且可以从小型家用计算机到高端服务器的任何位置,任何时间运行。
它使它们成为部署和扩展Web应用程序,数据库和后端服务的重要构建块,而无需依赖特定的堆栈或者提供程序。

今天,我们将部署带有最新WordPress软件包的Docker容器,并带有必要的准备工作,例如Nginx Web Server,PHP5,MariaDB Server等。
这是一些简短而精巧的步骤,可以在Docker容器中成功安装运行Nginx的WordPress。

1.安装Docker

在真正开始之前,我们需要确保我们在Linux机器中安装了Docker。
其中我们将CentOS 7作为主机运行,因此,我们将运行yum manager使用以下命令安装docker。

yum install docker

systemctl restart docker.service

2.创建WordPress Dockerfile

我们需要创建一个Dockerfile,它将自动安装wordpress及其必要的准备工作。
该Dockerfile将用于构建我们创建的WordPress安装的镜像。
这个WordPress Dockerfile从Docker Registry Hub获取CentOS 7镜像,并使用最新的可用软件包更新系统。
然后安装必要的软件,例如Nginx Web服务器,PHP,MariaDB,Open SSH Server等,这些软件对于Docker容器正常工作必不可少。
然后,它执行一个脚本,该脚本将立即启动WordPress的安装。

# nano Dockerfile

然后,我们需要在该Dockerfile中添加以下配置行。

FROM centos:centos7
MAINTAINER The CentOS Project <[email protected]>
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之后,我们需要创建一个名为start.sh的脚本,该脚本将运行并配置WordPress安装。
它将创建并配置数据库,wordpress的密码。
要创建它,我们需要使用我们最喜欢的文本编辑器打开start.sh。

# nano start.sh

打开start.sh之后,我们需要其中添加以下配置行。

#!/bin/bash
__check() {
if [ -f /usr/share/nginx/html/wp-config.php ]; then
exit
fi
}
__create_user() {
# Create a user to SSH into as.
SSH_USERPASS=`pwgen -c -n -1 8`
useradd -G wheel user
echo user:$SSH_USERPASS | chpasswd
echo ssh user password: $SSH_USERPASS
}
__mysql_config() {
# Hack to get MySQL up and running... I need to look into it more.
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() {
# Here we generate random passwords (thank you pwgen!). The first two are for mysql users, the last batch for random keys in wp-config.php
WORDPRESS_DB="wordpress"
MYSQL_PASSWORD=`pwgen -c -n -1 12`
WORDPRESS_PASSWORD=`pwgen -c -n -1 12`
# This is so the passwords show up in logs.
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
# There used to be a huge ugly line of sed and cat and pipe and stuff below,
# but thanks to @djfiander's thing at https://gist.github.com/djfiander/6141138
# there isn't now.
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
}
__start_mysql() {
# systemctl start mysqld.service
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
}
# Call all functions
__check
__create_user
__mysql_config
__handle_passwords
__httpd_perms
__start_mysql
__run_supervisor

添加以上配置后,我们需要将其保存然后退出。

4.创建配置文件

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

# nano nginx-site.conf

然后,我们将以下配置添加到配置文件中。

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;
#}
}
}

现在,我们将创建supervisord.conf文件,并添加以下几行,如下所示。

# nano supervisord.conf

然后,添加以下行。

[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 Jan 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。
为此,我们将在该目录中运行以下命令。

# docker build --rm -t wordpress:centos7 .

6.运行WordPress容器

现在,要运行我们新建的容器并分别为Nginx Web服务器和SSH访问打开端口80和22,我们将运行以下命令。

# CID=$(docker run -d -p 80:80 wordpress:centos7)

为了检查容器内执行的过程和命令,我们将运行以下命令。

#  echo "$(docker logs $CID )"

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

# docker ps

7. Web界面

最后,如果一切顺利,当将浏览器指向http://ip-address /或者http://mywebsite.com/时,WordPress将受到我们的欢迎。

现在,我们将逐步浏览Web界面,并为WordPress面板设置wordpress配置,用户名和密码。

然后,使用上面在WordPress登录页面中输入的用户名和密码。