Docker Compose deploys Nginx + WordPress + Https personal website

Docker Compose deploys Nginx + WordPress + Https personal website

Some time ago, I deployed Nginx + WordPress personal site through docker compose and configured SSL certificate. I recorded my configuration process here. Next, I will introduce how to use Docker Compose To quickly deploy a personal site that includes Nginx and WordPress, and configure an SSL certificate for it. In this way, you can simplify server management and quickly build a secure WordPress website.

1. Prerequisites

Before you start deploying, you need to ensure that the following conditions are met:

  • A server with Docker and Docker Compose installed (Linux is recommended).
  • A registered domain name (for SSL certificate generation).
  • Basic Linux command line operation skills.

If you don’t have Docker and Docker Compose installed yet, you can install them with the following command:

# Install Docker sudo apt update sudo apt install docker.io # Install Docker Compose sudo apt install docker-compose

2. Environmental Preparation

In the process of deploying Nginx + WordPress, we need to use the following Docker containers:

  • Nginx: acts as a reverse proxy server to handle HTTPS requests.
  • WordPress: Content management system for websites.
  • MySQL: The database used by WordPress.

Create a project directory to store the Docker Compose configuration files and related data:

mkdir wordpress-nginx-ssl cd wordpress-nginx-ssl

3.Docker Compose Configuration File

Next we will create a docker-compose.yml File, which contains the configuration of Nginx, WordPress and MySQL services. The following are the detailed steps of the configuration:

Nginx service configuration

First, in docker-compose.yml Add the Nginx reverse proxy configuration to the file. We will use a separate Nginx container to handle HTTPS requests and proxy them to the WordPress container.

version: '3' services: nginx: image: nginx:latest container_name: nginx ports: - "80:80" - "443:443" volumes: - ./nginx/conf.d:/etc/nginx/conf.d - ./nginx/ssl:/etc/nginx/ssl depends_on: - wordpress networks: - wp_net

in:

  • ports Map the host's ports 80 and 443 to the Nginx container.
  • volumes Used to mount Nginx configuration files and SSL certificates.
  • depends_on Indicates that Nginx depends on the WordPress service.
WordPress Service Configuration

Next, configure the WordPress service:

  wordpress: image: wordpress:latest container_name: wordpress environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: - ./wordpress_data:/var/www/html depends_on: - db networks: - wp_net

in:

  • environment Used to specify the configuration information for WordPress to connect to the MySQL database.
  • volumes The file directory of the WordPress website is mounted to the host for persistent storage.
MySQL Service Configuration

Finally, configure the MySQL service:

  db: image: mysql:5.7 container_name: mysql environment: MYSQL_ROOT_PASSWORD: rootpassword MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress volumes: - ./mysql_data:/var/lib/mysql networks: - wp_net
  • MYSQL_ROOT_PASSWORD is the password for the MySQL root user.
  • MYSQL_DATABASE Is the name of the database used by WordPress.
  • volumes The database file is mounted to ensure data persistence.
Defining the network

exist docker-compose.yml Finally, define a custom network:

networks: wp_net: driver: bridge

4. Generate SSL Certificate

In order for our website to use HTTPS, we need to generate an SSL certificate. Here we use Certbot To generate a free Let's Encrypt certificate. First install Certbot:

sudo apt install certbot

Next, use Certbot to generate the certificate:

sudo certbot certonly --standalone -d yourdomain.com -d www.yourdomain.com

The generated certificate will be stored in /etc/letsencrypt/live/yourdomain.com/ Next, copy the certificate to the Nginx mount directory in the Docker container:

sudo cp /etc/letsencrypt/live/yourdomain.com/fullchain.pem ./nginx/ssl/ sudo cp /etc/letsencrypt/live/yourdomain.com/privkey.pem ./nginx/ssl/

exist nginx/conf.d Create Nginx configuration file in the directory wordpress.conf, the content is as follows:

server { listen 80; server_name yourdomain.com www.yourdomain.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name yourdomain.com www.yourdomain.com; ssl_certificate /etc/nginx/ssl/fullchain. pem; ssl_certificate_key /etc/nginx/ssl/privkey.pem; location / { proxy_pass http://wordpress:80; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Warded -Proto $scheme; } }

This configuration will:

  • Forces HTTP requests to be redirected to HTTPS.
  • Use SSL certificates to encrypt communications.
  • Forward requests from Nginx to the WordPress container.

5. Start the service and test

Once all configurations are complete, run the following command to start the container:

docker-compose up -d

You can check whether the container has been started successfully by running the following command:

docker-compose ps

Access your domain name in the browser https://yourdomain.comIf everything went well, you should be able to see the WordPress installation page.

At this point our personal site has been built. We may encounter some configuration issues later, which will be updated later when we have time.

6. Summary

With Docker Compose, we can easily deploy an Nginx + WordPress site and quickly configure an SSL certificate for it to ensure the security of the site. This method is not only convenient for management, but also ensures the data persistence of the site and the efficient operation of the service. If you have any questions or suggestions, please leave a message in the comment area.

I wish you good luck with your website construction!

docker composedocker composedocker composedocker composedocker composedocker composedocker composedocker composedocker composedocker compose
No Comments

Send Comment Edit Comment

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠(ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ°Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
Emoticons
Emoji
Little Dinosaur
flower!
Next