Strapi 是一个基于 JavaScript 语言构建的开源无头内容管理系统(CMS)。与其他无头 CMS 一样,Strapi 不自带前端界面,而是通过 API 为前端提供服务,允许你使用 React 和 Next.js 等流行框架构建网站。Strapi 基于插件系统,是一个灵活的 CMS,其管理面板和 API 都可扩展,且每个部分都可以自定义以匹配任何使用场景。Strapi 还内置了用户系统,可以详细管理管理员和最终用户的访问权限。
在本教程中,你将学习如何在 Rocky Linux 9 服务器上安装社区版 Strapi CMS,并配置 Nginx 作为反向代理服务器。
前提条件
一台运行 Rocky Linux 9 的服务器,至少 2GB 内存和 1 个 CPU 核心。
一个具有 sudo 权限的非 root 用户。
一个完全限定的域名(FQDN),例如 strapi.example.com。
确保系统已更新。
$ sudo dnf update
安装一些系统需要的常用软件包。
$ sudo dnf install wget curl nano unzip yum-utils -y
其中一些软件包可能已经安装在系统上。
第一步 - 配置防火墙
第一步是配置防火墙。Rocky Linux 使用 Firewalld 防火墙。检查防火墙状态。
$ sudo firewall-cmd --state
running
防火墙使用不同的区域,public 区域是默认使用的区域。列出防火墙上所有活动的服务和端口。
$ sudo firewall-cmd --permanent --list-services
应该显示以下输出。
cockpit dhcpv6-client ssh
Strapi 需要 HTTP 和 HTTPS 端口才能运行。打开它们。
$ sudo firewall-cmd --permanent --add-service=http
$ sudo firewall-cmd --permanent --add-service=https
重新加载防火墙以应用更改。
$ sudo firewall-cmd --reload
再次列出所有服务。
$ sudo firewall-cmd --permanent --list-services
你应该得到以下输出。
cockpit dhcpv6-client http https ssh
第二步 - 安装和配置 PostgreSQL
Strapi 支持 PostgreSQL 11 及以上版本。Rocky Linux 9 默认附带 PostgreSQL 13。本教程将使用 PostgreSQL 15。
运行以下命令添加 PostgreSQL GPG 密钥。
$ curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql-key.gpg >/dev/null
安装 PostgreSQL 仓库 RPM 文件。
$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
禁用内置的 PostgreSQL 模块。
$ sudo dnf -qy module disable postgresql
现在,可以使用以下命令安装 PostgreSQL。
$ sudo dnf install -y postgresql15-server
初始化数据库。
$ sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
启用 PostgreSQL 服务。
$ sudo systemctl enable postgresql-15
启动 PostgreSQL 服务。
$ sudo systemctl start postgresql-15
启动 PostgreSQL shell。
$ sudo -i -u postgres psql
创建 Strapi 数据库。
postgres=# CREATE DATABASE strapidb;
创建 Strapi 用户并设置一个强密码。
postgres-# CREATE USER strapiuser WITH PASSWORD 'Your_Password';
将数据库所有者更改为 Strapi 用户。
postgres-# ALTER DATABASE strapidb OWNER TO strapiuser;
退出 shell。
postgres-# \q
验证你的凭据是否有效。
$ psql --username strapiuser --password --host localhost strapidb
输入密码后,如果能成功连接到数据库,说明配置正确。输入 \q 退出。
第三步 - 安装 Node.js
Rocky Linux 9 自带 Node v16,已过时。我们将安装 Node 的最新 LTS 版本,撰写本文时为 v18。
从 Nodesource 获取 Node v18 安装程序。
$ curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
安装 Node.js。
$ sudo dnf install nodejs -y
验证 Node.js 版本。
$ node -v
v18.13.0
第四步 - 安装 Strapi
运行以下命令安装 Strapi。
$ npx create-strapi-app@latest howtoforge-project
输入 y 继续安装。接下来系统会要求你选择安装类型。选择 Custom 并按以下方式回答问题。
? Choose your installation type Custom (manual settings)
? Choose your preferred language JavaScript
? Choose your default database client postgres
? Database name: strapidb
? Host: 127.0.0.1
? Port: 5432
? Username: strapiuser
? Password: Your_Password
? Enable SSL connection: No
根据你的需求,可以选择 TypeScript 或 JavaScript 作为 Strapi 的语言。
安装完成后,你就可以构建 Strapi 项目了。切换到项目目录。
$ cd howtoforge-project
运行以下命令构建项目,包括 Strapi Admin UI。
$ NODE_ENV=production npm run build
使用以下命令启动 Strapi 服务器。
$ node ~/howtoforge-project/node_modules/.bin/strapi start
你的应用程序应该在 URL http://<服务器IP>:1337 上可见。但首先需要在防火墙中打开端口。
$ sudo firewall-cmd --permanent --add-port=1337/tcp
$ sudo firewall-cmd --reload
打开 URL 后,你应该能看到 Strapi 的欢迎页面。

在终端中按 Ctrl + C 停止服务器。你应该删除防火墙规则,因为不再需要它。
$ sudo firewall-cmd --permanent --remove-port=1337/tcp
$ sudo firewall-cmd --reload
第五步 - 安装和配置 PM2
我们可以使用 PM2(Process Manager 2)来管理进程,而不是手动启动服务器,并为它创建一个 systemd 服务。
切换到主目录。
$ cd ~
安装 PM2。
$ sudo npm install pm2@latest -g
创建并打开 PM2 配置文件进行编辑。
$ sudo nano ecosystem.config.js
在文件中粘贴以下内容。确保输入正确的目录名和 PostgreSQL 凭据。
module.exports = {
apps: [
{
name: 'strapi',
cwd: '/home/navjot/howtoforge-project',
script: 'npm',
args: 'start',
env: {
NODE_ENV: 'production',
DATABASE_HOST: 'localhost',
DATABASE_PORT: '5432',
DATABASE_NAME: 'strapidb',
DATABASE_USERNAME: 'strapiuser',
DATABASE_PASSWORD: 'Your_Password',
},
},
],
};
按 Ctrl + X 并输入 Y 保存文件。
使用 PM2 在后台运行 Strapi 实例。
$ pm2 start ecosystem.config.js
在 PM2 下运行的应用程序在崩溃或被终止时会自动重启。
使用以下命令创建启动 systemd 脚本。
$ pm2 startup
复制上面输出中的命令并运行它。
$ sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u navjot --hp /home/navjot
保存 PM2 进程列表。
$ pm2 save
你的 Strapi 服务现在已在后台以生产模式运行。
第六步 - 安装 Nginx
Rocky Linux 9 自带的 Nginx 版本较旧。你需要下载官方 Nginx 仓库来安装最新版本。
创建并打开 /etc/yum.repos.d/nginx.repo 文件来创建官方 Nginx 仓库。
$ sudo nano /etc/yum.repos.d/nginx.repo
在文件中粘贴以下代码。
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
安装 Nginx 服务器。
$ sudo dnf install -y nginx
验证安装。
$ nginx -v
nginx version: nginx/1.22.1
启用并启动 Nginx 服务器。
$ sudo systemctl enable nginx --now
第七步 - 安装 SSL
我们需要安装 Certbot 来生成 SSL 证书。我们将使用 Snapd 包安装器。由于 Rocky Linux 不自带 Snapd,需要先安装它。它需要 EPEL 仓库才能工作。
$ sudo dnf install -y epel-release
安装 Snapd。
$ sudo dnf install -y snapd
启用并启动 Snap 服务。
$ sudo systemctl enable snapd --now
安装 Snap 核心包,并确保你的 Snapd 版本是最新的。
$ sudo snap install core && sudo snap refresh core
创建 Snapd 工作所需的链接。
$ sudo ln -s /var/lib/snapd/snap /snap
$ echo 'export PATH=$PATH:/var/lib/snapd/snap/bin' | sudo tee -a /etc/profile.d/snapd.sh
使用以下命令安装 Certbot。
$ sudo snap install --classic certbot
创建符号链接以确保可以运行 Certbot 命令。
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
验证安装。
$ certbot --version
certbot 2.2.0
运行以下命令生成 SSL 证书。
$ sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m name@example.com -d strapi.example.com
上述命令会将证书下载到服务器的 /etc/letsencrypt/live/strapi.example.com 目录。
生成 Diffie-Hellman 组证书。
$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
要进行 SSL 续期干运行测试。
$ sudo certbot renew --dry-run
如果没有看到错误,说明一切正常。你的证书将自动续期。
第八步 - 配置 Nginx
打开 /etc/nginx/nginx.conf 文件进行编辑。
$ sudo nano /etc/nginx/nginx.conf
在 include /etc/nginx/conf.d/*.conf; 行之前添加以下行。
server_names_hash_bucket_size 64;
创建并打开 /etc/nginx/conf.d/strapi.conf 文件进行编辑。
$ sudo nano /etc/nginx/conf.d/strapi.conf
在文件中粘贴以下代码。
server {
listen 80;
listen [::]:80;
server_name strapi.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name strapi.example.com;
access_log /var/log/nginx/strapi.access.log;
error_log /var/log/nginx/strapi.error.log;
ssl_certificate /etc/letsencrypt/live/strapi.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/strapi.example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/strapi.example.com/chain.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_stapling on;
ssl_stapling_verify on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:1337;
}
}
验证 Nginx 配置文件语法。
$ sudo nginx -t
重启 Nginx 服务。
$ sudo systemctl restart nginx
你现在可以通过 URL https://strapi.example.com 访问 Strapi CMS。访问 https://strapi.example.com/admin URL 来创建管理员用户。

第九步 - 升级 Strapi
升级 Strapi 的第一步是停止服务器。
$ cd ~
$ pm2 stop ecosystem.config.js
切换到项目目录并打开 package.json 文件进行编辑。
$ cd howtoforge-project
$ nano package.json
将所有 Strapi 包版本号升级到最新的稳定 Strapi 版本。你可以从 Strapi 的 GitHub 发布页面获取最新可用版本。
"dependencies": {
"@strapi/strapi": "4.5.5",
"@strapi/plugin-users-permissions": "4.5.5",
"@strapi/plugin-i18n": "4.5.5",
"pg": "8.6.0"
},
这里需要将 4.5.5 更改为最新的稳定版本。保存文件。
安装升级版本。
$ npm install
重新构建管理面板。
$ NODE_ENV=production npm run build
再次启动服务器。
$ cd ~
$ pm2 start ecosystem.config.js
你的 Strapi 安装现在已升级并运行。
总结
以上就是在 Rocky Linux 9 服务器上安装 Strapi CMS 并配置 Nginx 作为反向代理服务器的完整教程。如有任何问题,请在评论区留言。