看板 KnucklesNote
作者 Knuckles (站長 那克斯)
標題 [RockyLinux9] PHP 安裝與設定
時間 2023-09-22 Fri. 20:03:39
安裝環境: Linode 的 Rocky Linux 9
安裝 PHP
先安裝 Apache
[RockyLinux9] 網頁伺服器 Apache 安裝與設定 - KnucklesNote板 - Disp BBS
安裝 PHP
$ sudo dnf install php
==========================================================================
Package Arch Version Repository Size
==========================================================================
Installing:
php x86_64 8.0.27-1.el9_1 appstream 10 k
Installing dependencies:
libxslt x86_64 1.1.34-9.el9 appstream 240 k
nginx-filesystem noarch 1:1.20.1-14.el9 appstream 10 k
oniguruma x86_64 6.9.6-1.el9.5 appstream 217 k
php-common x86_64 8.0.27-1.el9_1 appstream 667 k
Installing weak dependencies:
php-cli x86_64 8.0.27-1.el9_1 appstream 3.1 M
php-fpm x86_64 8.0.27-1.el9_1 appstream 1.6 M
php-mbstring x86_64 8.0.27-1.el9_1 appstream 470 k
php-opcache x86_64 8.0.27-1.el9_1 appstream 512 k
php-pdo x86_64 8.0.27-1.el9_1 appstream 83 k
php-xml x86_64 8.0.27-1.el9_1 appstream 131 k
Transaction Summary
==========================================================================
Rocky Linux 9 預設安裝的是 PHP 8.0
且預設就會使用 php-fpm 來執行 php
啟動且設定開機執行 php-fpm
$ sudo systemctl start php-fpm
$ sudo systemctl enable php-fpm
安裝要用到的 php 的模組
$ sudo dnf install php-gd php-mysqlnd
安裝 Apache 的 FastCGI 模組
$ sudo dnf install mod_fcgid
重新啟動 httpd
$ sudo systemctl restart httpd
檢查看看有沒有裝成功
$ php -v
PHP 8.0.27 (cli) (built: Jan 3 2023 16:17:26) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.27, Copyright (c) Zend Technologies
with Zend OPcache v8.0.27, Copyright (c), by Zend Technologies
利用 phpinfo() 檢查看看 httpd 與 php 是否正常
$ sudo echo "<?php phpinfo(); ?>" > /var/www/html/test_php.php
打開瀏覽器,網址輸入 http://(網址或IP位址)/test_php.php
可以看到 PHP 版本為 8.0.27 和各項 PHP 模組的設定值
修改 PHP 設定檔
$ sudo vim /etc/php.ini
;不在網頁顯示error,而是記錄在 /var/log/httpd/error_log
display_errors = Off
log_errors = On
;ignore_repeated_errors = Off
ignore_repeated_errors = On
;ignore_repeated_source = Off
ignore_repeated_source = On
;html_errors = On
html_errors = Off
設定error的log檔位置
;error_log = php_errors.log
error_log = /var/log/php_errors.log
;設定時區
;date.timezone =
date.timezone = Asia/Taipei
; session 的設定
; 儲存 session 的 cookie 存活時間(秒),設為0的話瀏覽器關掉即到期
;session.cookie_lifetime = 0
session.cookie_lifetime = 3600
; session存活時間
;session.gc_maxlifetime = 1440
session.gc_maxlifetime = 3600
有修改設定的話要重啟 php-fpm, apache
$ sudo systemctl restart php-fpm
$ sudo systemctl restart httpd
修改 php-fpm 設定檔
$ sudo vim /etc/php-fpm.d/www.conf
以下為預設值,可視需要修改
[www]
user = apache
group = apache
listen = /run/php-fpm/www.sock
listen.acl_users = apache,nginx
listen.allowed_clients = 127.0.0.1
;可依記憶體大小調整這些值,會影響效能
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
;如果有設定 request_slowlog_timeout 的話,將執行過久的記錄在這
slowlog = /var/log/php-fpm/www-slow.log
;執行 php-fpm 時的錯誤記錄檔
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
;session 的儲存方式
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
;用來儲存 opcache 檔案的路徑
php_value[opcache.file_cache] = /var/lib/php/opcache
有修改設定的話要重啟 php-fpm, apache
$ sudo systemctl restart php-fpm
$ sudo systemctl restart httpd
設定 Opcache
opcache (Optimizer Plus Cache) 可將編譯好的PHP存在記憶體中,
再次使用時就可以減少執行時間,預設已安裝了
編輯設定檔
$ sudo vim /etc/php.d/10-opcache.ini
;將以下設定的註解取消
;要用來儲存opcache的記憶體大小
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
;要用來加速的檔案數量
opcache.max_accelerated_files=10000
;用來儲存 opcache 檔案的路徑,寫在 php-fpm 的設定檔
;opcache.file_cache=
參考:
https://www.linode.com/docs/guides/how-to-install-and-configure-fastcgi-and-php-fpm-on-centos-8/
https://docs.rockylinux.org/guides/web/php/
更新 PHP 為 8.1 版
參考
https://www.linuxcapable.com/how-to-install-php-on-rocky-linux/
RockyLinux9 只提供 PHP 到 8.0 版,換成新版的話,要安裝 EPEL 和 Remi 軟體庫
$ sudo yum install epel-release
安裝 Remi repository for RockyLinux 9
$ sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-9.rpm
啟用 CRB (Code Ready Builder)
$ sudo dnf config-manager --set-enabled crb
安裝 dnf-utils (原本的 yum-utils)
$ sudo dnf install dnf-utils
查看 Remi 軟體庫中能使用的 PHP 版本
$ sudo dnf module list php
Remi's Modular repository for Enterprise Linux 9 - x86_64 246 kB/s | 647 kB 00:02
Safe Remi's RPM repository for Enterprise Linux 9 - x86_64 392 kB/s | 975 kB 00:02
Last metadata expiration check: 0:00:01 ago on Wed 21 Feb 2024 06:59:54 PM CST.
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
php remi-8.3 common [d], devel, minimal PHP scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
啟用 PHP 8.1
$ sudo dnf module enable php:remi-8.1
更新 PHP 與相關模組
$ sudo dnf update php
Upgraded:
php-8.1.27-1.el9.remi.x86_64 php-cli-8.1.27-1.el9.remi.x86_64
php-common-8.1.27-1.el9.remi.x86_64 php-fpm-8.1.27-1.el9.remi.x86_64
php-gd-8.1.27-1.el9.remi.x86_64 php-mbstring-8.1.27-1.el9.remi.x86_64
php-mysqlnd-8.1.27-1.el9.remi.x86_64 php-opcache-8.1.27-1.el9.remi.x86_64
php-pdo-8.1.27-1.el9.remi.x86_64 php-pecl-igbinary-3.2.15-1.el9.remi.8.1.x86_64
php-pecl-msgpack-2.2.0-1.el9.remi.8.1.x86_64 php-pecl-redis5-5.3.7-1.el9.remi.8.1.x86_64
php-pecl-zip-1.22.3-1.el9.remi.8.1.x86_64 php-xml-8.1.27-1.el9.remi.x86_64
Installed:
fribidi-1.0.10-6.el9.2.x86_64 gd3php-2.3.3-9.el9.remi.x86_64 gdk-pixbuf2-2.42.6-3.el9.x86_64
highway-1.0.7-1.el9.x86_64 jxl-pixbuf-loader-0.7.0-1.el9.x86_64 libaom-3.8.0-1.el9.x86_64
libavif-0.11.1-4.el9.x86_64 libdav1d-1.2.1-1.el9.x86_64 libimagequant-2.17.0-1.el9.x86_64
libjxl-0.7.0-1.el9.x86_64 libraqm-0.8.0-1.el9.x86_64 libsodium-1.0.18-8.el9.x86_64
libvmaf-2.3.0-2.el9.x86_64 oniguruma5php-6.9.9-1.el9.remi.x86_64 php-sodium-8.1.27-1.el9.remi.x86_64
rav1e-libs-0.7.1-1.el9.x86_64 remi-libzip-1.10.1-1.el9.remi.x86_64 shared-mime-info-2.1-5.el9.x86_64
重啟 apache 與 php-fpm
$ sudo systemctl restart httpd
$ sudo systemctl restart php-fpm
查看目前 PHP 版本
$ php -v
PHP 8.1.27 (cli) (built: Dec 19 2023 20:35:55) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.27, Copyright (c) Zend Technologies
with Zend OPcache v8.1.27, Copyright (c), by Zend Technologies
--
--
※ 作者: Knuckles 時間: 2023-09-22 20:03:39 (台灣)
※ 編輯: Knuckles 時間: 2024-02-21 21:27:30 (台灣)