1. Install Docker
Before we really start, we need to make sure that Docker is installed on our Linux machines. The host we use is CentOS 7, so we use the following command to install Docker using the Yum Manager.
# systemctl Restart Docker.service
2. Create a WordPress Dockerfile
We need to create dockerfile to automate the installation of WordPress and its predecessor requirements. This dockerfile will be used to build a WordPress installation image. This WordPress dockerfile will get CentOS 7 mirroring from the Docker Registry Hub and upgrade the system with the latest available updates. It then installs the necessary software, such as the Nginx Web server, PHP, MARIADB, Open SSH Server, and other components that ensure that the Docker container runs properly. Finally, it performs a script that initializes the WordPress installation.
Then, we need to add the following configuration line to the Dockerfile.
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 p Hp-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 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://wordpre
ss.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 expose CMD ["/bin/bash", "/start.sh"]
3. Create a startup script
After we created the dockerfile, we needed to create a script for running and configuring the WordPress installation, named Start.sh. It creates and configures databases and passwords for WordPress. Open the start.sh with our favorite text editor.
After opening the start.sh, we will add the following configuration line to the file.
#!/bin/bash __check () {if [-f/usr/share/nginx/html/wp-config.php]; then exit fi} __create_user () {# Create user for SSH login ssh_userpass= ' pwgen-c-n-1 8 ' useradd-g wheel user echo User: $SSH _userpass | chpasswd echo SSH user password: $SSH _userpass} __mysql_config () {# Enable and run MySQL Yum-y erase mariadb erver rm-rf/var/lib/mysql//etc/my.cnf yum-y install mariadb mariadb-server mysql_install_db chown-r Mysql:mys Ql/var/lib/mysql/usr/bin/mysqld_safe & Sleep} __handle_passwords () {# Here we generate random passwords (thanks to Pwgen).
The previous two is for the MySQL user, the last random key for wp-config.php.
wordpress_db= "WordPress" mysql_password= ' pwgen-c-n-1 ' wordpress_password= ' pwgen-c-n-1 12 ' # This is the password displayed in the log. echo mysql root password: $MYSQL _password echo wordpress password: $WORDPRESS _password echo $MYSQL _password >/mys Ql-root-pw.txt echo $WORDPRESS _password >/wordpress-db-pw.txt # It turned out to be a long line of SED, cat, pipe and stuff, but thanks to # @djf Ianderhttps://gist.github.com/djfiander/6141138 # Now there's no sed-e ' s/database_name_here/$WORDPRESS _db/s/username_here/$WOR Dpress_db/s/password_here/$WORDPRESS _password//' Auth_key '/s/put your unique phrase here/' pwgen-c-n-1 '//' SEC Ure_auth_key '/s/put your unique phrase here/' pwgen-c-n-1 '//' Logged_in_key '/s/put your unique phrase here/' Pwgen- C-n-1 '//' Nonce_key '/s/put your unique phrase here/' pwgen-c-n-1 '//' Auth_salt '/s/put your unique phrase her e/' pwgen-c-n-1 '//' Secure_auth_salt '/s/put your unique phrase here/' pwgen-c-n-1 '//' Logged_in_salt '/s/put Your unique phrase here/' pwgen-c-n-1 ' Nonce_salt/s/put the unique your phrase ' here/pwgen-c '/'-n-1 e/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 starts mysqld service mysqladmin-u root password $MYSQL _password Mysql-uroot -p$mysql_password-e "CREATE DATABASE WordPress; GRANT all privileges in wordpress.* to ' WordPress ' @ ' localhost ' identified by ' $WORDPRESS _password ';
FLUSH privileges; " Killall mysqld Sleep} __run_supervisor () {supervisord-n} # calls all functions __check __create_user __mysql_c
Onfig __handle_passwords __httpd_perms __start_mysql __run_supervisor
After adding the above configuration, save and close the file.
4. Create a configuration file
Now we need to create the configuration file for the Nginx Web server, named Nginx-site.conf.
Then, add the following configuration information to the configuration file.
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.
# 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 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/ngin
x/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 ' one # #location ~/\.ht {#
Deny all;
#}
}
}
Now, create the supervisor.conf file and add the following line.
Then, add the line to the downlink.
[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) 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; 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_enable D=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/m Ysql--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 [even Tlistener:stdout] Command = supervisor_stdout buffer_size = Events = Process_log Result_handler = supervisor_s
Tdout:event_handler
When you are finished adding, save and close the file.
5. Build WordPress Container
Now that we have finished creating the configuration files and scripts, we are finally using dockerfile to create the containers that are needed to install the latest WordPress CMS (translator note Management system, Content management systems) and configure them according to the configuration file. To do this, we need to run the following command in the corresponding directory.
# Docker build--rm-t Wordpress:centos7.
6. Run WordPress Container
Now run the newly built container with the following command, and open the corresponding port 88 and 22nd for the Nginx Web server and SSH access.
# cid=$ (Docker run-d-P 80:80 wordpress:centos7)
Run the following command to check the process and the commands that are executed inside the container.
# echo "$ (Docker logs $CID)"
Run the following command to check that the port mapping is correct.
7. Web interface
Finally, if all is normal, when we use the browser to open http://ip-address/or http://mywebsite.com/will see the WordPress welcome interface.
Now, we will set the WordPress configuration, username and password for the WordPress panel through the Web interface.
Then, enter the WordPress login interface with the username and password above.
Summarize
We have successfully built and run the WordPress CMS on the LEMP stack of CentOS 7 as Docker OS. From a security perspective, running WordPress in a container is more secure and reliable for the host system. This article describes the full configuration of WordPress on the Nginx Web server running in the Docker container. If you have any questions, suggestions, feedback, please write them down in the comments box below so that we can improve and update our content. Thank you so much! Enjoy:-)