Hyperf 在 CentOS 等 RHEL 系统部署需注意 PHP ≥ 8.1、启用 swoole(5.0+)及 json/mbstring/pdo/opcache/redis 扩展,使用 Remi 仓库安装;SELinux 需放行端口如 semanage port -a -t http_port_t -p tcp 9501;应通过 systemd 托管服务并配置开机自启、日志与重启策略;firewalld 需开放对应端口。

Hyperf 在 CentOS(尤其是基于 Red Hat 的系统,如 CentOS 7/8、Rocky Linux、AlmaLinux)上部署时,需特别注意 PHP 环境、扩展依赖、SELinux 和 systemd 服务管理等环节。直接套用 Ubuntu/Debian 的安装步骤容易报错或运行异常。
PHP 版本与关键扩展必须匹配
Hyperf 3.x 要求 PHP ≥ 8.1,且必须启用以下扩展:
-
ext-swoole:推荐 5.0+(需编译时启用
--enable-openssl --enable-http2) - ext-json、ext-mbstring、ext-pdo、ext-redis(如用 Redis 组件)
- ext-opcache:生产环境必须开启,避免反复加载类文件
CentOS 默认源的 PHP 版本较旧(如 CentOS 7 自带 PHP 5.4),建议使用 Remi 仓库 安装 PHP 8.1+:
yum install epel-release -y && yum install https://www.php.cn/link/b53cbe6da81db747a73e52a5a48d2703enterprise/remi-release-8.rpm -yyum module reset php && yum module enable php:remi-8.1 && yum install php php-cli php-devel php-pdo php-opcache php-mbstring php-json php-redis -y
SELinux 与端口权限需显式放行
默认 SELinux 策略会阻止 Swoole 监听非标准端口(如 9501),导致 Permission denied 错误。
- 临时关闭验证:执行
setenforce 0(仅调试用) - 永久放行端口:运行
semanage port -a -t http_port_t -p tcp 9501(若无 semanage,先yum install policycoreutils-python-utils) - 确认 Swoole 进程上下文:启动后检查
ps auxZ | grep swoole,确保类型为httpd_t或自定义策略允许
使用 systemd 托管服务更稳定
不建议直接用 php bin/hyperf.php start 启动,应编写 systemd unit 文件实现开机自启、日志归集与崩溃重启。
示例 /etc/systemd/system/hyperf-app.service:
Description=Hyperf Application
After=network.target
[Service]
Type=simple
User=www
WorkingDirectory=/var/www/my-hyperf-app
ExecStart=/usr/bin/php bin/hyperf.php start
Restart=always
RestartSec=3
StandardOutput=journal
StandardError=journal
SyslogIdentifier=hyperf-app
[Install]
WantedBy=multi-user.target
启用服务:systemctl daemon-reload && systemctl enable hyperf-app && systemctl start hyperf-app
防火墙开放对应端口
CentOS 8+ 默认使用 firewalld,需手动添加端口规则:
firewall-cmd --permanent --add-port=9501/tcpfirewall-cmd --reload- 若启用 HTTPS 或 WebSocket,还需开放 443、80、或自定义 ws 端口
注意:不要同时启用 iptables 和 firewalld,二者冲突。










