Load Balancing in Apache 2.2 + PHP 5 Growbal Kuo 2011-11-20 1. Preparation 1.1 Install openssl-1.0.0d 1.2 Install PHP 5 2. Install Apache 2.2 2.1 Edit apache2_config1.sh: ./configure \ --prefix=/usr/local/apache2 \ --enable-so \ --enable-ssl \ --with-ssl=/usr/local/ssl \ --enable-mods-shared=all \ --enable-proxy \ --enable-proxy-balancer 2.2 Edit conf/httpd.conf #LoadModule #LoadModule #LoadModule #LoadModule #LoadModule #LoadModule #LoadModule auth_basic_module modules/mod_auth_basic.so auth_digest_module modules/mod_auth_digest.so authn_file_module modules/mod_authn_file.so authn_alias_module modules/mod_authn_alias.so authn_anon_module modules/mod_authn_anon.so authn_dbm_module modules/mod_authn_dbm.so authn_default_module modules/mod_authn_default.so LoadModule authz_host_module modules/mod_authz_host.so #LoadModule authz_user_module modules/mod_authz_user.so #LoadModule authz_owner_module modules/mod_authz_owner.so #LoadModule authz_groupfile_module modules/mod_authz_groupfile.so #LoadModule authz_dbm_module modules/mod_authz_dbm.so #LoadModule authz_default_module modules/mod_authz_default.so #LoadModule ldap_module modules/mod_ldap.so #LoadModule authnz_ldap_module modules/mod_authnz_ldap.so LoadModule include_module modules/mod_include.so LoadModule log_config_module modules/mod_log_config.so LoadModule logio_module modules/mod_logio.so LoadModule env_module modules/mod_env.so LoadModule ext_filter_module modules/mod_ext_filter.so LoadModule mime_magic_module modules/mod_mime_magic.so #LoadModule expires_module modules/mod_expires.so LoadModule deflate_module modules/mod_deflate.so LoadModule headers_module modules/mod_headers.so LoadModule usertrack_module modules/mod_usertrack.so LoadModule setenvif_module modules/mod_setenvif.so LoadModule mime_module modules/mod_mime.so #LoadModule dav_module modules/mod_dav.so LoadModule status_module modules/mod_status.so LoadModule autoindex_module modules/mod_autoindex.so LoadModule info_module modules/mod_info.so #LoadModule dav_fs_module modules/mod_dav_fs.so LoadModule vhost_alias_module modules/mod_vhost_alias.so LoadModule negotiation_module modules/mod_negotiation.so #LoadModule dir_module modules/mod_dir.so #LoadModule actions_module modules/mod_actions.so #LoadModule speling_module modules/mod_speling.so LoadModule userdir_module modules/mod_userdir.so LoadModule alias_module modules/mod_alias.so LoadModule rewrite_module modules/mod_rewrite.so LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_ftp_module modules/mod_proxy_ftp.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_connect_module modules/mod_proxy_connect.so #LoadModule cache_module modules/mod_cache.so #LoadModule suexec_module modules/mod_suexec.so #LoadModule disk_cache_module modules/mod_disk_cache.so #LoadModule file_cache_module modules/mod_file_cache.so #LoadModule mem_cache_module modules/mod_mem_cache.so #LoadModule cgi_module modules/mod_cgi.so # # The following modules are not loaded by default: # #LoadModule cern_meta_module modules/mod_cern_meta.so #LoadModule asis_module modules/mod_asis.so #GLK #LoadModule php5_module modules/libphp5.so # Virtual hosts Include conf/extra/httpd-vhosts.conf 2.3 Edit conf/extra/httpd-vhost.conf # GLK, for PHP cluster <VirtualHost *:80> #CustomLog "logs/lb-access_log" common #CustomLog "logs/lb-access_log" combined LogFormat "%v %h %l %u %t \"%r\" %>s %b %{MYCOOKIE}C %{Set-Cookie}o %{BALANCER_SESSION_STICKY}e %{BALANCER_SESSION_ROUTE}e %{BALANCER_WORKER_ROUTE}e %{BALANCER_ROUTE_CHANGED}e" vhost_common CustomLog "logs/lb-access_log" vhost_common CookieLog logs/lb-cookie.log #TransferLog logs/lb-proxy-transfer.log #ErrorLog logs/lb-proxy-error.log # ServerName lb.example.com DocumentRoot /usr/local/apache2/htdocs/ ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> Header add Set-Cookie "BALANCEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED ProxyPass / balancer://mycluster/ stickysession=BALANCEID nofailover=On # The trailing slash is important! ProxyPassReverse / http://ip_of_fms4/ ProxyPassReverse / http://ip_of_fms5/ #ProxyPreserveHost On # This will preserce the host header for the backend servers <Proxy balancer://mycluster> BalancerMember http://ip_of_fms4 BalancerMember http://ip_of_fms5 route=fms4 route=fms5 ProxySet lbmethod=byrequests </Proxy> ProxyPass /balancer-manager ! <Location /balancer-manager> SetHandler balancer-manager Order deny,allow Allow from all </Location> </VirtualHost> 3. Execute Apache HTTP server /usr/local/apache2/bin/apachectl start 4. Open your browsers to the Load-Balancing Server and check the log file logs/lb-access_log 5. Reference 5.1 Troubleshooting load balancer stickyness (http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html) If you experience stickyness errors, e.g. users loose their application sessions and need to login again, you first want to check whether this is because the back-ends are sometimes unavailable or whether your configuration is wrong. To find out about possible stability problems with the back-ends, check your Apache error log for proxy error messages. To verify your configuration, first check, whether the stickyness is based on a cookie or on URL encoding. Next step would be logging the appropriate data in the access log by using an enhanced LogFormat. The following fields are useful: %{MYCOOKIE}C The value contained in the cookie with name MYCOOKIE. The name should be the same given in the stickysession attribute. %{Set-Cookie}o This logs any cookie set by the back-end. You can track, whether the back-end sets the session cookie you expect, and to which value it is set. %{BALANCER_SESSION_STICKY}e The name of the cookie or request parameter used to lookup the routing information. %{BALANCER_SESSION_ROUTE}e The route information found in the request. %{BALANCER_WORKER_ROUTE}e The route of the worker chosen. %{BALANCER_ROUTE_CHANGED}e Set to 1 if the route in the request is different from the route of the worker, i.e. the request couldn't be handled sticky. 5.2 Load balancer scheduler algorithm At present, there are 3 load balancer scheduler algorithms available for use: Request Counting, Weighted Traffic Counting and Pending Request Counting. These are controlled via the lbmethod value of the Balancer definition. See the ProxyPass directive for more information. 5.3 Load balancer stickyness The balancer supports stickyness. When a request is proxied to some back-end, then all following requests from the same user should be proxied to the same back-end. Many load balancers implement this feature via a table that maps client IP addresses to back-ends. This approach is transparent to clients and back-ends, but suffers from some problems: unequal load distribution if clients are themselves hidden behind proxies, stickyness errors when a client uses a dynamic IP address that changes during a session and loss of stickyness, if the mapping table overflows. The module mod_proxy_balancer implements stickyness on top of two alternative means: cookies and URL encoding. Providing the cookie can be either done by the back-end or by the Apache web server itself. The URL encoding is usually done on the back-end. 5.4 Apache conf error shooting Apache 'Invalid command 'Order', perhaps misspelled or defined by a module not included in the server configuration' give the above error message when the "Order" directive is used in it's main or vhosts configuration file(s). This directive needs the authz_host_module module to be loaded into apache. If you compiled apache with shared modules, then look for the following line in httpd.conf and uncomment it: