ssh 远程连接服务器
使用 item-2,通过密钥连接,操作如下
1.在阿里云上生成密钥并重启服务器
2.cd ~/.ssh, 在该目录下新建 config 文件(无后缀名)
1 2 3 4 5 6 7
| # host_name Host aliyun-lite HostName xxx.xxx User root PreferredAuthentications publickey # 密钥文件路径 IdentityFile {src_to_identity_file}
|
3.连接指定 host
ssh aliyun-lite 即 ssh {host_name}
上传源码/文件到服务器
直接上传到服务器
ftp(文件传输协议)方式,ftp 允许用户以文件操作的方式(如文件的增、删、改、查、传送等)与另一主机相互通信
五个最佳 FTP 客户端工具: https://blog.csdn.net/yissan/article/details/51675651
在服务器上搭建一个 git/scp 服务进行同步
搭建 git 私服
https://segmentfault.com/a/1190000013999832
持续运行服务(node.js 环境)
安装 pm2 依赖
1 2 3 4 5 6
| pm2 start example.js //启动服务 pm2 list //查看启动的应用 pm2 show example.js //查看详细信息 pm2 logs //查看当前信息 pm2 stop example.js //停止example pm2 delete example.js //删除example
|
防火墙
1 2 3 4 5 6
| firewall-cmd --state
systemctl stop firewalld
systemctl start firewalld
|
docker 相关
基本概念
- 镜像
操作系统分内核和用户空间
对于 Linux 而言,内核启动后,会挂载root 文件系统为其提供用户空间支持, Docker 镜像是一个特殊的 root 文件系统,挂载后能提供一些额外功能
docker 镜像是分层存储的架构,即由一组文件系统组成
- 容器
镜像=类,容器=实例,容器也是分层的
容器写数据到数据卷,读取容器存储层或数据卷的数据,具体数据流后续再详谈。 容器消亡时,容器存储层消亡,数据卷不会消亡
- 仓库
一个 Docker Registry 中可以包含多个仓库(Repository),每个仓库可以包含多个标签(Tag),每个标签对应一个镜像,通常,一个仓库会包含同一个软件不同版本的镜像
创建 docker 环境
阿里云购买服务器时可选择 docker 镜像并一键部署
如何使用 docker 部署应用
容器管理工具(推荐)
portainer.io
https://documentation.portainer.io/v2.0/deploy/ceinstalldocker/
https://www.cnblogs.com/ExMan/p/11657069.html
定制镜像
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
| FROM node
ENV NODE_ENV=production ENV NODE_VERSION 14.17.3 ENV YARN_VERSION 1.22.11
ENV WORK_DIR=/usr/app/blog
RUN mkdir -p ${WORK_DIR}
RUN chmod -R 777 ${WORK_DIR} RUN mkdir -p ${WORK_DIR}/logs RUN chmod -R 777 ${WORK_DIR}/logs
WORKDIR ${WORK_DIR}
RUN node --version && npm --version && yarn --version
COPY ["package.json", "package-lock.json*", "./"]
RUN yarn global add pm2
RUN yarn
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./ ./
EXPOSE 80
RUN ls -alR -I "node_modules*"
CMD ["pm2-runtime", "start", "--raw", "app.js", "--env", "production"]
|
构建镜像并启动容器
cd 至项目在主机上的目录,执行 build.sh
1 2 3
| ./build.sh chmod 777 ./build.sh ./build.sh
|
项目结构参考:https://github.com/LahmYing/deploy-by-docker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
tag=$(date +%Y_%m_%d_%H_%M_%S)
docker build -t blog:${tag} .
docker run \ -itd \ --name blog_${tag} \ --restart always \ blog:${tag}
|
镜像操作
1 2 3 4 5 6 7 8
| docker search {image}
docker pull {image}
docker images
docker rmi {image}
|
容器操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| docker run {OPTIONS} {image}
docker run -d --name="nginx-lb" -p {主机端口}:{容器端口} {image}
docker ps
docker ps -a
docker start/stop {容器ID}
docker rm -f {容器A的ID} {容器B的ID}
|
结合阿里云飞流一键部署
阿里云飞流 https://flow.teambition.com/
步骤如下:
- 代码源: 授权使用 github
- node.js 构建命令:
yarn
- 构建镜像并推到阿里云镜像仓库
需要新建一个阿里云镜像仓库(公开,有时要在 portainer.io 修改容器与主机端口并新建容器,portainer 只能 pull 公开的镜像)
- 启动容器
1 2 3 4 5 6 7 8 9
| export myimage=$(echo $myimage | base64 -d) tag=$(date +%Y_%m_%d_%H_%M_%S) docker run \ -itd \ -p 80:80 \ --name blog_$tag \ --restart always \ $myimage
|