Setup Virtual Host For Laravel Project

Setup Virtual Host For Laravel Project

Using virtual hosts in apache can help you to manage multiple Laravel application endpoints.

First, you need to make sure that you have mod_rewrite enabled on your apache setup. If not then you can do that:

sudo a2enmod rewrite
sudo service apache2 restart

Once you enable the mod_rewrite in apache, you can simply copy the default configuration and make a new one with your new name:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/slashism.conf

Now open the newly added slashism.conf configuration file. (you can name it whatever you want)

sudo nano /etc/apache2/sites-available/slashism.conf

Remove the existing content in this copied file and add the following:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName slashism.local
    ServerAlias www.slashism.local
    DocumentRoot /var/www/slashism/public

    <Directory /var/www/slashism/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        Require all granted
    </Directory>

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

In the above configuration, we have told apache to register a name for our Laravel installation so when we open slashism.local in the browser, we would be able to see our Laravel application.

But before we proceed further and try to access slashism.local, we have to add an entry in the hosts file-

sudo nano /etc/hosts

Add this line at the end of the hosts file:

127.0.0.1 slashism.local

Everything is set. Now enable our newly added slashism.conf configuration and reload the apache2:

sudo a2ensite slashism.conf
sudo service apache2 reload

Now you can simply open your browser and navigate to slashism.local and you should be able to see the Laravel page.

In case you get an error on the page, make sure you set up the permissions on the cache and storage folder:

sudo chmod 777 -R storage/
sudo chmod 777 -R bootstrap/cache/