Debian+Apache环境下用户目录中Symfony部署的500错误排查
Alright, let's break this down step by step—since your Symfony setup works perfectly in /var/www/html but fails in your user's public_html directory, the issue is almost certainly tied to Apache's userdir configuration, Symfony's .htaccess rules, and file permissions. Here's how to fix it:
1. 确保Apache的Userdir模块已启用并正确配置
Debian often restricts user directory access by default, which blocks .htaccess files from working as expected.
- First, enable the
mod_userdirmodule if it's not already active:sudo a2enmod userdir sudo systemctl restart apache2 - Next, edit the userdir configuration file at
/etc/apache2/mods-available/userdir.confto allow.htaccessoverrides:
The critical line here is<Directory /home/*/public_html> AllowOverride All Options Indexes FollowSymLinks MultiViews Require all granted </Directory>AllowOverride All—the default setting is usuallyAllowOverride None, which completely ignores Symfony's.htaccessrules and triggers internal server errors. Save the file and restart Apache again:sudo systemctl restart apache2
2. 调整Symfony web目录的.htaccess规则
Symfony's default .htaccess assumes it's running from a root domain path, but your project lives under /~user/my-project/web. We need to update the rewrite base:
Open /home/user/public_html/my-project/web/.htaccess and find this commented line:
# RewriteBase /
Uncomment it and update it to match your user directory path:
RewriteBase /~user/my-project/web/
This tells Apache where to look for the actual application files, fixing the path mismatch that causes 500 errors.
3. Fix file permissions for Symfony's cache/log directories
In user directories, files are owned by your user account, but Apache runs as the www-data user. Symfony needs write access to var/cache and var/logs to function:
- Set the correct ownership and permissions with these commands:
cd /home/user/public_html/my-project sudo chown -R www-data:www-data var/cache var/logs sudo chmod -R 775 var/cache var/logs - For Symfony 3+ users, you can also use the official recommended ACL method (more flexible for shared access):
sudo setfacl -R -m u:www-data:rwX -m u:$USER:rwX var sudo setfacl -dR -m u:www-data:rwX -m u:$USER:rwX var
4. Optional: Redirect root project URL to the web directory
If you want http://nameserver.com/~user/my-project to automatically point to the web directory (instead of requiring users to add /web), create a .htaccess file in your project's root directory (/home/user/public_html/my-project) with these rules:
RewriteEngine On RewriteRule ^$ web/ [L] RewriteRule (.*) web/$1 [L]
内容的提问来源于stack exchange,提问作者Laurent

