How to deploy laravel project in shared hosting
When to deploy in shared hosting
- When you just completed some course on laravel or some tutorial on laravel. Followed it and completed it locally. And now you want to show your project to all the people, you can can opt the cheap hosting – shared hosting.
- When you are maintaining a blog/site(with shared hosting) where you write posts and share your information about laravel, in that case if you need to show some demo of your code, you can go for shared hosting and can deploy your projects there.
- Small applications which does not involve collecting any personal and secure information of the people like bank debit/credit card details and which does not involve any payment gateway systems.
Moreover laravel is not designed for shared hosting providers. If one can afford a bit more, one can set up an account at $10/month at laravel forge.
How to deploy in shared hosting
Saying that, now lets see how to deploy laravel application in shared hosting.Generally in shared hosting all the files are present in public_html or www or something similar directory. For example assume public_html folder at /user/bob/public_html. Now follow the simple 4 steps.
- Create a new folder(example laravelapps) in the same location where public_html or www or whatever folder it is. (now it should be at /user/bob/laravelapps).
- Upload all your laravel project files/code base to that except public folder to newly created directory laravelapps.
- If the shared hosting supports installing and updating composer do it or else copy the vendor folder too.
- Now create another folder in public_html folder and place the contents in public folder there. (/user/bob/public_html/testlaravel). Or can directly place the contents of public folder in public_html folder.
- Change path of
require __DIR__.'/../bootstrap/autoload.php';
and
$app = require_once __DIR__.'/../bootstrap/app.php';
to
require __DIR__.'/../laravelapps/bootstrap/autoload.php';
and
$app = require_once __DIR__.'/../laravelapps/bootstrap/app.php';
in index.php file located at public directory.If you have multiple projects uploaded to /user/bob/laravelapps you can change index file as,
require __DIR__.'/../laravelapps/project_name/bootstrap/autoload.php';
and
$app = require_once __DIR__.'/../laravelapps/project_name/bootstrap/app.php';
Now, laravel project which is deployed on a shared hosting can now be accessed at example.com/testlaravel if public directory contents are placed in testlaravel folder or at example.com if public directory contents are placed directly in public_html folder.
Feel free to browse some previous tutorials on bootgrid implementation, sending emails using sendgrid, visualization of data using highcharts.