PHP Session using Sharedance in Apache Web Cluster
Our new online shopping cart site is run on 3 Apache servers which mount the same document root in all nodes. With a load balancer in front of it to distribute the HTTP/HTTPS connections equally using weigh round-robin algorithm, we are facing big problem in session handling for the site; When user’s session is not exist in the current server, they need to authenticate once more if load balancer redirect the user to another server. We are considering following solutions:
- Mount the same partition of session.save_path directory in all nodes using GFS2:
- Serious IO issue due to high write in single directory
- Increase server load especially on the GFS2 locking (dlm) process
- Use memcached server:
- Need to have simple modification on the PHP code
- The sessions are saved in memory instead of disk. It is fast but session ID will be gone if server rebooted or service restarted.
- Set session to be stored in MySQL database:
- Need to modify the PHP code especially on how it handle locking. If session stored in file format, the file system will automatically handling the locking part.
- Increase the database server workload due to high read/write and it can generate to millions row within months.
At the end of the day, we are planning to use Sharedance because it is super simple. You just need to install the server, do some changes in php.ini, restart Apache and done! You are then set to have a session server which will be lookup by all web cluster nodes.
OS: CentOS 6.3 64bit
Server IP: 192.168.10.50
Session directory: /var/sharedance
Website: misterryan.com
Webserver #1: 192.168.10.101
Webserver #2: 192.168.10.102
Webserver #3: 192.168.10.103
Session Server (Sharedance)
1. We will use RPMforge to make our life easier:
$ rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt $ rpm -Uhv http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm |
2. Install Sharedance using yum:
$ yum install sharedance -y |
3. I want Sharedance to listen to the main IP and cache expiration should be 6 hours. Open /etc/sysconfig/sharedance via text editor and make sure you have following line:
SHAREDANCE_OPTIONS="--ip=192.168.10.50 --expiration=21600" |
You can use following command to check the complete list of options available:
$ sharedanced --help |
4. Make sure it is auto start on boot and start the Sharedance service:
$ chkconfig sharedance on $ service sharedance start |
5. You can check in the process list this 2 processes should exist and listening to port 1042:
$ ps aux | grep sharedanced 496 2621 0.0 0.0 111520 568 ? Ss 11:44 0:00 sharedanced [SERVER] 496 2622 0.0 0.0 111520 352 ? SN 11:44 0:00 sharedanced [CLEANUP] |
$ netstat -tulpn | grep sharedanced tcp 0 0 192.168.0.171:1042 0.0.0.0:* LISTEN 2621/sharedanced [SERVER] |
To allow firewall rules in iptables for Sharedance port:
$ iptables -I INPUT -p tcp --dport 1042 -j ACCEPT |
6. We need to copy the PHP session handler and sharedance PHP file provided by Sharedance which is located under /usr/share/doc/sharedance-0.6/php/ direcotry to all web cluster nodes so they can prepend in the php.ini. I will copy them to /etc/php.d directory in all nodes:
$ cd /usr/share/doc/sharedance-0.6/php/ $ scp session_handler.php sharedance.php 192.168.10.101:/etc/php.d/ $ scp session_handler.php sharedance.php 192.168.10.102:/etc/php.d/ $ scp session_handler.php sharedance.php 192.168.10.103:/etc/php.d/ |
Web Servers
1. Change the php.ini value of your server as below:
auto_prepend_file = /etc/php.d/session_handler.php session.save_handler = user |
2. Edit /etc/php.d/session_handler.php using text editor and make sure the first line contain the IP address of Sharedance server:
define('SESSION_HANDLER_HOST', '192.168.10.50'); |
3. Restart Apache web server to apply the changes:
$ service httpd restart |
Testing
I download this file: http://blog.secaserver.com/files/session.tar.gz and execute it from the web server to get following result:
We should see the same session ID exist in Sharedance session directory at /var/lib/sharedance:
$ ls -al | grep tqsncjk23k78cm747n4b1eq5l4 -rw------- 1 sharedance sharedance 24 Aug 1 12:17 tqsncjk23k78cm747n4b1eq5l4 |
Related Posts
- High Availability: Configure Piranha for HTTP, HTTPS and MySQL
- CentOS: Configure Piranha as Load Balancer (Direct Routing Method)
- High Availability: Web Server Cluster using Apache + Pound + Unison
- Linux: 2 Way File Synchronization and Replication using Unison + FTP
- Manage Multiple MySQL Servers using PHPmyAdmin
- Install MySQL Cluster in Debian
- High Availability: cPanel with MySQL Cluster, Keepalived and HAProxy
- Monitor MySQL Galera Cluster from Split-Brain
- CentOS 6: Install MySQL Cluster – The Simple Way
- Linux: Add New User and Group into .htpasswd
2 Responses to PHP Session using Sharedance in Apache Web Cluster
Leave a Reply Cancel reply
Sci/Tech – Google News- Microsoft Should Leave the Competition Out of Ads - CIO (blog) 23 May 2013
- Samsung Reports 10 Million Sales of New Phone - New York Times (blog) 23 May 2013
- New Kinect for Windows to improve human interaction with computers - PCWorld 23 May 2013
- In wake of hacks, Twitter introduces new two-step verification process - Christian Science Monitor 23 May 2013
- Amazon's Kindle Fire HD: A Stealth BYOD Tablet Competitor - Forbes 23 May 2013



This was the problem i was facing the moment i made my web cluster live in production.
You saved my life SECAGUY
CHEERS TO YOUR BRO!!!!!!
KEEP UP THE GOOD WORK>>>>>>>>>>>>>>>>>>>>>>>>>>
No problem Parth!