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
- Log in to the CPanel.
- Locate the Subdomains link under the Domains category.
- Under the Create a Subdomain section, key in the subdomain name, and the Document Root should auto populated.
- 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.
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.