昨天看到網易雲音樂node.js版API,可是由於對node.js的認識只在一些語句上面,苦於不知咋用.結果發現原來就直接node app.js
就能在本地服務器localhost
中運行.因而就想把他掛到個人vps上面.弄了一下午弄好了.把此次的踩坑經驗發上來php
個人vps是centos 6版本的html
具體步驟:node
1. node.js npm 環境配置
2. git配置(個人vps在作gitpage的時候已經配置了,因此就不寫了)
3. pm2
4. Nginx
複製代碼
參考linux
因爲linux上安裝文件是真的有不少方式,沒具體學過linux的我真的暈暈了,因爲我對node的版本沒有要求,就直接安裝了具體版本號.nginx
推薦如下操做在 /opt 目錄下進行git
wget http://developer.jpanj.com/node-v10.15.3-linux-x64.tar.xz
複製代碼
xz -d node-v10.15.3-linux-x64.tar.xz
複製代碼
若是xz命令不存在的話,能夠查看博客或者根據一下步驟一步步敲github
wget http://tukaani.org/xz/xz-5.2.2.tar.gz`
複製代碼
`tar -zxf xz-5.2.2.tar.gz`
複製代碼
./configure
make && make install
複製代碼
tar -xvf node-v10.15.3-linux-x64.tar
複製代碼
node
目錄出來這樣作的好處是,將來升級版本很是方便,只須要更新這個軟鏈就行npm
ln -s ./node-v10.15.3-linux-x64 ./node
複製代碼
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
複製代碼
能夠看到個人列表中有:vim
/usr/local/bin
/usr/bin
你們約定成俗邏輯是:windows
/usr/bin
下面的都是系統預裝的可執行程序,會隨着系統升級而改變。/usr/local/bin
目錄是給用戶放置本身的可執行程序的地方因此我推薦將軟鏈放在 /usr/local/bin
目錄下:
ln -s /opt/node/bin/node /usr/local/bin/node
ln -s /opt/node/bin/npm /usr/local/bin/npm
複製代碼
可是在這一步我遇到了-bash: cd: src: Too many levels of symbolic links
的問題,後續解決方案是我cd ~
以後 ln -s /opt/node/bin/node /usr/local/bin/node
來完成的。
若是遇到file exists
的問題,就到你軟鏈到的那個地址的命令文件刪掉
[root@dc8 ~]# node -v
v10.15.3
[root@dc8 ~]# npm -v
6.4.1
複製代碼
pm2的做用是可以將你的node後臺項目持久得掛載在後臺而且可以自動地幫你負載均衡之類的,適合我這種傻瓜嘻嘻
npm i -g pm2
由於不像windows,npm下載以後不可以直接在全局環境之中使用
因此也要像以前同樣
ln -s /opt/node/bin/pm2 /usr/local/bin/pm2
複製代碼
接下來就能將node項目一直掛載在後臺了
pm2 start app.js //啓動pm2項目
pm2 stop all //中止全部項目
PORT=4000 pm2 start app.js //使用的這個網易雲後臺程序,在項目目錄下啓動,更多命令內容建議看參考
複製代碼
cd /etc/yum.repos.d/
複製代碼
vim nginx.repo
複製代碼
填寫以下內容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
複製代碼
保存,則會產生一個/etc/yum.repos.d/nginx.repo文件。
下面直接執行以下指令便可自動安裝好Nginx:
yum install nginx -y
複製代碼
安裝完成,下面直接就能夠啓動Nginx了:
/etc/init.d/nginx start
複製代碼
如今Nginx已經啓動了,直接訪問服務器就能看到Nginx歡迎頁面了的。
若是還沒法訪問,則需配置一下Linux防火牆。
iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
複製代碼
這裏注意 以後Nginx要監聽哪一個端口就必須開放哪一個端口,我在這裏踩了一下坑 如今咱們釋放的是默認的80端口
service iptables save
複製代碼
service iptables restart
複製代碼
Nginx的命令以及配置文件位置:
/etc/init.d/nginx start # 啓動Nginx服務
/etc/init.d/nginx stop # 中止Nginx服務
/etc/nginx/nginx.reload # 重啓Nginx服務
/etc/nginx/nginx.conf # Nginx配置文件位置
chkconfig nginx on #設爲開機啓動
複製代碼
至此,Nginx已經所有配置安裝完成。
在你的nginx經過代理的方式轉發請求:進入nginx文件夾下的conf.d文件夾 cd conf.d
新建一個.conf後綴文件(在這個文件夾下面其實已經有一個默認的 default.conf配置文件了)參考:wiki.nginx.org/FullExample
//taotao.config 個人新建的配置文件
server {
listen 4001;#你但願在外網訪問的端口,記得必定要關掉防火牆
location / {
proxy_pass http://127.0.0.1:4030;#你在本機上運行的端口
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
# listen 80;
# server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
#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 /usr/share/nginx/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;
#}
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
# listen 80;
# server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
#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 /usr/share/nginx/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;
#}
}
複製代碼
配置好以後重啓一下nginx服務器就行啦
到這裏,一個簡單的能在外網訪問的node項目就完成了,但願會對你們有用