項目部署服務器2

參考文章:javascript

使用pm2部署項目提升工做效率    https://www.jianshu.com/p/51bf8cf5227c?from=groupmessagephp

上傳圖片網址: https://imgchr.com/css

 

1 使用碼雲的git倉庫保存代碼  https://gitee.com/html

首先創建代碼庫-->而後複製本地的用戶主目錄下的.ssh/id_rsa.pub文件的內容粘貼進去-->生成公鑰;java

而後把本地代碼關聯到mayun上node

而後開發服務器,複製公鑰:nginx

而後把這段代碼放在碼雲的公鑰中:git

這樣服務器也就有權限獲取碼雲上的代碼了github

注意的是要在服務器上安裝git: sudo apt-get install git web

1 首先在服務器上把碼雲上的代碼clone下來  git clone xxxx (和平時的git操做命令同樣)

PS:

方法二、本地初始化一個倉庫,設置遠程倉庫地址後再作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 

{
  "apps":[{
    "name": "myblog", //部署的應用的名字
    "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" : "zyl", //服務器上用來發布的用戶名
      // Multi host is possible, just by passing IPs/hostname as an array
      "host" : ["39.106.194.136"],//主機ip
    "port":"22",//端口號
// Branch
      "ref"  : "origin/master", //指定主分支master
      // Git repository to clone
      "repo" : "git@github.com:zhenyulei/server-blog.git", //倉庫地址
      // Path of the application on target servers
      "path" : "/home/zyl/www/website/myblog", //把項目部署到服務器的那個目錄下
      // 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" : "",
      // 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 && npm run prd && pm2 startOrRestart ecosystem.json --env production", //項目發佈到服務器上執行的命令
// Environment variables that must be injected in all applications on this env 
      "env" : { "NODE_ENV": "production" } } }
}

 其中本地代碼命令:

{
  "scripts": {
    "start": "node bin/www",
    "dev": "./node_modules/.bin/nodemon bin/www",
    "prd": "pm2 start bin/www",
    "test": "echo \"Error: no test specified\" && exit 1"
  }  
}

 

首先把該文件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代碼,
並部署到服務器上相應的文件夾中,而後在發佈部署。
若是依賴與數據庫的話,必定要保證在服務器上可以讓數據庫跑起來

可是一般會遇到下面的錯誤:

報錯pm2在服務器上找不到,緣由是由於它在服務器上用的是非交互的形式。 

因此切換到服務器端,在根目錄下編輯.bash文件:

cd //cd不帶參數就是切換到根目錄上
vi .bashrc  //編輯 .bashrc文件

 

 

 

 加上#號,保存後退出

而後經過命令加載文件  source .bashrc 

而後從新切換到本地終端再次執行:pm2 deploy ecosystem.json production 進行發佈

啓動服務後有如下樣子表示成功:

執行 pm2 list。能夠查看當前的list應用

 

最後要

1 修改域名指向

2 修改nginx配置文件,讓nginx識別域名以後,轉發到服務器上的3000端口(由於該項目是用在了3000端口)

cd /etc/nginx/conf.d
ls
sudo vi imooc-com-8081.conf

 

 

 

 把上面紅框中的內容改爲下面:

這裏的意思是網站全部訪問 39.106.194.136服務器的默認端口80,也就是訪問 39.106.194.136:80 都會被引導到   proxy_pass http://myblog;

對應的就是  server 127.0.0.1:8081;

引導到8081端口上。




upstream myblog{
  server 127.0.0.1:8081;
}
server {
  listen 80;
  server_name 39.106.194.136;
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Nginx-Proxy true;
    proxy_set_header   X-Forwarded-Host $server_name;

    proxy_pass http://myblog;
    proxy_redirect off; 
  }
}

 多個端口配置以下:

upstream myblog{
  server 127.0.0.1:8081;
}
upstream myweixin{
  server 127.0.0.1:8082;
}
server {
  listen 80;
  server_name www.blog.xiaozhumaopao.com; 
  //url中訪問這個url--就是經過proxy_pass的http://myblog--upstream myblog--
  //也就是server訪問本機127.0.0.1:8081; 的8081端口
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Nginx-Proxy true;
    proxy_set_header   X-Forwarded-Host $server_name;

    proxy_pass http://myblog;
    proxy_redirect off; 
  }
}
server {
  listen 80;
  server_name www.weixin.xiaozhumaopao.com;
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Nginx-Proxy true;
    proxy_set_header   X-Forwarded-Host $server_name;

    proxy_pass http://myweixin;
    proxy_redirect off; 
  }
}

 

而後經過  sudo nginx -s reload 重啓nginx。

若是報下面的警告:

 

zyl@iZ2ze1mqd75ujzu1ar18ngZ:/etc/nginx/conf.d$ sudo nginx -s reload

sudo: unable to resolve host iZ2ze1mqd75ujzu1ar18ngZ

nginx: [warn] conflicting server name "39.106.194.136" on 0.0.0.0:80, ignored

注意是否是寫了多個conf文件,

最後設置防火牆能夠訪問3000端口!!!!!

 sudo vi /etc/iptables.up.rules 

 

而後重啓防火牆: sudo iptbles-restore < /etc/iptables.up.rules 

 

 PS: 也能夠在阿里雲的控制檯上設置: https://help.aliyun.com/knowledge_detail/39922.html?spm=5176.13394938.0.0.2bda3ce4vcGqJY

而後就能夠在服務器上訪問該域名了。

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

此外,修改代碼後如何部署呢?

1.本地修改代碼

2.上傳到git庫

3. 執行命令: pm2 deploy ecosystem.json production 

刷新瀏覽器便可!
----------

部署新的項目,該項目帶有數據庫,

1 首先配置域名:這樣用 movie.iblack7.com 來進行訪問

2 確認新項目的端口是3001,和以前的3000用的是不一樣的端口

3. 修改數據庫

var mongoose = require('mongoose);
//imooc_movie_runner 表示數據庫用戶名 //F**k9001$ 數據庫的密碼 //訪問數據庫的ip地址,由於是本地訪問的,因此是 127.0.0.1:19999(數據庫的端口號,這裏作過修改,因此是19999) //imooc-movie 是數據庫的名字  
var dbUrl = 'mongodb://imooc_movie_runner:F**k9001$@127.0.0.1:19999/imooc-movie'
if(env === 'development'){
  dbUrl = 'mongodb://localhost/imooc-movie'
}
mongoose.connect(dbUrl)

4 新建 ecosystem.json  文件:

{
  "apps":[{
    "name": "myblog", //部署的應用的名字
    "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" : "zyl", //服務器上用來發布的用戶名
      // Multi host is possible, just by passing IPs/hostname as an array
      "host" : ["39.106.194.136"],//主機ip
    "port":"22",//端口號
// Branch
      "ref"  : "origin/master", //指定主分支master
      // Git repository to clone
      "repo" : "git@github.com:zhenyulei/server-blog.git", //倉庫地址
      // Path of the application on target servers
      "path" : "/home/zyl/www/website/myblog", //把項目部署到服務器的那個目錄下
      // 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" : "",
      // 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 && npm run prd && pm2 startOrRestart ecosystem.json --env production", //項目發佈到服務器上執行的命令
// Environment variables that must be injected in all applications on this env 
      "env" : { "NODE_ENV": "production" } } }
}

 

注意這裏要執行:

"post-deploy" : "npm install && npm run prd && pm2 startOrRestart ecosystem.json --env production", //項目發佈到服務器上執行的命令

 

 其中本地代碼:

{
  "scripts": {
    "start": "node bin/www",
    "dev": "./node_modules/.bin/nodemon bin/www",
    "prd": "pm2 start bin/www",
    "test": "echo \"Error: no test specified\" && exit 1"
  }  
}

 若是本地執行的是

"post-deploy" : "npm install && npm start && pm2 startOrRestart ecosystem.json --env production",

提示8081端口被佔用:

 

PM2用sudu是有權限的,可是執行命令的是由PM2fork出來的新進程進行處理,新進程是沒有root權限的,因此,用啓動文件的方式,讓PM2本身去啓動腳本。

因此執行腳本中,要使用 pm2 去開啓服務。

"post-deploy" : "npm install && npm run prd && pm2 startOrRestart ecosystem.json --env production",

因此對應的要在服務器上新建一個movie的文件夾:

cd /www
ls
sudo mkdir movie
sudo chmod 777 -R movie //給當前用戶受權

而後修改nginx的配置文件:新增一個movie的配置文件,複製原來的文件:

 

修改新增的配置文件: 

sudo vi movie-iblack7-com-3001.conf

這裏使用nginx 配置就是 監聽服務器上全部訪問 movie.iblack7.com:80 域名的訪問,都引導到本機的3001 端口上,

注意的是能夠新建多個配置i 文件,可是 server對象中的 server_name 不要同樣 不然服務器會報警告,不知道該定位到那個端口上了。 

 

 

保證本地修改代碼提交以後 

最後在本地執行:

  pm2 deploy ecosystem.json production setup //第一次部署 

 pm2 deploy ecosystem.json production

發現movie重啓了9次,說明有問題:

 

執行 pm2 logs 查看日誌,發現mongodb鏈接錯誤:

首先中止掉該服務 pm2 stop Movie 
在本地修改安裝更新依賴後,從新部署:  pm2 deploy ecosystem.json production 

修改防火牆,容許從 nginx 轉發的movie請求,轉發到後端的3001端口

vi /etc/iptables.up.rules //修改防火牆配置

 

 而後重啓防火牆: sudo iptbles-restore < /etc/iptables.up.rules 

重啓nginx: sudo nginx -s reload 

 

--------

若是本地執行

pm2 deploy ecosystem.json production

報錯:

則須要在服務器端先關掉 pm2服務,而後在本地從新執行pm2 deploy ecosystem.json production 命名。

注意,代碼庫中忽略代碼上傳的文件是: .gitignore  名字必定不要寫錯!!!!

 

-----------------

網站上傳代碼後使用nginx進行壓縮代碼:

nginx開啓gzip壓縮功能遇到的坑

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    #gzip_http_version 1.0;
    gzip_comp_level 8;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif;
    gzip_vary off;
    gzip_disable "MSIE [1-6]\.";

 

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

最後把協議改爲https在視頻的最後一章

相關文章
相關標籤/搜索