aliyun服务器实践

aliyun服务器实践

创建新的账号

默认使用 root 登录,由于 root 权限太高,出于安全考虑,创建 foolishmax(自定义)账号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 新增账号foolishmax
adduser foolishmax

# 修改foolishmax的密码
passwd foolishmax

# 为foolishmax账号添加sudo权限
# 找到文件位置 /etc/sudoers
whereis sudoers

# 修改权限
# u 标识所有者、w 表示写权限 + 表示添加
chmod u+w /etc/sudoers

# 编辑 /etc/sudoers
# 找到 `root ALL=(ALL) ALL`
# 再加一行 `work ALL=(ALL) ALL`
vim /etc/sudoers

# 还原权限
chmod u-w /etc/sudoers


# 切换foolishmax用户
su foolishmax

# 切换回root
su
# 输入密码
# 切换成功

下载常用工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// git
yum -y install git
git --version

// 安装docker
yum -y install docker
docker --version

// 安装docker-compose
// 1. 下载二进制文件
wget https://github.com/docker/compose/releases/download/1.24.0/docker-compose-Linux-x86_64
// 2.移动文件
mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
// 3.赋予可执行权限
chmod +x /usr/local/bin/docker-compose
// 4.创建软链
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
// 5.测试是否安装成功
docker-compose --version

开放端口

aliyun 控制台服务

发布测试机

思路:

  1. 使用 github actions 监听 dev 分支 push
  2. 登录测试机,获取最新 dev 分支代码
  3. 重建构建镜像 docker-compose build project-name
  4. 重启所有容器 docker-compose up -d

github actions
代码在项目 .github/workflows 目录下面的 .yml 格式文件
deplop-dev.yml示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
# github actions 中文文档 https://docs.github.com/cn/actions/getting-started-with-github-actions

name: deploy for dev

on:
push:
branches:
- 'dev' # 只针对 dev 分支
paths:
- '.github/workflows/*'
# - '__test__/**' # dev 不需要立即测试
- 'src/**'
- 'Dockerfile'
- 'docker-compose.yml'
- 'bin/*'

jobs:
deploy-dev:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: set ssh key # 临时设置 ssh key
run: |
mkdir -p ~/.ssh/
echo "${{secrets.WFP_ID_RSA}}" > ~/.ssh/id_rsa # secret 在这里配置 https://github.com/imooc-lego/biz-editor-server/settings/secrets
chmod 600 ~/.ssh/id_rsa
ssh-keyscan "182.92.xxx.xxx" >> ~/.ssh/known_hosts
- name: deploy # 部署
run: |
ssh work@182.92.xxx.xxx "
# 【注意】用 work 账号登录,手动创建 /home/work/imooc-lego 目录
# 然后 git clone https://username:password@github.com/imooc-lego/biz-editor-server.git -b dev (私有仓库,使用 github 用户名和密码)
# 记得删除 origin ,否则会暴露 github 密码

cd /home/work/imooc-lego/biz-editor-server;
git remote add origin https://wangfupeng1988:${{secrets.WFP_PASSWORD}}@github.com/imooc-lego/biz-editor-server.git;
git checkout dev;
git pull origin dev; # 重新下载最新代码
git remote remove origin; # 删除 origin ,否则会暴露 github 密码
# 启动 docker
docker-compose build editor-server; # 和 docker-compose.yml service 名字一致
docker-compose up -d;
"
- name: delete ssh key # 删除 ssh key
run: rm -rf ~/.ssh/id_rsa

评论