Deploying a Statamic Site on DigitalOcean Using Laravel Forge

Lokman Musliu Founder and CEO of Lucky Media
Lokman Musliu

February 1, 2024 · 5 min read

Deploying a Statamic Site on DigitalOcean Using Laravel Forge

Laravel Forge and DigitalOcean

When deploying applications, developers look for simple, efficient platforms with strong features. DigitalOcean is a popular cloud computing provider since 2012, known for its easy-to-use virtual machines, or Droplets and services for developers and small-to-medium businesses. Laravel Forge is a top server management tool, making it easy to deploy PHP applications, including Laravel and Statamic sites, on clouds like DigitalOcean.

Domain and Server Alignment

Before you deploy your Statamic site, make sure you have a domain with an A Record pointing to your DigitalOcean server. This step links your domain name to the server’s IP address, allowing people to visit your site.

Using Laravel Forge

You can easily manage servers and applications on the Laravel Forge website. Log in, select DigitalOcean as your provider, and begin the process of setting up your Statamic site.

Creating a New Site on DigitalOcean

Forge makes it easy to create a new site on DigitalOcean. Choose your root domain and check that the PHP version matches your Statamic project’s needs, as listed in the composer.json file. For example, we had to switch from PHP 7.4 to PHP 8.1 for our project.

Root Domain Forge

Connecting your Git Repository

It’s easy to connect your Git repository to Forge. Enter your repository details, choose the right branch, and Forge will install your Statamic site’s code on your DigitalOcean server.

GitHub configuration

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 "php@8.1 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 important, and Forge’s Let’s Encrypt integration lets you add free TLS certificates to your Statamic site for secure connections on DigitalOcean. In the SSL section, click Let’s Encrypt to get your certificate. Wait a few moments and you will see your certificate in the table as displayed in our image below.

laravel forge ssl filament

Setting up the Deployment script

Forge’s push-to-deploy feature makes deployment easy. We have created a template repository that we use when creating our Statamic site repo on GitHub. 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 better 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/*;

Configuring 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 important 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 important to configure the queue correctly to ensure all static pages are 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=youremail@email.com

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. After a successful deployment, your Statamic site is up and running on DigitalOcean, ready for users to enjoy.

Conclusion

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.

Technologies

StatamicForge
Lokman Musliu Founder and CEO of Lucky Media
Lokman Musliu

Founder and CEO of Lucky Media

Stay up-to-date

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

Let’s chat

We partner with a limited number of brands each quarter to ensure senior-level attention on every project.

lokman and arlind headshots
Teamwork

Related posts

March 9, 2022

Event Listeners for Statamic
Statamic