{"id":648,"date":"2019-12-24T16:20:51","date_gmt":"2019-12-25T00:20:51","guid":{"rendered":"http:\/\/209.126.2.187\/?p=648"},"modified":"2024-09-30T15:53:55","modified_gmt":"2024-09-30T22:53:55","slug":"containerized-wordpress-migration-and-ssl","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/12\/24\/containerized-wordpress-migration-and-ssl\/","title":{"rendered":"Containerized WordPress, Migration, and SSL"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>The world witnessed the popularity of containerized applications. Docker provides good isolation and is of good portability. It also could be the basic item in a distributed system. In this post, I introduced<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>how to create a WordPress site in docker;<\/li><li>how to migration WordPress;<\/li><li>how to make the site secure via SSL;<\/li><li>how to make database connection secure;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The official docker image of <a href=\"https:\/\/hub.docker.com\/_\/wordpress\">WordPress<\/a> provides a good end-to-end solution. If you get an external database, <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">docker run --name tech-blog -p 80:80 -p 443:443 -v ${PWD}\/wp-data:\/var\/www\/html -d wordpress<\/code><\/pre>\n\n\n\n<p>Visit <code>http:\/\/SERVER-ADDRESS<\/code> to install a fresh WordPress instance. If you are using  <a href=\"https:\/\/wordpress.org\/plugins\/duplicator\/\">Duplicator<\/a>, copy the two files into the mounted folder on the host and visit <code>http:\/\/DOCKER_ADDRESS\/installer.php<\/code>. <\/p>\n\n\n\n<p>The installer will let you configure the credentials of your external database, extract the data (posts, categories, etc), and load them into the new database. You can do the same thing using <code>mysqldump<\/code> directly. The installer will also help you restore site data not in database, like plugins, themes, etc. <\/p>\n\n\n\n<p>If you want to deploy a MySQL instance as well.  Keep reading.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a LAMP Container<\/h3>\n\n\n\n<p>WordPress lies on the LAMP stack: Linux, Apache, MySQL, PHP. We are lucky that there is a good open-sourced <a href=\"https:\/\/hub.docker.com\/r\/mattrayner\/lamp\">image<\/a> available.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">sudo docker run -it \\ \n-p \"80:80\" -p \"443:443\" -p \"3306:3306\" \\\n-v ${PWD}\/var\/www\/html:\/var\/www\/html \\\n-v ${PWD}\/mysql:\/var\/lib\/mysql \\\n--name tech-blog-container mattrayner\/lamp:latest<\/code><\/pre>\n\n\n\n<p>It is a common practice that not store data in containers. Thus, the CMD script in this image will create the corresponding folders. <code>app<\/code> will be your WordPress folder. <code>mysql<\/code> will store the persisted data of your database. You can also use docker Volumn and NFS from cloud providers to increase the availability and reliability of your data.<\/p>\n\n\n\n<p>You will get a random password for the user <code>admin<\/code>. Modify it by <code>phpmyadmin<\/code>. Visit the site <code>http:\/\/DOCKER_ADDRESS\/phpmyadmin<\/code> using the initial password and follow this <a href=\"https:\/\/synoguide.com\/2014\/02\/20\/change-password-sql-database-phpmyadmin\/\">post<\/a> to modify password.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a WordPress Database<\/h3>\n\n\n\n<p>As we know, WordPress uses the relational database. We need to provide an accessible database and user. Use <code>phpmyadmin<\/code> to create a table and edit the privilege of the user <code>admin<\/code> to grant all operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a Fresh WordPress or Migrate from Old Sites<\/h3>\n\n\n\n<p>We have two choices here.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create a Fresh WordPress<\/h4>\n\n\n\n<p>Download the <a href=\"https:\/\/wordpress.org\/download\/\">compressed file<\/a>. Unzip them into the <code>app<\/code> folder. Visit <code>http:\/\/DOCKER_ADDRESS\/<\/code>. Then follow the instruction of WordPress to finish the installation.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Migrate from Old Sites<\/h4>\n\n\n\n<p>Use the great plugin <a href=\"https:\/\/wordpress.org\/plugins\/duplicator\/\">Duplicator<\/a> to firstly create a package and an installing script. Then copy these two files into <code>app<\/code>. Visit <code>http:\/\/DOCKER_ADDRESS\/installer.php<\/code> and follow the instruction of duplication.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Install Certificates and Enable SSL<\/h3>\n\n\n\n<p>I have a previous <a href=\"http:\/\/161.97.122.139\/index.php\/2019\/03\/17\/install-ssl-certificate-and-change-to-https\/\">post<\/a> for CentOS and Apache 1. There are some changes on Ubuntu and Apache 2.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Apply for an SSL certificate. I applied a free personal SSL certificate from <a href=\"https:\/\/intl.cloud.tencent.com\/\">Tencent Cloud<\/a>.<\/li><li>Download the certificate and sent it to the server; copy the certificates into the container;<\/li><li>Attach to the container, enable SSL <code>a2enmod ssl<\/code>;<\/li><li>Modify the default configuration (<code>\/etc\/apache2\/sites-enabled\/000-default.conf<\/code>) and add the <code>443<\/code> virtual host (remove <code>80<\/code> only after <code>443<\/code> works);<\/li><li>Restart Apache2 <code>service apache2 restart<\/code>;<\/li><li>Finally use the handy plugin <a href=\"https:\/\/wordpress.org\/plugins\/really-simple-ssl\/\">Really Simple SSL<\/a> to enable encryption of your site.<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">&lt;VirtualHost *:443>\n        ServerAdmin webmaster@localhost\n        DocumentRoot \/var\/www\/html\n        SSLCertificateFile \/path\/to\/2_www.domain-name.com.crt\n        SSLCertificateKeyFile \/path\/to\/3_www.domain-name.com.key\n        SSLCertificateChainFile \/path\/to\/1_root_bundle.crt\n&lt;\/VirtualHost><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Secure DB Connection<\/h3>\n\n\n\n<p>If your database enables TLS\/SSL, it is better to let the php MySQL client use TLS as well. This is done by modifying the <code>wp-config.php<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"># add this line in front of your db configurations\ndefine('MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p><a href=\"https:\/\/falkus.co\/2018\/12\/wordpress-docker-image-with-mysql-client-ssl\/\">A post related to secure DB connection<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.digicert.com\/kb\/csr-ssl-installation\/ubuntu-server-with-apache2-openssl.htm#create_csr_openssl\">Install SSL on Apache2<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/hub.docker.com\/_\/wordpress\/\">WordPress Docker Image<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary The world witnessed the popularity of containerized applications. Docker provides good isolation and is of good portability. It also could be the basic item in a distributed system. In this post, I introduced how to create a WordPress site in docker; how to migration WordPress; how to make the site secure via SSL; how&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,22,45,13,71],"tags":[],"class_list":["post-648","post","type-post","status-publish","format-standard","hentry","category-linux","category-operation-systems","category-useful-tools","category-web","category-wordpress"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=648"}],"version-history":[{"count":18,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/648\/revisions"}],"predecessor-version":[{"id":1672,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/648\/revisions\/1672"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}