原創聲明:本文為作者原創,未經允許不得轉載,經授權轉載需注明作者和出處
本文檔為微信小程序商城NideShop服務端api的安裝部署教程
服務端api : https://github.com/tumobi/nideshop
微信小程序端 : https://github.com/tumobi/nideshop-mini-program
阿里云ECS Ubuntu 16.04 64
apt update -y
apt upgrade -y
apt install curl git -y
安裝nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
nvm安裝成功后,關閉當前終端,重新連接
驗證安裝是否成功
nvm --version
看到輸出版本信息0.33.2表示安裝成功
查看Node.js版本并安裝
nvm ls-remote
nvm install v8.2.1
node -v
看到輸出版本信息v8.2.1表示安裝成功
apt install mysql-server -y
安裝過程會要求設置mysql的密碼,并記住密碼
驗證mysql是否安裝成功
mysql -uroot -p
回車后輸入安裝時輸入的密碼,登錄成功后的樣子
mkdir /var/www
cd /var/www
git clone https://github.com/tumobi/nideshop
全局安裝ThinkJS
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
修改nideshop的數據庫配置db.js
vim src/common/config/db.js
修改后
注意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/不能訪問
安裝配置pm2
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/document_detail/25475.html?spm=5176.doc25475.3.3.ZAx4Uo
安裝certbot
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形式訪問是否成功
微信小程序商城客戶端GitHub: https://github.com/tumobi/nideshop-mini-program
打開文件config/api.js,修改NewApiRootUrl為自己的域名
var NewApiRootUrl = 'https://www.nideshop.com/api/';
注意https和后面的api/不能少
到此部署成功。如有問題請加QQ群:594430617