Apache
Running a redundant apache server does not require much from apache itself, but rather the underlying filesystem and a proxy in front of it. The filesystem is important, so the website can be shared between the servers. For this we will use GlusterFS. PHP sessions will have to be shared between the webservers as well and finally we will put a proxy in front of apache, which will be a combination of Corosync and HA-proxy.
In this setup we will configure 2 apache servers. None of these will be master/slave/primary/secondary. They will just be running with the same configuration. Since the servers are configured the same way it makes sense to share some files.
- 192.168.1.51 www (virtual IP-address)
- 192.168.1.52 www01
- 192.168.1.53 www02
Software
apt-get install apache2 php php-mysql memcached php-memcache php-mcrypt
Filesystem
Configure your system as a GlusterFS client, so you have the following in your fstab.
/etc/glusterfs/www.vol /var/www glusterfs defaults,_netdev,rw 0 0
Testpage
Make a simple webpage, that shows something unique about the system. This is usefull when debugging. index.php
1 <?php
2 header('Content-Type: text/plain');
3 session_start();
4 if(!isset($_SESSION['visits']))
5 $_SESSION['visits'] = 0;
6 $_SESSION['visits']++;
7 echo "client: " . $_SERVER['REMOTE_ADDR'] . "\n";
8 echo "server: " . $_SERVER['SERVER_ADDR'] . "\n";
9 echo "hostname: " . gethostname() . "\n";
10 echo "visits: " . $_SESSION['visits'] . "\n";
11 echo "cookie: ";
12 if (array_key_exists('PHPSESSID', $_COOKIE))
13 echo $_COOKIE['PHPSESSID'];
14 echo "\n";
15 ?>
PHP Sessions
Creating a PHP session on one webserver, does not create it on the other. As requests bounces back and forth between the two webservers, this will be a problem for PHP sites. The solution is called memcached.
Add this to /etc/php/7.0/mods-available/memcache.ini.
memcache.allow_failover="1" memcache.hash_strategy="consistent" memcache.hash_function="crc32" memcache.session_redundancy=3 memcache.maxreclevel=0 memcache.maxfiles=0 memcache.archivememlim=0 memcache.maxfilesize=0 memcache.maxratio=0
In /etc/php/7.0/apache2/php.ini find the entry session.save_handler = files and change it to the following.
session.save_handler = memcache session.save_path = "tcp://www01:11211, tcp://www02:11211"
Add the IP-address of the webserver to the memcache configurtion in /etc/memcached.conf. This will be different for each of the webservers. Make sure there is no space between the comma and the IP-addresses. For www01 it will look like this.
-l 127.0.0.1,192.168.1.52
Copy the configuration to all webservers and finally restart the services.
service memcached restart service apache2 restart
Statistics
You can ask memcached for som statistics, by sending it a stats command.
echo 'stats'|nc www01 11211
This will produce something like this.
STAT pid 1696 STAT uptime 1335 STAT time 1581299188 STAT version 1.4.33 STAT libevent 2.0.21-stable STAT pointer_size 64 STAT rusage_user 0.144000 STAT rusage_system 0.204000 STAT curr_connections 2 STAT total_connections 183 STAT connection_structures 3 STAT reserved_fds 20 STAT cmd_get 180 STAT cmd_set 540 STAT cmd_flush 0 STAT cmd_touch 0 STAT get_hits 13 STAT get_misses 167 STAT get_expired 1 STAT get_flushed 0 STAT delete_misses 0 STAT delete_hits 0 STAT incr_misses 168 STAT incr_hits 12 STAT decr_misses 0 STAT decr_hits 0 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT touch_hits 0 STAT touch_misses 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 40514 STAT bytes_written 7832 STAT limit_maxbytes 67108864 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT time_in_listen_disabled_us 0 STAT threads 4 STAT conn_yields 0 STAT hash_power_level 16 STAT hash_bytes 524288 STAT hash_is_expanding 0 STAT malloc_fails 0 STAT log_worker_dropped 0 STAT log_worker_written 0 STAT log_watcher_skipped 0 STAT log_watcher_sent 0 STAT bytes 33738 STAT curr_items 334 STAT total_items 528 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT evictions 0 STAT reclaimed 0 STAT crawler_reclaimed 0 STAT crawler_items_checked 0 STAT lrutail_reflocked 0