

A Linux blog by ioMeWeekly, includes tutorials, news, help, programming, tips and how-to guides for opensource applications.
systemctl stop firewalld systemctl disable firewalld vi /etc/selinux/config
disabled
rebootLets start, login to HAProxy Server:
yum install haproxy
vi /etc/haproxy/haproxy.cfg
#--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend LB bind 192.168.1.3:80 reqadd X-Forwarded-Proto:\ http default_backend LB #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend LB 192.168.1.3:80 mode http stats enable stats hide-version stats uri /stats stats realm Haproxy\ Statistics stats auth admin:pass4325 # HAProxy Statistic username/password balance roundrobin # Load balancing to use round-robin option httpchk option httpclose option forwardfor cookie LB insert server web1 192.168.1.9:80 check # backend server. server web2 192.168.1.10:80 check # backend server.We shell enable the service and start the service:
systemctl enable haproxy systemctl start haproxyAt Webserver create a test page.
vi /var/www/html/p.html
192.168.1.9 (To display Webserver IP address)
systemctl restart httpdTo access the statistic page, navigate to the HAProxy ip/stats,login with the username and pass as states in haproxy.cfg.
vi /etc/yum.repos.d/mongodb.repoIf you are running a 64-bit system, use the following configuration:
[mongodb] name=MongoDB Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/ gpgcheck=0 enabled=1else for 32-bit system, use:
[mongodb] name=MongoDB Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/ gpgcheck=0 enabled=1To install the latest stable version of MongoDB, issue the following command:
yum install -y mongodb-orgInstall php,mongo driver:
pecl install mongocreate a mongo (php) ini
vi /etc/php.d/mongo.iniAnd include the driver:
extension=mongo.soEnable mongod on startup
systemctl enable mongodstart mongod:
service mongod startOpen mongo client to test.
mongocreate database test:
use testLets do an insert:
db.collection1.insert({"name":"desc"})And lets do an query:
db.collection1.find();Lets proceed with using mongodb in php. Restart php-fpm:
systemctl restart php-fpm.serviceWe shell create a php script:
<?php $connection = new Mongo(); $dbname = $connection->selectDB('test'); $collection = $dbname->collection1; $arr = array( 'name' => 'MongoDB', 'desc' => 'MongoDB is a document database.' ); $collection->insert($arr); $result = $collection->find(); foreach ($result as $document) { echo $document["name"]."\n"; echo $document["desc"]."\n"; } ?>You can run above script in web server or just run:
php test_mongo.php
<?php ################# redis ################################################### $k=0; $t_time=0; echo "Benchmark redis : "; $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $original_string = 'abcdefghijklmnopqrstuvwxyz'; for ($i=0;$i<=10000;$i++){ $random_string = get_random_string($original_string, 10); $random_string2 = get_random_string($original_string, 10); $a1[$i]=$random_string; $a2[$random_string]=$random_string2; } $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $redis -> set($a1[$i],$a2[$a1[$i]]); } $t_time=timer_end($tstart); echo "Total Set time: ".$t_time."
"; for ($j=0;$j<10;$j++){ $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $value=$redis -> get($a1[$i]); } $t_time=timer_end($tstart); echo "Loop $j (Get) time: ".$t_time."
"; $k=$k+$t_time; } echo "Total 10 loop Get time $k
"; ################# memcache ################################################### $k=0; $t_time=0; echo "Benchmark memcache : "; $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211) or die ("Cannot connect"); $original_string = 'abcdefghijklmnopqrstuvwxyz'; for ($i=0;$i<=10000;$i++){ $random_string = get_random_string($original_string, 10); $random_string2 = get_random_string($original_string, 10); $a1[$i]=$random_string; $a2[$random_string]=$random_string2; } $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $memcache->set($a1[$i], $a2[$a1[$i]], false, 20) or die ("Cannot save data"); } $t_time=timer_end($tstart); echo "Total Set time: ".$t_time."
"; for ($j=0;$j<10;$j++){ $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $value = $memcache->get($a1[$i]); } $t_time=timer_end($tstart); echo "Loop $j (Get) time: ".$t_time."
"; $k=$k+$t_time; } echo "Total 10 loop Get time $k
"; function timer_start() { $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; return $time; } function timer_end($start) { $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; return $time-$start; } function get_random_string($valid_chars, $length) { $random_string = ""; $num_valid_chars = strlen($valid_chars); for ($i = 0; $i < $length; $i++) { $random_pick = mt_rand(1, $num_valid_chars); $random_char = $valid_chars[$random_pick-1]; $random_string .= $random_char; } return $random_string; } ?>
Benchmark redis : Total Set time: 0.12764096260071 Loop 0 (Get) time: 0.175616979599 Loop 1 (Get) time: 0.18264698982239 Loop 2 (Get) time: 0.13450312614441 Loop 3 (Get) time: 0.1352870464325 Loop 4 (Get) time: 0.13448214530945 Loop 5 (Get) time: 0.13312005996704 Loop 6 (Get) time: 0.15526604652405 Loop 7 (Get) time: 0.40630602836609 Loop 8 (Get) time: 0.13408994674683 Loop 9 (Get) time: 0.13564801216125 Total 10 loop Get time 1.726966381073 Benchmark memcache : Total Set time: 0.14455795288086 Loop 0 (Get) time: 0.12623596191406 Loop 1 (Get) time: 0.13038897514343 Loop 2 (Get) time: 0.1564610004425 Loop 3 (Get) time: 0.11394095420837 Loop 4 (Get) time: 0.11402487754822 Loop 5 (Get) time: 0.11300802230835 Loop 6 (Get) time: 0.11119389533997 Loop 7 (Get) time: 0.1115710735321 Loop 8 (Get) time: 0.11155104637146 Loop 9 (Get) time: 0.19941091537476 Total 10 loop Get time 1.28778672218322The results shows for simple Set and Get, redis is faster. For SET, redis is faster by 12% For GET, memcache is faster by 25% For other functions,more extensive tests/benchmarks have to perform on both.
git clone https://github.com/phpredis/phpredis cd phpredis phpize ./configure make && make installInclude the compiled extension to php.ini:
touch /etc/php.d/redis.ini echo extension=redis.so > /etc/php.d/redis.iniIf you use Apache (httpd), restart by:
systemctl restart httpd.serviceIf you use nginx, restart it by:
systemctl restart php-fpm.service systemctl restart nginx.service
wget http://download.redis.io/releases/redis-2.8.19.tar.gzuntar/unzip the package
tar -xzvf redis-2.8.19.tar.gzcompile the bundle
cd redis-2.8.19 makeIt is good to do a "make test" to test the functionality on your system. For this test you need tcl. If you don't have tcl, install it by:
yum install tclRun make test:
make testIf functionality test passed it will show:
\o/ All tests passed without errors!Lets install the excutables into /usr/local/bin/.
make installFor Fedora systemctl system, lets create a redis service script.
vi /usr/lib/systemd/system/redis.service
[Unit] Description=Redis persistent key-value database After=network.target [Service] ExecStart=/usr/local/bin/redis-server /etc/redis.conf --daemonize no ExecStop=/usr/local/bin/redis-shutdown User=redis Group=redis [Install] WantedBy=multi-user.targetMove sample redis.conf to /etc.
cp /root/download/redis-2.8.19/redis.conf /etc/redis.confCreate redis-shutdown script.
vi /usr/local/bin/redis-shutdown
#!/bin/bash # # Wrapper to close properly redis and sentinel test x"$REDIS_DEBUG" != x && set -x REDIS_CLI=/usr/local/bin/redis-cli # Retrieve service name SERVICE_NAME="$1" if [ -z "$SERVICE_NAME" ]; then SERVICE_NAME=redis fi # Get the proper config file based on service name CONFIG_FILE="/etc/$SERVICE_NAME.conf" # Use awk to retrieve port from config file PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE` # Just in case, use default port if [ "$SERVICE_NAME" = redis ]; then PORT=${PORT:-6379} else PORT=${PORT:-26739} fi # shutdown the service properly $REDIS_CLI -p $PORT SHUTDOWN NOSAVEEnable and start redis service.
systemctl enable redis.service systemctl start redis.serviceLets try the redis-cli. Set a key and get a key:
[root@fc21 temp]# redis-cli 127.0.0.1:6379> get key1 (nil) 127.0.0.1:6379> set key1 value1 OK 127.0.0.1:6379> get key1 "value1"
# systemctl stop httpd.service # systemctl disable httpd.serviceInstall php-fpm
# yum install php-fpm -yEnable and start php-fpm service:
# systemctl enable php-fpm.service # systemctl start php-fpm.serviceTo install Nginx
# yum install nginx -yEnable and start Nginx service
# systemctl enable nginx.service # systemctl start nginx.serviceTest Nginx Open up your web browser and navigate to http://localhost/ (example). You will see the default nginx welcome screen. Configure Nginx Open the file /etc/nginx/nginx.conf in vi
# vi /etc/nginx/nginx.confSet the worker_processes (No. Of CPU’s in your system you want to utilise). To see the no. Of CPU’s, you can use the command “lscpu”. For default you can set to 1. worker_processes 1; Example of working nginx.conf:
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes 1; error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; index index.html index.htm; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; #Change below value to suit your need: fastcgi_read_timeout 6000s; server { listen 80 default_server; server_name localhost; root /usr/share/nginx/html; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } # redirect server error pages to the static page /40x.html # error_page 404 /404.html; location = /40x.html { } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { } location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } } }Save and close the file. Restart Nginx service:
# systemctl restart nginx.serviceTest PHP Create a sample “phpinfo.php” file in the nginx document root folder:
# vi /usr/share/nginx/html/phpinfo.phpAppend the lines as shown below:
Save and close the file. Restart Nginx service:
# systemctl restart nginx.serviceNavigate to http://localhost/testphp.php (example). It will display all the details about PHP,build,setups.