Nginxhtml
使⽤用pm2+nginx部koa2(https)https://www.zhaofinger.com/de...前端
主要解决问题node
scp (最原始)nginx
scp docker-compose.yml root@47.98.252.43:/root/source/ #⽂件 scp -r mini-01 root@47.98.252.43:/root/source/ #⽂件夹
npm install -g pm2 pm2 start app.js --watch -i 2 // watch 监听⽂文件变化 // -i 启动多少个实例例 pm2 stop all pm2 list pm2 start app.js -i max # 根据机器器CPU核数,开启对应数⽬目的进程,那么每次都要执行这个命令不是很麻烦?
apps: - script : app.js instances: 2 watch : true env : NODE_ENV: production
启动配置git
pm2 start process.yml
Keymetrics在线监控 https://id.keymetrics.io/api/...github
pm2 link 8hxvp4bfrftvwxn uis7ndy58fvuf7l TARO-SAMPLE
pm2设置为开机启动web
pm2 startup
yum install nginx ----- apt update apt install nginx
# /etc/nginx/sites\-enable/taro server { listen 80; server\_name taro.josephxia.com; location / { root /root/source/taro\-node/dist; index index.html index.htm; } }
# 验证Nginx配置 nginx -t # 从新启动Nginx service restart nginx nginx -s reload
\# /etc/nginx/sites\-enable \# taro server { listen 80; server_name taro.josephxia.com; location / { root /root/source/taro-node/dist; index index.html index.htm; } location ~ \\.(gif|jpg|png)$ { root /root/source/taro-node/server/static; } location /api { proxy\_pass http://127.0.0.1:3000; proxy\_redirect off; proxy\_set\_header Host $host; proxy\_set\_header X\-Real\-IP $remote\_addr; proxy\_set\_header X\-Forwarded\-For $proxy\_add\_x\_forwarded\_for; } }
# 查看配置⽂文件位置 nginx -t # nginx: the configuration file /etc/nginx/nginx.conf syntax is ok # nginx: configuration file /etc/nginx/nginx.conf test is successful #重启 service nginx restart
特性 | 容器 | 虚拟机 |
---|---|---|
启动 | 秒级 | 分钟级 |
硬盘使⽤用 | 通常为 MB | 通常为 GB |
性能 | 接近原⽣生 | 弱于 |
系统⽀支持量量 | 单机⽀支持上千个容器器 | ⼀通常⼏几⼗十个 |
拉取官⽅方镜像mongodb
# 拉取官⽅方镜像 docker pull nginx # 查看 docker images nginx # 启动镜像 mkdir www echo 'hello docker!!' >> www/index.html # 启动 # www⽬目录⾥里里⾯面放⼀一个index.html docker run -p 80:80 -v $PWD/www:/usr/share/nginx/html -d nginx # 查看进程 docker ps docker ps -a // 查看所有 # 伪终端 ff6容器器的uuid # -t 选项让Docker分配⼀一个伪终端(pseudo-tty)并绑定到容器器的标准输⼊入上, # -i 则让容器器的标准输⼊入保持打开 docker exec -it ff6 /bin/bash # 停⽌止 docker stop ff6 # 删除镜像 docker rm ff6
#Dockerfile FROM nginx:latest RUN echo '<h1>Hello, Kaikeba!</h1>' > /usr/share/nginx/html/index.html
# 定制镜像 docker build -t mynginx . # 运⾏行行 # -d 守护态运⾏行行 docker run -p 80:80 -d mynginx
npm init -y npm i koa -s
// package.json { "name": "myappp", "version": "1.0.0", "main": "app.js", "scripts": { "test": "echo \\"Error: no test specified\\" && exit 1" }, "keywords": \[\], "author": "", "license": "ISC", "description": "myappp", "dependencies": { "koa": "^2.7.0" } }
// app.js const Koa \= require('koa') const app \= new Koa() app.use(ctx \=> { Math.random() \> 0.8 ? abc() : '' ctx.body \= 'Hello Docker' }) app.listen(3000, () \=> { console.log('app started at http://localhost:3000/') })
#Dockerfile #制定node镜像的版本 FROM node:10-alpine #移动当前⽬目录下⾯面的⽂文件到app⽬目录下 ADD . /app/ #进⼊入到app⽬目录下⾯面,相似cd WORKDIR /app #安装依赖 RUN npm install #对外暴暴露露的端⼝口 EXPOSE 3000 #程序启动脚本 CMD ["node", "app.js"]
# 定制镜像 docker build -t mynode . # 运⾏行行 docker run -p 3000:3000 -d mynode
# .dockerignore node_modules
// process.yml { "apps": [{ "name": "app-name", "script": "app.js", "instances": 2, "env": { "production": true } }\] }
# Dockerfile FROM keymetrics/pm2:latest-alpine WORKDIR /usr/src/app ADD . /usr/src/app RUN npm config set registry https://registry.npm.taobao.org/ && \ npm i EXPOSE 3000 #pm2在docker中使⽤用命令为pm2-docker CMD ["pm2-runtime", "start", "process.yml"]
# 定制镜像 docker build -t mypm2 . # 运⾏行行 docker run -p 3000:3000 -d mypm2
#docker-compose.yml app-pm2: container_name: app-pm2 #构建容器器 build: . # volumes: # - .:/usr/src/app ports: - "3000:3000"
// 强制从新构建并启 # --force-recreate 强制重建容器器 # --build 强制编译 docker-compose up -d --force-recreate --build
#docker-compose.yml version: '3.1' services: nginx: image: nginx:kaikeba ports:- 80:80 # 运⾏ docker-compose up # 后台运行 docker-compose up -d
#docker-compose.yml version: '3.1' services: mongo: image: mongo restart: always ports:- 27017:27017 mongo-express: image: mongo-express restart: always ports:- 8081:8081
// mongoose.js const mongoose = require("mongoose"); // 1.链接 mongoose.connect("mongodb://mongo:27017/test", { useNewUrlParser: true }); const conn = mongoose.connection; conn.on("error", () => console.error("链接数据库失败"));
// app.js const mongoose = require('mongoose'); mongoose.connect('mongodb://mongo:27017/test', {useNewUrlParser: true}); const Cat = mongoose.model('Cat', { name: String }); Cat.deleteMany({}) const kitty = new Cat({ name: 'Zildjian' }); kitty.save().then(() => console.log('meow')); app.use(async ctx => { ctx.body = await Cat.find() })
var http \= require('http') var createHandler \= require('github-webhook-handler'); var handler \= createHandler({path: '/webhooks', secret: 'myHashSecret'}) // 上⾯面的 secret 保持和 GitHub 后台设置的⼀一致 function run\_cmd(cmd, args, callback) { var spawn \= require('child\_process').spawn; var child \= spawn(cmd, args); var resp \= ""; child.stdout.on('data', function (buffer) { resp += buffer.toString(); }); child.stdout.on('end', function () { callback(resp) }); } http.createServer(function (req, res) { handler(req, res, function (err) { res.statusCode \= 404 res.end('no such location') }) }).listen(3000) handler.on('error', function (err) { console.error('Error:', err.message) }) handler.on('\*', function (event) { console.log('Received \*', event.payload.action); // run\_cmd('sh', \['./deploy-dev.sh'\], function(text){ console.log(text) }) handler.on('push', function (event) { console.log('Received a push event for %s to %s', event.payload.repository.name, event.payload.ref); // 分⽀支判断 if (event.payload.ref \=== 'refs/heads/master') { console.log('deploy master..') } // run\_cmd('sh', \['./deploy-dev.sh'\], function(text){ console.log(text) }) handler.on('issues', function (event) { console.log('Received an issue event for % action=%s: #%d %s', event.payload.repository.name, event.payload.action, event.payload.issue.number, event.payload.issue.title) })