在 Ubuntu 上,您可以使用 Nginx 或 Apache 作为 Web 服务器来限制对 PHP-FPM 的访问。这里我将为您提供两种方法:使用 Nginx 和使用 Apache。
方法1:使用 Nginx 限制访问
sudo apt update
sudo apt install nginx
sudo nano /etc/nginx/sites-available/default
server
块中,添加以下内容以限制对 PHP-FPM 的访问:location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据您的 PHP 版本更改
# 限制 IP 访问
allow 192.168.1.1; # 允许的 IP 地址
deny all; # 拒绝其他所有 IP 地址
# 限制用户访问
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
保存并退出编辑器。
重启 Nginx 以应用更改:
sudo systemctl restart nginx
方法2:使用 Apache 限制访问
sudo apt update
sudo apt install apache2
mod_rewrite
和 mod_authz_core
模块:sudo a2enmod rewrite
sudo a2enmod authz_core
sudo nano /etc/apache2/sites-available/000-default.conf
<Directory>
块中,添加以下内容以限制对 PHP-FPM 的访问:<Directory /var/www/html>
Options FollowSymLinks
AllowOverride All
Require all granted
# 限制 IP 访问
Order Deny,Allow
Deny from all
Allow from 192.168.1.1 # 允许的 IP 地址
# 限制用户访问
<FilesMatch \.php$>
Require user your_username
</FilesMatch>
</Directory>
保存并退出编辑器。
重启 Apache 以应用更改:
sudo systemctl restart apache2
这样,您就可以根据 IP 地址或用户限制对 PHP-FPM 的访问了。请注意,您需要根据您的实际需求调整示例中的 IP 地址和用户名。