Install multiple laravel projects in subfolders without subdomain Install multiple laravel projects in subfolders without subdomain nginx nginx

Install multiple laravel projects in subfolders without subdomain


I have successfully run a Laravel 5.4 project in a "subfolder" of another site by using a simple symlink.

There was no funky special rewrite rules in the Nginx configuration. No copy & paste of sections of the project. No mention of the subfolder in the routes. Just a regular Laravel 5 project neatly contained somewhere on the server and a symlink to it's public folder from the main site's document root.

/var/www/domain.com/public/project1 --> /var/www/project1/public

All the routes just work!

When writing your views you do have to wrap the paths for client-side assets in asset() helper function so the paths in the HTML will contain the subfolder and the browser can find them.

<!-- Styles --><link href="{{ asset('css/app.css') }}" rel="stylesheet">

But doing that does not make your code less flexible, because if you do run the site in an environment where it is not accessed via a subfolder, it works because asset() works with what the address bar contains.


Recently I had the exact same problem. I wanted to have

but I hated having to modify nginx conf every time I added a new project.

Here's what I came up with:

# Capture $project from /$projectname/controller/actionmap $request_uri $project {    ~^/(?<captured_project>[a-zA-Z0-9_-]+)/? $captured_project;    default / ;}server {    listen 11.22.33.44:80;    server_name customerdemo.example.com www.customerdemo.example.com;    # Use $project/public as root    root /sites/customerdemo.example.com/$project/public;    # Use index.php as directory index    index index.php;    # Include the basic h5bp config set (see https://github.com/h5bp/server-configs-nginx)    include h5bp/basic.conf;    # Process /projectname/the/rest/of/the/url    location ~ ^/([^/]+)/(.*) {        # Save the rest of the URL after project name as $request_url        set $request_url /$2;        # If the saved url refers to a file in public folder (a static file), serve it,        # else redirect to index.php, passing along any ?var=val URL parameters        try_files $request_url /index.php?$is_args$args;    }    # Process any URL containing .php (we arrive here through previous location block)    # If you don't need to serve any other PHP files besides index.php, use location /index.php here    # instead, to prevent possible execution of user uploaded PHP code    location ~ [^/]\.php(/|$) {        # Define $fastcgi_script_name and $fastcgi_path_info        fastcgi_split_path_info ^(.+?\.php)(/.*)$;        # Immediately return 404 when script file does not exist        if (!-f $document_root$fastcgi_script_name) {            return 404;        }        # Mitigate https://httpoxy.org/ vulnerabilities        fastcgi_param HTTP_PROXY "";        # Define PHP backend location (find yours by grepping "listen ="        # from your PHP config folder, e.g. grep -r "listen =" /etc/php/)        fastcgi_pass unix:/run/php/php7.0-fpm.sock;        # Set SCRIPT_FILENAME to execute        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;        # Include the default fastcgi parameters        include fastcgi_params;        # Overwrite REQUEST_URI (default is $request_uri) with $request_url we saved earlier        fastcgi_param  REQUEST_URI        $request_url;    }}

The result is that I don't have to do ANYTHING other than git clone in /sites/customerdemo.example.com/ folder and run composer install and php artisan migrate to add a new project.

Actually, I have developed myself a control panel where I can click on "Add project" and specify project details and a new bitbucket repo, mysql user and database will be created and customerdemo server will be given deploy access to this bitbucket repo and a webhook is set up in this new repo which will call a deployment script on customerdemo server each time someone commits to master on this repo which will trigger either git clone or git pull and composer install and database migration. That's why I needed dynamic Nginx configuration. ;-)


I think the problem might be in your nginx.conf file. Try this:

location ^~ /project1 {        alias /home/web/project1/public;        try_files $uri $uri/ @project1;    location ~ \.php {        fastcgi_pass     unix:/var/run/php5-fpm.sock;        fastcgi_index    index.php;        include          /etc/nginx/fastcgi_params;    }}location @project1 {    rewrite /project1/(.*)$ /project1/index.php?/$1 last;}

Also, please be sure that /home/web/project1/ is outside of your web root.

This being said, it is really not recommended to run Laravel in a subfolder. Much easier in a subdomain.

I got the basic idea for this suggestion from this gist.