How to Install WordPress with Caddy on CentOS 7


In this tutorial, you will install and configure WordPress backed by Caddy.

Please ensure that you have a CentOS 7 server set up, including a sudo non-root user and a firewall, MySQL is installed, and Caddy is installed including a domain name configured to point to your Droplet.

Open your terminal with:

Ctrl+Alt+T

Run the following commands:

Instal PHP

sudo yum update

You will be prompted for your password. Take caution when entering as there is no visual feedback.

Install PHP and the PHP extensions WordPress depends on:

sudo yum install php php-fpm php-mysql php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc

Check the version of PHP which has been installed once the installation finishes:

php -v

You'll see output which displays PHP's version number and all of WordPress' dependencies would've been installed.

Open the PHP-FPM configuration file:

sudo vi /etc/php-fpm.d/www.conf

Find the fragment that specifies the user account and group.

Replace:

; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache

With:

; RPM: apache Choosed to be able to access some dir as httpd
user = caddy
; RPM: Keep a group allowed to write in log dir.
group = caddy

Close the file and start the PHP service:

sudo systemctl start php-fpm

All of WordPress' dependencies have been installed.

Create a MySQL Database and Dedicated User

Log into your MySQL administrative account using the password you set for the MySQL root account during installation:

mysql -u root -p

Create a database called worpress:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Set permissions on the database:

GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'password';

Remember to replace password with a strong and secure password.

FLUSH PRIVILEGES;

EXIT;

Download WordPress

Change your directory:
cd /var/www

Download the latest compressed release of WordPress:

sudo curl -O https://wordpress.org/latest.tar.gz

Extract the compressed archive:

sudo tar zxf latest.tar.gz

A Wordpress directory will be created automatically. Delete the compressed file:

sudo rm latest.tar.gz

Change the permissions of WordPress files and directories so that all files are writable by Caddy:

sudo chown -R caddy:caddy wordpress

Configure Caddy to Serve the WordPress Website

Open the configuration:
sudo vi /etc/caddy/Caddyfile

Copy and paste the following configuration into the file.

example.com {
tls admin@example.com
root /var/www/wordpress
gzip
fastcgi / 127.0.0.1:9000 php
rewrite {
    if {path} not_match ^\/wp-admin
    to {path} {path}/ /index.php?_url={uri}
}}

Replace example.com with your own domain name
After changing the configuration file accordingly, save the file and exit.

Restart Caddy:

sudo systemctl restart caddy

Configure Wordpress

WordPress has a GUI installation wizard to finish its setup, including connecting to the database and setting up your first website.

When you visit your new WordPress instance in your browser for the first time, you'll see a list of languages. Choose the language you would like to use. On the next screen, it describe the information it needs about your database. Click Let's go!, and the next page will ask for database connection details. Fill in this form as follows:

  • Database Name should be wordpress, unless you customized it in Step 2.
  • Username should be wordpressuser, unless you customized it in Step 2.
  • Password should be the password you set for wordpressuser in Step 2.
  • Database Host and Table Prefix should be left to their default values.

When you click Submit, WordPress will check if the provided details are correct. If you receive an error message, double check that you entered your database details correctly.

Once WordPress successfully connects to your database, you'll see a message which begins with:

All right, sparky! You've made it through this part of the installation. WordPress can now communicate with your database.

Now you can click Run the install to begin the installation. After a short time, WordPress will present you with a final screen asking for your website details, such as the website title, the administrator account username, password, and e-mail address. The strong password will be auto-generated for you, but you can choose your own if you'd like.

After clicking Install WordPress, you will be directed to the WordPress dashboard. You have now finished the WordPress installation, and you can use WordPress freely to customize your website and write posts and pages.