I am trying to use two web servers to run my website (as opposed to the one I use now). I can easily create another LEMP stack on my hosting provider (Digital Ocean) and setup a load balancer to direct traffic to both servers via round-robin, but I don't know how to run the same code on both web servers.

For Example: I know I could upload the same php files to the second web server, but if I wanted to edit a php file I would have to manually edit the file on both droplets. Is there a way to setup the second web server as a sort of mirror, so it runs the code on the first web server?

I appreciate any advice, as I am new to development/server configuration.

  • Have you learned something new from your question and our answers? What solution have you came up with? – titus Jan 18 at 11:25

There are a few things you need in order for a load balancer to work right. It sounds like the thing you're mostly interested in is how to get the right code onto each server. Fortunately this is the easy part!

Use a web-accessible version control service.

There are lots of hosted options, and you can even host your own if you like.
The choice of version-control system is maybe less open-ended; git is strongly recommended although SVN is ok and slightly faster to learn.
Using hosted version control doesn't mean that your code has to be public, although if you want your code to be private you'll need to figure out how your servers will read it. More on that later.
Finally, tell your servers to download the code from the repository. You can do this once, with the understanding that every time you make changes you'll need to ssh into the boxes again to tell them to update, or you can set up a cron job to keep them updated all the time.

Other stuff:

Your load balancer needs to detect if one of the servers goes offline. It sounds like you have a managed load balancer solution on hand, so it probably has tools for this purpose built in.

You need a shared persistent storage of some kind. The first, and for some systems only, piece of this is to have your DB on a separate server. (Making the DB redundant too is its own topic.) Depending on your needs, that's often sufficient.

You man need a system for handling secret configuration data. For example, it's not a great idea to store the DB credentials (or version-control credentials) in version-control. You can keep them up-to-date manually, or you can look into something more complicated.

I've glossed over a lot of best-practice details, which matter, both to keep this short and because what's right for you depends on your situation. I've also neglected a lot of very nice commercial options for solving these problems, both because I'm trying to suggest small changes from your current setup and because capitalism is a disease.

  • I think you meant to put that on the question instead of one of the answers? – ShapeOfMatter Jan 17 at 17:18
  • Oh yes. I will move it. – titus Jan 18 at 11:24

Consider why you want to use two webservers in the first place.....

Probably the primary: Higher Availability...in the even one webserver crashes the other would pick up the slack.

If you have the entire site resident on one server, this defeats the purpose of the higher availability you are trying to achieve. Thus, for safety each webserver would have to have a copy of the same code to execute.

Problems associated with this issue are the storage and retrieval of any database data. Unless you are would be using a common backend server for these servers. Then both webservers could feed into the backend server. If you are depending on any caching then this might be a problem (like session parameters or previously read data).

Depending on what your final goal is....the resolution is either quite simple or mind bogglingly complex.


You asked simple question, here is simple answer.

I would guess you are not some "giga mega" website, pardon me for that.

You can simply add a CRON job to rsync web files each minute, or 5 or what ever schedule you think is fine knowing how often you would change files, or files get uploaded via web.

Besides that, you should or need to run another server which will act as a DB server if you have one.

So both servers have same data, you can extend that to the "master slave" DB configuration but I beleive you will be okay without that.

After you have third server, you are ready to turn on roundrobin and fire up your load balancer.

Except if you have sessions, then I belevie DigitalOcean loadbalancer have Sticky Sessions.

Would you say I understood your question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.