Deploy Laravel on Shared Hosting with Subdomain

To deploy Laravel on shared hosting is not too complicated compared to self hosted environment. This example show how to configure Laravel on shared hosting, on a subdomain. I’m using HostGator here, but it should be just same for other hosting solution.

SSH

I used to figuring out how to get Laravel works on my hosting without SSH, but life will be easier with SSH ability so that we can use the composer command we use on local development.

It’s simple to SSH into HostGator server. Based on their guide page, we just need to SSH into the server using our CPanel login, using port 2222. For example:

ssh -p 2222 cpanel_user_name@domain_name

Installing Composer

After we can SSH into the server, then we need composer. Note that I might get this steps wrong as I wrote this months after I installed composer.

curl -sS https://getcomposer.org/installer | /opt/php55/bin/php

Configure Subdomain

  1. Log in to the CPanel.
  2. Locate the Subdomains link under the Domains category.
  3. Under the Create a Subdomain section, key in the subdomain name, and the Document Root should auto populated.
  4. Click Create button and done. This will create a folder in your public_html folder with the subdomain name.

FTP Laravel Files to Subdomain Folder

Because Laravel put the public facing files in public folder, so by default when accessing the page we need to access via /public in the URL, for example, www.domain.com/laravel/public.

One way to fix this is to move all the folders in the public folder into the root folder of the subdomain folder, and update some files to point to the correct path.

Moving folder out from Public folder to root folder

So this is how the folder structure looks like so far after moving the files and folders in public folder out.

Laravel subdomain folder structure

Update the Index.php

The file index.php pointing to some path that we need to update since we moved it out from public.

Locate and change this:

require __DIR__.'/../bootstrap/autoload.php';

into this:

require __DIR__.'/bootstrap/autoload.php';

Note the ../ is removed, because now the bootstrap folder is in the same level as the index.php.

Also change this:

$app = require_once __DIR__.'/../bootstrap/start.php';

into this:

$app = require_once __DIR__.'/bootstrap/start.php';

Update bootstrap/paths.php

We have to do some update on bootstrap/paths.php as well.

Change this:

'public' => __DIR__.'/../public',

into this:

'public' => __DIR__.'/..',

Update .htaccess

After the files updated with new path, now we need to update .htaccess so it can route to our new folder properly.

This is the new .htaccess settings

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteBase /

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

After that the Laravel setup should be able to access via the subdomain and without using the public folder.

Errors and Troubleshoot

When we first FTP the files to the server, sometimes we might hit the HTTP 500 error, one of the common problem is permission issue. Try to give the files and folder 755 (or whatever appropriate).

Another common problem I face quite a number of times after I added new controllers, model or new table, it seems like after FTP in my files, the Laravel on server does not recognize them. So I’ve to run this command over the SSH on the server:

composer dump-autoload

It saved me so many times trying to figuring out what files is missing.

 

5 thoughts on “Deploy Laravel on Shared Hosting with Subdomain

  1. Why bother shared hosting? The biggest disadvantage of shared hosting is the security issues that the server faces.

    * Your hosting plan may have lesser features compared to dedicated hosting plans.

    * You do not have the choice over the software and application that you want to use as you cannot use any other software aside from what is provided by the hosting provider.

    You may experience resource limitation as everybody else in the network uses the same CPU, memory and hard drive.

    Or your server may get swamped by requests or overloaded which might cause it to stop.

    There are many more.

    So I believe dedicated hosting is much better. For Laravel, I would suggest Cloudways: http://www.cloudways.com/en/laravel-hosting.php

    Why? Because they provide DigitalOcean, Amazon and Google Compute Engine servers with a server management layer. The platform is built to automate server optimization (using caching mechanism including Ngnix, Apache, Memcache and Varnish).

    You just need to focus on building your Laravel project and the platform take care of everything. Laravel 5 is available in just 7 minutes hosted on top of 3 top cloud providers.

    1. Thanks for the suggestion.

      This post was never meant to be a suggestion to host Laravel. I wanted to demo something I put up quickly using Laravel to my friend, so I reuse the shared hosting I’m using, I never intended to host the Laravel project on the server.

      I agreed with you regarding the disavantages of shared hosting, and I’m definitely moving away from shared hosting in the future.

  2. There is no doubt that VPS outperforms shared hosting plans. Also, a VPS is easily more scalable than a dedicated server, so it is best to deploy Laravel on a VPS.

  3. i could not find bootstrap/paths.php but somehow in config/app.php i change the URL=>’subdomain url’ and in server.php, i edit public/index.php to index.php

Leave a Reply