{"id":139,"date":"2020-06-12T11:17:48","date_gmt":"2020-06-12T18:17:48","guid":{"rendered":"https:\/\/anilot.tk\/blog\/?p=139"},"modified":"2020-06-12T20:25:48","modified_gmt":"2020-06-13T03:25:48","slug":"how-to-set-up-wordpress-with-caddy-2","status":"publish","type":"post","link":"https:\/\/anilot.net\/blog\/index.php\/2020\/06\/12\/how-to-set-up-wordpress-with-caddy-2\/","title":{"rendered":"How to set up WordPress with Caddy 2"},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>5 Minute, 5 Second                <\/div>\n\n            <\/div>\n<p class=\"wp-block-paragraph\">This step-by-step guide will show you how to set up a WordPress blog based on the Caddy 2 web server, on Ubuntu 20.04 LTS. In this guide, WordPress will be put inside the \/wordpress folder of the website root.<\/p>\n\n\n\n<p class=\"has-drop-cap wp-block-paragraph\"><a rel=\"noreferrer noopener\" href=\"https:\/\/caddyserver.com\/\" target=\"_blank\">Caddy <\/a>is a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go.  Caddy has relatively limited adoption vs. other major platform such as Apache and Nginx, but is gaining share due to its simplicity of use yet still very powerful. It was recently updated to <a rel=\"noreferrer noopener\" href=\"https:\/\/caddyserver.com\/v2\" target=\"_blank\">Caddy 2<\/a>, but the documentation available online about the various configurations  using Caddy 2 is very limited. There are actually quite a few tricks in order to correctly configure Caddy v2 to work with WordPress. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Get the Prerequisites for WordPress<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First up, let&#8217;s install the necessary backbone packages for WordPress.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.1  Install PHP <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Since WordPress relies on PHP, we will need to install the required php packages on Ubuntu. You can do so by running the following commands<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo apt -y install php-fpm php-mysql php-curl php-gd php-mbstring php-common php-xml php-xmlrpc<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 Install MariaDB<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We will use MariaDB instead of MySQL. MySQL has played a key role in building the internet as we know it today. However, MariaDB has gained lots of traction due to its full compatibility with MySQL, superior out-of-box performance, and being fully open sourced. To install MariaDB, take the following steps.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo apt-get install software-properties-common\n$ sudo apt-get install software-properties-common\n$ sudo apt-key adv --recv-keys --keyserver hkp:\/\/keyserver.ubuntu.com:80 0xF1656F24C74CD1D8\n$ sudo add-apt-repository \"deb [arch=amd64,arm64,ppc64el] http:\/\/mariadb.mirror.liquidtelecom.com\/repo\/10.4\/ubuntu $(lsb_release -cs) main\"\n$ sudo apt update\n$ sudo apt -y install mariadb-server mariadb-client\n$ sudo mysql_secure_installation<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can verify if the installation is successful by running <code>$ mysql -u root -p<\/code> and log in as root using the password created during setup, then run<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\"SELECT VERSION();\"<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.3 Create WordPress database and user<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The next step is to create a database (&#8216;wp_website&#8217; in the below example), and a user  (&#8216;wp_username&#8217; in the below example) for WordPress to use. First get into the SQL console,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ mysql -u root -p<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then run the following quries<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MariaDB [(none)]> CREATE DATABASE wp_website;\nQuery OK, 1 row affected (0.001 sec)\n\nMariaDB [(none)]> GRANT ALL PRIVILEGES ON wp_website.* to 'wp_username'@'localhost' IDENTIFIED BY 'type_user_password_here';\nQuery OK, 0 rows affected (0.001 sec)\n\nMariaDB [(none)]> FLUSH PRIVILEGES;\nQuery OK, 0 rows affected (0.001 sec)\n\nMariaDB [(none)]> quit<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Install Caddy 2 Web server<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you have not installed Caddy on your server, you can follow this <a href=\"https:\/\/anilot.tk\/blog\/index.php\/2020\/06\/10\/install-caddy-2-web-server-on-ubuntu-20-04-guide\/\">Caddy 2 web server installation guide<\/a> to get Caddy 2 installed. When following the above guide, there is no need to worry about the Caddyfile, because I will show you how to configure the right Caddyfile for WordPress in Section 4 below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Download and Install WordPress<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With all the prerequisite steps done, we can now download and install WordPress. To resolve any potential WordPress write\/read issue, make sure the caddy user owns the html folder. In the below example, WordPress is copied to its own sub-folder \/WordPress from the site root.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ wget http:\/\/wordpress.org\/latest.tar.gz\n$ tar xvf latest.tar.gz\n$ sudo cp -R wordpress \/var\/www\/html\/\n$ sudo chown -R caddy:caddy \/var\/www\/html\n$  sudo mv \/var\/www\/html\/wordpress\/wp-config-sample.php \/var\/www\/html\/wordpress\/wp-config.php<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then update the wp-config.php with the WordPress database information created in Section 1.3.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Configure Caddyfile for WordPress<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now it&#8217;s the interesting part. Let&#8217;s edit <code>Caddyfile<\/code> which is usually located under <code>\/etc\/caddy\/<\/code>. Assuming you followed the <a href=\"https:\/\/anilot.tk\/blog\/index.php\/2020\/06\/10\/install-caddy-2-web-server-on-ubuntu-20-04-guide\/\">Caddy installation guide<\/a>, and the WordPressis installed to \/var\/www\/html\/wordpress.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">domain.com {\n    encode zstd gzip\n    root * \/var\/www\/html    \n\n    ####### WordPress ####### \n    redir \/wordpress \/wordpress\/\n\n    # Change to the right version, then change php-fpm www.config with caddy being the owner and listengroup following step 4\n    php_fastcgi unix\/\/run\/php\/php7.4-fpm.sock  \n \n    # Prevent malicious php uploads\n    @uploads {\n        path_regexp uploadpath \/uploads\\\/(.*)\\.php\n    }\n    rewrite @uploads \/\n\n   # Not really needed\n    #@wp-admin {\n    #    path not ^\\\/wp-admin\/*\n    #}    \n     \n    # Enable the static file server.\n    file_server <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, reload the Caddyfile to apply the changes.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo systemctl reload caddy<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5 Configure PHP-FPM to work with Caddy 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To ensure WordPress to have the correct write permission after being served by Caddy, we need to update the PHP-FPM config. Otherwise, WordPress might throw out &#8220;error occurred while updating&#8221; during theme update, or require FTP access to install any themes or plugins. If you already know the path of the PHP-FPM pool config, you can skip to Section 5.2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.1  Figure out the installation path of the running instance of PHP-FPM<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, figure out which path the FPM is running off of. To do this, create a <code>phpinfo.php<\/code> file and put it to the web root of your caddy server with the following content.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php \n    phpinfo(); \n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Visit this page (e.g. <code>https:\/\/yourdomain.com\/phpinfo.php<\/code>), and it should show the php configurations on the server. Note  the <em><code>Configuration File (php.ini) Path<\/code><\/em> text. It should look something like this: <code>\/etc\/php\/7.4\/fpm<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"481\" src=\"https:\/\/anilot.tk\/blog\/wp-content\/uploads\/2020\/06\/phpinfo-1024x481.png\" alt=\"\" class=\"wp-image-172\" srcset=\"https:\/\/anilot.net\/blog\/wp-content\/uploads\/2020\/06\/phpinfo-1024x481.png 1024w, https:\/\/anilot.net\/blog\/wp-content\/uploads\/2020\/06\/phpinfo-300x141.png 300w, https:\/\/anilot.net\/blog\/wp-content\/uploads\/2020\/06\/phpinfo-768x361.png 768w, https:\/\/anilot.net\/blog\/wp-content\/uploads\/2020\/06\/phpinfo.png 1413w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Don&#8217;t forget to remove the <code>phpinfo.php<\/code> after you are done, since it contains sensitive information about the web server. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 Modify the FPM Pool Config<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the FPM Pool config file, change the <code>listen.group<\/code>, <code>listen.user<\/code>, <code>user<\/code> and <code>group<\/code> to <code>caddy<\/code>, or the username created to run the Caddy server. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo vim \/etc\/php\/7.4\/fpm\/pool.d\/www.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then restart PHP-FPM service to apply the changes<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo service php7.4-fpm restart<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. Check if WordPress is Working with Caddy 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In browser, go to https:\/\/yourdomain.com\/wordpress and WordPress should be running. If not, take a look at the Common Issues sections below or leave a comment below. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Cheers!<\/p>\n        <div class=\"booster-block booster-reactions-block\">\n            <div class=\"twp-reactions-icons\">\n                \n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-1\" post-id=\"139\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/anilot.net\/blog\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/happy.svg\" alt=\"Happy\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Happy                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                        \n                                                <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-2\" post-id=\"139\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/anilot.net\/blog\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/sad.svg\" alt=\"Sad\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Sad                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-3\" post-id=\"139\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/anilot.net\/blog\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/excited.svg\" alt=\"Excited\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Excited                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-6\" post-id=\"139\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/anilot.net\/blog\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/sleepy.svg\" alt=\"Sleepy\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Sleepy                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                        \n                                                <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-4\" post-id=\"139\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/anilot.net\/blog\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/angry.svg\" alt=\"Angry\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">Angry<\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                        \n                    <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-5\" post-id=\"139\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/anilot.net\/blog\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/surprise.svg\" alt=\"Surprise\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">Surprise<\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n            <\/div>\n        <\/div>\n\n    ","protected":false},"excerpt":{"rendered":"<p>This step-by-step guide will show you how to set up a WordPress blog based on the Caddy 2 web server, on Ubuntu 20.04 LTS. In&hellip;<\/p>\n","protected":false},"author":1,"featured_media":141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ai_generated_summary":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[7],"tags":[11,12,13],"class_list":["post-139","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","tag-caddy","tag-web-server","tag-wordpress","has-post-thumbnail-archive"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/anilot.net\/blog\/wp-content\/uploads\/2020\/06\/WordPress_blue_logo.svg-e1591941485586.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/posts\/139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=139"}],"version-history":[{"count":13,"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/posts\/139\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/posts\/139\/revisions\/183"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/media\/141"}],"wp:attachment":[{"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anilot.net\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}