DEV Community

Bryan
Bryan

Posted on • Originally published at devlogbook.com

1 1

Cloudflare Load Balancing with Laravel Forge SFTP File Only Servers

Table Of Contents

Assumptions

  1. Using Laravel Forge to setup servers
  2. Syncing folder(s) between Server 1 and Server 2
  3. Using Load balancing with Cloudflare

Setting up servers with Laravel Forge

rsync is required to sync between servers, using Laravel Forge to setup the servers will automatically install rsync

  1. Setup 2 servers
    • Do not delete the default site, deleting it did not work with load balancing for some reason
  2. SSH into server #2
  3. Create SSH key in server #2 and choose to save the key into /home/forge/.ssh/
  4. Copy the contents of the newly generated key with cat/home/forge/.ssh/<key_name_from_step_2>.pub
  5. Add the key to server #1 in Laravel Forge SSH Keys
  6. Create the main folder in server #1 and #2 that you want to be synced between them
  7. Add a test file to server #1 folder for test syncing
  8. In server #2 run: sudo rsync -avzhe "ssh -i /home/forge/.ssh/<key_name_from_step_2>" forge@<server_1_ip_address>:<directory_from_server_1_to_sync> <server_2_directory_to_sync_into>
    • If successful, terminal will show the files that got synced
  9. Add cronjob to server #2 to have it sync, use crontab.guru to figure out time
    • DO NOT add job in Scheduler in Laravel Forge, you will be required to input a password for sudo
    • Run sudo crontab -e
    • Add to the end of crontab * * * * * sudo rsync -avzhe "ssh -i /home/forge/.ssh/<key_name_from_step_2>" forge@<server_1_ip_address>:<directory_from_server_1_to_sync> <server_2_directory_to_sync_into>

Setting up Load balancing with Cloudflare

  1. Select the domain in Websites section in Cloudflare
  2. On the left go to Traffic -> Load Balancing
  3. Select Manage Pools to create a new pool
  4. Creating new Pool
    • Create origins (get ips from both servers created from forge)
  5. Create Load Balancer
    • Choose subdomain (it will create a loadbalancer subdomain in DNS)
    • Select pool
    • Attach pool monitor
    • Traffic steering OFF
    • No custom rules
  6. On the left go to SSL/TSL -> Origin Server
  7. Create certificate
    • RSA (2048)
    • hostname, put the subdomain used for the loadbalancer
    • 15 years is fine
  8. Copy the private key and certificate (Both servers will use them)
  9. Go back to Laravel Forge and install (Existing) SSL for both servers using private key and certificate created in step 7-8

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay