Proper way to install WordPress & MySQL on Coolify

Introduction

Overview of Coolify and its capabilities

Coolify is an open-source platform that simplifies the deployment and management of applications, databases, and other services. Designed with developers in mind, it provides an intuitive web interface and support for containerized applications through Docker. Whether you’re hosting a static website, deploying a complex backend, or setting up a full-stack application, Coolify streamlines the process by eliminating much of the manual configuration.

In this guide, we’ll leverage Coolify’s flexibility to set up WordPress with a separate MySQL database. 😁

Why set up WordPress and MySQL as separate resources?

While Coolify provides a convenient “WordPress with MySQL” resource, we’ll take a different approach by creating two separate resources: one for WordPress and another for MySQL. This method offers several key advantages.

Firstly, separating services is simply a good practice in modern deployments. For instance, you can independently upgrade or maintain the database without impacting your WordPress instance, and vice versa.

Secondly, by choosing a standalone MySQL resource, you unlock additional features provided by Coolify, such as automatic backups, which are not available when using the combined “WordPress with MySQL” resource. These backups are crucial for maintaining data integrity and peace of mind in case of unexpected issues.

What you’ll achieve by the end of this guide

By the end of this guide, you’ll have a fully functional WordPress website connected to a separate MySQL database, all deployed on Coolify. You’ll also learn how to secure your site with HTTPS, access WordPress files, and manage your database with phpMyAdmin. This setup will not only be robust but also easy to maintain and extend as your needs grow.

Setting Up WordPress and MySQL Resources

Let’s start by creating a new project. I named mine “tutorial-project.”

Next, let’s add two resources: “WordPress Without Database” and “MySQL
After creating them, it’s a good idea to rename the default names to something more user-friendly.
We’ll do this by modifying the “Name” or “Service Name” fields by clicking on the respective resources.

Now we can run both resources, so open both of them and click “Start” or “Deploy“. 🛫

TIP: When both resources turn green, you’ll be able to view the newly created Docker containers using the command:

docker container ls --format "table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}"

In the attached screenshot, you can easily identify the two new containers for WordPress and MySQL. 🐬

Configuring resources

Now you’re probably expecting that we’ll simply launch the WordPress installer and get our site running. You’re partly right, but first, we need to connect our resources to the same network. 🛜

To explain this better, let’s inspect our new containers using the docker inspect command on our Coolify server. We’ll notice that in the “Networks” section, there isn’t a single network with the same name for both containers. 📦

This means the containers cannot “see” each other on the network, making it impossible for the WordPress container to use the MySQL database from the MySQL container.

We will fix it by ticking “Connect to predefined Network” and “Restart” in our WordPress resource. 🔁

Let’s configure the domain for our WordPress website. Open the WordPress resource, then click on “Settings” under the “Services” header.

If you have correctly set the A record in your domain’s DNS zone, you can now enter your domain in the “Domains” field, e.g., http://mywebsite.com,https://mywebsite.com. Coolify will automatically handle the redirections, and when it detects incoming traffic to any of these addresses, it will route it directly to your WordPress resource. 🛠️

(don’t forget to restart resource after saving) 🫡

In the last step, we need to create a new database for our WordPress instance and assign the appropriate permissions.

To do this, let’s open the MySQL resource on Coolify and take note of the “Root password”, “Normal user”, and “Normal user password” fields.
The “Normal user” will be the user we use for WordPress.

Let’s access the command line of our Coolify server and run the following commands:

docker exec -it <MYSQL_CONTAINER_NAME> mysql -uroot -p'<ROOT_PASSWORD>' -e "CREATE DATABASE wordpress;"

docker exec -it <MYSQL_CONTAINER_NAME> mysql -uroot -p'<ROOT_PASSWORD>' -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'mysql'@'%'; FLUSH PRIVILEGES;"

These commands will create a database named wordpress and grant the mysql user (Normal User) the necessary permissions to access it. 🔑

Now, finally we can begin with standard WordPress installation 😁

Installing WordPress

Open the domain you specified earlier in your browser, and you should see the WordPress installation welcome screen.

During the configuration process, enter mysql as the username, the appropriate password, and wordpress as the database name. The Database Host should be the name of your MySQL container.

And… here it is! 🚀

Configuring HTTPS for Secure Access

To force HTTPS on our domain, go to WordPress and change the site URL to one with https. 🗝️

After making this change, your site may stop working. To fix it, we need to access the command line and enter the following commands.

docker exec -it <WORDPRESS_CONTAINER_NAME> /bin/bash
apt update && apt install nano
nano wp-config.php

We need to add $_SERVER['HTTPS']='on'; in specific section of the file

Finally, to exit nano, press CTRL+X, then Y, hit Enter and that’s it!🎉

Managing WordPress Files

Sometimes you may need to upload, modify, or download a file located in your WordPress site directory. With Coolify, standard SSH access to these files is not available, so we need to find an alternative solution. 📂

To solve this, install the WordPress plugin WP File Manager.

Managing the MySQL Database with phpMyAdmin

A similar situation applies to the database. Sometimes it’s useful to access it in a user-friendly way. If you’d like to use phpMyAdmin, you can simply add a new resource in Coolify called “phpMyAdmin” 🪪

Next, just like before, select Connect To Predefined Network and configure an appropriate (and different) domain.

However, it’s important to note that this can pose a security risk. phpMyAdmin exposed to the internet can become a target for unauthorized access or bots attempting to log into your database.

If you need to use this solution, ensure that the phpMyAdmin resource remains turned off by default and only activate it when necessary. This precaution minimizes potential vulnerabilities and helps protect your data.

Wrapping Up

By following this guide, you’ve successfully set up WordPress with a separate MySQL database on Coolify. Along the way, we configured HTTPS, enabled easy file management with WP File Manager, and set up phpMyAdmin for convenient database access. 🔥

Separating WordPress and MySQL into individual resources not only enhances flexibility but also unlocks additional features like automated backups, helping safeguard your data. This approach aligns with best practices for modern deployments, promoting maintainability and long-term stability.

As you continue to develop your WordPress site, remember to periodically back up your resources, monitor for updates, and disable phpMyAdmin when not in use to minimize exposure to potential threats.

With your site now live and properly configured, you’re ready to focus on building and growing your online presence. 🎉

PS: If you want to configure S3 backups in your Coolify instance, check my another article ➡️ How to Set Up S3 Hosting on Your Own Server – hasto

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top