Deploy Laravel 5 application on shared hosting

In the following there are some useful methods for deploying a Laravel 5 application on a shared hosting, where you don’t have full control on the web server configuration.

A method could be usable or not depending on what options and tools a hosting service provides. First two methods are more advisable but maybe not usable on all services. Use last two methods as last alternative.

For each method below, we assume that:

Note also that the simplest and safest way to deploy Laravel on Apache web server would be to set the website document root to the Laravel’s [app]/public directory. But usually this is not allowed on shared hostings.

Method 1: set the right document root folder

If you are deploying your application on a subdomain, let’s say a subdomain named [app] (i.e. http://[app].example.com), most hosting services let’s you to specify the root folder for your subdomain.

So you can simply set the root folder for the subdomain to:

[app]/public

and put the Laravel application inside the folder named [app] on your hosting.

When your website is accessed at http://[app].example.com will be served your Laravel application from [app]/public.

Pros: this is the easiest and safest method.
Cons: the hosting service should allow you to choose the document root folder and it’s hard they give you this option for the main domain.

Method 2: create a symbolic link

Most shared hosting services doesn’t allow you to set the document root folder, but if you have an SSH access you should be able to replace the document root with a symbolic link.

Let be public_html the website document root.

Via SSH, create the ~/[app] folder in your home:

$ cd ~
$ mkdir [app]

Then you can deploy the Laravel application inside the [app] folder.

Create the symbolic link to [app]/public (be sure that public_html is empty):

$ rm -r public_html
$ ln -s [app]/public public_html

Now it’s like the document root folder is [app]/public.

Pros: safe and quite easy.
Cons: the hosting service should allow you to create symbolic links, this usually means you need an SSH access.

Method 3: add an .htaccess in the application root (unsafe)

You can add an .htaccess file in the root of your Laravel application, with this content:

<IfModule mod_rewrite.c>

  # Turn Off mod_dir Redirect For Existing Directories
  DirectorySlash Off
  
  # Rewrite For Public Folder
  RewriteEngine on
  RewriteRule ^(.*)$ public/$1 [L]

</IfModule>

In your hosting service just put the Laravel application, with the above .htaccess, inside the website document root folder (e.g. the public_html folder). All incoming requests will be rewritten to point inside the public folder.

This is an unsafe method since the Laravel application root folder become the website document root. This could publicly expose some private data (e.g. the .env file) if something go wrong, for example if you accidentally remove the .htaccess file from the application’s root.

Also this method breaks the url /public (e.g. http://example.com/public) that will show the content of the public directory instead of be available inside the Laravel application.

Pros: easy.
Cons: unsafe and breaks the url /public.

Method 4: move the public folder

You can find around the web some nice tutorial that will show you how to move the Laravel’s public folder under the website document root, leaving outside the rest of the application (in a private and safe place). You will need to change some paths inside the index.php file in order to get it working.

A really good tutorial is this one:
https://medium.com/laravel-news/the-simple-guide-to-deploy-laravel-5-application-on-shared-hosting-1a8d0aee923e#.kvr1tze9z

Personally, I would prefer to add the .htaccess file (method 3) over this method, since it is less invasive, it preserves the default Laravel application structure, and both the development and the deployment process will be easier.

But, if you need the most safe method for your application then you should take in account this last method.

Pros: safe and applicable on most hosting services.
Cons: a bit complex to implement; needs some tricks to properly works and to manage the deployment process.

References

http://novate.co.uk/deploy-laravel-5-on-shared-hosting-from-heart-internet/
https://medium.com/laravel-news/the-simple-guide-to-deploy-laravel-5-application-on-shared-hosting-1a8d0aee923e#.kvr1tze9z
https://laracasts.com/discuss/channels/general-discussion/how-do-you-protect-env-file-from-public
https://driesvints.com/blog/laravel-4-on-a-shared-host
http://wiki.dreamhost.com/Transparently_redirect_your_root_directory_to_a_subdirectory

  • I wouldn’t recommend shared hosting for Laravel. Shared hosting has serious downsides. The server resources are shared with more than one webmaster. This means the performance of your website could be adversely affected.This also makes your website vulnerable to security threats. Instead, go for cloud hosting since it is more secure and faster. Cloud hosting provider, like Cloudways, provides CDN, caching, ddos protection and many other services that could improve the security and performance of your Laravel website.

  • Agam Adhityo

    Great, work perfect. Using method no. 2
    Thanks

Categories

Category BootstrapCategory CoffeescriptCategory DrupalCategory GravCategory HTMLCategory JavascriptCategory JoomlaCategory jQueryCategory LaravelCategory MagentoCategory PHPCategory SharePointCategory SpringCategory ThymeleafCategory WordPressCategory Workflow

Comments

Developed and designed by Netgloo
© 2016 Netgloo