06-我的博客筆記-項目部署到騰訊雲服務器

購買雲服務器能夠選擇騰訊雲和阿里雲,前期可使用免費的雲服務器來部署本身的項目。我剛開始是試用的騰訊雲服務器(windows系統)、而後又試用了阿里雲的服務器(ubuntu系統)、最後仍是購買了騰訊雲的服務器(ubuntu)。各個平臺都遇到了不一樣的問題,可是經過google都獲得瞭解決。其實具體的步驟均可以參考各平臺的文檔,下面記錄下我在部署中遇到的問題以及流程。html

騰訊雲服務器(windows系統)

一、服務器新建ftp站點: 可參考在Win7的IIS上搭建FTP服務及用戶受權
二、進入騰訊雲後臺,設置安全組
三、安裝必要軟件:mongodb、node、npm、nginx
三、將代碼上傳到服務器指定目錄:下載FileZilla上傳工具,填寫服務器公網地址、用戶名、密碼(購買完成後騰訊會發送這些信息),端口默認21 (ftp傳輸方式)node

阿里雲(ubuntu)

一、購買後先重置下密碼,假如重置爲123
二、下載FileZilla鏈接服務器:填寫服務器公網地址、用戶名、密碼、端口填寫22,由於默認是sftp傳輸方式,用戶名填寫root,阿里雲的ubuntu默認是root,密碼就是第一步重置的密碼。鏈接完成後可看到服務器的目錄結構
三、經過ssh root@公網地址 這種方式鏈接服務器,進入服務器安裝必要的軟件
四、爲了方便終端操做,能夠先安裝Oh-my-zsh,步驟以下:nginx

安裝 zsh 套件
$ apt-get install zsh
安裝git
$ apt-get install git
安裝完以上兩步,執行下面的代碼
curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
把zsh設置成默認-替換bash,重啓終端
chsh -s /bin/zsh

五、安裝mongodb: 官方教程
六、安裝node、安裝npmgit

sudo apt-get install nodejs
sudo apt-get install npm

七、安裝pm2,經過pm2啓動node可使關閉終端時node依然運行。github

npm install pm2 -g

八、進入後端代碼的目錄,經過npm i 安裝後端代碼須要的庫,啓動nodemongodb

pm2 start index.js

可能出現的錯誤express

import express from 'express';
^^^^^^
SyntaxError: Unexpected token import

解決方法npm

npm install -g babel-cli
pm2 start --interpreter babel-node index.js

經過pm2 list 查看node是否啓動成功
九、安裝nginx
nginx 下載頁面查看最新穩定版本:http://nginx.org/en/download.htmlubuntu

// 下載
wget -o nginx-1.14.0.tar.gz http://nginx.org/download/nginx-1.14.0.tar.gz
// 解壓
tar -zxf nginx-1.14.0.tar.gz
// 進入nginx-1.14.0目錄  檢測安裝環境
./configure
// 編譯
make
make install
安裝完成

十、啓動配置nginx,進入nginx目錄,能夠看到目錄下有 sbin目錄和conf目錄,sbin目錄下能夠啓動nginx,conf目錄下能夠配置nginx,首先啓動nginx,查看nginx是否正常啓動.windows

cd /usr/local/nginx
cd sbin 
nginx

在瀏覽器輸入公網地址,出現下面的界面就說明nginx已經啓動
屏幕快照 2018-05-02 下午8.36.52.png
十一、修改nginx的配置,nginx配置的寫法具體含義可自行google,下面粘貼出針對個人博客,以及對我有所幫助的文檔。
nginx配置location總結及rewrite規則寫法

user   root root;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen       8081;
        server_name  localhost;

        root  /home/ubuntu/demo/darrenblog/blogadmin;
        index  index.html index.htm;

        location / {
            try_files $uri $uri/ @router;
            index index.html;
        }
        location @router {
            rewrite ^.*$ /index.html last;
        }
   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /admin/ {
            proxy_pass http://127.0.0.1:4000;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
        location /works/ {
            proxy_pass http://127.0.0.1:3000;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

    # 匹配任何以 /static/ 開頭的地址,匹配符合之後,中止往下搜索正則,採用這一條。解決加載本地圖片的跨越問題
    location ^~ /static/ {
    }
        location ~ .*\.(gif|jpg|jpeg|png)$ {  
            root /root/demo/server/darrenblog/uploads;#指定圖片存放路徑  
            proxy_store on;  
            proxy_store_access user:rw group:rw all:rw;  
            proxy_temp_path    /root/demo/server/darrenblog/uploads;#圖片訪問路徑  
            proxy_set_header    Host 127.0.0.1;    
            if ( !-e $request_filename)  
            {  
                proxy_pass http://127.0.0.1:3000;
            }  
        }  
    }

    server {
        listen       8089;
        server_name  localhost;

        root   /root/demo/client/blogclient;
        index  index.html index.htm;

        location / {
            try_files $uri $uri/ @router;
            index index.html;
        }

        location @router {
            rewrite ^.*$ /index.html last;
        }
        location /tourist/ {
            proxy_pass http://127.0.0.1:3000;
            proxy_set_header Host $host:$server_port;
        }
      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /tourist/* {
            proxy_pass http://127.0.0.1:3000;
        }
   }
}

騰訊雲服務器(ubuntu)

ubuntu服務器大體的配置上面都基本上描述了,騰訊雲的ubuntu服務器默認的用戶名是ubuntu,其餘的配置基本差很少,須要注意的是nginx的啓動。

啓動nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路徑,不加的話,nginx會自動加載默認路徑的配置文件,能夠經過 -h查看幫助命令。

具體配置

項目上傳  github 每次提交都是一個分支

線上地址

相關文章
相關標籤/搜索