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
- 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
- Laravel web application version controlled with git
- Server must have composer installed (and npm if needed)
Now onto deployment.
Migration Procedure
- SSH into the deployment server and move into the directory intended for files to be placed (not publichtml).
- Run the command below using the project’s repository URL
git clone
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.
- 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 Name | Description |
APP_NAME | Application name |
APP_ENV | Set to local if in development, Set to production if live. |
APP_DEBUG | Set 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 |
It is suggested to set the APP_DEBUG variable to true during deployment. Once the deployment is successful, it can be changed to false.
- The APP_KEY variable is populated by running the command:
php artisan key:generate
- Once complete, run the following composer command to populate your web application’s dependencies
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
- ProTool’s node modules can be installed by running the commands
npm install
npm run production
- (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.
<!--?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);
- At this point, the web app is ready to build its database tables and relationships. This is performed by running the command
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.