> ## Documentation Index
> Fetch the complete documentation index at: https://docs.1000w123.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Strapi 安装

> Strapi 安装部署

## 1. 安装 Node.js v18

### 注意事项

* **不要使用 root 用户安装 Node.js**。建议使用非 root 用户进行安装，以避免权限问题。

### 使用 NVM 安装 Node.js

1. **安装 NVM（Node Version Manager）**： 打开终端并执行以下命令安装 NVM：

`sudo apt update && sudo apt upgrade -y   curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash`

安装完成后，加载 NVM：

`source ~/.bashrc`

2. **安装 Node.js v18**： 使用 NVM 安装 Node.js：

`nvm install 18`

3. **设置默认 Node.js 版本**： 设置默认使用的 Node.js 版本：

`nvm alias default 18`

## 2. 使用 SQLite 数据库

SQLite 是一个轻量级的嵌入式数据库，适合小型项目。

### 配置 Strapi 使用 SQLite

1. **安装 Strapi**： 在项目目录中初始化 Strapi：

`npx create-strapi-app my-project --quickstart`

这将自动配置 Strapi 使用 SQLite。

2. **检查配置**： 确保 `config/database.js` 中的配置正确，使用 SQLite：

```
module.exports = ({ env }) => ({
     connection: {
       client: 'sqlite',
       connection: {
         filename: path.join(__dirname, '..', env('DATABASE_FILENAME', '.tmp/data.db')),
       },
       useNullAsDefault: true,
     },
   });
```

## 3. 安装 Nginx 和申请 SSL

### 安装 Nginx

1. **更新包列表并安装 Nginx**：

`sudo apt update   sudo apt install nginx`

2. **启动并启用 Nginx**：

`sudo systemctl start nginx   sudo systemctl enable nginx`

### 配置 Nginx 反向代理

1. **创建 Nginx 配置文件**： 在 `/etc/nginx/sites-available/` 中创建一个新的配置文件，例如 `cms.bookstreet.top.conf`：

`sudo nano /etc/nginx/sites-available/cms.bookstreet.top.conf`

2. **编辑配置文件**： 添加以下内容，将 Nginx 配置为反向代理：

```
server {
       listen 80;
       server_name cms.bookstreet.top;

       location / {
           proxy_pass http://localhost:1337;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection 'upgrade';
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
       }
   }
```

3. **启用配置并重启 Nginx**：

`sudo ln -s /etc/nginx/sites-available/strapi.conf /etc/nginx/sites-enabled/   sudo nginx -t   sudo systemctl restart nginx`

### 申请 SSL 证书

1. **安装 Certbot**：

`sudo apt install certbot python3-certbot-nginx`

2. **申请 SSL 证书**：

`sudo certbot --nginx -d cms.bookstreet.top`

3. **自动续期**： Certbot 会自动设置续期任务。你可以通过以下命令手动测试续期：

`sudo certbot renew --dry-run`

## 4. 安装 PM2

1. **安装 PM2**： 使用 npm 安装 PM2：

`npm install pm2 -g`

2. **使用 PM2 启动 Strapi**： 在 Strapi 项目的根目录中，使用 PM2 启动：

`pm2 start npm --name strapi – start`

3. **设置 PM2 开机自启动**：

`pm2 startup`

根据输出的提示，执行生成的命令（可能需要 `sudo`）。

4. **保存 PM2 进程列表**：

`pm2 save`

***

通过这些步骤，成功地安装和配置 Node.js、SQLite、Nginx 和 PM2，并为你的应用程序提供 SSL 支持。
