本文詳細介紹如何部署NodeJS項目到阿里雲ECS上,以及本人在部署過程當中所遇到的問題、坑點和解決辦法,能夠說是全網最全最詳細的教程了。同時講解了如何申請阿里雲免費SSL證書,以及一臺ECS服務器配置多網站的Nginx配置方法等。php
原文連接:http://www.kovli.com/2017/09/19/ecs-deploy/html
做者:Kovlinode
阿里雲ECS是阿里巴巴旗下,目前國內最成熟穩定的雲服務器提供商linux
本文基於阿里雲ECS雲服務器講解,目前有我的版,企業版,學生版能夠選擇。nginx
我的版 2折優惠活動,一年293元起,平均下來一個月20多便可擁有屬於本身的服務器了。c++
學生版 一個月9.5,24歲如下自動得到學生身份,學生推薦這個服務器。git
企業版 性能強大,有2-5折的活動。github
優惠券 阿里雲產品通用代金券最高1000元。web
這裏以我的版 爲例,流程都同樣的。mongodb
選購好雲服務器,初學者1核1G就足夠了,也就是默認的第一個入門級配置。
操做系統選擇CentOS
系統
點擊當即購買
按鈕,進入確認訂單
頁面,這裏選擇自定義密碼,設置ssh登陸密碼,後面要用,請牢記此密碼
購買好之後,進入控制檯
,左側功能欄選擇雲服務器ECS
點擊箭頭所指位置,便可獲取服務器的公網地址
,如圖
記住公網IP,下一步要用到。
打開 Terminal,
輸入 ssh root@公網IP
,
輸入以前本身設的服務器登陸密碼(若是忘了,能夠去上圖右側的管理-更多-重置密碼)
便可登錄服務器。
首次登錄可能會詢問公鑰,yes 便可。
關於 ssh 登錄,想要詳細瞭解的能夠看阮一峯寫的這篇ssh原理與應用
上述操做是使用 root 用戶身份登錄,會直接進入到下圖 紅色箭頭標出的 root 目錄下。
先 cd ..
跳轉到上一層, 再 ls -a
,就能夠看到相似下圖的目錄結構了。
圖片引用自《鳥哥的Linux》
1. 在配置 nginx 時,可能會依賴於 PCRE 包和 zlib 包,先進行安裝:
先 cd /usr/local
切換目錄
cd /usr/local yum -y install pcre pcre-devel yum install -y zlib-devel
2. 下載 nginx,這裏nginx版本號能夠根據須要選擇,我選擇1.13.3是當時最新的版本了,下面的node版本、mongodb版本均可以根據本身的須要選擇
cd /usr/local/src wget http://nginx.org/download/nginx-1.13.3.tar.gz
3. 解壓縮
tar -xvzf nginx-1.13.3.tar.gz
4. 配置nginx
下載解壓openssl
wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz tar -xvzf openssl-1.0.2l.tar.gz
cd 進入nginx解壓包裏,執行以前安裝的pcre-devel與openssl-devel解決依賴問題
cd nginx-1.13.3 yum -y install pcre-devel openssl openssl-devel
再執行配置腳原本進行編譯預處理
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.0.2l
成功後顯示以下信息,
Configuration summary + using system PCRE library + using OpenSSL library: /usr/local/src/openssl-1.0.2l + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx" nginx configuration file: "/usr/local/nginx/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"
make
make && make install
5. 使用 openssl 生成證書(如下介紹的是本身生成的供學習用,正常面向市場的產品請用認證的)
openssl req -new -x509 -nodes -out server.crt -keyout server.key
移動證書到nginx文件夾
mv server.crt /usr/local/nginx mv server.key /usr/local/nginx
認證的SSL證書,申請阿里雲免費ssl證書:
操做技巧:想看到免費的證書,品牌選擇Symantec
,證書類型選擇免費型DV SSL
6. 修改 nginx 配置文件:
vi /usr/local/nginx/nginx.conf
修改以下
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { # 關閉錯誤頁面的nginx版本數字,提升安全性 server_tokens off; include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; include /usr/local/nginx/sites-enabled-server/*; }
新建文件夾用於存放多網站的nginx配置文件
cd /usr/local/nginx/ mkdir sites-enabled-server
sites-enabled-server
裏面新增若干文件,以便這個ECS能夠給多網站使用
vim default
server { listen 80; server_name _; return 404; } server { listen 80; server_name 你的域名.com www.你的域名.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://127.0.0.1:nodejs配置的端口號; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #}
vim default-ssl
server { listen 443 ssl; server_name localhost; ssl_certificate server.crt; ssl_certificate_key server.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_pass http://127.0.0.1:nodejs配置的端口號; } } ~
繼續新增網站,例如,vim mywebsite.cn
server { listen 80; server_name mywebsite.cn www.mywebsite.cn; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://127.0.0.1:nodejs配置的mywebsite的端口號; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
7. 啓動nginx
/usr/local/nginx/sbin/nginx
node -- 編譯後二進制文件應在/usr/local/bin/node 下
mongodb -- 安裝在/usr/local/mongodb 下
下面就一步一步來
1. 首先升級CentOS
yum -y update
2. 升級後,跳轉到 /usr/local/src
, 這個文件夾一般用來存放軟件源代碼
cd /usr/local/src
3. 下載 nodejs 代碼,也可使用scp命令直接上傳,由於下載實在太慢了。
wget http://nodejs.org/dist/node-latest.tar.gz(下載最新) wget http://nodejs.org/dist/v7.10.0/node-v7.10.0.tar.gz(推薦,穩定版本)
4. 解壓
tar -xzvf node-v0.12.5.tar.gz
5. 進入解壓後的文件夾
cd node-v0.12.5
6. 執行配置腳原本進行編譯預處理
./configure
7. 安裝v8可能有警告, //須要安裝gcc
8. sudo yum install gcc-c++
//安裝gcc
9. 編譯源代碼及安裝
當編譯完成後,須要使之在系統範圍內可用, 編譯後的二進制文件將被放置到系統路徑,默認狀況下,Node二進制文件應該放在/user/local/bin/node
文件夾下
make && make install
10. 安裝pm2,建議 global 安裝
npm install pm2 -g
11. 創建超級連接, 否則 sudo node 時會報 "command not found"
Nodejs到這裏就基本安裝完成了。
軟件安裝位置:/usr/local/mongodb
數據存放位置:/var/mongodb/data
日誌存放位置:/var/mongodb/logs
1. 首先下載安裝包
cd /usr/local wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.6.tgz
2. 解壓安裝包,重命名文件夾爲mongodb
tar zxvf mongodb-linux-x86_64-2.6.0.tgz mv mongodb-linux-x86_64-2.6.0 mongodb
3. 建立數據和日誌存放目錄
mkdir /var/mongodb mkdir /var/mongodb/data mkdir /var/mongodb/logs
4. 打開rc.local文件,添加CentOS開機啓動項:
vim /etc/rc.d/rc.local
5. 將mongodb啓動命令追加到本文件中,讓mongodb開機自啓動:
/usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log --logappend --auth --port 27017 --fork
6. mongo權限設置
sudo vi /etc/mongod.conf
較新版本的增長安全校驗的方法是配置文件最後面加上(網上不少舊版寫法都是無效的)
security: authorization: enabled
以下所示:
systemLog: destination: file path: /var/mongodb/logs/log.log logAppend: true storage: dbPath: /var/mongodb/data net: bindIp: 127.0.0.1 security: authorization: enabled
而後重啓mongodb便可,這裏說明下,初學也能夠不設這個選項,這樣操做數據庫就不須要用戶名密碼了,從安全的角度看仍是設了比較好,後面都是以設了安全校驗來說解。
7. 關閉 vim 後,直接手動啓動mongodb
/usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log --logappend --auth --port 27017 --fork
8. 啓動mongo shell
cd /usr/local/mongodb/bin/ ./mongo
9. 在 mongo shell 中建立超級管理員以及相關數據庫到管理員及數據庫
use admin //admin database db.createUser({ user: "myAdmin", pwd: "******", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
10. 使用特權進入
./mongo -u "mydmin" -p "******" --authenticationDatabase "admin"
11. 建立新數據庫,並設置管理員
> use mywebsite switched to db mywebsite > db.createUser( ... { ... user: "mywebsiteAdmin", ... pwd: "******", ... roles: [ { role: "readWrite", db: "mywebsite" }] ... } ... )
> use secondwebsite switched to db secondwebsite > db.createUser( ... { ... user: "secondwebsiteAdmin", ... pwd: "******", ... roles: [ { role: "readWrite", db: "secondwebsite" }] ... } ... )
之後進入其中的數據庫須要權限,若是用超級管理員則不須要
db.auth("mywebsiteAdmin", "******")
12. mongodb數據遷移(用於從指定地址數據庫遷移到雲服務器上)
//存放到某個目錄 ./mongodump -h 127.0.0.1:27017 -d mywebsite -o /home/mywebsite -u "mywebsiteAdmin" -p "******" //取出 ./mongorestore -h 127.0.0.1:27017 -d mywebsite /home/mywebsite/mywebsite -u "mywebsiteAdmin" -p "******"
13. mongodb鏈接須要安全校驗的配置
以app.js爲例,
設置數據庫鏈接
vim app.js
數據庫鏈接相似下面的格式,因爲數據庫安裝在同一服務器,所以 host 爲127.0.0.1:
var dbUrl = 'mongodb://用戶名:登錄密碼@127.0.0.1/databaseFoo'; mongoose.connect(dbUrl)
例如個人app.js
裏的這一段代碼
// 配置數據庫 mongoose.Promise = global.Promise; let mongoConnectUrl = 'mongodb://' + config.database.USERNAME + ':' + config.database.PASSWORD + '@' + config.database.HOST; mongoose.connect(mongoConnectUrl);
到這裏 mongodb 基本已經安裝設置完成了。
把nodejs的程序放在 /home
下
cd /home
我使用碼雲管理代碼。它的私有庫是免費的。基本操做和 github 同樣。
1. 注意公鑰的生成與添加
ssh-keygen -t rsa -C "mywebsite@gmail.com"
查看與複製公鑰
cat ~/.ssh/id_rsa.pub
2. git安裝
yum install git
3. 複製代碼:
git clone https://git.oschina.net/xxxxxxx/nodeapp.git //你的repo地址
4. 進入 nodeapp 文件夾
cd nodeapp
(若後續代碼變動,提交到 git repo 後直接git pull便可部署代碼)
5. 安裝nodeapp的全部依賴
npm install
注意,使用淘寶鏡像避免未知問題
npm --registry https://registry.npm.taobao.org install
這裏根據你nodejs所使用的框架不一樣而不一樣,
例如koa2能夠在package.json
的scripts
裏設置,參考阮一峯的npm scripts 使用指南
thinkjs或者egg都有本身的啓動方式,就不一一敘述了
這裏要注意的是,若是直接 npm start
或 node app.js
啓動,則一旦退出 ssh 遠程登錄,服務就會中止運行。
使用pm2就能夠解決這個問題,若是跟着教程走,你已經全局安裝了pm2了,能夠直接使用
這篇文章是匯聚了網上諸多教程及本人從零開始部署遇到的各類問題和坑點的解決方案所造成的一篇詳細教程,因爲涉及點比較多,加上版本更新迭代,若是在部署過程當中遇到問題,能夠在下面評論提問(須要登陸Github帳號才能夠評論),或者給我發郵件(admin@kovli.com)。
本文所述的本地操做系統:MacOS系統,阿里雲ECS操做系統:CentOS 7.3,本地若是是Windows環境終端操做可能有所不一樣。
Nginx的一些經常使用操做方法:
/usr/local/nginx/sbin/nginx # 啓動 /usr/local/nginx/sbin/nginx -t #檢查配置文件編寫是否正確 /usr/local/nginx/sbin/nginx -s reload #Nginx平滑重啓 (通常用這個就能夠了) /usr/local/nginx/nginx.conf #配置文件。
文中已引用了部分參考文章,同時感謝下面的參考文章的做者:
http://www.jianshu.com/p/0496ef49b2a5
http://itbilu.com/other/relate/NJ2TJohl.html
https://segmentfault.com/a/1190000004051670
http://borninsummer.com/2015/06/17/notes-on-developing-nodejs-webapp/
https://bbs.aliyun.com/read/146189.html
https://segmentfault.com/q/1010000003937372
版權聲明:
轉載時請註明做者Kovli以及本文地址: http://www.kovli.com/2017/09/19/ecs-deploy/