本文檔爲微信小程序商城NideShop服務端api的安裝部署教程
服務端api : https://github.com/tumobi/nideshophtml
微信小程序端 : https://github.com/tumobi/nideshop-mini-programnode
阿里雲ECS Ubuntu 16.04 64python
apt update -y apt upgrade -y apt install curl git -y
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
nvm安裝成功後,關閉當前終端,從新鏈接
驗證安裝是否成功mysql
nvm --version
看到輸出版本信息0.33.2表示安裝成功nginx
nvm ls-remote nvm install v8.2.1 node -v
看到輸出版本信息v8.2.1表示安裝成功git
apt install mysql-server -y
安裝過程會要求設置mysql的密碼,並記住密碼github
驗證mysql是否安裝成功sql
mysql -uroot -p
回車後輸入安裝時輸入的密碼,登陸成功後的樣子
數據庫
mkdir /var/www cd /var/www git clone https://github.com/tumobi/nideshop
npm install thinkjs@2 -g thinkjs --version
cd /var/www/nideshop npm install
mysql -uroot -p -e "create database nideshop character set utf8mb4" mysql -uroot -p nideshop < /var/www/nideshop/nideshop.sql
vim src/common/config/db.js
修改後
npm
注意encoding,prefix的值
編譯項目
npm run compile
以生產模式啓動
node www/production.js
打開另外一個終端驗證是否啓動成功
curl -I http://127.0.0.1:8360/
輸出HTTP/1.1 200 OK,則表示成功
Ctrl + C中止運行
爲防止後面操做出現[Error] Error: Address already in use, port:8360. 的錯誤,必定要記得Ctrl + C中止運行,並確保curl -I http://127.0.0.1:8360/不能訪問
npm install -g pm2
修改項目根目錄下的pm2.json爲:
vim 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": { } }] }
若是服務器配置較高,可適當調整max_memory_restart和instances的值
pm2 startOrReload pm2.json
成功啓動
再次驗證是否能夠訪問
curl -I http://127.0.0.1:8360/
apt install nginx -y
測試本地是否能夠正常訪問
curl -I localhost
修改nginx配置
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak vim /etc/nginx/sites-available/default
修改後的內容
server { listen 80; server_name www.nideshop.com nideshop.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 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 = /development.js { deny all; } location = /testing.js { deny all; } location = /production.js { deny all; } location ~ /static/ { etag on; expires max; } }
nginx -t service nginx restart curl http://127.0.0.1/
若是返回的是下圖的json數據則表示nginx反向代理配置成功
注:阿里雲默認外網不能訪問80/443端口,請更改實例的安全組配置,配置教程:https://help.aliyun.com/docum...
apt install software-properties-common add-apt-repository ppa:certbot/certbot apt update -y apt install python-certbot-nginx -y certbot --nginx
certbot renew --dry-run
詳情文檔請查看:https://certbot.eff.org/#ubun...
微信小程序商城客戶端GitHub: https://github.com/tumobi/nid...
打開文件config/api.js,修改NewApiRootUrl爲本身的域名
var NewApiRootUrl = 'https://www.nideshop.com/api/';
注意https和後面的api/不能少
到此部署成功。若有問題請加QQ羣:594430617