Multiple Laravel installs in subfolders - Nginx rewrite rules, full guide
Last updated: May 25th 2021
Introduction
This guide explains how to run multiple Laravel installs on your server in subfolders of your domain (or IP). Sometimes, as a developer, you need to quickly deploy Laravel app without creating a subdomain, having to generate new SSL Certificates or opening new ports and configuring a firewall.
Please note: These instructions are based on the Webdock Perfect Server Stacks for Nginx. It will work on any NGINX configuration with slight changes, depending on your server setup.
Basic Nginx setup for Laravel app
You will need to install/set-up a Laravel app which resides in the default html folder /var/www/html , and responds to your server domain name (eg. example.com) or IP (x.x.x.x)
Things are pretty much straight forward here. The only change we have to make to get things working is to point the web-server root directive to the Laravel public folder.
In the Webdock Control Panel click on the small pencil icon next to your Web Root in your server overview screen, and change the web root to /var/www/html/public;
If you need to do this manually, edit your Nginx configuration, typically found at /etc/nginx/sites-available/webdock, and edit the following line to:
root /var/www/html/public;
Don`t forget to restart Nginx if you do this manual update:
# sudo systemctl restart nginx
Laravel app in subdirectory
What we try to achieve here is to run the Laravel application in a subdirectory of the main domain. For example: domain.com/second/
For the matter of semantics and good practice, we will place our second Laravel install in the folder: /var/www/second
Add this code to your Nginx configuration file, typically found at /etc/nginx/sites-available/webdock, at the bottom of the server block:
# SECOND location rewrite instructions location /second { alias /var/www/second/public; try_files $uri $uri/ @rewsecond; location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; } } location @rewsecond { rewrite /second/(.*)$ /second/index.php?/$1 last; } # end of the SECOND location
Please note the version number for php-fpm in the fastcgi_pass directive. If you are using a different version of php-fpm you will need to adjust the version number here, otherwise you will get a 502 Bad Gateway error from Nginx.
We introduced here an alias directive for the path instead of root inside location block. Although they seem to do the same job, there is some crucial differences regarding the path building algorithm they utilize. In the case of the root
directive, a full path is appended to the root including the location part, whereas in case of the alias
directive, only the portion of the path NOT including the location part is appended to the alias.
Also, we used a named location for our rewrite @rewsecond. You can change the named location to anything suits you, but keep in mind to describe right location block for a rewrite, since named location MUST BE outside any other location block you may have or refer and can potentially create confusion later.
Don`t forget to restart Nginx.
# sudo systemctl restart nginx
Multiple Laravel installations in dynamic subdirectories
In this section we are going to deploy Laravel applications in dynamic folders eg. /apps/__APP_NUMBER__
Although this approach may be unorthodox in many ways, this part will complete our guide to installing Laravel applications in subfolders/subdirectories.
Task
We want to configure Nginx rewrite rules for multiple Laravel installations stored in /var/www/apps/_APP_NO_/ (_APP_NO_ is numeric eg. 1001, 1002, 1003, ... and represent folders with separate Laravel installs )
Example:
/var/www/apps/1001/ -> domain.com/apps/1001/
/var/www/apps/1002/ -> domain.com/apps/1002/
/var/www/apps/1003/ -> domain.com/apps/1003
Solution
Add this code to your Nginx configuration file (right after the first location block)
Detailed explanation of what we have done here exceeds the scope of this article so we will leave you to check and experiment further. Capturing variables from regex expression definitely help us solve this complex problem. Moreover, explaining it on Laravel installs can give us a clue how can be done with less or more demanding tasks.
So, this is how should look like our default Webdock Nginx file extended with all examples:
You should not copy/paste this Nginx configuration wholesale without reading and understanding it first.
Summary
Laravel is a most popular PHP framework, and Nginx web server with reverse proxy capabilities is a natural choice. Sometimes, Nginx complex rewrite rules do not seem very user friendly, but once mastered, they will unleash full potential of your Webdock server stack.
Please be informed, the main goal of this article is to avoid common setbacks related to Laravel installs in different subdirectories and how Nginx should handle these. In production, your server config file definitely needs security, caching and redirect settings which this guide is not aimed at providing.
Author: Aleksandar Milivojevic is a PHP programmer for over 10 years (Laravel and Wordpress). He writes articles between projects, to help out the community and in order to popularize PHP in general.
Related articles
-
Laravel Chunked Upload - uploading HUGE files
In this article we show how you can upload very large files safely and effectively in Laravel using chunked uploads.
Last updated: September 29th 2021
-
The power of Laravel Queues
This guide shows how to run asynchronous jobs with Laravel
Last updated: July 29th 2021
-
Laravel and Redis 5 - Introduction, Installation and basic use
In this tutorial, we will explain the basic use of Redis with Laravel PHP framework. We show how you get Redis working on Webdock stacks as well as how you get Redis Commander working.
Last updated: October 7th 2020
-
Laravel and Redis 5 - A Functional Example
Completing our Laravel and Redis 5 guide, we show here a full functional example showing how you can use Redis with Laravel.
Last updated: October 8th 2020
-
Build an API in Laravel with JSON Web Tokens (JWT) (Part 1)
This tutorial will show you how to build an API application based on the Laravel PHP framework, which utilizes JSON Web Tokens (JWT) for protecting routes, issuing grants and claims over API resources.
Last updated: January 30th 2021
-
The Anatomy of JSON Web Tokens (JWT) (Part 2)
In this tutorial we will inspect JWT itself: structure, lifecycle, security. Also, we will explore a new possibility of the JWT usage and the benefits we could exploit.
Last updated: May 25th 2021
-
Database and tracking JSON Web Tokens (JWT) (Part 3)
In this article we will build a database and track our JSON Web Tokens.
Last updated: May 25th 2021
-
JSON Web Tokens and Laravel API custom filtering (JWT) (Part 4)
After successful implementation of the database for JWT, in this article we will discuss additional features and potential use of our tokens.
Last updated: May 25th 2021