Deploying your PHP-PostgreSQL project using Laravel Forge on AWS or DigitalOcean Server

Sydney EkpoSydney Ekpo
7 min read

Simplicity is the ultimate sophistication and so is Laravel Forge – a server management PAAS built for deployment of PHP, WordPress and Laravel Web applications.

PHP developers find Forge saves time and a ton of work on hands-on server provisioning and site deployments. This is because its intuitive user interface (UI), dashboard, and well-written APIs and scripts integrate with infrastructure providers like AWS and DigitalOcean, source code management (SCM) tools like GitHub and GitLab, and on-server database installations such as MySQL, PostgreSQL, and MariaDB.

Forge can install and manage multiple versions of PHP, common open-source software such as Nginx, phpMyAdmin, Redis, Memcached, Uncomplicated Firewall (UFW), and Let’s Encrypt on your Ubuntu instance via SSH, all at the click of a button, in just over 10 minutes. In addition, it includes proprietary software for monitoring CPU loads, RAM, and disk space on the server, along with the logrotate tool for logs.

Forge as it's fondly called was released by Laravel creator – Taylor Otwell – as a way to encourage monetized support for ongoing Laravel Framework Opensource Development. Over the years, it has morphed into a full-fledged industry subscription-based product and currently manages 528,901 applications.

This article will walk you through Forge's setup and features, its integration to AWS, DigitalOcean, and GitHub as well as the Laravel app itself and how it connects to the PostgreSQL database.

Requirements

To proceed, the following are required:

  • A Laravel Forge Account here.

  • An AWS or DigitalOcean account.

  • GitHub or Gitlab repository containing a Laravel Project.

  • Any preferred domain name.

A 5-day trial period is available to first-time users after which you will be required to register and activate a subscription on the Laravel Forge platform for as low as $12/month. This offers provisioning for a single server, push to deploy, unlimited sites, and deployments.

As regards Server Providers, you may opt for an AWS account and leverage the free tier plan or opt for a DigitalOcean account to get $200 worth of credit for 60days. The monthly cost of a server can be as low as $5 for both providers.

You will also need Source Code Management (SCM) platforms like GitHub or Gitlab containing the Laravel project you want deployed.

Lastly, you may purchase a domain such as .tech which retails for as low as $4.99/yr. Of course, free domains or domains sold by other Hosting platforms work as well. You can also deploy without a domain however, SSL certs will not apply and your content will have to be accessed by server IP only.

Forge Setup

The following are the sequence of events to get you up and running on forge:

Registration and Connection

Forge registration requires the usual data points – email address and password after which you’re directed to a page for authentication of SCM e.g Github via OAUTH. This is relatively painless and easily completed after a few clicks.

On the same page, you would be required to connect to your Server Provider Account. Please see the connections to AWS and DigitalOcean accounts below:

  • Amazon Web Service (AWS)

    Few requirements have been provided to ensure Forge works with AWS as intended, chief of which is to create an IAM user having Programmatic API Access and belong to a group with AmazonEC2FullAccess and AmazonVPCFullAccess policies.

    If you would be creating a new VPC, then it's smooth sailing however if you intend to use an existing VPC, you'll need to configure the subnet to auto-assign public addresses and modify the security group to allow Forge’s SSH connection from a specific IP.

  • DigitalOcean

    In contrast, all you need to connect the DigitalOcean droplet to forge is a single API token key generated.

The next step would be to add your SSH keys to your profile to enable you to log in manually to the server if you need to. SSH Keys can be generated in a Linux or Mac environment using this command:

ssh-keygen -t rsa -b 2048

On a Windows PC, you may use puttygen.exe to generate one and putty.exe for login.

Server, Database Creation and Provisioning

The creation of a Server from your linked provider on Forge is as easy as selecting your preferred specification from a row of menu drop-downs. One of the first things to do is to name your creation and pick a Server type depending on your needs.

I want an onboard database along with my app installed so "App Server" is it. I'd also be creating a new VPC and checking the box to add my server SSH Key to GitHub to facilitate continuous deployment.

Clicking on the green "create server" button starts the process and a notification pops up letting you know the estimated time to complete server creation and provisioning. In my attempt, it took about 15mins.

Practical observation is that with this setup, it would not do to select "App Server" and subsequently opt for less than 2GB of RAM and 32GB. Also, please note that whatever database you select – in my case PostgreSQL 12 - Forge installs and provisions the database username and password with the name - forge. However, it can create and manage new databases and users asides the default.

DNS Setting

Both public and private IP address of the server is displayed at the top of your page on completion of server creation and provisioning. The server can now be accessed by its public IP address over HTTP with the default page being the NGINX welcome page.

The next step would be to add the domain name you've purchased to your site as depicted below. Simply head to your DNS settings for your domain to create an A record pointing to the public IP address of the server. You may also create CNAME records for subdomain should you desire. When done, you would need to add the domain onto forge like so and click the wildcard option to allow for subdomains.

Adding Enviroment Variables

Forge provides an environment field where the contents of your .env file can be pasted and saved.

Deployment

If you're satisfied with the default deploy script Forge provides, you may go on to the next step else simply put yours and save. You may activate the quick deploy option also. This means any push to your repository triggers an automatic deployment to the server via the deploy script.

Install the Laraval App from GitHub

To ensure the Laravel App can connect to the server PostgreSQL database, check that in config/database.php, the pgsql array is present. If yes, it would indicate the PostgreSQL driver is installed. This is good. Please note that there will be no need to edit the file if your .env properly references the PostgreSQL fields as seen in the previous paragraph.

Further down is the “Deployment Branch” card which has a drop-down menu to select main or any other preferred branch within that repository for deployment.

Next is the “Update Git Remote” card and button Forge provides for cloning and "installing" the repository onto the server via the default deployment script above. You will be required to check the SCM option (i.e., GitHub) and enter your GitHub username/repository then click the green button to initiate. Voila! Your site is deployed!

SSL Encryption and Database connection

If you would like a free LetsEncrypt SSL certificate for your site, you may do so via the SSL menu as seen below.

As for database connection and manipulation, Forge recommends a third-party provider TablePlus however if you’d prefer other vendors or even opensource alternatives, PGAdmin4 is readily available.

Conclusion

The Laravel Forge PAAS provides good value for money in the provisioning of, deployment on, monitoring, and security for Servers hosting PHP, WordPress, and Laravel Web Apps. There is active technical support, a plethora of documentation, and a blog to forge a satisfactory experience every single time. Click here to learn more.

*This article was first written April 20, 2021 and amended slightly for 2023

0
Subscribe to my newsletter

Read articles from Sydney Ekpo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sydney Ekpo
Sydney Ekpo