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 courses 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 opt for 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 do 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 the same machine. So that others might have an opportunity to sneak into your files and codebase. 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, and also you can join Bluehost for as low as 3.95$ per month.
How to deploy in shared hosting
Saying that now let’s 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/codebase to that except public folder to newly created directory laravelapps.
- If the shared hosting supports installing and updating the composer do it or else copy the vendor folder too.
- Now create another folder in public_html folder and place the contents in the public folder there. (/user/bob/public_html/testlaravel). Or can directly place the contents of the public folder in public_html folder.
- Change the path of
$app = require_once __DIR__.'/../bootstrap/app.php';
$app = require_once __DIR__.'/../laravelapps/bootstrap/app.php';
in index.php file located at the 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 a public folder including .htaccess file and make sure your routes are correct in routes.php file.
Now, Laravel project which is deployed on 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.