Rocky Linux 9.1 安装 PHP 8.2

作者: 温新

分类: 【Linux】

阅读: 2519

时间: 2023-03-09 05:32:08

hi,我是温新,一名 PHPer

Rocky Linux 在 EL9 & EL8 中 不支持 PHP 8.2,现在我们将使用 Remi 源来安装最新版本的 PHP。

第一步:更新系统中所有的软件包

# 更新软件包
sudo dnf upgrade --refresh -y

第二步:安装 PHP Remi 源

1)安装 EPEL 源

sudo dnf install \
    https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm \
    https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-9.noarch.rpm

2)安装 Remi 源

sudo dnf -y install http://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf -y install  dnf-utils 

第三步:验证安装的 Remi 源

dnf module list
    
# 输出如下结果,说明已安装
Last metadata expiration check: 0:04:41 ago on Thu Mar  9 11:28:05 2023.
Rocky Linux 9 - AppStream
Name         Stream     Profiles                                Summary                                                 
maven        3.8        common [d]                              Java project management and project comprehension tool  
nodejs       18         common [d], development, minimal, s2i   Javascript runtime                                      
php          8.1        common [d], devel, minimal              PHP scripting language                                  
ruby         3.1        common [d]                              An interpreter of object-oriented scripting language    

# Remi 源部分
Remi's Modular repository for Enterprise Linux 9 - x86_64
Name         Stream     Profiles                                Summary                                                 
composer     2          common [d]                              Dependency Manager for PHP                              
memcached    remi       common [d]                              High Performance, Distributed Memory Object Cache       
php          remi-7.4   common [d], devel, minimal              PHP scripting language                                  
php          remi-8.0   common [d], devel, minimal              PHP scripting language                                  
php          remi-8.1   common [d], devel, minimal              PHP scripting language                                  
php          remi-8.2   common [d], devel, minimal              PHP scripting language                                  
redis        remi-5.0   common [d]                              Redis persistent key-value database                     
redis        remi-6.0   common [d]                              Redis persistent key-value database                     
redis        remi-6.2   common [d]                              Redis persistent key-value database                     
redis        remi-7.0   common [d]                              Redis persistent key-value database                     

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

第四步:删除已经的的 PHP

删除现有的 PHP 版本前,备份 php.ini 配置文件。

# 删除 PHP
sudo dnf -y remove php php-fpm
    
# 删除相关扩展包
sudo dnf -y remove php*
    
# 重置 PHP 模块列表
sudo dnf -y module list reset php

重置 PHP 模块列表后的输出信息如下:

# 重置 PHP 模块输出信息如下:
Last metadata expiration check: 0:10:35 ago on Thu Mar  9 11:28:05 2023.
Rocky Linux 9 - AppStream
Name                Stream                 Profiles                                 Summary                             
php                 8.1                    common [d], devel, minimal               PHP scripting language              

Remi's Modular repository for Enterprise Linux 9 - x86_64
Name                Stream                 Profiles                                 Summary                             
php                 remi-7.4               common [d], devel, minimal               PHP scripting language              
php                 remi-8.0               common [d], devel, minimal               PHP scripting language              
php                 remi-8.1               common [d], devel, minimal               PHP scripting language              
php                 remi-8.2               common [d], devel, minimal               PHP scripting language              

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

第五步:启用 Remi 源 安装 PHP 8.2

1)查看 PHP 版本

# 查看 PHP 版本
sudo dnf module list php

# 输出信息如下
Remi's Modular repository for Enterprise Linux 9 - x86_64
Name                Stream                 Profiles                                 Summary                             
php                 remi-7.4               common [d], devel, minimal               PHP scripting language              
php                 remi-8.0               common [d], devel, minimal               PHP scripting language              
php                 remi-8.1               common [d], devel, minimal               PHP scripting language              
php                 remi-8.2               common [d], devel, minimal               PHP scripting language              

2)启用(安装)PHP 8.2

sudo dnf -y module enable php:remi-8.2
    
# 输出信息如下:
Last metadata expiration check: 0:14:50 ago on Thu Mar  9 11:28:05 2023.
Dependencies resolved.
========================================================================================================================
 Package                     Architecture               Version                       Repository                   Size
========================================================================================================================
Enabling module streams:
 php                                                    remi-8.2                                                       
Transaction Summary
========================================================================================================================

3)安装 PHP

刚刚只是启用了 PHP 8.2,但还没有安装,下面进行安装

sudo dnf -y install php

第六步:nginx 用户

sudo dnf -y install php php-fpm

第七步:PHP 扩展安装(可选)

1)安装扩展

sudo dnf install php-cli php-fpm php-curl php-mysqlnd php-gd php-opcache php-zip php-intl php-common php-bcmath php-imagick php-xmlrpc php-json php-readline php-memcached php-redis php-mbstring php-apcu php-xml php-dom php-redis php-memcached php-memcache

这些扩展可以根据需要进行安装。

2)查询扩展

比如这里没有列出 Swoole 扩展,但是我们要安装该怎么办?查询扩展,然后进行安装。

sudo dnf search php82-php- | grep swoole

3)查询已安装的扩展

php -m

第八步:安装 php-devel

如果后续需要使用 phpize 就需要安装它了

sudo dnf -y install php-devel

第九步:查询 PHP 版本

php -v
    
# 输出信息
PHP 8.2.3 (cli) (built: Feb 14 2023 01:06:39) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.3, Copyright (c), by Zend Technologies

第十步:PHP 8.2 安装 PHP-FPM

1)修改 php-fpm 运行用户,编译 www.conf 文件。

# 编辑配置文件
vim /etc/php-fpm.d/www.conf

2)原内容

user = apache
group = apache

3)修改用户与用户组为 nginx

将 2)中的内容修改为 nginx

user = nginx
group = nginx

这个地方可以改为其他用户,如 www 用户

第十步:重启 php-fpm

sudo systemctl restart php-fpm.service

第十一步:配置 nginx 解析 PHP

1)修改 vim /etc/nginx/nginx.conf

# 修改一
root         /www;
# 修改二
location ~ \.php$ {
    root           /www;
    fastcgi_pass   unix:/run/php-fpm/www.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
} 

2)验证 nginx 配置是否正确

nginx -t

3)重启 nginx

sudo systemctl restart nginx.service

4)创建 www 目录

mkdir /www
echo '<?php phpinfo();' > /www/index.php

第十二步:添加其他用户操作 www 目录的权限

sudo usermod -a -G nginx 你的用户名
sudo chmod -R 775 /www

第十下:查询 nginx 版本

nginx -v
nginx version: nginx/1.20.1
请登录后再评论