Assuming docker is already installed, if not, follow the 5-minute Docker installation tutorial.

Download the image

Mysql :5.7: mysql:5.7: mysql:5.7: mysql:5.7:

Docker pull mysql:5.7 Docker pull wordpress Docker pull nginxCopy the code

2. Start the Container instance

Now that you have downloaded the image, start the container, mysql, wordpress and nginx respectively.

1. Start the mysql

Mysql > install mysql;

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

The meanings of the parameters are as follows: -d: indicates that the container runs in background daemon mode. –name: Specify the container name, here I specify wordpressdb; -p: specifies the mapping relationship between the host and the internal port of the container. [Host port number] : [internal port of the container]. Mysql :5.7 is the first 4 bits of nginx IMAGE ID

There are some nouns involved in these parameters. If you don’t understand them, you are advised to take a look at the basic concepts of Docker. To put it simply, Docker technology generates contaienr instance through the template image, and it can also regenerate a Contaienr instance next time. Image is like a template and can be used for many times. Contaienr instance can be regarded as a small virtual machine, multiple virtual machines in the LAN, need to map the port to the host machine, we know that direct access to the LAN Intranet IP is not accessible, through the host machine public IP: port mapping.

2. Start the wordpress

Docker install wordpress, compared to docker install mysql, here compared to two more parameters, will be explained later. If the installation is successful, you can see the wordpress process through Docker PS. Enter IP: 8080 in the browser, and you should be able 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
Copy the code

Mysql > select * from docker where docker is installed;

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

–volume: map /var/www/html to the current directory, so that you can operate on the current directory, otherwise you need to operate on the container.

Note: The default port 80 of the wordpress container is mapped to port 8080 of the host. Why not also map to port 80, because nginx will be installed later. Ikeguang.com is bound to port 80 of the cloud host, so nginx can only occupy port 80 of the host to avoid unnecessary trouble.

3. Start the nginx

If wordpress is installed and the site can be accessed by IP address, you can consider binding domain name, add a nginx reverse proxy.

1). Configure HTTP access:

Nginx configuration

For HTTP access, one server listens on port 80 by default. If the configuration HTTPS, need to add a server, listen on port 443, here the HTTP access first configured and posted 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 address :8080; }}Copy the code

If you access port 80 (nginx), the proxy is accessing port 8080 (wordpress). And the domain name configured by the cloud service provider is bound to port 80, so that when accessing the domain name, you can access the wordpress port 8080, and you can access the website.

Because my wordpress startup port is not 80, I need to add the configuration:

proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Copy the code

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
Copy the code

Note here:

  • Here is the directory of the container/etc/nginxMapping, you have to go throughdocker cpCommand to copy the entire nginx directory to the host, modify the file, and then execute the abovedocker runCommand. During folder mapping, all files in the folder should be the same, but the file contents may be different.

After the above configuration, it should be possible to access the website by entering the domain name ikeguang.com in the browser.

2) configure HTTPS access:

After configuring the domain name, you should be able to access the website through HTTPS: nginx.conf

include /etc/nginx/conf.d/*.conf;
server {
    Listen on port 443
    listen 443 ssl;
    # Corresponding domain nameserver_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 address :8080; } } server{ listen 80; server_name ikeguang.com www.ikeguang.com;Convert HTTP domain name requests to HTTPS
   rewrite ^(.*)$ https://$hostThe $1; Redirect all HTTP requests to HTTPS using the rewrite directive.

   location / {
      proxy_set_header Host $host;
	  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; Proxy_pass http://host IP address :8080; }}Copy the code

Delete the nginx container that started HTTP, configure SSL certificate, and start again, need to start port 80 and 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
Copy the code
  • -p 443:443If you need to configure SSL, this parameter is removed. Here, the domain name is bound successfully and HTTP access is available, and then the second step is to configure HTTPS access. SSL certificate can be applied for free from the cloud service provider, and then install according to the help document.