docker installs wordpress, binds domain names through nginx reverse proxy, and configures https

Posted by xgd on Sun, 03 Oct 2021 01:15:11 +0200

Assuming that docker has been installed, if it is not installed, you can follow it 5-minute installation docker tutorial
.

1, Download Image

The latest version is downloaded by default. If you want to specify the corresponding version, you can add the version after the colon. For example, mysql:5.7:

docker pull mysql:5.7
docker pull wordpress
docker pull nginx

2, Start container instance

After downloading the image, start the container. You need to start mysql, wordpress and nginx respectively.

1. Start mysql

Because wordpress needs to install the database, install mysql here and start:

docker container run -d \
--name wordpressdb \
-p 3306:3306 \
--env MYSQL_ROOT_PASSWORD=123456 \
--env MYSQL_DATABASE=wordpress \
mysql:5.7

Meaning of each parameter:
-d: Specifies that the container runs in the background as a daemon;
--Name: specify the container name. Here I specify wordpressdb;
-env environment parameter, MYSQL_ROOT_PASSWORD sets the password of the root user
-p: Specify the port number mapping relationship between the host and the container,
[host port number]: [container internal port]. Here I use host 3306 port and map container 3306 port;
mysql:5.7 is the first 4 bits of the IMAGE ID of nginx

Some nouns are involved in these parameters. If you don't understand them, it is recommended to look at the basic concept of docker. To put it simply, docker technology generates a containerr instance through a template such as image. Next time, another containerr instance can be generated. Image is like a template and can be used multiple times. The contaienr instance can be regarded as a small virtual machine. Multiple virtual machines are in the LAN, and the port needs to be mapped to the host. We all know that the intranet ip that directly accesses the LAN cannot be accessed, and can be accessed through the public ip of the host: the mapped port.

2. Start wordpress

When docker installs wordpress, there are two more parameters than when docker installs mysql, which will be explained later. If the installation is successful, you can see the wordpress process through docker ps. enter ip: 8080 in the browser. It should be possible to install wordpress on the web page.

docker run -d \
--name wordpress \
--link wordpressdb:mysql \
--volume "$PWD/wordpress":/var/www/html \
-p 8080:80 \
wordpress

--link: associate the mysql container. If you use mysql installed by non docker, such as mysql installed manually, you can specify the database connection through the parameter -- env:

-e WORDPRESS_DB_HOST=192.168.80.129:3306 \
-e WORDPRESS_DB_USESR=root \
-e WORDPRESS_DB_PASSWORD=123456

--volume: map the / var/www/html of the container to the current directory, so that you can directly operate the current directory to operate the container directory, otherwise you have to go into the container to operate. We all know that the container is a castrated version of a small virtual machine, and there are no common commands such as vim by default;

Note: by default, the wordpress container opens port 80. Here, port 80 of the container is mapped to port 8080 of the host machine. Why not also map to port 80, because nginx will be installed later. Nginx generally listens to port 80. Cloud service providers generally configure domain names: ikeguang.com Bind the 80 port of the virtual machine, so nginx can only occupy the 80 port of the host machine to avoid unnecessary trouble.

3. Start nginx

If wordpress is installed above, the website can be accessed normally through the ip address. After publishing articles, you can consider binding the domain name and adding nginx for reverse proxy.

1) . configure http access:

nginx configuration

When accessing http, a server listens to port 80 by default; If https is configured, you need to add a server to listen to port 443. Here, configure http access first, and post my configuration:

include /etc/nginx/conf.d/*.conf;
server{
   listen 80;
   server_name ikeguang.com www.ikeguang.com;
   
   location / {
      proxy_set_header Host $host;
	  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://Host ip:8080;
   }
}

Here, http: / / host ip:8080, because our wordpress mapping host port is 8080. Here, nginx proxy host port 8080, that is, if you access port 80 (nginx), it is equivalent to the proxy accessing port 8080 (wordpress), and the domain name configured by the cloud service provider is also bound to port 80. If you access the domain name, you will access wordpress on port 8080, You can visit the website.

Note: since my wordpress startup port is not 80, I need to add a configuration:

proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Start nginx container
Now that nginx.conf is configured, start the nginx container:

docker run -d -p 80:80 --name nginx -v /usr/share/nginx/html:/usr/share/nginx/html -v /etc/nginx:/etc/nginx -v /var/log/nginx:/var/log/nginx nginx

Note here:

  • Here, to map the container directory / etc/nginx, first copy a complete nginx directory to the host computer through the docker cp command, then modify the file, and then execute the docker run command above. During folder mapping, all files in the folder should be the same, and the file contents can be different.

After the above configuration, the domain name can be entered through the browser: ikeguang.com Visit the website.

2) . configure https access:

After the previous configuration, you should be able to access the website through the domain name. Next, configure https:
nginx.conf configuration:

include /etc/nginx/conf.d/*.conf;
server {
    #Listening 443 port
    listen 443 ssl;
    #Corresponding domain name
    server_name ikeguang.com www.ikeguang.com;
    ssl_certificate ssl/1_ikeguang.com_bundle.crt;
    ssl_certificate_key ssl/2_ikeguang.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_prefer_server_ciphers on;
    location / {
	  proxy_set_header Host $host;
	  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://Host ip:8080;
   }
}
server{
   listen 80;
   server_name ikeguang.com www.ikeguang.com;
   #Convert the domain name request from http to https
   rewrite ^(.*)$ https://$host$1; # Redirect all HTTP requests to HTTPS through the rewrite instruction.

   location / {
      proxy_set_header Host $host;
	  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://Host ip:8080;
   }
}

Delete the nginx container that started http earlier, configure the ssl certificate, and start again. You need to start port 80 and port 443.

docker run -d -p 80:80 -p 443:443 --name nginx -v /usr/share/nginx/html:/usr/share/nginx/html -v /etc/nginx:/etc/nginx -v /var/log/nginx:/var/log/nginx nginx
  • -p 443:443. If ssl is configured, this parameter is required. Start to remove it. Here, bind the domain name successfully, http can be accessed, and then configure https access in the second step. You can apply for a free ssl certificate from the cloud service provider, and then install it according to the help document. There's nothing wrong.

Topics: Docker Nginx https