skip to content
Slashism

Setup Virtual Host For Laravel Project

/ 2 min read

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

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

Terminal window
sudo a2enmod rewrite
sudo service apache2 restart

Now copy the default configuration and make a new one with a new name:

Terminal window
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/secret-project.conf

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

Terminal window
sudo nano /etc/apache2/sites-available/secret-project.conf

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

<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName secret-project.local
ServerAlias www.secret-project.local
DocumentRoot /var/www/secret-project/public
<Directory /var/www/secret-project/>
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 registered a name for our Laravel installation so when we open secret-project.local in the browser, we would be able to see our Laravel application.

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

Terminal window
sudo nano /etc/hosts

Add this line at the end of the hosts file:

127.0.0.1 secret-project.local

Everything is set now. Enable your newly added secret-project.conf configuration and reload the apache2:

Terminal window
sudo a2ensite secret-project.conf
sudo service apache2 reload

Now you can open your browser and navigate to secret-project.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:

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