購買云服務器可以選擇騰訊云和阿里云,前期可以使用免費的云服務器來部署自己的項目。我剛開始是試用的騰訊云服務器(windows系統)、然后又試用了阿里云的服務器(ubuntu系統)、最后還是購買了騰訊云的服務器(ubuntu)。各個平臺都遇到了不同的問題,但是通過google都得到了解決。其實具體的步驟都可以參考各平臺的文檔,下面記錄下我在部署中遇到的問題以及流程。
騰訊云服務器(windows系統)
1、服務器新建ftp站點: 可參考在Win7的IIS上搭建FTP服務及用戶授權。
2、進入騰訊云后臺,設置安全組
3、安裝必要軟件:mongodb、node、npm、nginx
3、將代碼上傳到服務器指定目錄:下載FileZilla上傳工具,填寫服務器公網地址、用戶名、密碼(購買完成后騰訊會發送這些信息),端口默認21 (ftp傳輸方式)
阿里云(ubuntu)
1、購買后先重置下密碼,假如重置為123
2、下載FileZilla連接服務器:填寫服務器公網地址、用戶名、密碼、端口填寫22,因為默認是sftp傳輸方式,用戶名填寫root,阿里云的ubuntu默認是root,密碼就是第一步重置的密碼。連接完成后可看到服務器的目錄結構
3、通過ssh root@公網地址 這種方式連接服務器,進入服務器安裝必要的軟件
4、為了方便終端操作,可以先安裝Oh-my-zsh,步驟如下:
安裝 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
5、安裝mongodb: 官方教程
6、安裝node、安裝npm
sudo apt-get install nodejs
sudo apt-get install npm
7、安裝pm2,通過pm2啟動node可以使關閉終端時node依然運行。
npm install pm2 -g
8、進入后端代碼的目錄,通過npm i 安裝后端代碼需要的庫,啟動node
pm2 start index.js
可能出現的錯誤
import express from 'express';
^^^^^^
SyntaxError: Unexpected token import
解決方法
npm install -g babel-cli
pm2 start --interpreter babel-node index.js
通過pm2 list 查看node是否啟動成功
9、安裝nginx
nginx 下載頁面查看最新穩定版本:http://nginx.org/en/download.html
// 下載
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
安裝完成
10、啟動配置nginx,進入nginx目錄,可以看到目錄下有 sbin目錄和conf目錄,sbin目錄下可以啟動nginx,conf目錄下可以配置nginx,首先啟動nginx,查看nginx是否正常啟動.
cd /usr/local/nginx
cd sbin
nginx
在瀏覽器輸入公網地址,出現下面的界面就說明nginx已經啟動
11、修改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查看幫助命令。