Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Benounnas Oussama's avatar

Create an apache virtualhost for REST Laravel API

i have a laravel API for the backend and a vuejs interface for the front, my question is how to create two virtual hosts where apache can point to:

  1. www.example.com => /var/www/example folder (where my public front is deployed)
  2. www.api.example.com or www.example.com/api => /var/www/api (where the requests are received)

right now i have two configs inside /etc/apache2/sites-available/example.com.conf

<VirtualHost *:80>
       
         ServerName example.local
         ServerAlias example.local
         DocumentRoot /var/www/example/public
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

    
</VirtualHost>

and the API

<VirtualHost *:80>
         ServerAlias api.local
         DocumentRoot /var/www/api/public


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


        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
0 likes
3 replies
ahmeddabak's avatar
Level 47

This should work, notice the ServerNameand the ServerAlias

//example.com.conf
<VirtualHost *:80>
       
         ServerName example.local
         ServerAlias www.example.local
         DocumentRoot /var/www/example/public

         <Directory "/var/www/example/public">
                  AllowOverride All
                  Require all granted
         </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
//api.example.com.conf
<VirtualHost *:80>
 
         ServerName api.example.local
         ServerAlias www.api.example.local
         DocumentRoot /var/www/api

         <Directory "/var/www/api">
                  AllowOverride All
                  Require all granted
         </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
1 like
Tray2's avatar

If you are running in development mode don't fotgrt to add them to your hosts file.

1 like
Benounnas Oussama's avatar

thank you @ahmeddabak for your answer, it helped half the way, it has a typo in <Directory " /var/www/example/public">, i couldn't restart the apache2 service, so i had to make some changes and add the api dns to the hosts file as @tray2 said. @ahmeddabak answer was :

<VirtualHost *:80>
       //example.com.conf
         ServerName example.local
         ServerAlias www.example.local
         DocumentRoot /var/www/example/public

         <Directory " /var/www/example/public">// <= here is the typo because of the double quote
                  AllowOverride All
                  Require all granted
         </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

so my fix was to delete them:

//example.com.conf
<VirtualHost *:80>
       //example.com.conf
         ServerName example.local
         ServerAlias www.example.local
         DocumentRoot /var/www/example/public

         <Directory   /var/www/example/public>// <= no double quote
                  AllowOverride All
                  Require all granted
         </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

added the two IPs (here because i'm deploying in local machine, if you're using a hosting you should write the server IP address) sudo nano /etc/hosts file:

127.0.1.1       example.local
127.0.1.1       api.example.local

Please or to participate in this conversation.