1、安裝nodejs
一、查看操做系統信息
uname -anode
cat /etc/centos-releaselinux
二、安裝wgetnginx
yum install wget -y
三、安裝nodejs
一、下載 wget https://nodejs.org/dist/v10.13.0/node-v10.13.0-linux-x64.tar.xz
二、解壓 xz -d node-v10.13.0-linux-x64.tar.xz
三、解壓 tar xvf node-v10.13.0-linux-x64.tar
四、安裝nodejs
一、cd node-v10.13.0-linux-x64
二、cd bin
三、mv ./node /bin/
四、mv ./npm /bin/
五、mv ./npx /bin/
六、cd .. cd lib
七、mv ./node_modules /lib/
五、安裝nrm
#npm install nrm -g
六、安裝 ngnix
#rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#yum install -y nginx
運行nginx
#service nginx start
重啓nginx,進入/usr/sbin
#nginx -s reload
查看nginxV默認安裝目錄 whereis nginxweb
開機運行nginx npm
# systemctl enable nginx.service //開機運行json
# systemctl start nginx.service //運行nginx服務centos
# systemctl stop nginx.server //中止nginx服務api
#systemctl restart nginx.service //重啓nginx服務
服務器
七、安裝pm2
npm install pm2 -g網絡
2、node監聽的8080端口,網站沒法訪問
一、查看防火牆
啓動: systemctl start firewalld
關閉: systemctl stop firewalld
查看狀態: systemctl status firewalld
開機禁用 : systemctl disable firewalld
開機啓用 : systemctl enable firewalld
查看端口: firewall-cmd --zone=public --list-ports
二、開放8080端口
添加
firewall-cmd --zone=public --add-port=8080/tcp --permanent (--permanent永久生效,沒有此參數重啓後失效)
firewall-cmd --zone=public --add-port=9615/tcp --timeout=600(臨時開放端口600秒,不能執行firewall-cmd --reload,不然無效了)
從新載入
firewall-cmd --reload
查看
firewall-cmd --zone=public --query-port=8080/tcp
刪除
firewall-cmd --zone=public --remove-port=8080/tcp --permanent
3、全局化運行node
一、node server.js & (該方法若是某一進程出錯,則服務中止,不推薦)
二、pm2
啓動: pm2 start app.js
啓動: pm2 start app.js -i 4 #cluster mode模式啓動4個app.js的應用實例進行負載均衡
啓動: pm2 start app.js --name="api" #啓動應用程序並命名爲"api"
啓動: pm2 start app.js --watch #當文件發生變化時自動重啓應用
中止:pm2 stop app_name|app_id
刪除:pm2 delete app_name|app_id
重啓:pm2 restart app_name|app_id
中止全部:pm2 stop all
查看全部的進程:pm2 list
查看全部的進程狀態:pm2 status
查看應用程序全部信息:pm2 show app_name|app_id
查看某一個進程的信息:pm2 describe app_name|app_id
啓動監控:pm2 monit
查看日誌: pm2 logs
運行WEB:pm2 web (端口:9615,http://xxx.xxx.xxx.xx:9615,返回是json格式信息 )
三、pm2 開機自動運行
1)、先啓動pm2:#pm2 start app.js
2)、保存當前運行狀態:#pm2 save
3)、生成服務:#pm2 startup
錯誤:[PM2][ERROR] Failure when trying to write startup script
提示拒絕建立服務文件(原來是我開了雲鎖加固了系統,禁止修改文件,暫時關閉先,等建立完再開啓)
關閉後終於建立成功!
4)、重啓服務器:#reboot now
4、查看進程,Kill進程
一、查看 ps -ef
二、Kill kill [pid]
5、查看網絡鏈接狀態
netstat -atunlp