1. Overview
对于我而言,我更喜欢用docker以及docker相关的工具来搭建应用程序环境。
为什么使用docker
- 不需要安装过多的软件
设想使用传统技术(非docker)部署一个asp.net core的网站在一台linux的server上,我们需要安装至少如下的软件:
- .netcore
- 数据库软件
- web服务器
如果使用docker以及相关技术部署,需要安装的是
- docker-engine
- docker-compose
- 方便migration
假如我们需要把所有的ServerA上部署的软件移植到ServerB上,我们只需要打包docker-container所对应的volume,放到ServerB上,然后重新再ServerB上启动docker-container即可。
- 不需要的软件可以随时停止
假如,现在ServerA有了别的用途,我们只需要在ServerA上停止之前部署过的docker-container即可,无需像传统方式一样卸载软件
基于上述的优势,我个人比较喜欢使用docker以及相关的技术部署应用程序。
2. 先决条件
- docker
- docker-compose
3. 停止nginx服务
如果在Server上安装了nginx的话,建议停掉nginx,因为我们在docker中会使用到nginx默认监听的端口。
可以运行如下命令停止nginx:
systemctl stop nginx
systemctl disable nginx
复制代码
4. 编写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
复制代码
其中,你需要在docker-compose.yml
的同级目录创建nginx文件夹,在其中添加nginx.conf
文件,文件内容如下:
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;
# 下面这两行是部署的ssl证书,接下来一篇文章会教大家如何申请免费的ssl证书
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;
}
}
复制代码
6. 启动docker-compose
docker-compose down
docker-compose up -d
复制代码
运行上述命令之后,就可以通过访问http://localhost 或 https://localhost 来访问你的Wordpress网站了。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END