Deploying PHP applications (on DigitalOcean)

Deploying PHP applications (on DigitalOcean)

This is my very first hashnode post!

So every time I want to deploy a Laravel app to a digital ocean droplet I encounter a number of issues, I decided to write it down somewhere so I can save my mind for my pets birthday ;)

This guide is probably useful for any Linux server, since I'm writing for ubuntu(18.04) droplets.

A few things to get started

What's needed to run a server for a php application is:

  • PHP itself
  • MYSQL
  • phpMyAdmin (I like to view my databases in a nice GUI)
  • Composer
  • Git(already installed in fresh ubuntu droplets)
  • Nginx / Apache (but I will focus only on Nginx)
  • Let's stop there for now!

First things first, you need to setup your droplet to handle a web application, here is a pretty great guide I use almost all the time. This will help you get your environment ready. How To Install Linux, Nginx, MySQL, PHP on Ubuntu 18.04

Deployment

Deployment is simply pulling your code from wherever it is into the /var/www/html directory in your new droplet. If you have your code in a git repository, then it should be as easy as git pull. I sincerely recommend you use git, its good for your teeth ;)

After pulling the code you may need to install whatever dependencies your app needs using composer. The first issue I always get is some php extensions missing which can prevent composer from installing the rest of the dependencies. To fix this, use this guide to install the missing extensions, Installing php extensions

Issues encountered (Laravel/WordPress applications)

403(Access denied)

This error sometimes mean file/folder permissions are not properly set up, or the server blocks are not allowing .php files to be run. To get around this, make sure the server block has index.php alongside index.html, index.htm etc. Edit

server {
        ....
        root /var/www/html/application;
        index index.html index.htm index.nginx-debian.html;
        ....
}

To

server {
        ....
        root /var/www/html/application;
        index index.html index.htm index.nginx-debian.html index.php;
        ....
}

404(Page not found)

When I try to run the applications in /var/www/html, the first thing I notice is a 404! error even though my Laravel routes are all setup.

Edit

location / {
        try_files $uri $uri/ =404;
}

To

location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?q=$uri&$args; 
 }

Hopefully this helps anyone else facing issues after doing everything needed