有问题,请直接到 GitHub 提 issue:https://github.com/ly525/luban-h5/issues/new

Linux

阿里云/腾讯云/Digital Ocean 以 Centos 为例,Ubuntu等其它发行版,请自行调整

安装基础软件

  1. 请自行安装:node、npm、yarn、pm2
  2. 安装教程请参见:https://www.yuque.com/xpm1xa/rgf7kz/gpfy6g#09H3u
  1. node: https://linuxize.com/post/how-to-install-node-js-on-centos-7/
  2. pm2: https://pm2.keymetrics.io/docs/usage/quick-start/

安装参考:

  1. # install yarn
  2. curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
  3. sudo yum install yarn -y
  4. # install pm2
  5. npm install pm2 -g
  6. # pm2 examples
  7. # pm2 start server.js
  8. # pm2 stop server
  9. # pm2 restart server
  10. # pm2 stop all

在服务器上执行如下代码即可

  1. git clone https://github.com/ly525/luban-h5.git
  2. cd luban-h5
  3. ./luban-h5.sh init # 安装依赖,构建前后端
  4. ./luban-h5.sh start # 启动项目即可
  5. ./luban-h5.sh stop # 停止服务
  1. 如何访问:
    1. 配置防火墙安全组,添加 1337 端口,通过 http://服务器IP:1337 访问即可访问
    2. http://服务器IP:1337/admin 为鲁班的管理后台

Docker 部署

TODO,PR is welcome

=========================================================================================
分割线:下面的内容已经废弃,请忽略
=========================================================================================

已经废弃<请忽略>

保留的目的:仅作参考

阿里云/腾讯云/Digital Ocean

云服务器(centos)

首先需要安装一些基础的软件包,请自行安装(具体参照其官网文档)

  1. node
  2. npm
  3. nginx
  4. yarn
  5. pm2

以下脚本仅供参考

  1. # install yarn
  2. curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
  3. sudo yum install yarn -y
  4. # install nginx
  5. sudo yum install nginx -y
  6. # install pm2
  7. npm install pm2 -g
  8. # pm2 examples
  9. # pm2 start server.js
  10. # pm2 stop server
  11. # pm2 restart server
  12. # pm2 stop all

nginx 配置文件 demo

  1. server {
  2. listen 443;
  3. server_name your_domain;
  4. client_body_buffer_size 20M;
  5. client_max_body_size 20M;
  6. proxy_buffer_size 20M;
  7. proxy_buffers 32 20M;
  8. proxy_busy_buffers_size 20M;
  9. ssl on;
  10. ssl_certificate cert/luban-api.pem;
  11. ssl_certificate_key cert/luban-api.key;
  12. ssl_session_timeout 5s;
  13. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  14. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  15. ssl_prefer_server_ciphers on;
  16. gzip on;
  17. gzip_min_length 1k;
  18. gzip_buffers 4 16k;
  19. gzip_disable "MSIE [1-6]\.";
  20. gzip_comp_level 3;
  21. gzip_types image/png application/json text/plain application/x-javascript text/css application/xml text/javascript application/javascript;
  22. # TODO 这边的 psd-files、engine-assets、third-libs 开发的时候,可以走 proxy_pass
  23. # 生产环境的时候,最好走另外的 location,直接让 nginx 访问静态文件
  24. location ~ ^/(upload|content-manager|users-permissions|works|admin|psd-files|workforms|third-libs|engine-assets) {
  25. proxy_set_header Host $host;
  26. proxy_set_header X-Real-IP $remote_addr;
  27. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  28. proxy_pass http://localhost:1337;
  29. }
  30. location / {
  31. root /home/centos/codebase/luban/luban-h5-dist/front-end/;
  32. #try_files $uri $uri/ /index.html;
  33. }
  34. }

本地执行:

请先提前调研:flightplan
在项目根目录作如下操作:

  1. yarn add flightplan // 部署脚本,官方链接
  2. npm run deploy:esc // 云服务器 (Elastic Compute Service, 简称 ECS)

本地部署脚本

  1. var os = require('os');
  2. var plan = require('flightplan');
  3. // configuration
  4. local_dist_dir = './'; // root path for luban-h5
  5. remote_project_dir = '~/codebase/luban/luban-h5'; // root path for luban-h5 on server
  6. remote_project_api_dir = '~/codebase/luban/luban-h5/back-end/h5-api'; // api root path for luban-h5 on server
  7. // production server config
  8. plan.target('production', {
  9. host: 'your host ip', // your server ip
  10. username: 'centos', // your server username
  11. // 更新为绝对路径
  12. privateKey: `${os.homedir}/.ssh/id_rsa` // your privateKey to rsync files
  13. });
  14. /**
  15. * 1. setup folders
  16. * 2. sync files
  17. * 3. install dependencies
  18. * 4. (re)start api service
  19. * 5. soft link nginx conf
  20. *
  21. * 1. 创建同步文件件
  22. * 2. 同步本地在 git 中的文件(你也可以在服务器端git clone)
  23. * 3. 在 h5-api 目录安装依赖
  24. * 4. 使用pm2 重启服务
  25. * 5. 给 nginx 文件做一个软件链接
  26. *
  27. */
  28. // init remove server path
  29. // 在第一步的时候,需要打开这一项:初始化服务器,现在还不完整,需要补充
  30. // plan.remote(remote => {
  31. // // remove.exec(`mkdir -p ${remote_project_dir}`)
  32. // remove.sudo(`yum install nginx -y`)
  33. // remote.with(`mkdir -p ${remote_project_dir}`, () => {
  34. // // remote.log('Install dependencies');
  35. // // remote.exec('yarn');
  36. // remote.exec('pwd');
  37. // });
  38. // });
  39. // run commands on localhost
  40. plan.local(local => {
  41. // local.log('=> Run build');
  42. // local.exec('npm run build');
  43. // local.log('=> Build finish');
  44. local.log('=> Copy files to remote hosts');
  45. // TODO reference: https://github.com/pstadler/flightplan/issues/142
  46. local.with(`cd ${local_dist_dir}`, () => {
  47. // const filesToCopy = local.exec('find . -type f', { silent: true })
  48. const filesToCopy = local.git('ls-files', {silent: true}) // get list of files under version control
  49. local.transfer(filesToCopy, remote_project_dir);
  50. local.log('=> Copy finish');
  51. });
  52. });
  53. // run commands on the target's remote hosts
  54. plan.remote(remote => {
  55. remote.with(`cd ${remote_project_api_dir}`, () => {
  56. remote.log('Install dependencies');
  57. remote.exec('yarn');
  58. remote.exec('pm2 restart server')
  59. });
  60. });

中间可能遇到的一些问题:

  1. Cannot parse privateKey: Unsupported key format
    解决方案请参照:Cannot parse privateKey: Unsupported key format

Docker 部署

以下方案来自:https://github.com/yi-ge/luban-h5#docker-%E9%83%A8%E7%BD%B2,非常非常感谢。 但仅供参考,此方案不是鲁班的官方的解决方案

鲁班决定暂时不按照这种方案来做,可以先使用上面部署脚本来发布到服务器上,进行部署。 鲁班后期考虑出一个简单版本docker image的部署方案,直接拉镜像,在服务器端就能直接跑起来。

请确保您的80端口和443端口均打开,如果已经配置了其它项目,请进行手工调整。

  1. 修改yourdomain.tld为您的前端域名并解析到服务器。
  2. 修改api.yourdomain.tld为您的后端域名并解析到服务器。
  3. 依次执行以下命令。
  1. # Clone luban-h5
  2. git clone https://github.com/ly525/luban-h5.git
  3. cd luban-h5
  4. # Install require package
  5. docker run --rm -v `pwd`:/root -w /root/back-end/h5-api node:12.8.1 bash -c "yarn && NODE_ENV=production yarn build"
  6. docker run --rm -v `pwd`:/root -w /root/front-end/h5 \
  7. --env "PUBLIC_PATH=/" \
  8. --env "PROD_API_ORIGIN=api.yourdomain.tld" \
  9. node:12.8.1 bash -c "yarn && NODE_ENV=production yarn build && node build/engine.webpack.js"
  10. # Start back-end
  11. docker run --detach \
  12. --name nginx-proxy \
  13. --publish 80:80 \
  14. --publish 443:443 \
  15. --volume /etc/nginx/certs \
  16. --volume /etc/nginx/vhost.d \
  17. --volume /usr/share/nginx/html \
  18. --volume /var/run/docker.sock:/tmp/docker.sock:ro \
  19. jwilder/nginx-proxy
  20. docker run --detach \
  21. --name nginx-proxy-letsencrypt \
  22. --volumes-from nginx-proxy \
  23. --volume /var/run/docker.sock:/var/run/docker.sock:ro \
  24. jrcs/letsencrypt-nginx-proxy-companion
  25. docker run -itd -m 512m \
  26. --restart=always \
  27. --name luban-h5-front \
  28. -v `pwd`/front-end/h5/dist:/usr/share/nginx/html \
  29. --env "NODE_ENV=production" \
  30. --env "VIRTUAL_HOST=yourdomain.tld" \
  31. --env "LETSENCRYPT_HOST=yourdomain.tld" \
  32. --env "PROD_API_ORIGIN=api.yourdomain.tld" \
  33. nginx
  34. # Start font-end
  35. docker run -itd -m 1024m \
  36. --name luban-h5-api \
  37. --restart=always \
  38. -v `pwd`:/root -w /root/back-end/h5-api \
  39. -p 1337:1337 \
  40. --env "NODE_ENV=production" \
  41. --env "VIRTUAL_PORT=1337" \
  42. --env "VIRTUAL_HOST=api.yourdomain.tld" \
  43. --env "LETSENCRYPT_HOST=api.yourdomain.tld" \
  44. --restart=always node:12.8.1 \
  45. yarn start

稍等片刻,访问https://你的域名,部署完成。

关于Nginx、SSL的配置,可以参考:https://github.com/JrCs/docker-letsencrypt-nginx-proxy-companion


docker 自我学习,请忽略

  1. docker --rm
  2. 相当于:
  3. 1. docker run hello-world(hello-worldimage-name)
  4. 2. docker rm hello-world-container-id(通过 docker ps -a 查看)
  5. Docker容器退出时,默认容器内部的文件系统仍然被保留,以方便调试并保留用户数据。
  6. 但是,对于foreground容器,由于其只是在开发调试过程中短期运行,其用户数据并无保留的必要,因而可以在容器启动时设置--rm选项,这样在容器退出时就能够自动清理容器内部的文件系统。示例如下:
  7. docker run --rm bba-208等价于docker run --rm=true bba-208
  8. 显然,--rm选项不能与-d同时使用,即只能自动清理foreground容器,不能自动清理detached容器
  9. 注意,--rm选项也会清理容器的匿名data volumes
  10. 所以,执行docker run命令带--rm命令选项,等价于在容器退出后,执行docker rm -v
  11. Docker命令详解(run篇)
  12. 命令格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  13. Usage: Run a command in a new container
  14. 中文意思为:通过run命令创建一个新的容器(container
  15. 常用选项说明
  16. -d, --detach=false 指定容器运行于前台还是后台,默认为false
  17. -i, --interactive=false 打开STDIN,用于控制台交互
  18. -t, --tty=false 分配tty设备,该可以支持终端登录,默认为false
  19. -u, --user="" 指定容器的用户
  20. -a, --attach=[], 登录容器(必须是以docker run -d启动的容器)
  21. -w, --workdir="" 指定容器的工作目录
  22. -c, --cpu-shares=0 设置容器CPU权重,在CPU共享场景使用
  23. -e, --env=[], 指定环境变量,容器中可以使用该环境变量
  24. -m, --memory="" 指定容器的内存上限
  25. -P, --publish-all=false 指定容器暴露的端口
  26. -p, --publish=[], 指定容器暴露的端口
  27. -h, --hostname="" 指定容器的主机名
  28. -v, --volume=[], 给容器挂载存储卷,挂载到容器的某个目录
  29. --volumes-from=[], 给容器挂载其他容器上的卷,挂载到容器的某个目录
  30. --cap-add=[], 添加权限,权限清单详见:http://linux.die.net/man/7/capabilities
  31. --cap-drop=[], 删除权限,权限清单详见:http://linux.die.net/man/7/capabilities
  32. --cidfile="" 运行容器后,在指定文件中写入容器PID值,一种典型的监控系统用法
  33. --cpuset="" 设置容器可以使用哪些CPU,此参数可以用来容器独占CPU
  34. --device=[], 添加主机设备给容器,相当于设备直通
  35. --dns=[], 指定容器的dns服务器
  36. --dns-search=[], 指定容器的dns搜索域名,写入到容器的/etc/resolv.conf文件
  37. --entrypoint="" 覆盖image的入口点
  38. --env-file=[], 指定环境变量文件,文件格式为每行一个环境变量
  39. --expose=[], 指定容器暴露的端口,即修改镜像的暴露端口
  40. --link=[], 指定容器间的关联,使用其他容器的IPenv等信息
  41. --lxc-conf=[], 指定容器的配置文件,只有在指定--exec-driver=lxc时使用
  42. --name="" 指定容器名字,后续可以通过名字进行容器管理,links特性需要使用名字
  43. --net="bridge" 容器网络设置:
  44. bridge 使用docker daemon指定的网桥
  45. host //容器使用主机的网络
  46. container:NAME_or_ID >//使用其他容器的网路,共享IPPORT等网络资源
  47. none 容器使用自己的网络(类似--net=bridge),但是不进行配置
  48. --privileged=false 指定容器是否为特权容器,特权容器拥有所有的capabilities
  49. --restart="no" 指定容器停止后的重启策略:
  50. no:容器退出时不重启
  51. on-failure:容器故障退出(返回值非零)时重启
  52. always:容器退出时总是重启
  53. --rm=false 指定容器停止后自动删除容器(不支持以docker run -d启动的容器)
  54. --sig-proxy=true 设置由代理接受并处理信号,但是SIGCHLDSIGSTOPSIGKILL不能被代理
  55. 示例
  56. 运行一个在后台执行的容器,同时,还能用控制台管理:docker run -i -t -d ubuntu:latest
  57. 运行一个带命令在后台不断执行的容器,不直接展示容器内部信息:docker run -d ubuntu:latest ping www.docker.com
  58. 运行一个在后台不断执行的容器,同时带有命令,程序被终止后还能重启继续跑,还能用控制台管理,docker run -d --restart=always ubuntu:latest ping www.docker.com
  59. 为容器指定一个名字,docker run -d --name=ubuntu_server ubuntu:latest
  60. 容器暴露80端口,并指定宿主机80端口与其通信(: 之前是宿主机端口,之后是容器需暴露的端口),docker run -d --name=ubuntu_server -p 80:80 ubuntu:latest
  61. 指定容器内目录与宿主机目录共享(: 之前是宿主机文件夹,之后是容器需共享的文件夹),docker run -d --name=ubuntu_server -v /etc/www:/var/www ubuntu:latest

Heroku

因为 Strapi + postgresql 在 Heroku 上还有一些坑,需要官方解决,这个方案暂时pending