Migrating a Laravel Application to a Shared Hosting Server with Git

This post details the process of migrating a laravel application from development in localhost onto a live server. While the title is specific to shared hosting, the process should be pretty much the same for dedicated hosting as well. The following items are required prior to migration

  1. PuTTY (or an equivalent SSH client)
    • In some shared hosting providers such as godaddy, terminals via the web browers are provided and you can use those instead
  2. Laravel web application version controlled with git
  3. Server must have composer installed (and npm if needed)

Now onto deployment.

Migration Procedure

  1. SSH into the deployment server and move into the directory intended for files to be placed (not publichtml).
  2. Run the command below using the project’s repository URL
1
git clone <project-url>

Credentials will be requested for the source code files to be downloaded. An alternative method would also be to zip the project files and upload to server manually.

  1. In the root directory, make a copy of the env.example file and name it .env. This env file holds the environment configuration values for the Laravel web app and is not committed into source control as its values will differ between environments. The table below shows the main configuration values to be filled.
Variable NameDescription
APP_NAMEApplication name
APP_ENVSet to local if in development, Set to production if live.
APP_DEBUGSet totrueif in development. Set tofalseif live.
DB_The DB_ group represents the database connection on the server.
MAIL_The MAIL_ group configures the mailer account
env Configuration Values

It is suggested to set the APP_DEBUG variable to true during deployment. Once the deployment is successful, it can be changed to false.

  1. The APP_KEY variable is populated by running the command:
1
php artisan key:generate
  1. Once complete, run the following composer command to populate your web application’s dependencies
1
composer install

In a local environment, composer update can be run to update all dependencies. However, running this command in a shared hosting environment may timeout. Instead composer install is used to only install the dependencies listed in the composer.lock file

  1. ProTool’s node modules can be installed by running the commands
1
2
npm install
npm run production
  1. (Situational Step) The index.php file within the public folder is the entrance point for all users upon first loading the site. As such, it can be separated from the rest of the project and the paths within the index file simply be updated to reflect where the project resides. The two (2) paths to be updated are autoload.php and bootstrap/app.php. The code below shows the original index file and points to be updated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <[email protected]>
 */

/*
function public_path($path = '')
{
    return realpath(__DIR__)
        .($path ? DIRECTORY_SEPARATOR.$path : $path);
}*/



define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/


//Update this path
require __DIR__.'/../vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/


//Update this path
$app = require_once __DIR__.'/../bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/


$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);
  1. At this point, the web app is ready to build its database tables and relationships. This is performed by running the command
1
php artisan migrate --seed

If there are any dependencies missing, laravel will throw detailed error messages toresolve the issue.The web app can now be visited at its designated URL. A full manual test of the (with APP_DEBUG set to true) should be performed at this point to ensure the installation was successful.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.