Load Balancing in Apache 2.2 + PHP 5

GrowbalKuo2011-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

#LoadModuleauth_basic_module modules/mod_auth_basic.so

#LoadModuleauth_digest_module modules/mod_auth_digest.so

#LoadModuleauthn_file_module modules/mod_authn_file.so

#LoadModuleauthn_alias_module modules/mod_authn_alias.so

#LoadModuleauthn_anon_module modules/mod_authn_anon.so

#LoadModuleauthn_dbm_module modules/mod_authn_dbm.so

#LoadModuleauthn_default_module modules/mod_authn_default.so

LoadModuleauthz_host_module modules/mod_authz_host.so

#LoadModuleauthz_user_module modules/mod_authz_user.so

#LoadModuleauthz_owner_module modules/mod_authz_owner.so

#LoadModuleauthz_groupfile_module modules/mod_authz_groupfile.so

#LoadModuleauthz_dbm_module modules/mod_authz_dbm.so

#LoadModuleauthz_default_modulemodules/mod_authz_default.so

#LoadModuleldap_module modules/mod_ldap.so

#LoadModuleauthnz_ldap_module modules/mod_authnz_ldap.so

LoadModuleinclude_module modules/mod_include.so

LoadModulelog_config_module modules/mod_log_config.so

LoadModulelogio_module modules/mod_logio.so

LoadModuleenv_module modules/mod_env.so

LoadModuleext_filter_module modules/mod_ext_filter.so

LoadModulemime_magic_module modules/mod_mime_magic.so

#LoadModuleexpires_module modules/mod_expires.so

LoadModuledeflate_module modules/mod_deflate.so

LoadModuleheaders_module modules/mod_headers.so

LoadModuleusertrack_module modules/mod_usertrack.so

LoadModulesetenvif_module modules/mod_setenvif.so

LoadModulemime_module modules/mod_mime.so

#LoadModuledav_module modules/mod_dav.so

LoadModulestatus_module modules/mod_status.so

LoadModuleautoindex_module modules/mod_autoindex.so

LoadModuleinfo_module modules/mod_info.so

#LoadModuledav_fs_module modules/mod_dav_fs.so

LoadModulevhost_alias_module modules/mod_vhost_alias.so

LoadModulenegotiation_module modules/mod_negotiation.so

#LoadModuledir_module modules/mod_dir.so

#LoadModuleactions_module modules/mod_actions.so

#LoadModulespeling_module modules/mod_speling.so

LoadModuleuserdir_module modules/mod_userdir.so

LoadModulealias_module modules/mod_alias.so

LoadModulerewrite_module modules/mod_rewrite.so

LoadModuleproxy_module modules/mod_proxy.so

LoadModuleproxy_balancer_module modules/mod_proxy_balancer.so

LoadModuleproxy_ftp_module modules/mod_proxy_ftp.so

LoadModuleproxy_http_module modules/mod_proxy_http.so

LoadModuleproxy_connect_module modules/mod_proxy_connect.so

#LoadModulecache_module modules/mod_cache.so

#LoadModulesuexec_module modules/mod_suexec.so

#LoadModuledisk_cache_module modules/mod_disk_cache.so

#LoadModulefile_cache_module modules/mod_file_cache.so

#LoadModulemem_cache_module modules/mod_mem_cache.so

#LoadModulecgi_module modules/mod_cgi.so

#

# The following modules are not loaded by default:

#

#LoadModulecern_meta_module modules/mod_cern_meta.so

#LoadModuleasis_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 /

ProxyPassReverse /

#ProxyPreserveHost On # This will preserce the host header for the backend servers

<Proxy balancer://mycluster>

BalancerMember route=fms4

BalancerMember route=fms5

ProxySetlbmethod=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

(

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: