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

如何允许Apache服务器外部访问及解决PI监控PHP脚本空白页问题

Troubleshooting Your Raspberry Pi Daemon Monitor PHP Script & Apache External Access

Hey there! Let's break down your two problems step by step—getting that PHP script to show output through Apache, and opening up Apache to external connections.

Fixing the Blank PHP Page (CLI Works, Web Doesn't)

The most likely culprits here are permission mismatches, environment differences, or suppressed errors. Here's what to check:

  • Apache User SSH Permissions
    When you run the script in CLI, it uses your user account (which has SSH access to your Pis). But Apache runs as the www-data user by default, which probably doesn't have SSH keys set up for your Pis.

    1. Switch to the www-data user:
      sudo su - www-data -s /bin/bash
      
    2. Generate an SSH key pair (leave passphrase blank for automated access):
      ssh-keygen -t ed25519
      
    3. Copy the public key to each Raspberry Pi:
      ssh-copy-id pi@your-pi-ip-address
      
    4. Test the connection directly as www-data to confirm it works:
      ssh pi@your-pi-ip systemctl status your-daemon.service
      
  • Use Absolute Paths in Your PHP Script
    Apache's environment has a limited PATH compared to your CLI user. Instead of calling ssh or systemctl, use their full paths (you can find these with which ssh in CLI):

    // Instead of exec("ssh pi@pi-ip systemctl status daemon.service", $output);
    exec("/usr/bin/ssh pi@pi-ip /usr/bin/systemctl status daemon.service", $output);
    print_r($output);
    
  • Enable Error Reporting to See What's Breaking
    Blank pages often mean errors are being hidden. Add these lines at the top of your PHP script to reveal issues:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
  • Check for Security Restrictions
    Some systems use SELinux (RHEL/CentOS) or AppArmor (Ubuntu) that block Apache from initiating SSH connections.

    • For SELinux, temporarily disable it to test:
      sudo setenforce 0
      
      If the script works after this, you'll need to add a SELinux rule to allow Apache's SSH access permanently.
    • For AppArmor, check the Apache profile to ensure it doesn't restrict outgoing SSH traffic.

Allowing External Access to Apache

To let devices outside your server's local network (or even just other local devices) access your Apache-hosted page:

  • Configure Apache to Listen on All Interfaces
    Open your Apache ports config file (usually /etc/apache2/ports.conf on Debian/Ubuntu, /etc/httpd/conf/httpd.conf on RHEL/CentOS) and make sure the Listen directive targets all IPs, not just localhost:

    Listen 0.0.0.0:80
    # If using HTTPS, add this too:
    Listen 0.0.0.0:443
    
  • Update Virtual Host Configuration
    If you're using virtual hosts, ensure the <VirtualHost> block uses *:80 instead of 127.0.0.1:80 to accept requests from any IP:

    <VirtualHost *:80>
        ServerName your-monitor-domain.com
        DocumentRoot /var/www/monitor-script-folder
        # ... other configs ...
    </VirtualHost>
    

    Don't forget to restart Apache after changes:

    # Debian/Ubuntu
    sudo systemctl restart apache2
    # RHEL/CentOS
    sudo systemctl restart httpd
    
  • Open Firewall Ports
    Your server's firewall is probably blocking incoming HTTP/HTTPS traffic.

    • For Ubuntu/Debian (ufw):
      sudo ufw allow 80/tcp
      sudo ufw allow 443/tcp
      sudo ufw reload
      
    • For RHEL/CentOS (firewalld):
      sudo firewall-cmd --add-service=http --permanent
      sudo firewall-cmd --add-service=https --permanent
      sudo firewall-cmd --reload
      
  • Verify Network Accessibility
    From an external device, test if you can reach the server's IP on port 80:

    nc -zv your-server-ip 80
    

    If this succeeds, you should be able to access the monitor page via http://your-server-ip in a browser.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:12:04