要在Nginx服务器上启用phpinfo功能,您需要确保PHP已正确安装并与Nginx集成。以下是针对Linux和Windows系统的详细步骤:
# 对于Ubuntu/Debian
sudo apt-get install php-fpm
# 对于CentOS/RHEL
sudo yum install php-fpm
编辑Nginx的站点配置文件(通常在/etc/nginx/sites-available/your_site
):
server {
listen 80;
server_name your_domain.com;
root /var/www/html;
index index.php index.html index.htm;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据实际PHP版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
sudo echo "<?php phpinfo(); ?>" > /var/www/html/info.php
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm # 根据实际PHP版本调整
取消注释或添加以下行:
extension_dir = "ext"
cgi.fix_pathinfo=1
编辑nginx.conf文件:
server {
listen 80;
server_name localhost;
root C:/www;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在网站根目录(如C:\www)创建info.php文件:
<?php phpinfo(); ?>
C:\php\php-cgi.exe -b 127.0.0.1:9000 -c C:\php\php.ini
在浏览器中访问:
http://your_server_ip_or_domain/info.php
如果必须保留,至少应该限制访问:
location = /info.php {
allow 192.168.1.0/24; # 仅允许特定IP访问
deny all;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
考虑使用.user.ini
文件在特定目录启用phpinfo,而不是全局启用
如果您遇到任何问题,请检查Nginx错误日志和PHP-FPM日志以获取更多调试信息。