在CentOS系统下进行PHP安全配置,可以遵循以下步骤:
更新系统和软件包:
sudo yum update
sudo yum update php php-mysqlnd php-gd php-xml php-mbstring php-pear php-bcmath
配置PHP安全选项:
/etc/php.ini
,根据需要进行以下配置:
display_errors
设置为Off
,以防止错误信息泄露给用户。display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log
upload_max_filesize = 2M
post_max_size = 8M
file_uploads = On
httponly
和secure
。session.cookie_httponly = On
session.cookie_secure = On
bcrypt
。password_hash_default = bcrypt
配置Web服务器安全:
/etc/httpd/conf/httpd.conf
或相关的虚拟主机配置文件,添加以下内容:<Directory "/var/www/html">
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
/etc/nginx/nginx.conf
或相关的虚拟主机配置文件,添加以下内容: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:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
配置防火墙:
firewalld
或iptables
配置防火墙,只允许必要的端口和服务。sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
定期备份:
监控和日志分析:
fail2ban
来防止暴力破解攻击。通过以上步骤,你可以大大提高CentOS系统下PHP应用的安全性。记得定期更新和审查你的配置,以应对新的安全威胁。