Configure virtual host in Laravel with Apache

Author
By Darío Rivera
Posted On in Apache

One of the first questions that arise after installing Laravel is how to configure a virtual host if you don't want to start the server with the command php artisan serve. Assuming that you have installed an apache server, a laravel application in the route /var/www/html/your-domain.com and that your domain or subdomain is your-domain.com, you must perform the following configuration in the httpd.conf / httpd-vhost.conf file or in a file created exclusively for the handling of your application.

<VirtualHost *:80>
    ServerName   your-domain.com
    DocumentRoot "/var/www/html/your-domain.com/public/"
    ErrorLog     "/var/www/html/your-domain.com/error.log"
    CustomLog    "/var/www/html/your-domain.com/access.log" combined

    <Directory "/var/www/html/your-domain.com/public">
        Options +Indexes +FollowSymLinks
        DirectoryIndex index.php
        AllowOverride None
        Require all granted

        <IfModule mod_rewrite.c>
            <IfModule mod_negotiation.c>
                Options -MultiViews
            </IfModule>

            RewriteEngine On

            # Handle Front Controller...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]

            # Handle Authorization Header
            RewriteCond %{HTTP:Authorization} .
            RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        </IfModule>
    </Directory>

</VirtualHost>

Like any virtual host configured on the machine, don't forget to add the domain or subdomain to the hosts file of the operating system. I highly recommend performing this configuration in a file created exclusively for the site, which could be your-domain.com.conf in the directory /etc/apache2/sites-available. Once done with this, you'll need to create a symbolic link in the sites-enabled in the following way

export SITES_AV=/etc/apache2/sites-available
export SITES_EN=/etc/apache2/sites-enabled
ln -s $SITES-AV/your-domain.com.conf $SITES_EN/

Alternatively, use the apache command to start the site like this

a2ensite your-domain.com

Finally, you'll need to restart apache so that the new virtual host is functional. See you soon!.


Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.