DEV Community

loading...

Laravel 5.6 on Digital Ocean

alchermd profile image John Alcher ・Updated on ・3 min read

So I was trying to deploy a project on Digital Ocean (referral link) in which I used the Laravel 5.6 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) 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:

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

But according to Laravel's Server Requirements

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 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:

$ 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:

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

$ sudo cp .env.example .env

... and generate a new APP_KEY with

$ 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

$ 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:

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

$ 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 is pretty much a pain point when I'm setting up my server. I even hit the hourly limit that Let's Encrypt 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. 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!

Discussion (4)

pic
Editor guide
Collapse
defman profile image
Sergey Kislyakov πŸ‡·πŸ‡Ί πŸ‡ΊπŸ‡Έ

You should not chmod 777'ing the .env file. Giving write permissions to anyone is a security issue.

Collapse
alchermd profile image
John Alcher Author

Thanks for the reply! What would be the correct approach then?

Collapse
defman profile image
Sergey Kislyakov πŸ‡·πŸ‡Ί πŸ‡ΊπŸ‡Έ

I'd go with chmod 644 and chown $USER to fix the permissions issue.

Collapse
azazqadir profile image
Muhammad Azaz Qadir

Manually installing Laravel on DigitalOcean server seems quite a hassle. After setup, you have to manage the server as well. have you tried out managed platforms that provides 1-click Laravel server? I think DigitalOcean is also providing 1-click solution in it Marketplace? If you really want to go with DigitalOcean, try out the Marketplace and see if there is 1-click solution for Laravel.