Saturday 21 November 2015

Installing HA-Proxy on Cent OS 7



This example assumes that firewall and selinux is disabled. We are going to use the following three servers.

Apache1: 192.168.124.2 apache1.example.com
Apache2: 192.168.124.3 apache2.example.com
HAProxy: 192.168.124.10 haproxy.example.com



All three servers should be able to reach each other. Enter the following in the /etc/hosts file in all three nodes

192.168.124.2 apache1.example.com apache1
192.168.124.3 apache2.example.com apache2
192.168.124.10 haproxy.example.com haproxy

Now install apache service on the two apache nodes using the following command

#yum install httpd -y

Create a sample HTML file in the apache nodes in differentiate them and start the httpd service

#systemctl start httpd.service


Install and enable Epel repository on the haproxy node

#yum install epel-release -y

Install HA proxy

#yum install haproxy -y

The default configuration file haproxy.cfg is located in /etc/haproxy/

Lets make a backup of the original file

#cd /etc/haproxy

#cp haproxy.cfg haproxy.cfg.bkp


Now lets edit the configuration file. We need to clear the existing frontend/backend config and create our own config

Delete the following lines in haproxy.cfg

------------------------------------------------------------------------
frontend  main *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check
------------------------------------------------------------------------

Now add the following lines
-------------------------------------------------------------------------
#webapp1 is the name we give for our frontend
frontend webapp1
#Makes the haproxy service listen on port number 80
bind *:80
#defining the backen name
default_backend webapp1_servers
#send X-Forwarded-For header
option  forwardfor


#Backend configuration
backend webapp1_servers
#Load balencing protocol
balance     roundrobin
server  apache1 192.168.124.89:80 check
server  apache2 192.168.124.51:80 check
-------------------------------------------------------------------------

Note: The X-Forwarded-For request header helps you identify the IP address of a client when you use an HTTP or HTTPS load balancer. Because load balancers intercept traffic between clients and servers, your server access logs contain only the IP address of the load balancer.





Save the config file and restart haproxy service

#systemctl restart haproxy.service


Now hit the haprosy node's IP address in the browser and it should direct us to one of the web servers.

Install and Configure Nginx with php-fpm on Cent OS 7.


Install Epel repository

#yum install epel-release -y

Install nginx and php

#yum install php php-fpm php-gd nginx -y

#systemctl start nginx.service

#systemctl start php-fpm.service

At this point we should be able to get test page for Nginx server when accessed in the browser. http://localhost

The default document root for nginx is /usr/share/nginx/html/

Now lets create a php info file in /usr/share/nginx/html

#vim phpinfo.php
------------------
<?php phpinfo(); ?>
------------------

Now try to open the file in the browser. http://localhost/phpinfo.php

The file will get downloaded instead of getting executed. To fix this we need to include the following configuration in /etc/nginx/nginx.conf. This will pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 for execution.

-----------------------------------------------
location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
}
-----------------------------------------------


Restart nginx and php-fpm service

#systemctl start nginx.service

#systemctl start php-fpm.service

Now we should be able to open http://localhost/phpinfo.php


Now Lets create another server block with a document root of /var/www/html

Create example.conf  in /etc/nginx/conf.d/example.conf

Include the following config in the example.conf

----------------------------------------------------------------
server {
        listen       80;
        server_name  example.com;
        root         /var/www/html;
        index index.php;

location ~ \.php$ {
        root           /var/www/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
}

}
-----------------------------------------------------------------

#mkdir -p /var/www/html

#vim /var/www/html/index.php
-----------------------
<? phpinfo(); ?>
------------------------

Save the file and restart nginx service and you should be have the example.com site up and running.

Tuesday 3 November 2015

Split large sql file into multiple tables






The below bash script will split a large Mysql dump file into multiple files depending upon the number of tables in it

Usage
#./<script> <dump.sql>


#######################################################################
#!/bin/bash

####
# Split MySQL dump SQL file into one file per table
# based on http://blog.tty.nl/2011/12/28/splitting-a-database-dump
####

if [ $# -ne 1 ] ; then
  echo "USAGE $0 DUMP_FILE"
fi

csplit -s -ftable $1 "/-- Table structure for table/" {*}
mv table00 head

for FILE in `ls -1 table*`; do
      NAME=`head -n1 $FILE | cut -d$'\x60' -f2`
      cat head $FILE > "$NAME.sql"
done

rm head table*

########################################################################

Note: I did not create this, found it online

High Availability with IREDMAIL Integrated with Active Directory

This is step by step guide for Centos 7. Server1 will be the Active node and Server2 will be the failover node. After failover when ...