Putting a Laravel App into Production

Putting a Laravel App into Production

Manually deploying your Laravel project to a live web server

Manually deploying your Laravel project to a live web server
Manually deploying your Laravel project to a live web server

Need a new t-shirt?


Introduction

You’ve been developing your new Laravel project locally for some time, you’ve hit your feature milestones and the app has passed testing. Everything is checked in to your git master branch, so now it’s time to deploy it to a live server and let your end customers access it.

Whilst there are a number of tools to help automate or simplify the deployment process, such as Laravel Forge, we believe it’s beneficial to go through the process of manually deploying a Laravel project at least once, as it helps with understanding the architecture and can make for smoother updates and easier troubleshooting in the future.

In this guide we’ll be doing just that, we’re not going to focus heavily on the server configuration side of things as this can vary depending on your host but instead on the steps required to get your app from your local environment into your production environment.

Prerequisites

We’ll assume you have access to a web server configured with the LAMP (Linux, Apache, MySQL, PHP) or LEMP (Linux, Nginx, MySQL, PHP) stack, with (at time of writing for Laravel 5.8), the following specs:

  • PHP >= 7.1.3
  • BCMath PHP Extension
  • Ctype PHP Extension
  • JSON PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

You’ll also need SSH access to run terminal commands as part of the deployment process. 

We like DigitalOcean for quickly spinning up a server instance and getting everything configured (use this link to get a 30 day free trial with $50 credit to get you up and running). There’s some good guides on their site explaining these setup steps, and an example server configuration for Nginx in the Laravel docs.

Finally, we’ll assume you’ve checked your code into your git repository and you have all the necessary credentials to access it from your server.

Putting your Laravel app into production

Using the example server config file from the Laravel docs above, on your web server you will have an /example.com/ directory with a /public/ directory inside (depending on your specific server configuration this could be a /public_html/ directory or something else).

So first of all we’ll clone our project into the /example.com/ directory from our git repository. That directory is not publicly accessible and therefore maintains the security of the project files. Then we’ll create a symlink between our project’s /public/ directory and the /example.com/public/ directory.

So from inside the /example.com/ directory, run 

git clone http://[GIT_SERVER]/your-app.git

You should see your project files in the /example.com/your-app/ directory now.

To link the /example.com/your-app/public/ directory to your /example.com/public/ directory run the following commands:

mv public public_bak
ln -s your-app/public public

Check that the paths in the following two lines in your index.php in /your-app/public/ are correct relative to the location of your project files:

require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';

If your app requires a database make sure you have a suitable database set up and have the credentials to hand.

Now we need to update the project’s .env file. These are usually not checked into git repositories by default as they can contain sensitive password information and local and production deployments will require different settings.

There should be an .env.example file in your project which you can rename to .env. The key areas to edit now are the database settings if a database is being used, and this section:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

Set APP_DEBUG to false and APP_ENV to production, and update the APP_NAME and APP_URL accordingly. If you leave APP_DEBUG as true, in the event of errors you’ll be displaying sensitive debug information to the world (see our Seaborne Freight article), so make sure you change it!

We need to set some folder permissions so they are writeable, specifically the /storage/ and /bootstrap/cache/ folders. These commands may vary depending on your server configuration, but the following commands should set the necessary folder permissions:

chmod -R o+w storage
chmod -R o+w bootstrap/cache

Now we need to install Composer, so run the following:

composer installoptimize-autoloader — no-dev

We need to generate an application key (the APP_KEY setting from the .env file above), to do this, run:

php artisan key:generate

Laravel uses this key for (amongst other things) encrypting cookies.

If you have any database migrations that need to be run to structure your tables, then run the following:

php artisan migrate

If you have seed data to populate your tables with, then run:

php artisan db:seed

See our article on dummy data for more about seeding databases.

That’s basically it! You should be able to go to your new site and see your Laravel project default page. Test out some of your app’s features and make sure the database is working as expected.

Optionally at this stage you can optimise your site’s performance with cache commands (see our article on improving the performance of your Laravel app).

To cache the site config, run:

php artisan config:cache

To cache the routes, run: 

php artisan route:cache

We hope this article has been a helpful overview of what’s involved with deploying Laravel based websites and web apps. If you need help with a deployment don’t hesitate to get in touch.

Related posts:
Laravel Wink - A Beginner's guide
Adding Google ReCaptcha to forms in Laravel
Multiple websites from one Laravel instance