Laravel Forge
- Server
- Backups
- Site
- SSL
- Packages
- GitHub Actions
- Cache-Control
- Password Protection
- Cron Jobs
- Redis
- Page Cache
- Bots
- Production
Server#
First, set up a server on Laravel Forge. Store the sudo password and database password in 1Password as a server item. Name it after the production domain and remember to add the IP address to the server.
Backups#
Remember to add backup snapshots on the server, whether on DigitalOcean, Glesys, or your service of choice. Preferably set up daily backups and store them for at least seven days.
Site#
Create a site on Laravel Forge using the production domain with the subdomain www. We prefer not to use naked domains for websites. Additionally, delete the default site created during the initial setup.
SSL#
Enable SSL for the site using Let's Encrypt. This process will take a few minutes.
Packages#
If you're building a WordPlate site and using private package repositories like ACF, remember to add the credentials under Composer Package Authentication.
GitHub Actions#
Use the GitHub Actions SSH key to deploy the site, which you can find in 1Password. Add the public key to the server and the private key to GitHub Secrets. Create a new secret named SSH_PRIVATE_KEY on GitHub with the private key. Also, add the server's IP as a variable called SERVER_IP. Additionally, include the FORGE_API_TOKEN secret to enable calls to the Forge API.
Cache-Control#
I recently read "Your Cache Headers Could Probably Be More Aggressive", which serves as a reminder to review the Cache-Control header of your websites. Below is a straightforward nginx configuration for this purpose.
location ~* \.(?:css|js|woff2|jpg|jpeg|png|gif|svg|webp|avif|mp4)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
Password Protection#
During the development phase, you may want to password-protect the site. Enable basic authentication through the dashboard on Laravel Forge with an easy-to-remember username and password, which we can send to the client.
Cron Jobs#
If we are building a WordPlate site, it is recommended to disable the default cron in WordPress. This can be done by adding the following to the environment:
DISABLE_WP_CRON=true
Then, set up a cron job in the Laravel Forge dashboard to call the WordPress cron every minute.
* * * * * curl https://example.se/wordpress/wp-cron.php
Redis#
Install Redis Object Cache plugin as a must-use plugin. This plugin will cache the database queries, which can significantly improve performance.
To enable Redis through the dashboard, you'll temporarily need to allow file edits.
DISALLOW_FILE_MODS=false
Forge installs Redis without a memory limit, and WordPress never removes its old query cache keys. Left alone, Redis grows until the server runs out of memory and starts swapping. Set a limit and let Redis evict the least recently used keys. Around 10–15% of the server's memory is a good starting point.
echo -e "\nmaxmemory 512mb\nmaxmemory-policy allkeys-lru" | sudo tee -a /etc/redis/redis.conf
sudo service redis-server restart
Then give the cache keys a maximum lifetime in wp-config.php.
define('WP_REDIS_MAXTTL', env('WP_REDIS_MAXTTL', 86400));
Check that the limit is in place. The number of expires should grow together with keys.
redis-cli CONFIG GET maxmemory
redis-cli CONFIG GET maxmemory-policy
redis-cli INFO keyspace
If the server is already swapping, flush the cache. A restart alone won't help since Redis loads its data from disk again.
redis-cli FLUSHALL ASYNC
Only do this when Redis is used for the WordPress object cache alone. If Laravel queues or sessions share the same Redis, use volatile-lru instead and skip the flush.
Page Cache#
WordPress builds every page with PHP, even when nothing has changed. Let nginx store the finished pages with its FastCGI cache and serve them without touching PHP.
Add storage/cache/.gitkeep to the project and ignore everything else in that directory. This keeps the cache outside the web root.
Define the cache zone above the server block in the nginx configuration of the domain. Every site on the server needs its own zone name.
fastcgi_cache_path /home/forge/example.se/storage/cache levels=1:2 keys_zone=WORDPRESS:100m inactive=1d max_size=1g;
fastcgi_cache_key "$scheme$request_method$http_host$request_uri";
fastcgi_ignore_headers Cache-Control Expires Set-Cookie Vary;
fastcgi_cache_use_stale error timeout invalid_header http_500;
Then turn the cache on in the general site configuration, and skip it for requests that must reach PHP.
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 1d;
fastcgi_cache_valid 404 2h;
set $skip_cache 0;
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wordpress/wp-admin/|/wp-json/|/wordpress/xmlrpc.php|/wordpress/wp-.*.php|/feed/|/wordpress/index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in_|wordpress_user_sw_|bid_1_password_protected_auth") {
set $skip_cache 1;
}
Add the following to the location ~ \.php$ block. nginx drops the headers from the server level as soon as a location has its own add_header, so repeat them here.
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Fastcgi-Cache $upstream_cache_status always;
Check the response header. The first request returns MISS and the next one HIT. Logged in users get BYPASS.
curl -sI https://www.example.se/ | grep -i fastcgi-cache
Pages stay cached for a day, so WordPress has to clear the cache when the content changes. Install the Nginx Cache plugin and go to Tools → Nginx Cache in the dashboard. Enter /home/forge/example.se/storage/cache as the Cache Zone Path, check "Automatically flush the cache when content changes" and save. Use the Purge Cache button to clear it by hand.
Bots#
SEO crawlers hit every archive, filter and language version, and each unique page adds new cache keys. Block them in the nginx configuration of the site.
if ($http_user_agent ~* "(AhrefsBot|DotBot|SemrushBot|SERankingBacklinksBot|MJ12bot)") {
return 403;
}
Production#
When the time comes, change the DNS records to point to the new server. Remember to update the www subdomain as well.
A example.se "123.45.678.90"
CNAME www.example.se "example.se."
Next, set the environment variable to production in the Laravel Forge dashboard.
Once the domain is active, update the cron job to reflect the production domain.
Disable basic authentication in the Laravel Forge dashboard.