Setting document root for Laravel project on Apache virtual host

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Setting Document Root for Laravel Project on Apache Virtual Host Body:

Setting up a Laravel application on an Apache server can be a challenging task, especially if you're new to both Laravel and Apache configuration. In this comprehensive guide, we will go through the process of properly setting up your Laravel project on an Apache virtual host, allowing easy access via http://appname.com and ensuring the correct document root is used for the application.

Understanding Document Root

First and foremost, understanding what a document root is crucial in this process. The document root is simply where your website files are stored and served by the web server. By default, Apache sets it to /var/www/html, meaning that when you visit http://localhost or http://server_ip in a browser, you'll be presented with the contents of this directory. In Laravel projects, we typically set up a different document root for each application, usually within the project folder itself. For example, if your Laravel project is saved at /var/www/appname and has the public folder containing web-accessible files, its document root would be set to /var/www/appname/public.

Setting the Document Root for Apache Virtual Host

To change the document root for your Laravel application on an Apache virtual host running in Ubuntu VM, you'll need to modify the appropriate config file. In this case, that would be /etc/apache2/available-sites/appname.conf:
    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName appname.com
        ServiceAlias www.appname.com
        DocumentRoot /var/www/appname/public

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
In this example, we've specified the document root as /var/www/appname/public, ensuring that when users visit http://localhost or the server IP, they will be routed to the Laravel application. To use only http://appname.com for accessing your app, you can alter the ServerName and ServiceAlias directives in your configuration file accordingly:
    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName appname.com
        ServiceAlias www.appname.com
        DocumentRoot /var/www/appname/public

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

Testing and Troubleshooting

After making the necessary changes to your Apache virtual host configuration, you will need to restart Apache to apply these modifications (which can be done via 'sudo service apache2 restart'):
    sudo service apache2 restart
Next, test your Laravel application's access using the specified URL. If you encounter any issues, it may be due to incorrect permissions or ownership of the web-accessible files and folders. Make sure that the user running the Apache process has proper permissions over these locations (typically www-data). You can further troubleshoot any additional errors by consulting the server logs found in ${APACHE_LOG_DIR}/error.log and ${APACHE_LOG_DIR}/access.log.

Conclusion

Setting up a Laravel project on an Apache virtual host can be a challenging process for beginners, but with the correct understanding of document roots and configurations, it becomes significantly more manageable. By following the provided steps and troubleshooting any potential issues, you'll soon have your Laravel application running smoothly under the appropriate domain names. For more resources on configuring Laravel projects on Apache servers and optimizing their performance, be sure to visit https://laravelcompany.com.