Setting up nginx to manage a WordPress instance on Docker

Setting up nginx to manage a WordPress instance on Docker

In this guide, we will see how to configure nginx as a reverse proxy web server for a WordPress instance installed on Docker.

Using Docker containers to run web applications is a common practice due to their efficiency and scalability. In this guide, we will see how to configure nginx as a reverse proxy web server for a WordPress instance installed on Docker.

To allow containers to communicate with each other, it is useful to create a Docker network.


docker network create wordpress-net

WordPress requires a MySQL database. Run the following command to create a MySQL container:


docker run --name wordpressdb -e MYSQL_ROOT_PASSWORD=your_password -e MYSQL_DATABASE=wordpress -e MYSQL_USER=wp_user -e MYSQL_PASSWORD=wp_password --network wordpress-net -d mysql:5.7

Replace your_password, wp_user, and wp_password with your preferred credentials.

Now you can start the WordPress container by connecting it to the same network as the database:


docker run --name wordpress --network wordpress-net -e WORDPRESS_DB_HOST=wordpressdb:3306 -e WORDPRESS_DB_USER=wp_user -e WORDPRESS_DB_PASSWORD=wp_password -e WORDPRESS_DB_NAME=wordpress -p 8080:80 -d wordpress

To make nginx work as a reverse proxy for your WordPress container, you need to edit the nginx configuration file. Assuming nginx is installed on the same host, edit the file /etc/nginx/sites-available/default or create a new configuration file in the same directory.

Here is an example configuration:


server {
     listen 80;
     server_name your_domain.com;

     location / {
         proxy_pass http://localhost:8080;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Forwarded-Proto $scheme;
     }
}

Make sure to replace your_domain.com with your actual domain. To apply the changes, restart nginx.

In conclusion, setting up nginx to manage a WordPress instance on Docker may seem complex, but following these steps will help you achieve an efficient and scalable hosting environment. Remember to keep your systems updated and regularly monitor performance and security.