Ubuntu 16.04 下部署Node.js+MySQL微信小程序商城
环境介绍本文档为微信小程序商城NideShop服务端api的安装部署教程
服务端api :https://github.com/tumobi/nideshop
微信小程序端 :https://github.com/tumobi/nideshop-mini-program
阿里云ECS Ubuntu 16.04 64
更新系统和安装git、vim、curlaptupdate-yaptupgrade-yaptinstallcurlgit-y通过nvm安装node.js
安装nvm
curl-o-https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh|bash
nvm安装成功后,关闭当前终端,重新连接
验证安装是否成功
nvm--version
看到输出版本信息0.33.2表示安装成功
查看Node.js版本并安装
nvmls-remotenvminstallv8.2.1node-v
看到输出版本信息v8.2.1表示安装成功
安装MySQL 5.7aptinstallmysql-server-y
安装过程会要求设置mysql的密码,并记住密码
验证mysql是否安装成功
mysql-uroot-p
回车后输入安装时输入的密码,登录成功后的样子
登录成功后
开始运行NideShop下载NideShop的源码
mkdir/var/wwwcd/var/wwwgitclonehttps://github.com/tumobi/nideshop
全局安装ThinkJS
npminstallthinkjs@2-gthinkjs--version
安装依赖
cd/var/www/nideshopnpminstall
创建数据库并导入数据
mysql-uroot-p-e"createdatabasenideshopcharactersetutf8mb4"mysql-uroot-pnideshop</var/www/nideshop/nideshop.sql
修改nideshop的数据库配置db.js
vimsrc/common/config/db.js
修改后
配置数据库信息
注意encoding,prefix的值
编译项目
npmruncompile
以生产模式启动
nodewww/production.js
打开另一个终端验证是否启动成功
curl-Ihttp://127.0.0.1:8360/
输出HTTP/1.1 200 OK,则表示成功
Ctrl + C停止运行
使用 PM2 管理服务为防止后面操作出现[Error] Error: Address already in use, port:8360. 的错误,一定要记得Ctrl + C停止运行,并确保curl -I http://127.0.0.1:8360/不能访问
安装配置pm2
npminstall-gpm2
修改项目根目录下的pm2.json为:
vimpm2.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
pm2startOrReloadpm2.json
成功启动
成功启动
再次验证是否可以访问
curl-Ihttp://127.0.0.1:8360/使用 nginx 做反向代理
aptinstallnginx-y
测试本地是否可以正常访问
curl-Ilocalhost
修改nginx配置
cp/etc/nginx/sites-available/default/etc/nginx/sites-available/default.bakvim/etc/nginx/sites-available/default
修改后的内容
server{listen80;server_namewww.nideshop.comnideshop.com;#此处改为你的域名root/var/www/nideshop;set$node_port8360;indexindex.jsindex.htmlindex.htm;if(-f$request_filename/index.html){rewrite(.*)$1/index.htmlbreak;}if(!-f$request_filename){rewrite(.*)/index.js;}location=/index.js{proxy_http_version1.1;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerHost$http_host;proxy_set_headerX-NginX-Proxytrue;proxy_set_headerUpgrade$http_upgrade;proxy_set_headerConnection"upgrade";proxy_passhttp://127.0.0.1:$node_port$request_uri;proxy_redirectoff;}location=/development.js{denyall;}location=/testing.js{denyall;}location=/production.js{denyall;}location~/static/{etagon;expiresmax;}}
重新启动nginx并验证nginx是否还可以正常访问
nginx-tservicenginxrestartcurlhttp://127.0.0.1/
如果返回的是下图的json数据则表示nginx反向代理配置成功
nginx转发成功
配置https访问注:阿里云默认外网不能访问80/443端口,请更改实例的安全组配置,配置教程:https://help.aliyun.com/document_detail/25475.html?spm=5176.doc25475.3.3.ZAx4Uo
安装certbot
aptinstallsoftware-properties-commonadd-apt-repositoryppa:certbot/certbotaptupdate-yaptinstallpython-certbot-nginx-ycertbot--nginx
配置自动更新证书
certbotrenew--dry-run
详情文档请查看:https://certbot.eff.org/#ubuntuxenial-nginx
测试浏览器使用https形式访问是否成功
配置https访问成功
修改NideShop微信小程序客户端的配置微信小程序商城客户端GitHub:https://github.com/tumobi/nideshop-mini-program
打开文件config/api.js,修改NewApiRootUrl为自己的域名
varNewApiRootUrl='https://www.nideshop.com/api/';
注意https和后面的api/不能少
到此部署成功。如有问题请加QQ群:594430617
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。