Running a custom PHP app on Digital Ocean's App Platform
I tried to understand AWS server-less platform long ago. Couldn't. Tried to get the hang of it's competitor, Digital Ocean App Platform - could.
I am using MAMP Pro 3.6 on my macOS Mojave.
First create a empty github repo at github.com/new under your username, say krishnan.
cd ~/www
mkdir test
cd test
echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/krishnan/test.git
git push -u origin main
touch index.php
code index.php
Add to index.php the following :
<?php
echo "It's working";
phpinfo();
?>
git add index.php
git commit -m "Added index"
git push -u origin main
Setup your app on Digital Ocean's App Platform here : cloud.digitalocean.com/apps - connect your github repo to the newly create Digital Ocean app. Under Domain you should see something like test-some-random-phrase.ondigitalocean.app - note that down. (You can connect your own domain to the app by adding a CNAME record to your domain in your domain control panel).
Now, back to your terminal, install composer as mentioned here (getcomposer.org/download) :
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
touch composer.json
code composer.json
Add to composer.json :
{
"require": {
"php": "^8.0.0"
}
}
/Applications/MAMP/bin/php/php8.0.0/bin/php composer.phar update
git add composer.json composer.lock
git commit -m "Added composer"
git push -u origin main
Once the build has completed (which does take some amount of minutes), goto test-some-random-phrase.ondigitalocean.app you'll see phpinfo();
The concept of this App Platform is that you push code to github and connect that github repo to the Digital Ocean app project. So .... each time you make changes to the code and push it to github, it automatically gets pushed to DigitalOcean and site get updated with the pushed code. (GitHub has a desktop app called GitHub desktop that can manage the repo if you don't want to use terminal).
The idea behind this, is that we don't need to worry about server management as there's no server for us to connect to like a droplet. A droplet is a VPS which is like a computer that has a linux OS and we need to install the software and configure it to make it work, for which we need sys-admin skills. App Platform, on the hand, is server-less - we just need to deploy code from github. No management of software like PHP, Ubuntu etc.
The app on App Platform is not connected to any database since we cannot have a database on the App platform itself. In order to use a database we need to connect code to a standalone database on another server like digitalocean.com/products/managed-databases which charges a minimum of $15 / mo which need not be managed by us since it's automatically managed by DigitalOcean. Btw, we don't necessarily need to use a DigitalOcean database if we don't want to. We can still use another hosting provider's database if it allows remote connections.