How to run or host WordPress with Caddy2 server

PHP Version       : 8.0
WordPress version : 5.7.2
Caddy version     : v2.4.3

Installing Dependencies

To install WordPress, you must required to have PHP and MySQL or mariaDB

Installing PHP

This is for Ubuntu/Debian Distro.

  • sudo apt update
  • sudo apt install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
  • If the above command returns package not found or similar error do this step else go to next step, then you need to add the PPA, you can add them by sudo add-apt-repository ppa:ondrej/php;sudo apt update and now repeat the above step
  • Now remove the apache2 server which is installed when installing PHP, by sudo apt purge apache2*
  • Check the PHP Version by running php -v

Installing MYSQL Database

If you don't have MYSQL installed refer this blog by digitalocean

Downloading Wordpress

  • Navigate to the required directory, where do you want to save wordpress. here I use /home/illuminate/php_sites
  • Download the latest Wordpress by wget https://wordpress.org/latest.tar.gz
  • extract the tar file by tar -xzvf latest.tar.gz
  • Delete the tar file after extraction completed
  • go to the wordpress/ by cd wordpress

Creating DB for Wordpress

  • If you have successfully installed mysql, create a seperate user and db for wordpress
  • Login to mysql by mysql -u adminusername -p and now Enter the password
  • replace the databasename with the database name you want.
CREATE DATABASE databasename;
use databasename;
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON *.* TO 'username'@'localhost';
flush privileges;
  • After the above steps successful, exit the mysql by pressing ctrl+d

Editing the Caddyfile

  • Now add the entry to serve Wordpress
example.com {
    root * /home/illuminate/php_sites/wordpress
    php_fastcgi unix//run/php/php8.0-fpm.sock
    file_server
    encode gzip

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

    rewrite @disallowed '/index.php'
}
  • Now reload the caddy server.
  • and its done.

Configuring Wordpress

  • When you visit the domain configured in caddyfile, it will ask to configure if not configured.
  • Enter the database credentials as we created.
  • If it fails to write wp-config.php file, then copy the configuration from web
  • create a wp-config.php file inside the directory where wp-content, wp-admin, wp-content are present, for me its /home/illuminate/php_sites/wordpress
  • paste the contents copied into the file, save and begin the installation.

36