手机版

在Ubuntu 16.04下部署Node.js MySQL微信小程序商城

时间:2021-12-12 来源:互联网 编辑:宝哥软件园 浏览:

本文档是关于在微信小程序商城中安装和部署NideShop的服务器api的教程。服务器API :https://github.com/tumobi/nideshop微信小程序:https://github.com/tumobi/nideshop-mini-program

环境介绍

阿里巴巴云ECS Ubuntu 16.04 64

更新系统和安装git、vim、curl

apt update-yaptupgrade-yaptinstallCurlgit-y

通过nvm安装node.js

安装nvm

curl-o-https://raw.githubusercontent.com/CreationIX/NVM/v0.33.2/install.sh |成功安装bashnvm后,关闭当前终端并重新连接以验证安装是否成功。

Nvm - version看到输出版本信息0.33.2,表示安装成功。

检查Node.js的版本并安装它

nvmls-remote nvminstall v8.2.1 Node-V看到输出版本信息V 8 . 2 . 1,表示安装成功。

安装MySQL 5.7

apt installmysql-server-y安装过程将要求您设置mysql的密码并记住密码。

验证mysql是否已成功安装。

Mysql-uroot-p输入输入后输入安装时输入的密码,成功登录后会是什么样子。

登录成功后

成功登录后

开始运行NideShop

下载NideShop的源代码

mkdir/var/www/CD/var/wwwgitclone https://github . com/tumo bi/nide shop Thinkjs的全球安装

npstallthinkjs @ 2-gthinkjs-版本安装依赖关系

CD/var CD/var/www/nide shonpminstall创建数据库并导入数据。

MySQL-uroot-p-e ' CreateDatabase nideshop character setuutf 8 MB 4 ' MySQL-uroot-PNIDeshop/var/www/nide shop/nide shop . SQL修改nide shop的数据库配置db.js。

通过修改vimsrc/common/config/db.js

配置数据库信息

配置数据库信息

注意编码、前缀的价值。

编译项目

Npmruncompile在生产模式下启动

Nodewww/production.js打开另一个终端验证启动是否成功。

Curl-Ihttp://127.0.0.1:8360/输出HTTP/1.1 200 OK,表示Ctrl C停止运行成功。

为防止错误[错误]错误:地址已被使用,端口:8360。一定要记住Ctrl C停止运行,确保curl -I http://127.0.0.1:8360/无法访问。

使用 PM2 管理服务

安装和配置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转发成功

nginx转发成功

注:阿里云默认外网不能访问80/443端口,请更改实例的安全组配置,配置教程:https://help.aliyun.com/document_detail/25475.html?spm=5176.doc25475.3.3.ZAx4Uo

配置https访问

  • 安装certbot

aptinstallsoftware-properties-commonadd-apt-repositoryppa:certbot/certbotaptupdate-yaptinstallpython-certbot-nginx-ycertbot--nginx
  • 配置自动更新证书

certbotrenew--dry-run

详情文档请查看:https://certbot.eff.org/#ubuntuxenial-nginx

  • 测试浏览器使用https形式访问是否成功

    配置https访问成功

    配置https访问成功

修改NideShop微信小程序客户端的配置

微信小程序商城客户端GitHub:https://github.com/tumobi/nideshop-mini-program打开文件config/api.js,修改NewApiRootUrl为自己的域名

varNewApiRootUrl='https://www.nideshop.com/api/';

注意https和后面的api/不能少

到此部署成功。

版权声明:在Ubuntu 16.04下部署Node.js MySQL微信小程序商城是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。