So, I decided to give DigitalOcean a try. They are very cheap and I wanted to load balance XenForo for quite some time. I paid $5 bucks and started creating droplets. I choose Debian. After a quick
apt-get install nginx php5-fpm php5-mysqlnd php5-gd mysql-server-5.5 I was up and running. Then, I followed one of the thousands guides you can find on the internet about load balancing with nginx.
My idea was simple: nginx at the front as the master and several droplets with php5-fpm as php slave servers. No web server. After a few hours of testing and setting up everything, the mini load balancer was ready. Afterwards, I started to tackle every major problem a cheap load balancer has:
1) sync: I used csync2 after reading very good comments on the forums. My only complaint is that I can't sync from my home computer. My IP is passing through a NAT and csync2 doesn't allow that.
2) Attachment: I have about 35GB on attachments. Since DigitalOcean doesn't let you buy additional space for your droplets, I would have to buy at least the 80GB droplet. I wasn't happy at all because my site doesn't need all that power at off peak hours, not to mention all the replication involved when using multiple servers and storing files in the filesystem. My solution is simple. Use
this addon, store attachments on Amazon S3 and don't let guest to see attachments. Shouldn't be too expensive.
3) Avatars: This one is tricky but I think I came up with a good solution
Code:
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass backend;
if ($args ~ avatar-upload) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
That's it. When the user tries to upload an avatar, through the admin control panel or their profile, Nginx doesn't use the backend servers and instead, it uses the local php5-fpm server. And since my site is rather small, I can serve all the avatars from the front server and no replication is needed.
4) I know this balancer is not redundant and if the database droplet or the front droplet fail, my entire site will go down. However, I think I can do pretty good with snapshots and backups. Although data loss might be possible for some time frame in case the database droplet crashes, I can be up and running again after some moderate time and my site is not mission critical.
My next idea is to find a way to automate scaling up and down. I'm planning to have the following setup:
- 1 front nginx/php5-fpm droplet - $10 (I need to test this with my live traffic but I'm going to start with a 1gb droplet)
- 1 mysql server (probably the 4gb droplet) - $40 (I don't know how this droplet will behave at peak times (800-1000 users according to XenForo)
- scale up with as many 512mb droplets as needed (they are CHEAP! $5). Scale down once the high load times are over
- buy backups only for the database server ($8).
- store attachments on Amazon S3 (~$4 to store 35gb in attachments... not very sure about bandwidth but I won't let visitors see attachments anymore. That should cut bandwith costs by a lot. Amazon S3 bandwidth is at $0.120 per GB right now)
I'm open for suggestions. Right now I'm paying $209 for a dedicated but I'm sure I can pay a lot less with this setup without sacrifying performance. And yeah, I'm going to have lots of fun!