如果對nodeJS的后端的系統,源代碼在github上,https://github.com/saucxs/nodeJSBlog ,如果覺得可以,請請star
並fork
項目
接下來你會看到以下部分:
一、安裝nginx
二、將Nginx設置為Windows服務
三、將Nginx設置為Windows服務
四、 將項目文件上傳到服務器指定的地方
五、使用nssm在windows服務器上部署nodeJS
六、但是外網訪問不了
之前弄過linux服務器,弄過win服務器,感覺linux服務器作為服務器才是最佳的選擇,選擇ubuntu系統,或者centos最為服務器也比win服務器好,配置更簡單,逼格更高,但是有出現win服務器
的時候也可以玩一玩,遇到坑,踩一踩,填一填,就會收獲很多。
下面進入到正題。
一、安裝nginx
下載windows版nginx (http://nginx.org/download/nginx-1.12.2.zip),之后解壓到需要放置的位置(C:\nginx)
二、將Nginx設置為Windows服務
需要借助"Windows Service Wrapper"小工具,項目地址: https://github.com/kohsuke/winsw
下載地址: http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/1.18/winsw-1.18-bin.exe
下載該工具后,將其放在 Nginx安裝目錄下,並重命名為nginx-service.exe,創建配置文件nginx-service.xml(名字要和工具名一樣)。
下面是nginx-service.xml文件內容:
<service> <id>nginx</id> <name>Nginx Service</name> <description>High Performance Nginx Service</description> <logpath>D:\xampp\nginx\logs</logpath> <log mode="roll-by-size"> <sizeThreshold>10240</sizeThreshold> <keepFiles>8</keepFiles> </log> <executable>D:\xampp\nginx\nginx.exe</executable> <startarguments>-p D:\xampp\nginx</startarguments> <stopexecutable>D:\xampp\nginx\nginx.exe</stopexecutable> <stoparguments>-p D:\xampp\nginx -s stop</stoparguments> </service>
在cmd中運行如下命令安裝windows服務
C:\nginx\nginx-service.exe install
在服務中,瀏覽器可以正常訪問,localhost。
注意:
(1)卸載服務
C:\nginx\nginx-service.exe uninstall
(2)查看系統服務
在命令行中輸入
services.msc
(3)啟動服務(命令行)
net start nginx
(4)關閉服務(命令行)
net stop nginx
三、將Nginx設置為Windows服務
配置項Nginx核心配置文件是nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http{
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
client_header_buffer_size 4k;
open_file_cache max=51200 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
types_hash_max_size 2048;
client_max_body_size 10m;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;
gzip on;
gzip_disable "msie6";
server {
listen 80;
server_name www.mwcxs.top;
root XXXX;
set $node_port 8361;
index index.js index.html index.htm;
if ( -f $request_filename/index.html ){
rewrite (.*) $1/index.html break;
}
if ( !-f $request_filename ){
rewrite (.*) /index.js;
}
location = /index.js {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:$node_port$request_uri;
proxy_redirect off;
}
location = /production.js {
deny all;
}
location = /testing.js {
deny all;
}
location ~ /static/ {
etag on;
expires max;
}
}
}
記住一定不要用記事本編輯conf文件,推薦使用notepad++,因為本人就是使用記事本在服務器上修改,導致了服務進行意外停止,啟動不了服務。報錯如下圖所示:
1、超級無敵大錯特錯不能容忍的操作習慣-用記事本修改配置文件引起的1067錯誤
由於這種原因修改.conf為后綴的配置文件產生的1067錯誤是毀滅性的,只要你用記事本編輯保存了配置文件,不管你怎么嘗試都無法啟動,用其它軟件再次編輯保存也無效,直到你拿原始文件覆蓋為止。
推薦用源代碼編輯利器Notepad6.3.3簡體中文綠色版編輯配置文件
參考:http://www.upupw.net/bug/n61.html
問題才得以解決。先在本地使用notepad++修改配置項,再ftp上傳到服務器。
四、將項目文件上傳到服務器指定的地方
通過ftp上到服務器指定地方
五、使用nssm在windows服務器上部署nodeJS
正常情況下:
在服務器上推薦使用 pm2 來管理 Node.js 服務,來保證系統正常運行。
編輯並保存根目錄下的pm2.json
{
"apps": [{ "name": "liblog", "script": "www/production.js", "cwd": "C:/Work/liblog", "max_memory_restart": "1G", "autorestart": true, "node_args": [], "args": [], "instances": "max", "exec_mode": "cluster", "env": { } }] }
注意:注意:cwd為項目在服務器上的路徑
然后正常是啟動服務使用
pm2 start pm2.json
在Linux上,可以輕松的使用forever或者pm2來部署nodejs應用。但是在windows下就麻煩了,pm2明確的說支持Linux & MacOS,forever在windows下貌似問題多多:
為什么要使用pm2,而不是像本地環境一樣,直接使用npm start。
因為服務器是windows的,所以很麻煩的。
今天先說下比較簡單的nssm。nssm會監控你安裝的node服務,如果node掛了,nssm會自動重啟它。
nssm安裝使用
目前最新版的是2.23(下載地址),下載之后解壓,根據你的系統選擇32位和64位的版本,直接在nssm.exe
所在目錄運行命令行,輸入nssw install
+你的服務名,例如:
nssm install NodeJSBlog
在Path
中選擇你安裝的node.exe,Startup directory
選擇你的node應用的目錄,Argument
輸入你的啟動文件,例如在我桌面上運行index.js
(在Startup directory目錄執行node index.js
):
如果你啟動的是 npm start ,就是 node www/development.js
點擊Install Service:
之后運行:
nssm start NodeJSBlog
注意:
在nssm.exe所在的目錄執行
(1)卸載(刪除)該服務
nssm remove 服務名
(2)停止該服務
一種去服務列表中,手動重啟,暫停
一種是命令行:nssm uninstall 服務名
注意:
nssm start <servicename> nssm stop <servicename> nssm restart <servicename>
服務已經啟動:
可以在服務器的瀏覽器上查看
六、但是外網訪問不了
6.1外網訪問,系統報nginx 504 Gateway Time-out,Windows服務器外網無法訪問web的解決方法
才發現是自己服務器的防火牆沒有開啟web的80 端口。
(1)新建規則,選擇端口
(2)選擇tcp,以及特定的本地端口
(3)選擇允許連接
(4)使用該規則選擇全部
(5)名稱以及描述
這樣設置之后
6.2內網可以訪問,外網訪問不了,報服務器502
其實502的錯誤是很多原因造成的,但是自己的系統也突然出現了這個問題,發現了是自己的nginx配置的日志過大,網頁文件沒多少,最后排查得知是日志文件占滿了空間。一般日志文件在這個目錄:
nginx/logs文件夾下面
解決方案:
logs文件夾都刪掉,刪不掉就不刪,然后重啟服務器,一切正常。
PS:window服務下的nginx,參考這篇文章的:二、將Nginx設置為Windows服務
外網就可以訪問nginx的配置的nodeJS后台系統。歡迎訪問:http://www.mwcxs.top
如果在win服務器上配置nodeJS服務,可以留言交流