You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 18.04服务器Nginx无法运行PHP文件求助

Troubleshooting PHP File Access Issues on Nginx (Ubuntu 18.04)

Hey there, let’s work through why your PHP files are throwing a "This site can't be reached" error while HTML files load perfectly fine. Since your PHP-FPM service shows as active and running, we can focus on a few key areas that often cause this mismatch:

1. Verify Unix Socket Permissions

Nginx needs proper access to the PHP-FPM Unix socket to pass requests to PHP. Let’s check the socket’s ownership first:

ls -l /var/run/php/php7.2-fpm.sock

Look for the owner and group fields—they should both be www-data (the default user Nginx runs under). If they aren’t:

  • Open your PHP-FPM pool config file:
    nano /etc/php/7.2/fpm/pool.d/www.conf
    
  • Find and update these lines:
    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0660
    
  • Save the file and restart PHP-FPM:
    systemctl restart php7.2-fpm
    

2. Check Nginx Error Logs

Logs are your best friend for debugging—let’s pull the latest Nginx error messages while testing your PHP file:

  • In one terminal, run:
    tail -f /var/log/nginx/error.log
    
  • In another browser tab, try accessing your phpinfo.php file. The log will show exactly why the request fails (e.g., permission denied on the socket, invalid file paths).

3. Ensure PHP File Permissions

Make sure your PHP files are readable by the Nginx user:

  • Set the correct ownership for your web root:
    chown -R www-data:www-data /var/www/html
    
  • Set secure file permissions:
    chmod -R 644 /var/www/html/*.php
    chmod -R 755 /var/www/html/
    

4. Test TCP Connection Instead of Unix Socket

Sometimes Unix socket issues are tricky to debug—let’s switch PHP-FPM to listen on a TCP port temporarily to rule out socket-specific problems:

  • Edit your PHP-FPM pool config again:
    nano /etc/php/7.2/fpm/pool.d/www.conf
    
  • Replace the listen line (which points to the socket) with:
    listen = 127.0.0.1:9000
    
  • Restart PHP-FPM:
    systemctl restart php7.2-fpm
    
  • Update your Nginx config (the default server block) to use the TCP port instead of the socket:
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass 127.0.0.1:9000; # Replace the socket line with this
    }
    
  • Restart Nginx:
    systemctl restart nginx
    
  • Try accessing your PHP file again. If this works, the issue was related to the Unix socket’s configuration or permissions.

5. Confirm No Firewall Interference

While Unix sockets aren’t affected by network firewalls, double-check that your local firewall (ufw) isn’t blocking unexpected traffic:

ufw status

You should see 80/tcp allowed (since HTML works). If you switched to TCP port 9000, ensure it’s allowed for local connections (though by default, ufw allows loopback traffic).


内容的提问来源于stack exchange,提问作者Huskeer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:54:36