准备工作
- 一台服务器, 安装好 docker,这里以阿里云
云服务器 ECS
为例
docker 下载 nginx nodejs mysql redis 镜像
- 这里如果下载很慢,建议使用 docker 镜像加速器
docker pull nginx node mysql redis
- 查看下载是否成功,如果成功列出镜像,则成功,反之失败
docker images
安装 Docker Compose
docker-compose -v
编写 deploy.yml 文件
- 注意:里面声明的端口,如果需要外网可以访问,需要去阿里云控制台->安全组->管理规则下添加端口允许访问规则才能被外部访问,比如 80 端口和 443 端口
- cert 目录为 nginx 要配置的 ssl 证书目录
- 注意: services 下的 nginx 名称如果原来存在,则需要更换名称,比如 nginx1 等,不然会覆盖原来的 nginx 容器
- 以下 你的密码 需要替换为你自己的密码
version: '3'
services:nginx:image: nginxcontainer_name: nginxports:- '80:80'- '443:443'volumes:- '/docker/www:/usr/share/nginx/html'- '/docker/config:/etc/nginx/config'- '/docker/config/nginx.conf:/etc/nginx/nginx.conf'- '/docker/cert:/etc/nginx/cert'node:image: nodecontainer_name: nodeports:- '3600:3600'volumes:- '/docker:/usr/share/apps'- ./logs:/app/logsexternal_links:- mysql:mysql- redis:redisstdin_open: truetty: trueredis:image: rediscontainer_name: redisports:- '6379:6379'command: redis-server --requirepass 你的密码stdin_open: truetty: truemysql:image: mysqlcontainer_name: mysqlports:- '3306:3306'environment:- 'MYSQL_ROOT_PASSWORD=你的密码'stdin_open: truetty: true
运行 deploy.yml 文件
- 上传文件到服务器,通过 xftp 或者其它的工具,这里上传到
/docker/config/
目录和 deploy.yml 文件相对应 - 运行文件
docker-compose -f /docker/config/deploy.yml up -d
- 查看运行是否成功
- 如果存在容器,说明运行成功,反之失败则需要检查代码和环境
docker ps
上传项目文件
- 上传 nodejs 项目文件到
/docker/www/demo/server
目录
安装 pm2
docker exec -it node bash
npm i pm2 -g
pm2 start /usr/share/apps/config/pm2.config.js --env production
module.exports = {apps: [{name: 'node',cwd: '/usr/share/apps/www/demo/server',script: 'dist',instances: 1,autorestart: true,watch: false,max_memory_restart: '1G',env: {NODE_ENV: 'development'},env_production: {NODE_ENV: 'production'}}]
}
配置 nginx 服务
- 如果没有ssl证书,则不要配置 https 服务,用 http 即可
- 获取去下载阿里云的临时免费证书
# nginx.conf -> 上传到服务器 /docker/config/nginx.conf 和 deploy.yml 文件相对应user nginx;
worker_processes 1;error_log /var/log/nginx/error.log error;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;events {worker_connections 1024;
}http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;#gzip on;# HTTP serverserver {listen 80;server_name biaov.cn;rewrite ^(.*)$ https://$host$1 permanent;}# HTTPS serverserver {listen 443 ssl;ssl_certificate /etc/nginx/cert/acmes/cert.pem;ssl_certificate_key /etc/nginx/cert/acmes/key.pem;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;index index.html index.htm;server_name biaov.cn;root /usr/share/nginx/html/home;location / {try_files $uri $uri/ /index.html;}error_page 500 502 503 504 /50x.html;location = /50x.html {alias /usr/share/nginx/html/error/50x.html;}}
}
上传前端代码到服务器
- 上传到 /docker/www/home 目录和 deploy.yml 文件, nginx.conf 文件相对应
查看是否成功
- 访问 biaov.cn 查看是否成功,和 nginx.conf 文件相对应