Hello readers, here we will discuss an interesting topic now, the most asked and debated question – laravel project in shared hosting ? Some say you can and some say you should not do it. Here we will discuss when to deploy our project to shared hosting and how to deploy 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.
Most of the people say shared hosting is not secure as all the accounts are hosted in same machine. So that others might have an opportunity to sneak into your files and code base. However most of the shared hosting providers, keeps up all the necessary security to avoid all such things. It is up to us what type of project we are hosting, as mentioned above, all sample applications, test applications, applications for demo purpose and all small applications can be hosted but security and private data related applications are not preferred with shared hosting.
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
$app = require_once __DIR__.'/../bootstrap/app.php';
$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,
$app = require_once __DIR__.'/../laravelapps/project_name/bootstrap/app.php';
Make sure you upload all the files in public folder including .htaccess file and make sure your routes are correct in routes.php file.
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.