How to configure virtual host for laravel

MANISH DAYMA
3 min readApr 28, 2021

Install Laravel

For configuring a virtual host for a laravel project you have to get a laravel project ready. If you do not have one please follow our article on How to configure laravel on linux.

Otherwise you can also follow the walk through of the official Laravel Documentation

Configure Apache to user virtual host

Of course we need apache to configure a virtual host so if you do not have apache please refer to our article on How to setup laravel on linux.

One of the most common encountered difficulties when configuring Laravel on Apache HTTP server is that the mod_rewrite extension is not enabled. Laravel requires mode_rewrite to properly transform URLs to be interpreted by its routing function via .htaccess.

On some Ubuntu platforms, mod_rewrite will be enabled by default, but for enhanced security, the default is now for it to be disabled. To enable we will execute this following command.

sudo a2enmod rewrite
sudo service apache2 restart

Create virtual host

To create a new virtual host we will create a new configuration file name blog.local.conf that will content our named virtual host.

You need administrative right to manipulate those files so do not forget to use sudo if you are not in root access.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/blog.local.conf
sudo nano /etc/apache2/sites-available/blog.local.conf

Edit configuration file

The content of the file should look similar like this :

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port t$
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

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

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Let’s edit it and make it look like this :

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

<Directory /var/www/blog/>
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>

Add entry to host

As you should notice previously we add blog.local as server name. It’s the short name for our host. We have to configure the system so when we put blog.local in the browser to redirect it to the localhost.

To do so we gonna add an entry to the machine hosts file.

sudo nano /etc/hosts

Add this line at the end of the file

127.0.0.1       blog.local

Now our configuration is setup. We have to enable the new site. To do this we execute the following command

sudo a2ensite blog.local.conf

After executing this command you will be prompted to reload Apache.

sudo service apache2 reload

To test your configuration, open your default browser and go to http://blog.local and you should get the homepage of your laravel application.

If you get any error give 777 permission to those folder bootstrap/cache/ and storage/. Go to the root folder of your project.

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

This is the end of our article on ” How to configure virtual host for laravel “. If you have any problem doing this configuration feel free to ask question in comment i will try to help you achieve this.

Thanks for reading.

Manish Dayma

instagram.com/manishdayma22

twitter.com/manishdayma22

--

--