Web application on 2 servers

Hi

I would like to try hosting my web app on 2 servers with Apache behind an nginx reverse proxy and a third server with my MariaDB database.

It is a php website with log in sessions.

I am a little unclear - do I just have the exact same files on 2 different servers with some session tracking (sticky) load balancing by nginx?

How do I divide my files?

Also anyone have any reading suggestions on this? (Server configurations?)

Thanks!
Karen

When load balancing like this there are two options regarding sessions:

  1. Store sessions in a shared store - for example, store sessions in your MySQL database. That way both web servers can read them and it doesn’t matter which server is called for any request, since both servers will be able to handle all requests.
  2. Store sessions locally on the server and use sticky sessions - This ensures that once a visitor was routed to web server A they will always be routed to web server A because that is where there cookie is.

The advantage of a shared session store is that it doesn’t matter how many servers you have. If one ever goes down the other will just handle all traffic and your visitors will never know (apart from the site being slower than usual, probably). With sticky sessions however if one if the servers breaks down the other will handle the traffic, but everyone that was on the broken server no longer has a valid session and will be logged out / their cart will be empty / whatever.

Storing sessions in MySQL is however a bit more complicated to do and a tad slower.

If you have some leeway in what you install I would suggest redis instead of MySQL for storing sessions, as that’s way faster for that use case (i.e. don’t start throwing everything in Redis, it’s good at sessions, not at everything ;))

Regarding your application files, yes you need to install the exact same files on the two servers. I would recommend to store them in the exact same path as well; you don’t technically have to, but it’s a lot easier if you do.

Also, I would suggest using some automation to create your web servers, like Ansible, so that you can be 100% sure that they are exactly the same, and if one ever crashes you can easily create a new one.

Perfect!

Thank you ever so much :slight_smile:

Karen

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.