CentOS7.3下部署基于Node.js的微信小程序商城

  • • 发表于 8年前
  • • 作者 微信小程序商城开发
  • • 2847 人浏览
  • • 0 条评论
  • • 最后编辑时间 8年前
  • • 来自 [技 术]

原创声明:本文为作者原创,未经允许不得转载,经授权转载需注明作者和出处

本文档为微信小程序商城NideShop项目的安装部署教程,欢迎star

环境介绍

阿里云 ECS CentOS 7.3 64 云服务器ECS优惠券领取链接

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

  1. yum update -y
  2. yum install curl git -y

通过 nvm 安装 Node.js

  • 安装 nvm

    1. curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash

    验证安装是否成功

    1. source ~/.bashrc
    2. nvm --version

    看到输出版本信息 0.33.5 表示安装成功

  • 查看最新 8.x 版本 Node.js 版本并安装

    1. nvm ls-remote
    2. nvm install v8.2.1
    3. node -v

    看到输出版本信息 v8.2.1 表示安装成功

    必须安装 Node.js 8.x 以上版本

安装 MySQL 5.7

  1. yum install https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm -y
  2. yum install mysql-community-server -y

启动 mysql

  1. systemctl start mysqld
  2. systemctl enable mysqld

查找 root 的初始密码

  1. cat /var/log/mysqld.log | grep password

更改密码

  1. mysql_secure_installation

回车后输入查找到的密码,然后按照料提示更改密码
注意新密码必须包含特殊字符、数字、和大小写字母且不得少于8位,否则更改失败。

验证 mysql 是否安装成功

  1. mysql -uroot -p

回车后输入查找到的密码,登录成功后的样子

image.png

开始运行 NideShop

  • 下载 NideShop 的源码
    1. mkdir /var/www
    2. cd /var/www
    3. git clone https://github.com/tumobi/nideshop
  • 全局安装 ThinkJS 命令

    1. npm install -g think-cli
    2. thinkjs -v
  • 安装依赖

    1. cd /var/www/nideshop
    2. npm install
  • 创建数据库并导入数据

    1. mysql -uroot -p -e "create database nideshop character set utf8mb4"
    2. mysql -uroot -p nideshop < /var/www/nideshop/nideshop.sql
  • 修改 Nideshop 的数据库配置

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

    修改后
    ```
    24 /*
    25
    model adapter config
    26 @type {Object}
    27
    /
    28 exports.model = {
    29 type: ‘mysql’,
    30 common: {
    31 logConnect: isDev,
    32 logSql: isDev,
    33 logger: msg => think.logger.info(msg)
    34 },
    35 mysql: {
    36 handle: mysql,
    37 database: ‘nideshop’,
    38 prefix: ‘nideshop_’,
    39 encoding: ‘utf8mb4’,
    40 host: ‘127.0.0.1’,
    41 port: ‘3306’,
    42 user: ‘root’,
    43 password: ‘你的密码’,
    44 dateStrings: true
    45 }
    46 };

  1. >注意 encodingprefix 的值
  2. 编译项目

npm run compile

  1. 以生产模式启动

node production.js

  1. 打开另一个终端验证是否启动成功

curl -I http://127.0.0.1:8360/

  1. 输出 HTTP/1.1 200 OK,则表示成功
  2. ** Ctrl + C 停止运行**
  3. > 为防止后面操作出现[Error] Error: Address already in use, port:8360. 的错误,一定要记得Ctrl + C停止运行,并确保curl -I http://127.0.0.1:8360/不能访问
  4. ### 使用 PM2 管理服务
  5. + 安装配置 pm2

npm install -g pm2

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

vim pm2.json

  1. 修改后的内容如下

{
“apps”: [{
“name”: “nideshop”,
“script”: “production.js”,
“cwd”: “/var/www/nideshop”,
“exec_mode”: “fork”,
“max_memory_restart”: “256M”,
“autorestart”: true,
“node_args”: [],
“args”: [],
“env”: {

  1. }

}]
}

  1. 如果服务器配置较高,可适当调整 max_memory_restart instances的值
  2. + 启动pm2

pm2 start pm2.json

  1. 成功启动
  2. ![image.png](http://upload-images.jianshu.io/upload_images/3985656-21a6aa802f7bb1ed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  3. 再次验证是否可以访问

curl -I http://127.0.0.1:8360/

  1. ### 使用 nginx 做反向代理

yum install nginx -y
systemctl start nginx.service
systemctl enable nginx.service

  1. 测试本地是否可以正常访问

curl -I localhost

  1. 修改nginx配置

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
vim /etc/nginx/nginx.conf

  1. 内容如下(只需更改 server 里面的内容)

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

Load dynamic modules. See /usr/share/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘“$http_user_agent” “$http_x_forwarded_for”‘;

  1. access_log /var/log/nginx/access.log main;
  2. sendfile on;
  3. tcp_nopush on;
  4. tcp_nodelay on;
  5. keepalive_timeout 65;
  6. types_hash_max_size 2048;
  7. include /etc/nginx/mime.types;
  8. default_type application/octet-stream;
  9. # Load modular configuration files from the /etc/nginx/conf.d directory.
  10. # See http://nginx.org/en/docs/ngx_core_module.html#include
  11. # for more information.
  12. include /etc/nginx/conf.d/*.conf;
  13. server {
  14. listen 80;
  15. server_name nideshop.com www.nideshop.com; # 改成你自己的域名
  16. root /var/www/nideshop/www;
  17. set $node_port 8360;
  18. index index.js index.html index.htm;
  19. if ( -f $request_filename/index.html ){
  20. rewrite (.*) $1/index.html break;
  21. }
  22. if ( !-f $request_filename ){
  23. rewrite (.*) /index.js;
  24. }
  25. location = /index.js {
  26. proxy_http_version 1.1;
  27. proxy_set_header X-Real-IP $remote_addr;
  28. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  29. proxy_set_header Host $http_host;
  30. proxy_set_header X-NginX-Proxy true;
  31. proxy_set_header Upgrade $http_upgrade;
  32. proxy_set_header Connection "upgrade";
  33. proxy_pass http://127.0.0.1:$node_port$request_uri;
  34. proxy_redirect off;
  35. }
  36. location ~ /static/ {
  37. etag on;
  38. expires max;
  39. }
  40. }

}

  1. + 重新启动nginx并验证nginx是否还可以正常访问

nginx -t
systemctl restart nginx.service
curl http://127.0.0.1/

  1. 如果返回的是下图的json数据则表示nginx反向代理配置成功
  2. ![nginx转发成功](http://upload-images.jianshu.io/upload_images/3985656-ff191d58e075c41c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  3. > 注:阿里云默认外网不能访问80/443端口,请更改实例的安全组配置,配置教程:https://help.aliyun.com/document_detail/25475.html?spm=5176.doc25475.3.3.ZAx4Uo
  4. ### 配置https访问
  5. + 安装certbot

yum install epel-release -y
yum install certbot-nginx -y
certbot —nginx

  1. 如果 certbot -nginx 这步出错,则执行

pip install —upgrade —force-reinstall ‘requests==2.6.0’ urllib3

  1. 重新执行 certbot --nginx
  2. + 配置自动更新证书

certbot renew —dry-run

  1. > 详情文档请查看:https://certbot.eff.org/#ubuntuxenial-nginx
  2. + 测试浏览器使用https形式访问是否成功
  3. ![配置https访问成功](http://upload-images.jianshu.io/upload_images/3985656-9b0cfb1db7c99c3e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  4. ### 修改NideShop微信小程序客户端的配置
  5. 微信小程序商城客户端GitHub: https://github.com/tumobi/nideshop-mini-program
  6. 打开文件config/api.js,修改NewApiRootUrl为自己的域名

var NewApiRootUrl = ‘https://www.nideshop.com/api/‘;
```

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

到此部署成功。如有问题请加QQ群:497145766

分享到:

0条评论
我来说两句...
x
 
Ctrl+Enter
作者

微信小程序商城开发

微信小程序商城开发

APP:0 帖子:5 回复:11 积分:94

已加入社区[2986]天

主人太懒,签名没设置!

作者详情》