Laravel 5.6 on Digital Ocean
Tags: laravel, digitalocean, php
Published: 2018-05-13 13:26:11

---
title: Laravel 5.6 on Digital Ocean
published: true
description: Starting a fresh Laravel app on the Digital Ocean platform
tags: laravel, digitalocean, php
---


So I was trying to deploy a project on [Digital Ocean](https://m.do.co/c/1da7a7ef7eda) (referral link) in which I used the [Laravel 5.6](https://laravel.com/) framework to get things started. Now all I wanted is to put up a simple *"coming soon"* page of some sort while we keep working on the application itself. Now, I found this guide ([How To Deploy a Laravel Application with Nginx on Ubuntu 16.04](https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04)) and tried to follow it to setup my server. But boy was it hard! This article will list all the things I wish I've known earlier before I start setting up my DO server:

## 1. Use PHP 7.2

The DO tutorial wants us to install `PHP 7.0` with the following command:

```bash
$ sudo apt-get install php7.0-mbstring php7.0-xml composer unzip
```

But according to [Laravel's Server Requirements](https://laravel.com/docs/5.6#installation)

![](https://i.imgur.com/l62Xz9e.png)

We need PHP version `7.1.3` or greater. I say we might as well go for the latest release, PHP `7.2.*` right? Here's a quick and easy [guide](https://jakelprice.com/article/php-70-to-php-72-how-to-upgrade-your-server) to install PHP `7.2` on your DO server.

### Getting an 'ascii codec can't decode' error?

If you're adding the `ppa:ondrej/php` repository and ran into this error, try installing a language pack:

```bash
$ sudo apt-get install language-pack-en
```

This should fix any ascii codec errors you might encounter.


## 2. Generating an App Key

The DO tutorial wants us to create a `.env` file and put in the following:

```env
APP_ENV=production
APP_DEBUG=false
APP_KEY=b809vCwvtawRbsG0BmP1tWgnlXQypSKf
APP_URL=http://example.com

DB_HOST=127.0.0.1
DB_DATABASE=laravel
DB_USERNAME=laraveluser
DB_PASSWORD=password
```

Well, I wouldn't want an `APP_KEY` that came from an article online. So what I did was copy the `.env.example` into `.env`

```bash
$ sudo cp .env.example .env
```

... and generate a new `APP_KEY` with

```bash
$ php artisan key:generate
```

But this would raise a *permission denied* error of some sort. To remedy this, let's give write permission to the `.env` file with

```bash
$ sudo chmod -R 777 .env
$ php artisan key:generate
```

This should remove the error and give you a fresh application key!

## 3. Typo Alert!

At the section where we configure Nginx, there's a sneaky typo that will mess up your day (at least mine did) when gone unnoticed:

![](https://i.imgur.com/b5CR7Jy.png)

See that? Instead of opening `/etc/nginx/sites-enabled/example.com`, you should instead open `/etc/nginx/sites-avaiable/example.com` because that's the new server block that we copied. Beware of this one!

## 4. I did everything right, but I'm getting a 502!

This is most likely because of step #1. We are using PHP `7.2` now, but our server block is still configured to use the PHP extension `php7.0-fpm`. To update our configuration, open `/etc/nginx/sites-available/example.com` and change this line:

```
server {
        ...


        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # Change the line below and update to use php7.2-fpm
                # fastcgi_pass unix:/run/php/php7.0-fpm.sock;

                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

        ...
}
```

Give Nginx a restart 

```bash
$ sudo systemctl reload nginx
```

... and your app should be live!

## Bonus: Overcoming TLS Issues

Though our app is now live, it is still served with plain-old HTTP. To strike confidence into our customers, let's serve our app with HTTPS and get that fancy *secure* lock in the URL bar. (just kidding, HTTPS is not a replacement for good security practices)

The [Step 6 - Securing your Application with TLS](https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04#step-6-%E2%80%94-securing-your-application-with-tls) is pretty much a pain point when I'm setting up my server. I even hit the hourly limit that [Let's Encrypt](https://letsencrypt.org/) enforce when validating a website because I wasn't able to make it work.

Then I found a simple solution. What I did is completely ignore the tutorial and follow this documentation on [securing Nginx with Let's Encrypt](https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04). Make sure you are at the root of your project (ex. `/var/www/html/example_project/`) when you run the commands. You should have an HTTPS secured application by now!