Loading... # 如何在CentOS 8上安装PHP7.4 / 7.3或7.2 ## 安装启用Remi存储库 以root或具有sudo特权的用户身份运行以下命令来安装和启用Remi存储库 > root 身份可直接进行命令。具有sudo特权的用户需要以sudo 接上命令来执行命令。 ``` dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm ``` ## 获取PHP版本列表 运行以下命令可获取所有的php版本列表 ``` dnf module list php ``` **版本列表** ``` root@dwtowen: ~ # sudo dnf module list php [16:42:21] Repository epel is listed more than once in the configuration Last metadata expiration check: 0:40:57 ago on Thu 22 Oct 2020 04:01:35 PM CST. CentOS-8 - AppStream Name Stream Profiles Summary php 7.2 [d] common [d], devel, minimal PHP scripting language php 7.3 common [d], devel, minimal PHP scripting language Remi's Modular repository for Enterprise Linux 8 - x86_64 Name Stream Profiles Summary php remi-7.2 common [d], devel, minimal PHP scripting language php remi-7.3 [e] common [d], devel, minimal PHP scripting language php remi-7.4 common [d], devel, minimal PHP scripting language php remi-8.0 common [d], devel, minimal PHP scripting language Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled root@dwtowen: ~ ``` ## 安装PHP 直接运行以下命令即可安装对应版本的PHP,根据版本列表直接换PHP对应版本号即可. **PHP 7.4** ``` dnf module reset php ``` ``` dnf module enable php:remi-7.4 ``` **PHP 7.3** ``` dnf module reset php ``` ``` dnf module enable php:remi-7.3 ``` **安装PHP常用模块** ``` dnf install php php-opcache php-gd php-curl php-mysqlnd php-zip ``` **设置php-fpm 开机启动** ``` systemctl enable --now php-fpm ``` ## PHP与Apache 如果SELinux在您的系统上运行,则需要更新SELinux安全或者直接关闭SELinux **更新SELinux安全** ``` chcon -Rt httpd_sys_rw_content_t /var/www ``` **关闭SELinux** 查看 selinux 是否开启 `getenforce` 可以查看 `SELinux` 状态,如果输出为 `enforcing` 表示已开启 1. 临时关闭 `selinux` ``` setenforce 0 ``` 设置 SELinux 成为 permissive 模式 ``` setenforce 1 ``` 设置 `SELinux` 成为 `enforcing` 模式 2. 永久关闭 `selinux` 修改 `/etc/selinux/config` 文件 将 `SELINUX=enforcing` 改为 `SELINUX=disabled` **重启Apache** ``` systemctl restart httpd ``` ## PHP与Nginx 默认情况下,PHP FPM以user身份运行`apache`。为使Nginx拥有足够权限,需要将用户更改为`nginx` 打开 `www.conf` 修改用户 ``` vim /etc/php-fpm.d/www.conf ``` 作如下修改 ``` ... user = nginx ... group = nginx ``` ## 更改PHP所有权 ``` chown -R root:nginx /var/lib/php ``` **重启 PHP** ``` systemctl restart php-fpm ``` **添加conf配置项** 添加以下位置块,以便Nginx可以处理PHP文件 ``` server { # . . . other code location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` **重启 PHP** ``` systemctl restart nginx ``` **更新SELinux安全** 更新SELinux安全(关闭请跳过) ``` chcon -Rt httpd_sys_rw_content_t /var/www ``` End Thanks! 😑 最后修改:2020 年 12 月 11 日 03 : 38 PM © 允许规范转载 赞赏 如果觉得我的文章对你有用,请随意赞赏 ×Close 赞赏作者 扫一扫支付 支付宝支付 微信支付