Ubuntu14.04下Apache2部署的PHP5文件间歇性无法运行求助
Hey there, let's tackle this frustrating intermittent issue with your main.php file. It's tricky when something works sometimes and not others, but we can break it down step by step to find the root cause:
1. Check Apache & PHP Error Logs
Logs are your best friend for intermittent problems—they’ll usually show what’s failing when the issue pops up.
- Apache Error Log: Run
tail -f /var/log/apache2/error.login a terminal to stream logs in real-time. Then refreshlocalhost/main.phpa few times (both when it works and fails) and watch for error messages like permission denied, PHP module crashes, or resource limits. - PHP Error Log: By default, PHP might not show errors on the page. Edit your PHP config file at
/etc/php5/apache2/php.ini:- Set
display_errors = Onanderror_reporting = E_ALL - Look for the
error_logline to find where PHP logs are stored (usually/var/log/php5/apache2.log) - Restart Apache with
sudo service apache2 restartand test again—you might see errors directly on the page now.
- Set
2. Verify File Permissions & Ownership
Odd permissions can sometimes cause intermittent issues (especially if other processes are modifying the file). Check your main.php file:
- Run
ls -l /var/www/html/main.phpto see permissions and ownership. - Ensure the file is readable by the Apache user (
www-data). Fix permissions with:sudo chown www-data:www-data /var/www/html/main.php sudo chmod 644 /var/www/html/main.php
3. Check PHP-FPM Stability (If Used)
Ubuntu 14.04 often uses PHP-FPM with Apache. If the FPM process crashes or hangs, it can cause intermittent failures:
- Check FPM status:
sudo service php5-fpm status - Restart FPM to rule out a stuck process:
sudo service php5-fpm restart - Check FPM logs for errors:
tail -f /var/log/php5-fpm.log
4. Confirm Apache PHP Module & Config
Make sure Apache is properly configured to parse PHP:
- Verify the PHP5 module is loaded:
apache2ctl -M | grep php5(you should seephp5_module (shared)). If not, enable it withsudo a2enmod php5and restart Apache. - Check your default site config (
/etc/apache2/sites-available/000-default.conf)—ensure the/var/www/htmldirectory block has:
This ensures Apache can parse PHP files in that directory.<Directory /var/www/html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
5. Test a Minimal PHP File
Rule out issues with your main.php code by creating a simple test file:
- Create
/var/www/html/test.phpwith:<?php echo "PHP is working!"; phpinfo(); ?> - Visit
localhost/test.phpmultiple times. If this works consistently, the problem is likely in yourmain.phpcode—check for things like unhandled MySQL connection timeouts, race conditions, or conditional code that fails under certain circumstances (e.g., missing variables, file I/O errors).
6. Check System Resources
Intermittent failures can also stem from system resource limits:
- Run
toporhtopto monitor CPU and memory usage while testingmain.php. Look for spikes or processes being killed. - Check system logs for out-of-memory (OOM) events:
grep -i "out of memory" /var/log/syslog—if the OOM killer is terminating Apache/PHP processes, you’ll need to free up memory or adjust system limits.
If none of these steps resolve the issue, share the error messages from your Apache/PHP logs or snippets of your main.php code (especially parts involving MySQL or file operations) and we can dig deeper.
内容的提问来源于stack exchange,提问作者alhelal

