项目部署服务器2

参考文章:

使用pm2部署项目提高工作效率    https://www.jianshu.com/p/51bf8cf5227c?from=groupmessage

1 使用码云的git仓库保存代码  https://gitee.com/

首先建立代码库-->然后复制本地的用户主目录下的.ssh/id_rsa.pub文件的内容粘贴进去-->生成公钥;

然后把本地代码关联到mayun上

然后开发服务器,复制公钥:

然后把这段代码放在码云的公钥中:

这样服务器也就有权限获取码云上的代码了

注意的是要在服务器上安装git: sudo apt-get install git 

1 首先在服务器上把码云上的代码clone下来  git clone xxxx (和平时的git操作命令一样)

PS:

方法2、本地初始化一个仓库,设置远程仓库地址后再做push

和方法1的差别,在于先创建仓库。

$ git init 
$ git remote add origin https://gitee.com/用户个性地址/HelloGitee.git




上面的代码是,在本地新建的文件,然后设置远程的仓库地址,一般来说我习惯先在远程设置仓库,然后clone下来初始化的仓库,再把代码放进去,所以不需要上面的步骤

============== 

不使用ssh方法,如果使用的是http,是不是就不用设置ssh的一些公钥了?这样在服务器端clone http的代码也不需要公钥了?

最终的效果是在服务器上clone代码库。

PM2的地址: http://pm2.keymetrics.io/

查看其文档:http://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/

目的:在服务器上使用pm2部署代码:

http://pm2.keymetrics.io/docs/usage/deployment/

首先在服务器端新建www/website文件夹: sudo mkdir website 

在本地代码的根目录下新建配置文件:ecosystem.json {  // Applications part  "apps" : [{

"name"      : "website", //部署的应用的名字
    "script"    : "app.js",//启动的入口脚本
    "env": {
      "COMMON_VARIABLE": "true"//启动时传入的变量
    },
    // Environment variables injected when starting with --env production
    // http://pm2.keymetrics.io/docs/usage/application-declaration/#switching-to-different-environments
    "env_production" : {
      "NODE_ENV": "production" //生产环境的变量
    }
  }],
  // Deployment part
  // Here you describe each environment
  "deploy" : { //部署的任务
    "production" : {
      "user" : "imooc_manager", //服务器上用来发布的用户名
      // Multi host is possible, just by passing IPs/hostname as an array
      "host" : ["212.83.163.1", "212.83.163.2", "212.83.163.3"],//主机ip
    "port":"39999",//端口号
// Branch "ref" : "origin/master", //指定主分支master // Git repository to clone "repo" : "[email protected]:repo.git", //仓库地址 // Path of the application on target servers "path" : "/www/website/production", //把项目部署到服务器的那个目录下 // Can be used to give options in the format used in the configura- // tion file. This is useful for specifying options for which there // is no separate command-line flag, see 'man ssh' // can be either a single string or an array of strings "ssh_options": "StrictHostKeyChecking=no", //把ssh的key校验取消掉 // To prepare the host by installing required software (eg: git) // even before the setup process starts // can be multiple commands separated by the character ";" // or path to a script on your local machine "pre-setup" : "apt-get install git", // Commands / path to a script on the host machine // This will be executed on the host after cloning the repository // eg: placing configurations in the shared dir etc "post-setup": "ls -la", // Commands to execute locally (on the same machine you deploy things) // Can be multiple commands separated by the character ";" "pre-deploy-local" : "echo 'This is a local executed command'" // Commands to be executed on the server after the repo has been cloned "post-deploy" : "npm install && pm2 startOrRestart ecosystem.json --env production" // Environment variables that must be injected in all applications on this env "env" : { "NODE_ENV": "production" } } } }

首先把该文件git--push到远程代码库中

然后在本地项目中运行:  pm2 deploy ecosystem.json production setup // 让pm2 连接上服务器,第一次部署 

其中 production 是ecosystem.json  中的配置参数名字

执行命令后发现失败:setup paths failed 是因为该用户在服务器端的website文件夹上没有新建文件的权限,

在服务器上执行  sudo chmod 777 website  //给当前用户的website文件夹设置权限为 可读写可操作

切换到服务器上发现 website/production文件夹下多了三个文件夹

current/ shared/ source/

其中

current 是当前服务所运行的文件夹

shared 是日志文件等共享的数据

source 是clone下来的源代码

然后在本地执行: pm2 deploy ecosystem.json production 运行 

首先在本地控制台登陆服务器,然后通过本地的pm2的配置文件ecosystem.json
和命令: pm2 deploy ecosystem.json production setup
通知服务器在远程代码库中clone代码,
并部署到服务器上相应的文件夹中,然后在发布部署。
如果依赖与数据库的话,一定要保证在服务器上能够让数据库跑起来

但是通常会遇到下面的错误:

猜你喜欢

转载自www.cnblogs.com/xiaozhumaopao/p/11618704.html