LNMP是指Linux系统下Nginx、MySQL和PHP的组合,这是一个非常流行的用于部署Web应用的环境组合。在Linux服务器上配置LNMP环境时,安全性是一个重要的考虑因素。以下是一个基本的LNMP安全设置指南:
1. 更新和升级软件
保持系统的软件包和内核处于最新状态是确保安全的关键步骤之一。及时应用补丁和安全更新可以修复已知的漏洞和弱点。
sudo apt update && sudo apt upgrade -y
2. 安装Nginx
安装Nginx并确保其配置文件中禁用了目录列表和版本信息。
sudo apt install nginx -y
在Nginx配置文件中添加以下内容:
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本修改 } location ~ /\.ht { deny all; } }
3. 安装MySQL
安装MySQL并运行安全脚本来提高其安全性。
sudo apt install mysql-server -y sudo mysql_secure_installation
4. 安装PHP
安装PHP及其MySQL扩展和FastCGI进程管理器。
sudo apt install php-fpm php-mysql -y
5. 配置PHP
编辑PHP的配置文件/etc/php/7.4/fpm/php.ini
(根据你的PHP版本修改路径),禁用不安全的PHP函数,设置open_basedir
限制PHP的访问范围。
display_errors = Off expose_php = Off enable_dl = Off disable_functions = eval,passthru,exec,system,chroot,scandir,shell_exec,proc_open,proc_get_status,ini_alter,ini_a open_basedir = /var/www/html:/tmp/
6. 配置Nginx支持PHP
确保Nginx配置文件中的fastcgi_pass
指向正确的PHP-FPM套接字文件。
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本修改 }
7. 设置文件和目录权限
为网站目录设置正确的权限,确保Nginx和PHP-FPM进程有权访问这些文件。
chown -R www-data:www-data /var/www/html find /var/www/html -type d -exec chmod 755 {} \; find /var/www/html -type f -exec chmod 644 {} \;
8. 使用防火墙
配置防火墙以限制对服务器的访问。
sudo ufw allow 'Nginx Full' sudo ufw enable
9. 定期备份数据
定期备份数据库和网站文件,以防数据丢失。
sudo mysqldump -u root -p your_database_name > your_database_backup.sql
10. 监控和日志记录
配置日志记录以监控服务器的安全事件。
sudo cp /etc/rsyslog.conf /etc/rsyslog.conf.bak sudo nano /etc/rsyslog.conf # 编辑配置文件以启用详细日志记录 sudo systemctl restart rsyslog
以上步骤提供了一个基本的LNMP安全设置框架,具体配置可能需要根据实际环境和需求进行调整。