在CentOS系統中配置PHP的安全設置,可以遵循以下步驟:
首先,確保你的系統和PHP都是最新的。
sudo yum update -y
sudo yum install epel-release -y
sudo yum install php php-cli php-fpm php-mysqlnd php-gd php-mbstring php-xml php-pear php-bcmath -y
編輯PHP-FPM的配置文件 /etc/php-fpm.d/www.conf
或 /etc/php-fpm.conf
,根據需要進行調整。
; 設置監聽端口
listen = /run/php-fpm/www.sock
; 設置用戶和組
user = apache
group = apache
; 設置最大連接數
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
; 設置請求超時時間
request_terminate_timeout = 60s
; 禁用危險函數
disable_functions = eval, assert, system, shell_exec, passthru, exec, popen, curl_exec, curl_multi_exec, parse_ini_file, show_source
編輯PHP的主配置文件 /etc/php.ini
或 /etc/php.d/
目錄下的相關文件。
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log
file_uploads = On
upload_max_filesize = 10M
post_max_size = 10M
session.cookie_httponly = On
session.cookie_secure = On
session.cookie_samesite = Strict
password_hash_default = bcrypt
disable_functions = eval, assert, system, shell_exec, passthru, exec, popen, curl_exec, curl_multi_exec, parse_ini_file, show_source
如果你使用的是Apache或Nginx,還需要配置相應的Web服務器。
編輯 /etc/httpd/conf/httpd.conf
或 /etc/httpd/conf.d/
目錄下的相關文件。
<Directory "/var/www/html">
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
編輯 /etc/nginx/nginx.conf
或 /etc/nginx/conf.d/
目錄下的相關文件。
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
最后,重啟PHP-FPM和Web服務器以應用更改。
sudo systemctl restart php-fpm
sudo systemctl restart httpd # 如果使用Apache
# 或者
sudo systemctl restart nginx # 如果使用Nginx
通過以上步驟,你可以大大提高CentOS系統中PHP的安全性。記得定期檢查和更新你的系統和軟件,以確保安全漏洞得到及時修復。