Certainly, you have encountered the case of dividing multiple domains for one website. A simple example is the sales site ” webbanhang.com ” and the admin page for that sales web page ” admin.webbanhang.com “.
In this article, we will discuss how to configure Virtual Host for a Laravel project locally using Apache as a web server.
What is virtual host?
Virtual host is a definition that only functions to embed multiple domains into one IP address of a server. And by private installation, the Server will know which domain names will work in any folder. This means a lot because we do not need each domain name an IP address, just a server can embed hundreds of domains working together.
Config Apache to use Virtual host
We need apache to configure a virtual host. If you have not installed apache, you can follow the instructions at this link: https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu- 18-04 .
One of the most common problems when configuring a Laravel project on an Apache HTTP Server is that mod_writer
is not enabled. Laravel needs a mod_writer
to convert the URL to its routing through .htaccess
On some Ubuntu platforms, the default mod_writer
will be enabled, but for security reasons the default is now disabled. To enbale mod_writer
we need to run the command
1 2 3 |
sudo a2enmod rewrite sudo service apache2 restart |
Create virtual host
To create a new virtual host we need to create a new config file, which will declare the name of our virtual host. Here I will for example have 2 websites: 1 is localdriver.local and 2 is admin.localdriver.local.
You need admin privileges to manipulate those files, so don’t forget to use sudo if you don’t have root access.
1 2 3 |
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/admin.localdriver.local.conf sudo nano /etc/apache2/sites-available/admin.localdriver.local.conf |
Edit config file
The config file will look like this after it is created.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<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 <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> 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 |
Please correct the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<VirtualHost *:80> ServerAdmin <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> ServerName admin.localdriver.local ServerAlias www.admin.localdriver.local # Đây là đường dẫn đến folder project của bạn DocumentRoot /var/www/localdriver/public <Directory /var/www/localdriver/> 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 the entry to the host
Above you have admin.localdriver.local as the server name. We need to configure the system so that when we enter admin.localdriver.local in the browser, it redirects to localhost.
To do this we need to add the entry to the host file of the machine
1 2 |
sudo nano /etc/hosts |
Add this line at the end of the file
1 2 |
127.0.0.1 admin.localdriver.local |
Now that the config is done, we need to activate the new site.
1 2 |
sudo a2ensite admin.localdriver.local.conf |
Then reload apache server again
1 2 |
sudo service apache2 reload |
With the localdriver.local page, you can do the same
Install the Laravel project
Prepare the env file
In the project env file change APP_URL
and add APP_ADMIN_URL
as follows:
1 2 3 |
APP_URL=http://localdriver.local APP_ADMIN_URL=http://admin.localdriver.local |
Route declaration
We have 2 different domains, so now we need to split the route by domain. Here I will simply group the routes by domain
1 2 3 4 5 6 7 8 9 10 11 |
Route::group(['domain' => env('APP_ADMIN_URL')], function() { Route::get('/', function() { return 'Đây là trang admin'; } } Route::group(['domain' => env('APP_URL')], function() { Route::get('/', function() { return 'Đây là trang người dùng'; } } |
Test
We are done preparing. Now let’s run it