在Ubuntu上配置LNMP(Linux, Nginx, MySQL, PHP)防火墙主要涉及配置UFW(Uncomplicated Firewall)。以下是详细步骤:
首先,确保你的系统上已经安装了UFW。如果没有安装,可以使用以下命令进行安装:
sudo apt update
sudo apt install ufw
安装完成后,启用UFW:
sudo ufw enable
系统会提示你确认是否启用UFW,输入y
并按回车键。
接下来,配置UFW规则以允许必要的端口。LNMP通常需要以下端口:
你可以使用以下命令来添加这些规则:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 3306/tcp
sudo ufw allow 9000/tcp
为了能够远程管理服务器,确保允许SSH访问:
sudo ufw allow ssh
你可以使用以下命令查看当前的UFW状态和规则:
sudo ufw status
如果你需要临时禁用UFW,可以使用以下命令:
sudo ufw disable
确保你的Nginx、MySQL和PHP-FPM服务已经正确配置并运行。
server {
listen 80;
server_name example.com www.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/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
确保MySQL配置文件(通常位于/etc/mysql/my.cnf
或/etc/mysql/mysql.conf.d/mysqld.cnf
)中没有阻止外部访问的设置。
确保PHP-FPM配置文件(通常位于/etc/php/7.4/fpm/pool.d/www.conf
)中的listen
参数设置正确:
listen = /run/php/php7.4-fpm.sock
最后,重启Nginx、MySQL和PHP-FPM服务以应用配置更改:
sudo systemctl restart nginx
sudo systemctl restart mysql
sudo systemctl restart php7.4-fpm
通过以上步骤,你应该能够在Ubuntu上成功配置LNMP防火墙。如果有任何问题,请检查日志文件以获取更多信息。