以下是Ubuntu PHP环境的安全设置要点:
系统更新
定期更新系统和软件包,修复安全漏洞:
sudo apt update && sudo apt upgrade
PHP配置优化
php.ini:
disable_functions=exec,system,passthru,shell_execupload_max_filesize=2M,post_max_size=8Mallow_url_fopen=Off,allow_url_include=Offexpose_php=Off,display_errors=Off,log_errors=Onopen_basedir=/var/www/html:/tmpwww.conf:
user=www-data,group=www-datalisten=/run/php/php{version}-fpm.sockWeb服务器安全
sudo a2enmod security2 mod_evasiveServerTokens Prod,ServerSignature Offlocation ~* \.(php|conf)$ { deny all; }防火墙与权限管理
sudo ufw allow 'Nginx Full'
sudo ufw deny 9000/tcp # 若PHP-FPM使用TCP端口需放行
sudo ufw enable
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
加密与日志监控
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx
/var/log/php{version}-fpm.log和Web服务器访问日志其他安全措施
/etc/ssh/sshd_config,设置PermitRootLogin no参考来源: