Linux 如何对 php 应用程序进行负载平衡?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5085599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load balance a php application?
提问by Chris Abrams
I am looking for guides, advice, or samples of how to load balance a php application. My setup is Ubuntu 10.04 and PHP 5.3. I have never load balanced servers before and I am looking for any help that is offered.
我正在寻找有关如何对 php 应用程序进行负载平衡的指南、建议或示例。我的设置是 Ubuntu 10.04 和 PHP 5.3。我以前从未对服务器进行负载平衡,我正在寻找提供的任何帮助。
Update:
It's a web application that is expected to have a few hundred users using it at the same time. MySQL will be the database. There will be sessions used for the users but I have heard that sessions cannot be carried over multiple servers. There will be very frequent content updates. There will be files but I'll just use a CDN for those.
更新:
这是一个 Web 应用程序,预计将有数百名用户同时使用它。MySQL 将成为数据库。会有用于用户的会话,但我听说会话不能通过多个服务器进行。会有非常频繁的内容更新。会有文件,但我只会使用 CDN。
采纳答案by tucaz
load balancing a web application has not much to do with the application itself but more with hosting and infrastructure. However there are still some key points you have to pay attention when building an app that is supposed to be load balanced. Here are some:
Web 应用程序的负载平衡与应用程序本身没有太大关系,而更多地与托管和基础设施有关。但是,在构建应该进行负载平衡的应用程序时,您仍然需要注意一些关键点。这里有一些:
- Do not use state server. Meaning you cannot use the traditional session object because it is stored in the memory of the local machine so an object maybe available in one server but not in the other
- Do not use disk to store anything. Same reason as the above. You might write a file to server 1 disk and try to read in server 2
- Bottom line you should avoid any kind of resources that are server dependent. If you need to use something like this (state server, disk, etc) you should store a shared resource so all load balance machines access it
- 不要使用状态服务器。这意味着您不能使用传统的会话对象,因为它存储在本地机器的内存中,因此一个对象可能在一个服务器中可用,但在另一台服务器中不可用
- 不要使用磁盘来存储任何东西。原因和上面一样。您可以将文件写入服务器 1 磁盘并尝试在服务器 2 中读取
- 最重要的是,您应该避免使用任何依赖于服务器的资源。如果您需要使用这样的东西(状态服务器、磁盘等),您应该存储一个共享资源,以便所有负载平衡机都可以访问它
If this is a problem for an existing application, for example, you can workaround this by setting affinity on the Load Balancer meaning that all requests that come from a specific user will be served by the same server. Obviously this approach has the downside of making your app less scalable since one server can get more processing than the other.
例如,如果这是现有应用程序的问题,您可以通过在负载均衡器上设置亲缘关系来解决此问题,这意味着来自特定用户的所有请求都将由同一服务器提供服务。显然,这种方法的缺点是使您的应用程序的可扩展性降低,因为一台服务器可以获得比另一台服务器更多的处理。
So, regarding the software to do the load balancer I'm not a Linux guy but I see lots of people talking about Apache as WebServer and HAProxy as the load balancer app.
因此,关于执行负载均衡器的软件,我不是 Linux 人,但我看到很多人将 Apache 称为 WebServer,将 HAProxy 称为负载均衡器应用程序。
Hope it helps!
希望能帮助到你!
回答by Sean Milheim
There are PHP classes that allow you to store the session in MySQL.
有 PHP 类允许您将会话存储在 MySQL 中。
http://php.net/manual/en/function.session-set-save-handler.php
http://php.net/manual/en/function.session-set-save-handler.php
I would put something like nginx on a front end server and use that for your load balancing. Behind that you could have your two web servers and even use nginx to serve your static content.
我会将 nginx 之类的东西放在前端服务器上,并将其用于负载平衡。在这之后,您可以拥有两个 Web 服务器,甚至可以使用 nginx 来提供静态内容。
Behind your webservers you would have your mysql db.
在您的网络服务器后面,您将拥有您的 mysql 数据库。
I've designed a few networks that have received just over 1,000/imp /sec and started the network design with this. After the traffic increased I usually just scaled out the infrastructure from there adding more webservers and more read-only db's and swapping out nginx with a pair of F5's.
我设计了一些网络,这些网络的接收速度刚刚超过 1,000/imp/sec,并以此开始了网络设计。在流量增加后,我通常只是从那里扩展基础设施,添加更多网络服务器和更多只读数据库,并用一对 F5 替换 nginx。