FILEEM

POWER OF DREAM

strapi部署相关问题

《strapi部署相关问题》

环境

Node LTS(v12 或 V14)永远不会支持 Node 的奇数版本。
NPM v6 或 LTS Node 版本附带的任何版本
至少 1 个 CPU (强烈推荐至少 2 个)
至少 2 GB 的 RAM(中等推荐 4)
32 GB可用空间
支持的数据库版本
MySQL >= 5.6
MariaDB >= 10.1
PostgreSQL >= 10
SQLite >= 3
MongoDB >= 3.6
受支持的操作系统
Ubuntu >= 18.04(仅限 LTS)
Debian >= 9.x
CentOS/RHEL >= 8
macOS Mojave 或更新版本(不支持 ARM)
WIN 10

简单安装和启动开发环境

# npm安装 有指南
npx create-strapi-app my-project --quickstart
# 构建&重新构建
npm run build
# 启动开发模式
npm run develop


源码安装

拉源码

git clone https://github.com/strapi/strapi

生产模式构建

cd ./my-project/
npm install
NODE_ENV=production npm run build

生成生产模式配置文件,内容替换

cd ./my-project/
sudo nano ecosystem.config.js

模板如下

module.exports = {
  apps: [
    {
      name: 'strapi',
      cwd: 'https://fileem.com/home/your-name/my-strapi-project/my-project',
      script: 'npm',
      args: 'start',
      env: {
        NODE_ENV: 'production',
        DATABASE_HOST: 'localhost', // database endpoint
        DATABASE_PORT: '5432',
        DATABASE_NAME: 'strapi', // DB name
        DATABASE_USERNAME: 'your-name', // your username for psql
        DATABASE_PASSWORD: 'password', // your password for psql
      },
    },
  ],
};

用PM2启动,相当于守护进程

pm2 start ecosystem.config.js

解决strapi部署之后,加载admin panel过慢点问题

版本 v3.x
追加中间件: /config/middleware.js

module.exports = {
  settings: {
    gzip: {
      enabled: true,
      options: {
        br: false
      }
    }
  },
};

API 文档安装

npm run strapi install documentation

./extensions/documentation/config/settings.json手动创建文件以自定义 swagger ui 设置

{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "DOCUMENTATION",
    "description": "",
    "termsOfService": "YOUR_TERMS_OF_SERVICE_URL",
    "contact": {
      "name": "TEAM",
      "email": "contact-email@something.io",
      "url": "mywebsite.io"
    },
    "license": {
      "name": "Apache 2.0",
      "url": "https://www.apache.org/licenses/LICENSE-2.0.html"
    }
  },
  "x-strapi-config": {
    "path": "https://fileem.com/documentation",
    "showGeneratedFiles": true,
    "pluginsForWhichToGenerateDoc": [
      "email",
      "upload",
      "users-permissions"
    ]
  },
  "servers": [
    {
      "url": "http://localhost:1337",
      "description": "Development server"
    },
    {
      "url": "YOUR_STAGING_SERVER",
      "description": "Staging server"
    },
    {
      "url": "YOUR_PRODUCTION_SERVER",
      "description": "Production server"
    }
  ],
  "externalDocs": {
    "description": "Find out more",
    "url": "https://strapi.io/documentation/"
  },
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {},
  "tags": [],
  "components": {}
}

更改 settings.json 文件中的字段时,需要手动重新启动服务器。npm run build

更改面板端口和默认面板目录

修改config/server.js:
port,并新增url: ‘/console’,可以自定义

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 5000),
  admin: {
    url: 'https://fileem.com/console',
    auth: {
      secret: env('ADMIN_JWT_SECRET', '******f'),
    },
  },
});

创建 CRON 任务

https://strapi.io/documentation/developer-docs/latest/guides/scheduled-publication.html#create-a-cron-task

自定义数据响应

https://strapi.io/documentation/developer-docs/latest/guides/custom-data-response.html#custom-data-response
例如根据发布时间条件来确定是否要在结果中返回video

const { sanitizeEntity } = require('strapi-utils');

module.exports = {
  async find(ctx) {
    let entities;
    if (ctx.query._q) {
      entities = await strapi.services.lessons.search(ctx.query);
    } else {
      entities = await strapi.services.lessons.find(ctx.query);
    }

    return entities.map(entity => {
      const lessons = sanitizeEntity(entity, {
        model: strapi.models.lessons,
      });
      if (new Date(lessons.publish_at) > new Date()) {
        delete lessons.video;
      }
      return lessons;
    });
  },
};

设置JWT的过期时间

https://strapi.io/documentation/developer-docs/latest/development/plugins/users-permissions.html#jwt-configuration
expiresIn: 以秒或描述时间跨度 zeit/ms 的字符串表示。
例如:60、“45m”、“10h”、“2 天”、“7d”、“2y”。数值为秒计数。如果使用字符串,确保提供时间单位(分钟、小时、天、年等),否则默认使用毫秒单位(“120”等于“120ms”)。
路径 – extensions/users-permissions/config/security.json

{
  "jwt": {
    "expiresIn": "1d"
  }
}

点赞

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注