在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的安全性。记得定期检查和更新你的系统和软件,以确保安全漏洞得到及时修复。