在Ubuntu系统中,使用PHP-FPM时,可以通过配置Nginx或Apache来限制对PHP文件的访问。这里分别为Nginx和Apache提供示例。
对于Nginx:
打开Nginx配置文件,通常位于/etc/nginx/sites-available/
目录下,例如your_domain.conf
。
在server
块中,添加以下内容:
location ~ \.php$ {
# 限制访问的IP地址
allow 192.168.1.1; # 允许的IP地址
deny all; # 拒绝其他所有IP地址
# 其他配置...
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据实际情况修改PHP-FPM版本
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
保存更改并退出。
重新加载Nginx配置:
sudo nginx -t # 检查配置文件语法是否正确
sudo systemctl reload nginx # 重新加载配置
对于Apache:
打开Apache配置文件,通常位于/etc/apache2/sites-available/
目录下,例如your_domain.conf
。
在<Directory>
块中,添加以下内容:
<Directory "/var/www/html"> # 根据实际情况修改网站根目录
# 限制访问的IP地址
Order deny,allow
Deny from all
Allow from 192.168.1.1 # 允许的IP地址
# 其他配置...
</Directory>
保存更改并退出。
重新加载Apache配置:
sudo systemctl reload apache2 # 重新加载配置
这样,只有指定的IP地址可以访问PHP文件,其他IP地址将被拒绝。请根据实际情况修改IP地址和配置文件路径。