Setting Document Root for Laravel Project on Apache Virtual Host
When migrating a PHP and Laravel application to a new Apache environment, getting the virtual host configuration right is often where deployment stalls. The specific symptom you are facingâseeing a directory listing of /var/www/appname instead of the Laravel application when visiting http://appname.comâis a classic sign that the web server is serving files from the wrong location.
The issue lies in how Apache resolves requests versus how Laravel is structured. By default, a fresh installation of Laravel places the entry point for your application inside the public directory. This directory contains index.php, which is the bootstrap file that loads the entire framework. Everything else in the parent directory (like .env, vendor, or configuration files) should remain hidden from direct web access.
In your current configuration file located at /etc/apache2/available-sites/appname.conf, you have correctly set the DocumentRoot to /var/www/appname/public. This is the critical setting that tells Apache where to look for files when a user accesses the domain root. However, there is a subtle but vital detail regarding URL rewriting and directory listing that often trips up developers new to Apache.
If your browser displays a folder listing of /var/www/appname, it indicates two possibilities: either the DocumentRoot directive is being ignored or overridden by a default site configuration, or your web server is still serving the contents of the parent directory due to a mismatch in permissions or .htaccess rules.
First, ensure that no other virtual host configuration files are overriding your settings. Apache processes configuration files in a specific order based on their location and priority. If a default site configuration exists (often found in /etc/apache2/sites-available/000-default.conf), it might be catching the request before your custom vhost takes effect, or vice versa depending on your virtual host setup. To enforce your specific DocumentRoot, you must ensure that your appname.conf is enabled and active:
sudo a2ensite appname.conf
sudo systemctl restart apache2
After restarting the service, test the URL again. If the directory listing persists, inspect the actual running configuration using the -S flag to see which virtual host Apache has bound to port 80 for your server name:
sudo apache2ctl -S
You will likely see multiple entries if you have enabled multiple sites. Look for the line corresponding to appname.com. If it points to /var/www/appname instead of /var/www/appname/public, check that your configuration file is being read correctly. Sometimes, syntax errors or missing directives cause Apache to revert to a previous valid state or default behavior.
Additionally, verify that .htaccess files in the parent directory are not interfering. While modern Laravel setups often rely on public/.htaccess for URL rewriting, ensuring that the parent directory does not have an overly permissive configuration is wise. If you do not need to serve static assets from the root of /var/www/appname, ensure that Apache does not inadvertently serve them by checking the Options directive within your virtual host block or a corresponding .htaccess.
Another common pitfall involves the ServerAlias. In your config, you included ServiceAlias www.appname.com. The standard directive for this is ServerAlias, not ServiceAlias. Using an incorrect directive name will result in Apache ignoring it silently. While this specific typo won't cause a directory listing issue, it prevents your "www" subdomain from working correctly. Correct this line to:
ServerAlias www.appname.com
Finally, consider the trade-offs between placing DocumentRoot and using .htaccess for rewriting. Your current setup relies on Apache's rewrite module. If you are not explicitly configuring a RewriteEngine On block or referencing public/.htaccess effectively, you might encounter 403 Forbidden errors when accessing dynamic routes even if the correct files are served. However, your primary hurdle right now is simply ensuring the physical path matches the URL structure.
Once the DocumentRoot correctly points to /var/www/appname/public and the server restarts cleanly, Apache will serve index.php from that directory for root requests, triggering Laravel's routing logic instead of displaying a raw file system listing. If you require further guidance on securing your Laravel deployment or managing multi-tenant environments, resources like https://laravelcompany.com offer community insights, though mastering the core Apache configuration remains essential for production stability.