Deploying a laravel application on shared hosting
There are many guides out there to deploy laravel application on shared hosting. Well here is a simple guide too . In my opinion you should never deploy your application in shared hosting as it may cause many serious security issues and performance is bad as all the resources are shared. But sometimes it is not going to work as we webmaster’s wish as if customer has already decided to host on shared one.
Well, there are many ways and i choose to make route helper for migration, seeder, public disk, cache & config.You can do it manually too, but if you want to do it helper way save below code in route.php.
<?php
// migration
Route::get('/migrate', function () {
Artisan::call('migrate');
dd('Migration completed!');
});
// seeder
Route::get('/seed', function () {
Artisan::call('db:seed');
dd('Seeder completed|');
});
// clear cache and config
Route::get('/clear', function () {
Artisan::call('cache:clear');
Artisan::call('config:clear');
dd('Cache & config cleared.');
});
you can always export db but it’s alway’s good to start fresh, right ? so i alway’s prefer creating seeder for user admin and other important data.
As you can see , these routes are not protected. You can protect these routes with auth middleware or as per your requirements. Basically after the installation of the project i remove migration & seeder route.
/migrate route will call artisan command migrate, which will migrate all your migration file.
/seed route will call artisan command to seed all the seeder
/clear route will clear cache and config.
You can create other routes for optimizing class loader and calling config:cache and many other.
Now Let’s start with setting up your database.
Setup your database :
- Create database
- Create user
- Assign user to the database with all the privileges.
After setting up your database.
Create a folder in your root : example ntest
as you can see it’s not on public_html but outside .
[ “It’s better to create two zip compression of your projects. One for public folder of your laravel project which will go to public_html. Other zip compression for all other folders and file except .env file & public file. Or you can create one zip compression extract them in any folder and move them to the right structure.” ]
Upload zip compression of folder and php file to ntest folder and extract there.
Upload public zip compression to public_html.
Now bootstrapping our laravel application :
You can find index.php in public_html. Let’s edit the file.
Edit the commented out to the folder name which was ntest.
Why didn’t we upload .env file ?
Your .env file should not be committed to your application’s source control, since each developer / server using your application could require a different environment configuration. Furthermore, this would be a security risk in the event an intruder gain access to your source control repository, since any sensitive credentials would get exposed.
This is what laravel has to say about .env. so instead of uploading .env file, let’s edit our config file for database . Go to config folder and find database.php and edit it with your requirements for the database connection, host, db, username , password and also don’t forget to edit your app_key, app_name and mail system .
And create htaccess in public_html to point our domain to the public folde
Above htaccess file will remove public from url and that user cannot visit our folder’s and all directly for security purposes.
Lastly, you can migrate and seed your migration & seeder using our routes that we’have made or you can import database from your local database to server.
cool, your website is up and running 🙂 if you got into any problem please drop in your query in below comment section.