Skip to content

Instantly share code, notes, and snippets.

@mage1k99
Last active July 18, 2023 04:21
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mage1k99/07ce2b09d47920bf5f94a69f51ddeb30 to your computer and use it in GitHub Desktop.
Save mage1k99/07ce2b09d47920bf5f94a69f51ddeb30 to your computer and use it in GitHub Desktop.
How to install WordPress with caddy2

How to Install WordPress in Caddy 2

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


Note!

This one is outdated: please check the offical guide here

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 relaod 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.
@wodim
Copy link

wodim commented Jul 15, 2023

I may be wrong but this configuration (file_server) seems to allow everybody to download all your php files, especially wp-config.php

@mage1k99
Copy link
Author

mage1k99 commented Jul 18, 2023

Yes file_server will serve all the files inside the root directory. I'll update this @wodim This one is outdated. we can use this to do the job https://caddyserver.com/docs/caddyfile/directives/php_fastcgi#expanded-form

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment