Ubuntu 16.04服务器访问PHP文件出现HTTP ERROR 500求助
Let’s work through this issue systematically—500 errors almost always leave a trail in logs, so we’ll start with the most actionable checks to pin down the root cause.
1. First, Check Apache’s Error Logs
This is the fastest way to get specific details about what’s breaking. On Ubuntu 16.04, Apache’s error logs live in /var/log/apache2/. Run this command to watch logs in real-time while you trigger the login action:
sudo tail -f /var/log/apache2/error.log
Try logging in again, and you’ll see exactly what’s causing the 500 error—whether it’s a permission block, missing file, PHP fatal error, or database connection failure.
2. Verify PHP Error Logs
Sometimes Apache’s log won’t show full PHP-specific details. Check where PHP is logging errors by looking at its configuration:
grep -i error_log /etc/php/7.0/apache2/php.ini
This will output the path to your PHP error log (often /var/log/php7.0/apache2.log). View it with:
sudo tail -f [path-to-your-php-error-log]
Look for fatal errors, missing extensions, or failed database connections—these are common culprits for login script 500s.
3. Fix File Ownership (Critical Step!)
You set permissions to 755 (directories) and 644 (files), but file ownership is just as important. Apache runs as the www-data user on Ubuntu, so all files in /var/www/html need to be owned by this user/group to let PHP read/write properly. Run this command to update ownership recursively:
sudo chown -R www-data:www-data /var/www/html
This fixes issues where PHP can’t write temporary files (like for PDF generation) or access config files because they’re owned by root or your personal user.
4. Check PDF Generation Dependencies
Your original issue was a PDF generation 500 error—this suggests your project uses a tool like wkhtmltopdf, dompdf, or TCPDF that might be missing dependencies. For example, wkhtmltopdf requires xvfb on headless servers (like your Ubuntu 16.04 box). Install them with:
sudo apt-get update && sudo apt-get install wkhtmltopdf xvfb
If your project uses Composer-managed PHP libraries for PDFs, make sure dependencies are installed as the Apache user to avoid permission issues:
cd /var/www/html && sudo -u www-data composer install
5. Validate Project Configuration Files
When you cloned from GitHub, it’s easy to miss critical setup steps:
- A sample config file (like
config.php.example) might not have been renamed toconfig.php - Database credentials in the config could be incorrect (double-check if your private network DB is local or requires specific access rules)
- The PHP session save path (check
session.save_pathinphp.ini) might not be writable bywww-data
6. Check .htaccess Rules (If Your Project Uses Them)
A missing or misconfigured .htaccess file can trigger 500 errors. Make sure:
- The
.htaccessfile exists in/var/www/html(GitHub might ignore it via.gitignore, so you may need to copy it from a sample or recreate it) - Apache has
AllowOverride Allset for/var/www/htmlin its site config (usually/etc/apache2/sites-available/000-default.conf). If not, update the config and restart Apache withsudo systemctl restart apache2.
Once you’ve worked through these steps, the error logs should point you directly to the issue. If you find a specific error message (like a missing PHP extension or failed DB connection), we can dive deeper into that fix.
内容的提问来源于stack exchange,提问作者Adem Tepe

