Deploying a Statamic Site on DigitalOcean Using Laravel Forge

lokman musliu
Lokman Musliu

February 1, 2024 · 5 min read · 281 views

Header Image

Laravel Forge and DigitalOcean

When it comes to deploying applications, developers seek platforms that offer simplicity, efficiency, and robust features. DigitalOcean has been a go-to cloud computing provider since 2012, known for its straightforward virtual machines, or "Droplets," and a suite of services designed for developers and small-to-medium-sized businesses. Meanwhile, Laravel Forge is the premier tool for server management, allowing for the effortless deployment of PHP applications, including Laravel and Statamic sites, on various cloud hosts like DigitalOcean.

Pre-Deployment Checklist: Domain and Server Alignment

Before initiating the deployment of your Statamic site, ensure you have a domain with an A Record pointing to your DigitalOcean server. This step is critical as it establishes the connection between your domain name and the server's IP address, paving the way for users to access your site.

Accessing Laravel Forge

To kick off the deployment, head over to the Laravel Forge website. Here, you can manage your servers and applications with ease. Log in to your account, select "DigitalOcean" as your cloud provider, and begin the process of setting up your Statamic site.

Creating a New Site on DigitalOcean

Forge simplifies the creation of a new site on your DigitalOcean server. Specify your root domain and ensure the selected PHP version is compatible with your Statamic project, as indicated in the composer.json file. In our case, we had to change the default PHP version "7.4" to our project requirements of PHP8.1

Root Domain

Connecting Your Git Repository

Linking your Git repository to Forge is a breeze. Input your repository details, choose the correct branch and let Forge install your Statamic site's code onto your DigitalOcean server.

Github

Configuring Your Environment Settings

Customize your Statamic site's environment within Forge:

  • Update the APP_NAME

  • Secure your APP_URL with HTTPS

  • Adjust STATAMIC_STACHE_WATCHER and

  • STATAMIC_STATIC_CACHING_STRATEGY for optimal performance.

  • APP_KEY= should be a unique, random string. If this key is missing, you can generate one by navigating to the command section in Forge and executing "[email protected] artisan key:generate".

APP_NAME="Your Site"
APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_URL=https://yoursite.com

STATAMIC_LICENSE_KEY=
STATAMIC_STACHE_WATCHER=false
STATAMIC_STATIC_CACHING_STRATEGY="full"
STATAMIC_REVISIONS_ENABLED=false
STATAMIC_GRAPHQL_ENABLED=false
STATAMIC_API_ENABLED=false

Securing Your Statamic Site with SSL

Security is a top priority, and Forge's integration with Let's Encrypt allows you to easily secure your Statamic site with free TLS certificates, ensuring encrypted connections for your users on DigitalOcean. Go to SSL and click Let's Encrypt to create your certificate. Wait a few moments and you will see your certificate in the table as displayed in our image below.

laravel forge ssl

Setting Up the Deployment Script

Forge's push-to-deploy feature streamlines the deployment process. We have created a template repository that we use when creating our Statamic site repo on GitHub. Feel free to use the template for your Statamic site too. Customize your deployment script by copying necessary commands from the Statamic-Starter "Deploying on Forge" section and excluding any migration commands to prevent database conflicts. Here you will only copy from the npm ci command and below.

For simple Statamic sites remove the commands queue, search:update and static:warm. Go to the Deployments section on Forge and paste the copied commands as you can see below:

cd /home/forge/yoursite.com
git pull origin $FORGE_SITE_BRANCH

$FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader

npm ci && npm run build
$FORGE_PHP artisan cache:clear
$FORGE_PHP artisan route:cache
$FORGE_PHP artisan statamic:stache:warm
$FORGE_PHP artisan statamic:static:clear
$FORGE_PHP artisan statamic:static:warm --queue

( flock -w 10 9 || exit 1
    echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock

Configuring Nginx for Enhanced Performance

Forge sets up Nginx and other server components to provide a cutting-edge environment for your Statamic site. Import and customize Nginx configurations from your repository to maximize performance on DigitalOcean.

Now go back to the Statamic-Starter and copy the nginx config. Let's start by copying the map section. Switch to Forge and on the top right corner click the Edit Files button located next to the Deploy Now button. Select Edit Nginx Configuration and paste it in the end just before the "# FORGE CONFIG (DO NOT REMOVE!)". See the code below as an example:

}

map $sent_http_content_type $expires {
    default    off;
    text/css    max;
    ~image/    max;
    application/javascript    max;
    application/octet-stream    max;
    application/font-woff      max;
    application/font-woff2     max;
    application/font-ttf      max;
    font/woff2    max;
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/yoursite.com/after/*;

Enhancing Nginx for Static Caching on Statamic

In the Statamic-Starter copy the command expires $expires; and paste it on the Nginx Configuration that you modified in the previous step.

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/yoursite.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name yoursite.com;
    server_tokens off;
    root /home/forge/yoursite.com/public;
    expires $expires;

    # FORGE SSL (DO NOT REMOVE!)

Static caching is crucial for a fast Statamic site. Replace the default Nginx location block with the one from Statamic's documentation to take full advantage of Nginx's caching on DigitalOcean. Navigate to Static Caching -> Nginx and copy the location code. Hit Save!

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/kids.luckym.dev/server/*;

location / {
	try_files /static${uri}_${args}.html $uri /index.php?$args;
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }

Configuring the Queue for Optimal Performance

As you prepare to deploy your Statamic site, it's essential to configure the queue correctly to ensure all static pages are primed and ready for your users. Within your project's .env section, set your QUEUE_CONNECTION to "redis" to make use of its fast, in-memory data store capabilities. Here's a snippet for clarity:

QUEUE_CONNECTION=redis
REDIS_DATABASE=0

If you're running multiple sites on the same server, assign each one to a distinct Redis database to prevent any overlap. Simply increment the REDIS_DATABASE number accordingly (e.g., 0, 1, 2, etc.).

Sample Environment Configuration File

Below, you'll find a full example of our frequently used configuration, which you can tailor to fit the specific needs of your Statamic site:

APP_NAME="Lucky Media"
APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_URL="https://www.luckymedia.dev"


STATAMIC_LICENSE_KEY=
STATAMIC_STACHE_WATCHER=false
STATAMIC_STATIC_CACHING_STRATEGY="full"
STATAMIC_REVISIONS_ENABLED=false
STATAMIC_GRAPHQL_ENABLED=false

STATAMIC_GIT_ENABLED=true
STATAMIC_GIT_PUSH=true
STATAMIC_GIT_DELAY=60
STATAMIC_GIT_QUEUE_CONNECTION=redis
STATAMIC_GIT_USER_NAME=your_username
STATAMIC_GIT_USER_EMAIL=[email protected]

SAVE_CACHED_IMAGES=true

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

# Not needed with Statamic
# DB_CONNECTION=mysql
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=laravel
# DB_USERNAME=forge
# DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=redis
REDIS_DATABASE=0
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=""
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

DEBUGBAR_ENABLED=false

Deploying your Statamic Site

Hit the Deploy Now button and wait for the output. Done! After a successful deployment, your Statamic site is up and running on DigitalOcean, ready for users to enjoy.

Your Statamic Site is Now Live

Laravel Forge has provided a seamless and secure deployment experience, from server management to live deployment. Enjoy the benefits of a well-deployed Statamic site on a robust DigitalOcean server, and take advantage of the powerful features that Forge offers for ongoing management and monitoring.


Bring Your Ideas to Life 🚀

Kickstart Your Statamic Project with the #1 Statamic Agency

Are you planning a new Statamic project or thinking about migrating your WordPress site to Statamic? Learn more about our expertise as a renowned Statamic development agency.

lokman musliu
Lokman Musliu

Founder and CEO of Lucky Media

Technologies:

Statamic
Forge
Heading Pattern

Related Posts

Stay up to date

Be updated with all news, products and tips we share!