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

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/publicpublic

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:

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:

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: