Create a complete WordPress site in Docker

Source: Internet
Author: User
Tags ssh access ssh server docker ps docker run docker registry
This article mainly introduces how to set up a complete WordPress site in Docker. Docker is the most popular virtual machine technology. if you need it, refer to Docker

1. install Docker

Before we start, we need to ensure that Docker has been installed on our Linux machine. The host we use is CentOS 7, so we use the following command to install docker using the yum manager.

  # yum install docker

    # systemctl restart docker.service

2. create Dockerfile for WordPress

We need to create a Dockerfile for automatically installing wordpress and its pre-requirements. This Dockerfile will be used to build the WordPress installation image. This WordPress Dockerfile will get the CentOS 7 image from the Docker Registry Hub and upgrade the system with the latest available updates. It then installs necessary software, such as the Nginx Web server, PHP, MariaDB, and Open SSH server, and other components necessary to ensure the normal operation of Docker containers. At last, it executes a script to initialize WordPress installation.

  # nano Dockerfile

Then, we need to add the following configuration lines to the Dockerfile.

 FROM centos:centos7  MAINTAINER The CentOS Project 
 
    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. create a startup script

After Dockerfile is created, we need to create a script for running and configuring WordPress installation, named start. sh. It creates and configures the database and password for WordPress. Open start. sh in our favorite text editor.

  # nano start.sh

After start. sh is enabled, 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 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-server rm-rf/var/lib/mysql // etc/my. cnf yum-y install mariadb-server mysql_insta Ll_db chown-R mysql: mysql/var/lib/mysql/usr/bin/mysqld_safe & sleep 10 }__ handle_passwords () {# Here we generate a random password (thanks to pwgen ). The first two random keys for the mysql user and the last for the wp-config.php. WORDPRESS_DB = "wordpress" MYSQL_PASSWORD = 'pwgen-c-n-1 12 '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>/mysql-root-pw.txt echo $ WORDPRESS_PASSWORD>/wordpress-db-pw.txt # it turns out to be a long line including sed, cat, pipe, and stuff, but thanks to the # @ 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} _ 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

After adding the above configuration, save and close the file.
4. create a configuration file

Now we need to create a configuration file for the Nginx Web server named nginx-site.conf.

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

Create the supervisor. conf file and add the following lines.

  # nano supervisord.conf

Then, add the following lines.

  [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

After adding the file, save and close the file.
5. build a WordPress container

Now, after creating the configuration file and script, we finally need to use Dockerfile to create the container required to install the latest WordPress CMS (Content Management System, configure 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 the WordPress container

Now, run the following command to run the newly built container and open ports 88 and 22 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 executed inside the container.

 # echo "$(docker logs $CID )"

Run the following command to check whether the port ING is correct.

  # docker ps

7. Web interface

Finally, if everything works, we will see the welcome page of WordPress when we open http: // ip-address/or http://mywebsite.com/in a browser.

Now, we will set the WordPress configuration, user name, and password for the WordPress panel on the Web interface.

Then, enter the above user name and password to the WordPress logon interface.

Summary

We have successfully built and run WordPress CMS on CentOS 7 as the LEMP stack of docker OS. In terms of security, running WordPress in a container is more secure and reliable for the host system. This article describes the complete configuration of WordPress on the Nginx Web server running in the Docker container. If you have any questions, suggestions, or feedback, please write them down in the comment box below so that we can improve and update our content. Thank you very much! Enjoy :-)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.