Namaste!
Today we will learn how to Deploy Your Laravel Project from Git to Shared Linux Hosting. It is very simple to do it. Let’s get started,
Introduction
Developing Laravel in the nearby environment (Laravel Project from Git to Shared Linux Hosting) or localhost is so easy that we continually forget about how does truly Laravel is deployed on a live server. There is a wrong notion spread throughout the developer network that, Laravel wishes AWS or VPS server in any other case there may be no want to code in Laravel. They are proper but to a point only due to the fact Laravel framework is evolved on PHP and PHP needs server to execute scripts written. Therefore server may be whatever; it can be AWS, Digital Oceans VPS, or virtually bigrock or GoDaddy’s shared website hosting server.
So, let’s see how to deploy a Laravel project from the Git repository to your shared Linux hosting server in simple and easy steps.
Below are the steps to deploy Laravel Project from Git to Shared Linux Hosting.
Steps are
- Open Terminal
- Clone git repository outside the public_html directory
- Run composer install
- Move contents of public directory to public_html and delete public directory from project
- Edit index.php in public_html and set its permission as 644
- Create MySQL database and Database User
- Rename .env.example in project directory to .env and edit mysql data values as shown in step 6
- Run php artisan migrate:fresh — seed
- Create Storage directory symlink
- Give permission to storage and bootstrap directories
- Run php artisan config:clear, php artisan key:generate and composer dump-autoload
Open Terminal
Every shared hosting comes with a Cpanel which is the central control panel where you change settings as permissions are granted to you. Search the terminal in the search box and navigate to the terminal.
Clone Git repository outside the public_html directory
If you’ve been following my blog, you may remember that we developed a simple cradle application in laravel. I had created a repository on GitHub, so we will use this repository.
Navigate to the Simple CRUD repository and clone it outside your server’s public_html directory.
Now, our repository simple-crud is placed just outside the public_html
Run composer install
By default vendor folder is not included in the Laravel package we need to run composer install
Now laravel and its dependencies are installed via composer.
Move contents of public directory to public_html and delete public directory from project
hence I have created a subdirectory called simple-crud where I am going to move my repo’s public directory’s contents. You can delete the repository’s public folder now.
Edit index.php in public_html and set its permission as 644
Our next step would be to edit index.php (located at where we moved the public folder’s contents), set a path as per the location of the repository
Don’t get me wrong with ../ because they’re so ambiguous, I was even swept away when I first finished.
Create MySQL database and Database User
Create a Database as you usually do in Cpanel. Keep noted database name, user, and password
Rename .env.example in project directory to .env
Now, edit MySQL data values as shown in step 6
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=decodbfe_simple_crud
DB_USERNAME=decodbfe_simple
DB_PASSWORD=p!]KT+iahoK*
Run php artisan migrate:fresh — seed
Now it’s time to migrate our tables and seed database. Run php artisan migrate:fresh — seed
Here you may get errors as SQLSTATE[42000]: Syntax error or access violation: 1071
Specified key was too long; max key length is 1000 bytes (SQL: alter table users
add unique users_email_unique
(email
))
It can be prevented by editing app/Providers/AppServiceProvider.php of our repository as shown below
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema; //NEW: line to add
class AppServiceProvider extends ServiceProvider
{
/**
- Register any application services. *
- @return void / public function register() { // } /*
- Bootstrap any application services. *
@return void
*/
public function boot()
{
Schema::defaultStringLength(191); //NEW: Increase StringLength
} }
Create Storage directory symlink
Since our app will run as http://yourdomain.com or http://yourdomain.com/simple-crud but there is no storage folder there therefore we will create a symlink from our repository to our public-facing directory. In my case, it is like, running in a terminal.
ln -s /home/username/simple-crud/storage/app/public/ /home/username/public_html/simple-crud/storage
Give permission to storage and bootstrap directories
Next, in our repository, we need to give permission to storage and bootstrap directories, run the following command in terminal
chmod -R o+w storage/ bootstrap/
Run php artisan config:clear, php artisan key:generate and composer dump-autoload
Finally, we have reached the last step, run following commands one by one.
php artisan config:clear
php artisan key:generate
composer dump-autoload
At last, we can not access our application using http://yourdomain.com or http://yourdomain.com/simple-crud
Conclusion
In this article, we learned how to deploy a Laravel Project to Shared Linux Hosting from Git.
Thank You!