Setting up WordPress with Caddy on Ubuntu

Setting up WordPress with Caddy on Ubuntu

This guide is a mix between an Ubuntu tutorial and gist with some fine tune elements that worked for me.

Prerequisites

  • Have a ubuntu server up and running, maybe other OS go smooth, but I haven’t tested them
  • Have a domain correctly mapped to that server
  • Have caddy (v2) working all ready, you cleaned your apache or you nginx, easy apt install caddy after cleaning.

Download

Download WordPress from source and manage permissions on the install dir.

sudo mkdir -p /srv/www
sudo chown www-data: /srv/www
curl https://wordpress.org/latest.tar.gz | sudo -u www-data tar zx -C /srv/www

Install deps

Notice Im not using specif versions, like php-fmp@7.4, not, im getting things off the apt shells, they know what they are doing, this way I get a more future resilient script (hopefully).

sudo apt install php-fpm \
    ghostscript \
    libapache2-mod-php \
    mysql-server \
    php \
    php-bcmath \
    php-curl \
    php-imagick \
    php-intl \
    php-json \
    php-mbstring \
    php-mysql \
    php-xml \
    php-zip

DB time

Using MYSQL
Let’s generate a strong password

openssl rand -base64 20

Now let’s go sql

mysql -u root
mysql> CREATE DATABASE wordpress;
mysql> CREATE USER wordpress@localhost IDENTIFIED BY '<your-password>';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON wordpress.* TO wordpress@localhost;
mysql> FLUSH PRIVILEGES;
mysql> quit

Enable MySQL with

sudo service mysql start

At this point you should have a note with credentials:

DB_NAME=wordpress
USER_NAME=wordpress
USER_PASSWORD=<your-password>

Create the db user and stuff

CREATE USER wordpress@localhost IDENTIFIED BY '<your-password>';
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON wordpress.* TO wordpress@localhost;

Configure WordPress with that user

sudo -u www-data sed -i 's/database_name_here/wordpress/' /srv/www/wordpress/wp-config.php
sudo -u www-data sed -i 's/username_here/wordpress/' /srv/www/wordpress/wp-config.php
sudo -u www-data sed -i 's/password_here/<your-password>/' /srv/www/wordpress/wp-config.php

Replace weak lines with strong lines

In the wp-config.php replace the lines:

define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
define( 'NONCE_SALT',       'put your unique phrase here' );

with the ones you found in the strong lines factory: https://api.wordpress.org/secret-key/1.1/salt/

Caddy Config

your.domain {
    # good practice to signal on behalf of who 
    # are the certs getting issue
	tls your@email.com

    # logs are optional
	log {
		output file /var/log/caddy/your.domain
		format console
	}

	root * /srv/www/wordpress
	encode gzip
	file_server
	php_fastcgi unix//run/php/php-fpm.sock

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

	rewrite @disallowed '/index.php'
}

Apply the latter with systemctl restart caddy, should go smooth, at least you have other bad things in your file.

Finish it on the browser

Go to your domain, at this point you should have a working wordpress site, on SSL,
if fails to load, give it a couple of minutes, those SSL certs aren’t going to issue
and process it selfs, leave Caddy do his thing. When things clear up, finish the setup
on your new WordPress portal.

Hopefully not, but if things go wrong, check out the log, that’s why I put it there:

tail -n 10 -f /var/log/caddy/your.domain

hit refresh and see what happens, good luck.

Allow WordPress access file system for plugins and friends

Nice, you have WordPress up and running, now you need the next thing, wish might be
installing plugins, with no FTP please, this stackoverflow-thread talks about it.

You can do it by allowing access to www-data on the wp-content dir for now.

sudo chown -R www-data:www-data /srv/www/wordpress/wp-content/

Good Job

confeti.jpg

Take that coffee/tea/your-beverage-of-choice, you earned.

1 Like

Hi Dago!

Suggestion: Please update the section “Replace weak lines with strong lines” by removing the secrets and point users to the official API:
https://api.wordpress.org/secret-key/1.1/salt/
where they can fetch unique strong secrets.

2 Likes

OOo thanks man, yikes, so dummy me :joy::joy::joy:

Hi @Dago. Nice tutorial. I think you forgot to add try_files to rewrite into pretty URLs.

That’s not necessary because the php_fastcgi directive has built-in try_files.

2 Likes

That’s nice, we keep learning forward about Caddy. To be honest @Dante this is the first time I install Wordpress, and what great idea to don’t forget how, than to than write about it, in the best place you could do it. As @francislavoie noticed, those pretty urls are getting handle all ready, but where can i learn more about it and how too use it.

The docs explain how this all works:

Thanks @francislavoie