在Debian上安全运行PHP涉及多个步骤,包括安装PHP、配置Web服务器、设置安全选项以及定期维护。以下是详细的指南:
sudo apt update
sudo apt upgrade -y
sudo apt install php php-cli php-fpm php-mysql php-gd php-curl php-opcache
php -v
sudo apt install apache2
sudo a2enmod rewrite
sudo systemctl restart apache2
/var/www/html)。sudo apt install nginx
/etc/nginx/sites-available/default),添加以下内容:location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
sudo systemctl restart nginx
编辑php.ini文件:
display_errors = Off
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php_errors.log
register_globals = Off
open_basedir = /var/www:/tmp
allow_url_fopen = Off
allow_url_include = Off
wget http://download.suhosin.org/suhosin-0.9.37.1.tar.gz
tar zxvf suhosin-0.9.37.1.tar.gz
cd suhosin-0.9.37.1
phpize
./configure --with-php-config=/usr/bin/php-config
make
sudo make install
在 php.ini 中添加:extension = suhosin.so
重启PHP-FPM服务:
sudo systemctl restart php8.2-fpm
ufw 或 iptables 限制入站和出站流量,仅允许必要的端口(如HTTP、HTTPS和SSH):sudo ufw enable
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 22/tcp
sudo ufw reload
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo systemctl restart sshd
定期备份数据: 制定一个备份计划,定期备份服务器的关键数据,并存储在安全的地点。
监控与日志管理: 利用监控工具监控系统状态,及时发现异常活动,并确保所有的登录尝试和服务活动都被记录到日志文件中。
通过以上步骤,可以显著提高Debian系统上PHP应用的安全性,减少潜在的安全风险。