This website currently uses a WordPress template and Caddy as its web server running on Debian 12, and I will post a blog about how I did that. 🙂

Prerequisites

  • You need to have a server and domain, which is obvious.
  • Install the latest Caddy service.
  • Make sure your Caddy services running successfully. (It’s not difficult, and I am sure you can do it by yourself)

Procedures

1. First install dependencies like PHP, WordPress requires PHP to function:

sudo apt install php php-fpm php-mysql php-xml php-gd php-mbstring php-curl

2. You need a database to store WordPress data, choose whatever database you want, I am using MariaDB for this website, and here are the install procedures:

sudo apt update
sudo apt install mariadb-server

After have MariaDB on your server, you need to set it up first:

sudo mysql_secure_installation

Now, I believe you have set your database up, have you recalled your SQL commands? Cause we are gonna use it! First log in to the MariaDB shell, and make sure you keep your database password in a safe place.

sudo mysql -u root -p

Now you have accessed the MariaDB shell, it’s time to create a new database for our website!

CREATE DATABASE wordpress_db DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Create a new MariaDB user and grant privileges to this user on the WordPress database.

GRANT ALL ON wordpress_db.* TO 'wordpress_user'@'localhost' IDENTIFIED BY 'password';

Flush the privileges to ensure that the current instance of MariaDB knows about the recent permission changes and then you can exit the database shell:

FLUSH PRIVILEGES;
EXIT;

3. We have done so much for WordPress, but where is it? Let’s install WordPress right now!

# Change to the directory where you want to install WordPress
cd /var/www

# Download WordPress
sudo wget https://wordpress.org/latest.tar.gz

# Extract WordPress
sudo tar xzf latest.tar.gz

# Change the ownership to your web server user
sudo chown -R www-data:www-data wordpress

# Remove the tar file
sudo rm latest.tar.gz

You may wonder, why a lot of tutorials suggest you to choose /var/www as the location to host your web? I also had this question before, and here is the answer:

There is this thing called the Linux Filesystem Hierarchy where the layout of Linux systems is described.
1.18. /var
Contains variable data like system logging files, mail and printer spool directories, and transient and temporary files. Some portions of /var are not shareable between different systems. For instance, /var/log, /var/lock, and /var/run. Other portions may be shared, notably /var/mail, /var/cache/man, /var/cache/fonts, and /var/spool/news. Why not put it into /usr? Because there might be circumstances when you may want to mount /usr as read-only, e.g. if it is on a CD or on another computer. ‘/var’ contains variable data, i.e. files and directories the system must be able to write to during operation, whereas /usr should only contain static data. Some directories can be put onto separate partitions or systems, e.g. for easier backups, due to network topology or security concerns. Other directories have to be on the root partition, because they are vital for the boot process. ‘Mountable’ directories are: ‘/home’, ‘/mnt’, ‘/tmp’, ‘/usr’ and ‘/var’. Essential for booting are: ‘/bin’, ‘/boot’, ‘/dev’, ‘/etc’, ‘/lib’, ‘/proc’ and ‘/sbin’.

Though it does not specifically have /var/www/ the 1st bit of this (“Contains variable data”) was enough for the creators of apache to add a “www” directory and all the others followed that (though there are systems and softwares that follow their own rules).

https://askubuntu.com/questions/877261/why-is-var-www-a-recommended-location-to-host-your-web-app

That’s the reason why developers usually choose /var/www to store their web files, I think it’s quite interesting after I learned it.

4. Now we have got the WordPress ready, let’s config Caddy to set your website online!

Create a new Caddy configuration file for your WordPress site. Replace your_domain.com with your actual domain name:

sudo nano /etc/caddy/Caddyfile
your_domain.com {
    # Set the root directory of your WordPress site.
    root * /var/www/wordpress

    # Use the PHP FastCGI Process Manager to handle PHP files.
    php_fastcgi unix//run/php/php8.2-fpm.sock
    file_server
    encode gzip

    # Use the TLS certificates if your website has one
    tls /etc/letsencrypt/live/your_domain.com/fullchain.pem /etc/letsencrypt/live/your_domain.com/privkey.pem

    # Rewrite rules to support WordPress permalink structure
    @rewrites {
        not file
        not path /wp-admin*
    }
    rewrite @rewrites /index.php?{query}

    # Block access to .htaccess and other hidden files
    @blocked {
        path .htaccess
        path .*
    }
    respond @blocked 403

    # Optimization of static files generated by WordPress
    @static {
        file
        path *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.woff2
    }
    header @static Cache-Control max-age=5184000
}

If everything goes well, Caddy and WordPress are ready to go, all you need is to restart your Caddy service and check its status:

sudo systemctl reload caddy
sudo systemctl reload caddy | cat

If Caddy running successfully with no error logs, then you can type your domain in your browser, and WordPress’s set-up wizard page will show up:

Congratulations! That means all your previous steps are working! The last thing you have to do is to link the databases that we have created with WordPress to finish the installation.

Enjoy ur fancy little new website! 😛

Last modified: March 23, 2024

Author

Comments

Write a Reply or Comment

Your email address will not be published.