在 CentOS 上搭建 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 环境后,安全管理非常重要。下面从 系统层、服务层、应用层、运维层 四个维度,给你一套实用、可落地的 CentOS LAMP 安全管理方案。
yum update -y
定期检查并更新安全补丁(建议开启自动更新或定时任务)。
编辑 SSH 配置:
vi /etc/ssh/sshd_config
修改:
PermitRootLogin no
PasswordAuthentication no
使用 SSH 密钥登录 更安全。
useradd admin
passwd admin
usermod -aG wheel admin
只开放必要端口:
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --permanent --remove-service=ssh --zone=public
firewall-cmd --reload
systemctl list-unit-files | grep enabled
关闭如 telnet、ftp 等老旧服务。
vi /etc/httpd/conf/httpd.conf
ServerTokens Prod
ServerSignature Off
Options -Indexes
示例(phpMyAdmin):
<Directory "/usr/share/phpmyadmin">
Require ip 192.168.1.0/24
</Directory>
yum install certbot python2-certbot-apache
certbot --apache
mysql_secure_installation
CREATE USER 'appuser'@'localhost' IDENTIFIED BY '强密码';
GRANT SELECT,INSERT,UPDATE ON appdb.* TO 'appuser'@'localhost';
bind-address=127.0.0.1
vi /etc/php.ini
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
display_errors = Off
log_errors = On
upload_max_filesize = 2M
post_max_size = 8M
避免使用 CentOS 默认过旧的 PHP(如 5.x)。
chown -R apache:apache /var/www/html
chmod -R 755 /var/www/html
禁止 777
上传目录不给执行权限:
<Directory "/var/www/html/uploads">
php_admin_flag engine off
</Directory>
/var/log/httpd//var/log/mariadb//var/log/php_errors.log防止暴力破解:
yum install epel-release
yum install fail2ban
systemctl enable fail2ban
mysqldump -u root -p appdb > backup.sql
tar czf html.tar.gz /var/www/html
✅ 不用 root
✅ 不用弱密码
✅ 不开放多余端口
✅ 不上线测试环境
✅ 定期更新和备份
如果你愿意,我可以:
你现在是 CentOS 几?LAMP 用在什么场景?