學習node的時候也寫了一些demo。可是隻是限於本地測試,歷來沒有發佈。今天嘗試發佈項目。
須要準備的東西html
發佈步驟node
共計兩個文件linux
/** * index.js * 啓動: node index.js * app 跨域訪問測試 * @type {[type]} */ var express = require('express'); //Post方式請求參數放在請求體裏面,需引用body-parser解析body var bodyParser = require("body-parser"); var app = express(); // 引用 app.use(bodyParser.urlencoded({ extended: false })); //設置跨域訪問 app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); res.header("X-Powered-By", ' 3.2.1'); res.header("Content-Type", "application/json;charset=utf-8"); res.header("Content-Type", "application/json;charset=utf-8"); next(); }); //json數據 var data = { "name": "Test", "age": "19" }; app.get('/', function(req, res) { console.log('get..........'); console.log(req.query); if (req.query && req.query.callback) { var str = req.query.callback + "(" + JSON.stringify(data) + ")"; //jsonp console.log('jsonp: ' + str); res.end(str); } else { console.log('json: ' + JSON.stringify(data)); res.end(JSON.stringify(data)); } }); app.post('/', function(req, res) { console.log('post............'); console.log(req.body); console.log('json: ' + JSON.stringify(data)); res.end(JSON.stringify(data)); }); app.listen(8085, function() { console.log('Listening on port 8085...'); });
登陸服務器,在根目錄下能夠安裝。nginx
wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz && cd lnmp1.4 && ./install.sh lnmp
運行。估計20 分鐘。咱們採用手動上傳(固然你能夠使用git).
我用的是Mac,使用的工具是Cyberduck。咱們上傳的位置是/home/wwwroot/default/LHAAPPgit
這是我配置好的文件,此時目錄應該只用index.js 和 package.json(不用理會index.html)github
咱們要安裝package,json裏面的包文件。必須在服務器安裝node 我使用的centos系統
express
yum -y install nodejsnpm
此時能夠 npm i
,項目配置好了。json
咱們發佈的項目確定是須要別人訪問,就須要域名。如今咱們來配置域名。
解析域名,找到要解析的域名
vim
進入以後點擊 添加解析
我如下的域名,都使用example.com 代替
www.example.com 已經被佔用了。咱們須要一個二級域名,二級域名是app.example.com
在服務器下運行lnmp vhost add
根據提示填入信息
Please enter domain(example: www.lnmp.org):
app.example.com
Enter more domain name(example: lnmp.org *.lnmp.org):
enter
Default directory:
/home/wwwroot/LHAAPP/(你本身的目錄文件)
Allow Rewrite rule? (y/n)
n
Allow access log? (y/n)
y
Enter access log filename(Default:test.ibs-bj.com.log):
enter
Create database and MySQL user with same name (y/n)
n
Add SSL Certificate (y/n)
n
要按兩次enter
已經配置好了。咱們測試一下,在LHAAPP 下配置一個index.html 測試一下。
若是不明白反向代理
請自行百度。
用vim 打開usr/local/nginx/conf/nginx.conf
輸入
pstream app.example.com { # Nodejs app upstream server 127.0.0.1:8085; keepalive 64; } server { listen 80; server_name app.example.com; #charset koi8-r; #access_log logs/host.access.log main;
保存,從新啓動nginx
中止 nginx -s quit
啓動 nginx -c /usr/local/nginx/conf/nginx.conf
命令行進入 咱們的項目目錄
運行
pm2 start index.js
咱們看到
參考文章