1. Overview

As for me, I prefer to use Docker and docker-related tools to build the application environment.

Why docker

  • There is no need to install too much software

    To imagine deploying an asp.net core website on a Linux server using traditional technology (other than docker), we need to install at least the following software:

    • .netcore
    • Database software
    • The web server

    If deploying with Docker and related technologies, you need to install

    • docker-engine
    • docker-compose
  • Convenient migration

    If we need to transplant all the software deployed on ServerA to ServerB, we just need to package the volume corresponding to the Docker-Container, put it on ServerB, and then start the Docker-Container on ServerB again.

  • Unwanted software can be stopped at any time

    If ServerA is now used for other purposes, we simply need to stop the Docker-Container on ServerA, without uninstalling the software in the traditional way

Based on the advantages mentioned above, I personally prefer to deploy applications using Docker and related technologies.

2. Prerequisites

  • docker
  • docker-compose

3. Stop the nginx service

If nginx is installed on the Server, it is recommended to stop nginx, because we will use the default listening port of nginx in Docker.

To stop nginx, run the following command:

systemctl stop nginx
systemctl disable nginx
Copy the code

4. Write the docker – compose. Yml

version: '3'

services:
  mysql:
    image: mariadb
    volumes:
      - ./data/mysql:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: mysql_root_pass
      MYSQL_DATABASE: db_name
      MYSQL_USER: user_name
      MYSQL_PASSWORD: user_pass
    restart: always

  wordpress:
    image: WordPress: php7.3 - FPM - alpine
    volumes:
      - ./data/html:/var/www/html
    depends_on:
      - mysql
    environment:
      WORDPRESS_DB_HOST: mysql
      MYSQL_ROOT_PASSWORD: mysql_root_pass
      WORDPRESS_DB_NAME: db_name
      WORDPRESS_DB_USER: user_name
      WORDPRESS_DB_PASSWORD: user_pass
      WORDPRESS_TABLE_PREFIX: wp_
    links:
      - mysql
    restart: always

  nginx:
    image: nginx
    volumes:
      - ./nginx:/etc/nginx/conf.d
      - ./data/html:/var/www/html
      - /etc/ssl:/etc/ssl
    ports:
      - 80: 80
      - 443: 443
    links:
      - wordpress
Copy the code

You need to create an nginx folder in the same directory as docker-comemess. yml and add the nginx.conf file to it.

server {
    listen 80;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name yafeya.japaneast.cloudapp.azure.com;
 
    root /var/www/html;
    index index.php;
 
    access_log /var/log/nginx/hakase-access.log;
    error_log /var/log/nginx/hakase-error.log;

    The following two lines are the deployed SSL certificates. The next article will teach you how to apply for a free SSL certificate
    ssl_certificate           /etc/ssl/fullchain.pem;
    ssl_certificate_key       /etc/ssl/private.pem;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:! aNULL:! eNULL:! EXPORT:! CAMELLIA:! DES:! MD5:! PSK:! RC4;
    ssl_prefer_server_ciphers on;
 
    location / {
        try_files $uri $uri/ /index.php? $args;
    }
 
    location ~ \.php$ {
	fastcgi_split_path_info ^(.+\.php)(/.+)$;
	fastcgi_pass wordpress:9000;
    	fastcgi_index index.php;
    	include fastcgi_params;
    	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    	fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }

    location ~ * \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;}}Copy the code

6. Start the docker – compose

docker-compose down
docker-compose up -d
Copy the code

After running the above command, you can access your WordPress site by visiting http://localhost or https://localhost.