2018-12-24lesson

Dec 24, 2018

购买了一个服务器,折腾了一天给相关环境整理了下。

1
2
3
OS: centos 6 x86_64 # docker 一定要64位
服务器采用docker + koa + mongodb
前端采用Next.js + mobx + antd 组合

服务器配置

由于docker 在centos6 下只能采用1.7.1 版本,导致docker-compose 只能采用1.5.2 版本。

环境要求:python2.7.12;.yml file version为1

参考博文

服务器Dockerfile:

1
2
3
4
5
6
7
8
9
FROM node:8.12.0-alpine

WORKDIR /server

COPY . /server
RUN npm install

EXPOSE 9527
CMD ["yarn", "start"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mongo:
image: "mongo:3.6.9-stretch"
volumes:
- ./data:/data/db
ports:
- "27017:27017"
web:
build: .
command: npm start
volumes:
- ./dist:/server/dist
ports:
- "9527:9527" #我就想用这个端口
links:
- mongo

package.json/script:

1
2
3
4
5
6
7
"scripts": {
"watch": "tsc -w -p ./src",
"debug": "nodemon --watch ./dist --inspect=0.0.0.0:9222 --nolazy ./dist/www.js",
"docker-debug": "docker-compose up",
"start": "node ./dist/www.js",
"mocha-test": "mocha -t 8000 --recursive "
}
  • 运行docker 出现iptables failed - No chain/target/match by that name
1
2
iptables -t filter -N DOCKER
service docker restart
  • mongodb 总是连不上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1. docker-compose.yml 的links 不能缺少
2. mongoose.connect(db_url) 的db_url 确认是docker-compose 的image 名。如我的"mongo"
3. 失败重连
const connectWithRetry = () => {
console.log("MongoDB connection with retry");
mongoose.connect(db_url, options).then(() => {
console.log("MongoDB is connected");
}).catch(err => {
console.log("MongoDB connection unsuccessful, retry after 5 seconds.");
setTimeout(() => connectWithRetry(), 5000)
})
}

connectWithRetry();
  • 服务器在vscode debug

参考vscode 原文我是原文

Nextjs 配置

接下来是nextjs 推送记录

  1. nextjs 编译环境有要求:RAM 1G

我服务器就1G,不然编译的时候会报”code 137”,提示内存不足,同时导致服务器mongodb 一直弹提示centos mongodb [ftdc] serverStatus was very slow: { after basic: 163, after asserts:

解决办法是采用开发机器yarn build 后,将.next 文件夹git commit ,然后同步过来。

  1. nextjs 使用pm2 start:
1
2
3
4
5
6
7
#https://github.com/zeit/next.js/issues/109
# for development
pm2 start npm --name "next" -- run dev

# for production
npm run build
pm2 start npm --name "next" -- start
  1. nextjs 需要在域名根目录运行, 否则出现static 静态路由找不到。其他route 也会找不到的情况。

nginx 配置如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server
{
listen 80 default_server;
server_name www.lsgood.top lsgood.top;
root /var/www;
index index.html index.htm;
location ~^/facicon\.ico$ {
root /var/www;
}
location / {
proxy_pass http://localhost:9005;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /japeneeza/ {
proxy_pass http://127.0.0.1:9527/;
}
}
  1. nextjs 没有storage;同时 server side 获取不到cookie
1
2
3
4
5
6
7
8
9
10
11
12
13
yarn add js-cookie cookieParser

cookie set get cookie (自行参考cookie 保存token)
eg: jsCookie.set("token", ${token})

server.js(express)
const cookieParser = require("cookieParser")
server.use(cookieParser())

然后在pages 里的pageClass.getInitialProps = async function ({ctx}) => {
cosnt { req } = ctx;
console.log("token:", req.cookies.token)
}

让你知道技术的力量

A: 什么?不相信我的能力?
A(咆哮): 我要让你们知道 the ability of super technology!!!

门外, 砰砰砰: 开门,查水表.

A: 大家快点收拾东西,藏起来,丢到窗外,快点快点。。。