There are some ways to deploy an application made with Laravel. In order of recommendation, follow the options.
The deployment options below are for the Laravel 5. I don’t know if they work in other versions.
Setting Documentroot to the /public folder
This is the way recommended by the Laravel team and the safest of all. No need to change files .htaccess
, just use the default Laravel and ready. The only thing to be done is to configure your Apache. In Virtualhosts make point the DocumentRoot
to the folder public
as follows:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/path/to/app/public"
<Directory "/path/to/app/public">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Using alias and . htaccess
A way to deploy without touching the DocumentRoot
is using Apache aliases and setting a .htaccess
inside the public folder a little different from the .htaccess
pattern. Using Alias, your Virtualhost would look like this below:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/path/to/apps"
Alias /app1 "/path/to/apps/app1/public"
<Directory "/path/to/apps/app1/public">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
Alias /app2 "/path/to/apps/app1/public"
<Directory "/path/to/apps/app1/public>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
This way you can put multiple applications in the same DocumentRoot
and separated by directories. In each application, in the directory public
, would need to set the .htaccess
as follows:
Application 1:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /app1
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Application 2:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /app2
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Your applications would respond in URLS http://www.example.com/app1 and http://www.example.com/app2. Take a good look at the .htaccess
on the line RewriteBase /app2
. This is to fix a paging problem of Laravel itself.
Moving the files from the public folder
This is the least recommended way, because it opens security loopholes. Meanwhile, let’s go to it. Use at your own risk.
- Copy all contents of the public folder to the root of your project.
- Open the file
index.php
at the root of the project and make the following modifications
Of:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
To:
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/start.php';
This solution will allow you to request any file at its root, including the file .env
. Try to access (http://example.com/app/.env) and you will see your database connection information there!
You will have to protect all files that you do not want direct request using the .htaccess
. I didn’t test it, but I believe that way:
RewriteRule \.(.env)$ - [F]
Once again, do not recommend this solution. Try to contact host and ask to point the Document Root from the URL to the public folder. Use at your own risk.
Related: http://answall.com/questions/43685/problema-com-subpastas-e-reescrita-de-url-laravel
– Wallace Maxters