DevOps PHP Laravel Apache

Run Laravel Project in Subfolder (Apache)

by Daniel Loureiro on 2 min read

Let's say you want to run Laravel in a URL subfolder. For example, https://domain/my-laravel.

https://domain/            <--- Non-Laravel (React, Vue, Angular, WordPress, etc.)
https://domain/my-laravel  <--- Laravel

The root site is stored on /main-site, and Laravel is stored on /laravel-app.

/var/www/mysite/main-site
/var/www/mysite/laravel-app

On Apache, use the Alias directive to point https://../my-laravel to /var/.../laravel-app:

<VirtualHost *:80>
    ServerName 11.22.33.44
    ServerAdmin [email protected]

    DocumentRoot /var/www/mysite/main-site/dist
    Alias /my-laravel /var/www/mysite/laravel-app/public

    <Directory /var/www/mysite/main-site>
        AllowOverride All
    </Directory>

    <Directory /var/www/mysite/laravel-app>
        AllowOverride All
    </Directory>

    ErrorLog /var/www/mysite/error.log
    CustomLog /var/www/mysite/access.log combined
</VirtualHost>

INFO

/dist is the default folder configuration for Angular and React projects. Change it according to your project.

/public is the default folder for Laravel applications.

# Two Laravel URL sub-folders

Let's say the Laravel project has an /admin route. You can access it with https://.../my-laravel/admin, but it's ugly. Instead, you want to access it with https://.../admin.

Use the ScriptAlias directive for this:

<VirtualHost *:80>
    ServerName 11.22.33.44
    ServerAdmin [email protected]

    DocumentRoot /var/www/mysite/main-site/dist
    Alias /api /var/www/mysite/laravel-app/public
    ScriptAlias /admin /var/www/mysite/laravel-app/public

    <Directory /var/www/mysite/main-site>
        AllowOverride All
    </Directory>

    <Directory /var/www/mysite/laravel-app>
        AllowOverride All
    </Directory>

    ErrorLog /var/www/mysite/error.log
    CustomLog /var/www/mysite/access.log combined
</VirtualHost>

# Difference of Alias vs ScriptAlias

  • Alias: Suppress the URL folder name. When you access https://.../folder/something, Laravel only gets /something.
  • ScriptAlias: Do not suppress the URL folder name. When you access https://.../folder/something, Laravel gets /folder/something.

Comments

Copyright 2021 - Daniel Loureiro