How to Run a WordPress Website on Caddy 2

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn how to host a WordPress website on a Caddy 2 web server. Instructions are specifically for Ubuntu.

1. Install Caddy

To install Caddy, check out the official documentation, or watch my video below for help installing on Ubuntu.

YouTube video

2. Install WordPress

Download and install WordPress with the appropriate permission with the following commands.

cd /var/www
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
chown -R www-data:www-data /var/www/wordpress
find wordpress/ -type d -exec chmod 755 {} \;
find wordpress/ -type f -exec chmod 644 {} \;

3. Install PHP

Install PHP and then remove the Apache dependencies that we don’t need.

apt install php php-mysql
apt purge apache2*

Check your version of PHP with the following. You will need it later for your Caddyfile.

php -v

Install additional PHP packages that WordPress needs.

apt install php-mysqlnd php-gd php-json php-mbstring php-xml php-imagick php-intl php-dom php-curl php-zip

4. Install MySQL

Install MariaDB which is a MySQL database that WordPress will use.

apt install mariadb-server
mysql_secure_installation

Go into the MySQL command prompt.

mysql

From the MySQL command prompt, create a database and database user for WordPress.

create database example_db default character set utf8 collate utf8_unicode_ci; 
create user 'example_user'@'localhost' identified by 'example_pw';
grant all privileges on example_db.* TO 'example_user'@'localhost';
flush privileges;
exit

5. Configure Caddy for WordPress

Edit your Caddyfile at /etc/caddy/Caddyfile like the following. Make sure to replace the domain name with your own and also use the version of PHP that you installed above.

tonyboni.com, www.tonyboni.com {
    root * /var/www/wordpress
    php_fastcgi unix//run/php/php7.4-fpm.sock
    file_server
    encode gzip

    @disallowed {
        path /xmlrpc.php
        path *.sql
        path /wp-content/uploads/*.php
    }

    rewrite @disallowed '/index.php'
}

Apply your changes with the following command.

systemctl restart caddy

6. Setup WordPress

Navigate to your domain name in a web browser and complete the WordPress installation which includes linking your database and providing information about your website.

You now have a WordPress website running on a Caddy server!

YouTube video


Meet Tony

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

Leave a Comment