爲何選擇SAE? 免費二級域名和支持https訪問,不用備案,可用於作微信小程序服務器。php
SAE推薦連接:http://sae.sina.com.cn/html
選擇對應的部署環境
自定義 -> 開發言語:自定義 -> 運行環境:雲容器 -> 語言版本:自定義 -> 部署方式:手工部署 -> 環境配置:選擇第一項(測試選最低配置便可) -> 實例個數:1(測試用選擇1個便可) -> 二級域名:填寫你的域名(這裏爲:tumobi.applinzi.com) -> 應用名稱:填寫你的名稱(tumobi)node
文中出現tumobi.applinzi.com的地方,請替換爲你配置的二級域名mysql
選擇部署環境nginx
windows下的配置教程:http://www.sinacloud.com/home/index/faq_detail/doc_id/173.htmlgit
apt update -y apt upgrade -y apt install nginx curl vim -y service nginx start curl localhost
此時發如今外網並不能訪問http://tumobi.applinzi.com/,錯誤返回
502 Bad Gateway
這個錯誤官方文檔有說明:http://www.sinacloud.com/doc/sae/docker/vm-getting-started.htmlgithub
解決方法:更改nginx默認監聽的端口80爲5050,並從新啓動nginxsql
vim /etc/nginx/sites-available/defaultnginx -t service nginx restart
此處輸入圖片的描述docker
再次訪問 http://tumobi.applinzi.com/,成功返回
Welcome to nginx!npm
安裝nvm
https://github.com/creationix/nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
nvm安裝成功後,關閉當前終端,從新鏈接
查看最新版本的Node.js並安裝
nvm ls-remoteNVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node nvm install v8.1.4node -v
建立MySQL成功後,選擇管理操做,進入到phpmyadmin頁面,選項導入
選擇nideshop項目根目錄下的nideshop.sql文件
下載NideShop的源碼
apt install git -ycd /var/www git clone https://github.com/tumobi/nideshop
安裝ThinkJS
npm install thinkjs@2 -g --registry=https://registry.npm.taobao.org --verbosethinkjs --version
安裝依賴
cd /var/www/nideshop npm install --registry=https://registry.npm.taobao.org --verbose
配置mysql
vim src/common/config/db.js
修改後:
QQ截圖20170715125320.png
啓動:
npm start curl localhost:8360
Node.js鏈接MySQL參考文檔:http://www.sinacloud.com/doc/sae/docker/howto-use-mysql.html#nodejs
編譯項目
npm run compile
修改nginx配置
/etc/nginx/sites-available/default修改後
server { listen 5050 default_server; server_name tumobi.applinzi.com; #注意:修改爲你的域名 root /var/www/nideshop; set $node_port 8360; 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; } }
測試經過nginx訪問
啓動服務
node www/production.js
外網經過瀏覽器訪問: http://tumobi.applinzi.com/
安裝配置pm2
npm install -g pm2
修改項目根目錄下的pm2.json爲:
{"apps": [{ "name": "nideshop", "script": "www/production.js", "cwd": "/var/www/nideshop", "exec_mode": "cluster", "instances": 1, "max_memory_restart": "256M", "autorestart": true, "node_args": [], "args": [], "env": { } }] }
啓動pm2
pm2 startOrReload pm2.json
參考文檔:ThinkJS線上部署文檔:https://thinkjs.org/zh-cn/doc/2.2/deploy.html
config/api.js
var NewApiRootUrl = 'http://tumobi.applinzi.com/api/';