如何允许Apache服务器外部访问及解决PI监控PHP脚本空白页问题
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 thewww-datauser by default, which probably doesn't have SSH keys set up for your Pis.- Switch to the
www-datauser:sudo su - www-data -s /bin/bash - Generate an SSH key pair (leave passphrase blank for automated access):
ssh-keygen -t ed25519 - Copy the public key to each Raspberry Pi:
ssh-copy-id pi@your-pi-ip-address - Test the connection directly as
www-datato confirm it works:ssh pi@your-pi-ip systemctl status your-daemon.service
- Switch to the
Use Absolute Paths in Your PHP Script
Apache's environment has a limitedPATHcompared to your CLI user. Instead of callingsshorsystemctl, use their full paths (you can find these withwhich sshin 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:
If the script works after this, you'll need to add a SELinux rule to allow Apache's SSH access permanently.sudo setenforce 0 - For AppArmor, check the Apache profile to ensure it doesn't restrict outgoing SSH traffic.
- For SELinux, temporarily disable it to test:
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.confon Debian/Ubuntu,/etc/httpd/conf/httpd.confon RHEL/CentOS) and make sure theListendirective targets all IPs, not just localhost:Listen 0.0.0.0:80 # If using HTTPS, add this too: Listen 0.0.0.0:443Update Virtual Host Configuration
If you're using virtual hosts, ensure the<VirtualHost>block uses*:80instead of127.0.0.1:80to 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 httpdOpen 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
- For Ubuntu/Debian (ufw):
Verify Network Accessibility
From an external device, test if you can reach the server's IP on port 80:nc -zv your-server-ip 80If this succeeds, you should be able to access the monitor page via
http://your-server-ipin a browser.
内容的提问来源于stack exchange,提问作者Kai

