在新浪云上部署(CentOS)

不用买域名、不用备案、不用配置https快速部署 NideShop 微信小程序商城

一、购买新浪云SAE

  • 为什么选择SAE?免费二级域名和支持https访问,不用备案,可用做微信小程序服务器。
  • SAE推荐链接:http://sae.sina.com.cn/
  • 选择对应的部署环境
    • 自定义
    • 开发言语:自定义
    • 运行环境:云容器
    • 语言版本:自定义
    • 部署方式:手工部署
    • 操作系统:centos 7.5 1804
    • 环境配置:高级 II
    • 实例个数:1(测试用选择1个即可)
    • 二级域名:填写你的域名(这里为:nideshop2.applinzi.com)
    • 应用名称:填写你的名称(nideshop2)

文中出现 nideshop2.applinzi.com 的地方,请替换为你配置的二级域名

创建云服务器配置选项.png

二、通过SSH连接云容器

windows下的配置教程:http://www.sinacloud.com/home/index/faq_detail/doc_id/173.html

三、安装配置nginx

  1. yum update -y
  2. yum install -y epel-release
  3. yum install -y nginx curl vim net-tools git
  4. nginx
  5. curl -I localhost

此时发现在外网并不能访问 https://nideshop2.applinzi.com/,错误返回 502 Bad Gateway 这个错误官方文档有说明:http://www.sinacloud.com/doc/sae/docker/vm-getting-started.html

解决方法:更改nginx默认监听的端口80为5050,并重新启动nginx

  1. vim /etc/nginx/nginx.conf
  2. nginx -t
  3. nginx -s reload

更改监听的端口后

更改nginx监听的端口.png

再次访问 https://nideshop2.applinzi.com/,成功返回 Welcome to nginx on Fedora!

四、通过nvm安装node.js

  • 安装nvm https://github.com/creationix/nvm

    1. curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
  • nvm安装成功后,执行以下命令

    1. source ~/.bashrc
  • 查看最新版本的Node.js并安装

    1. nvm ls-remote
    2. NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node nvm install v8.11.3
    3. nvm use --delete-prefix v8.11.3
    4. node -v

五、配置共享型MySQL并导入数据

创建MySQL成功后,选择管理操作,进入到phpmyadmin页面,选项导入 选择nideshop项目根目录下的nideshop.sql文件

六、本地部署NideShop

  • 下载NideShop的源码
    1. cd /var/www
    2. git clone https://github.com/tumobi/nideshop
  • 安装ThinkJS
    1. npm install think-cli -g --registry=https://registry.npm.taobao.org --verbose
    2. thinkjs --version
  • 安装依赖

    1. cd /var/www/nideshop
    2. npm install --registry=https://registry.npm.taobao.org --verbose
  • 配置mysql

    1. vim src/common/config/database.js

    修改后:

    1. const mysql = require('think-model-mysql');
    2. module.exports = {
    3. handle: mysql,
    4. database: 'app_' + process.env.APPNAME,
    5. prefix: 'nideshop_',
    6. encoding: 'utf8mb4',
    7. host: process.env.MYSQL_HOST,
    8. port: process.env.MYSQL_PORT,
    9. user: process.env.ACCESSKEY,
    10. password: process.env.SECRETKEY,
    11. dateStrings: true
    12. };

    Node.js连接MySQL参考文档:http://www.sinacloud.com/doc/sae/docker/howto-use-mysql.html#nodejs

七 通过nginx、pm2进行线上部署

  • 编译项目

    1. npm run compile
  • 修改nginx配置 /etc/nginx/nginx.conf 修改后

  1. user nginx;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log;
  4. pid /run/nginx.pid;
  5. include /usr/share/nginx/modules/*.conf;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  11. '$status $body_bytes_sent "$http_referer" '
  12. '"$http_user_agent" "$http_x_forwarded_for"';
  13. access_log /var/log/nginx/access.log main;
  14. sendfile on;
  15. tcp_nopush on;
  16. tcp_nodelay on;
  17. keepalive_timeout 65;
  18. types_hash_max_size 2048;
  19. include /etc/nginx/mime.types;
  20. default_type application/octet-stream;
  21. include /etc/nginx/conf.d/*.conf;
  22. server {
  23. listen 5050 default_server;
  24. root /var/www/nideshop/www;
  25. set $node_port 8360;
  26. index index.js index.html index.htm;
  27. if ( -f $request_filename/index.html ){
  28. rewrite (.*) $1/index.html break;
  29. }
  30. if ( !-f $request_filename ){
  31. rewrite (.*) /index.js;
  32. }
  33. location = /index.js {
  34. proxy_http_version 1.1;
  35. proxy_set_header X-Real-IP $remote_addr;
  36. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  37. proxy_set_header Host $http_host;
  38. proxy_set_header X-NginX-Proxy true;
  39. proxy_set_header Upgrade $http_upgrade;
  40. proxy_set_header Connection "upgrade";
  41. proxy_pass http://127.0.0.1:$node_port$request_uri;
  42. proxy_redirect off;
  43. }
  44. location ~ /static/ {
  45. etag on;
  46. expires max;
  47. }
  48. }
  49. }

重新启动 nginx

  1. nginx -s reload
  • 测试通过nginx访问 启动服务

    1. node production.js

    外网通过浏览器访问: https://nideshop2.applinzi.com/ 测试成功后记得: Ctrl + C 停止运行

  • 安装配置pm2

    1. npm install -g pm2

    修改项目根目录下的pm2.json为:

    1. {
    2. "apps": [{
    3. "name": "nideshop",
    4. "script": "production.js",
    5. "cwd": "/var/www/nideshop",
    6. "exec_mode": "fork",
    7. "max_memory_restart": "1G",
    8. "autorestart": true,
    9. "instances": 1,
    10. "node_args": [],
    11. "args": [],
    12. "env": {
    13. }
    14. }]
    15. }

    启动pm2

    1. pm2 startOrReload pm2.json
  • 参考文档:ThinkJS线上部署文档:https://thinkjs.org/zh-cn/doc/3.0/deploy.html

八 修改NideShop微信小程序的配置

微信小程序商城GitHub: https://github.com/tumobi/nideshop-mini-program 打开文件config/api.js,修改NewApiRootUrl为自己的域名,注意 https 和后面的 api/ 不能少

  1. var NewApiRootUrl = 'https://nideshop2.applinzi.com/api/';

九 微信小程序端运行效果图

首页

专题

分类

商品列表

商品详情

购物车

订单中心

如使用的是阿里云服务器,请参考另一篇文章:CentOS 7.3 下部署基于 Node.js的微信小程序商城