在阿里云上部署(CentOS)

推荐使用阿里云服务器部署,备案快,只需要在 APP 上即可完成备案

环境介绍

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

  • 推荐配置:4CPU、8G内存、4M宽带、40G系统盘 推荐配置适合正式上线使用,你也可根据自身需要更改配置,如果图片多或是图片加载慢则推荐使用云存储和CDN。 推荐配置20180813112415.png

  • 最低配置:1CPU、1G内存、1M宽带、20G系统盘 最低配置适合开发和测试阶段使用,正式环境不推荐使用。 最低配置20180813110355.png

更新系统和安装 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/database.js

修改后

  1. const mysql = require('think-model-mysql');
  2. module.exports = {
  3. handle: mysql,
  4. database: 'nideshop',
  5. prefix: 'nideshop_',
  6. encoding: 'utf8mb4',
  7. host: '127.0.0.1',
  8. port: '3306',
  9. user: 'root',
  10. password: '你的密码',
  11. dateStrings: true
  12. };

注意 encoding,prefix 的值

编译项目

  1. npm run compile

以生产模式启动

  1. node production.js

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

  1. 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 管理服务

  • 安装配置 pm2
    1. npm install -g pm2

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

  1. vim 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": "256M",
  8. "autorestart": true,
  9. "node_args": [],
  10. "args": [],
  11. "env": {
  12. }
  13. }]
  14. }

如果服务器配置较高,可适当调整 max_memory_restart 和instances的值

  • 启动pm2
    1. pm2 start pm2.json
    成功启动

image.png

再次验证是否可以访问

  1. curl -I http://127.0.0.1:8360/

使用 nginx 做反向代理

  1. yum install nginx -y
  2. systemctl start nginx.service
  3. systemctl enable nginx.service

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

  1. curl -I localhost

修改nginx配置

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

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

  1. user nginx;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log;
  4. pid /run/nginx.pid;
  5. # Load dynamic modules. See /usr/share/nginx/README.dynamic.
  6. include /usr/share/nginx/modules/*.conf;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. tcp_nopush on;
  17. tcp_nodelay on;
  18. keepalive_timeout 65;
  19. types_hash_max_size 2048;
  20. include /etc/nginx/mime.types;
  21. default_type application/octet-stream;
  22. # Load modular configuration files from the /etc/nginx/conf.d directory.
  23. # See http://nginx.org/en/docs/ngx_core_module.html#include
  24. # for more information.
  25. include /etc/nginx/conf.d/*.conf;
  26. server {
  27. listen 80;
  28. server_name nideshop.com www.nideshop.com; # 改成你自己的域名
  29. root /var/www/nideshop/www;
  30. set $node_port 8360;
  31. index index.js index.html index.htm;
  32. if ( -f $request_filename/index.html ){
  33. rewrite (.*) $1/index.html break;
  34. }
  35. if ( !-f $request_filename ){
  36. rewrite (.*) /index.js;
  37. }
  38. location = /index.js {
  39. proxy_http_version 1.1;
  40. proxy_set_header X-Real-IP $remote_addr;
  41. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  42. proxy_set_header Host $http_host;
  43. proxy_set_header X-NginX-Proxy true;
  44. proxy_set_header Upgrade $http_upgrade;
  45. proxy_set_header Connection "upgrade";
  46. proxy_pass http://127.0.0.1:$node_port$request_uri;
  47. proxy_redirect off;
  48. }
  49. location ~ /static/ {
  50. etag on;
  51. expires max;
  52. }
  53. }
  54. }
  • 重新启动 nginx 并验证 nginx 是否还可以正常访问
    1. nginx -t
    2. systemctl restart nginx.service
    3. curl http://127.0.0.1/

如果返回的是下图的 json 数据则表示 nginx 反向代理配置成功

nginx转发成功

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

配置https访问

  • 安装certbot
    1. yum install epel-release -y
    2. yum install certbot-nginx -y
    3. certbot --nginx

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

  1. pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3

重新执行 certbot —nginx

  • 配置自动更新证书
    1. certbot renew --dry-run

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

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

配置https访问成功

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

微信小程序商城客户端GitHub: https://github.com/tumobi/nideshop-mini-program

打开文件 config/api.js,修改 NewApiRootUrl 为自己的域名

  1. var NewApiRootUrl = 'https://www.nideshop.com/api/';

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

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