Compiling shared PHP modules

So you want to add postgres support to PHP (or something like that), but you don’t want to re-compile the whole thing. You just want a shared module.

Its easier than I first thought. Here is how I have done it.

First create a test php page to check phpinfo() and find which version of php you are running, then download it from php.net. I’m using 5.4.14 so my example will reflect that.

Then I like to put the source in someplace like /usr/local/src/php-5.4.14

/usr/local/src/php-5.4.14 # ./configure --with-pgsql=shared,/usr/local/pgsql
/usr/local/src/php-5.4.14 # make
/usr/local/src/php-5.4.14 # cp modules/pgsql.so /usr/lib64/php/extensions/

Note we only have to use the configuration line for the shared module we are compiling. We specify that we want it as a shared module, and the path (if needed). This of course assumes you already have postgres installed.

Then load that extension from php.ini by adding the following line.

[extension section]
extension=pgsql.so

Restart apache, and check your phpinfo() page again. You should see a section titled pgsql

Thats it =;)

Posted in Apache | Leave a comment

Compiling PHP 5.x From Scratch

I was asked to compile php5.4.14 on one of my server with mysql support.

I downloaded php 5.4.14 from mysql website and compiled it.

Later on mcrypt, ftp, openssl support were added to it.

I was also told to add dbase support. with –enable-dbase option php was not compiling. So to make dbase work I did

Get the source for dbase extension

As I told you, also DBase extension is no longer included, so:

1) cd /opt/php/php5-5.3.2/ext/

2) mkdir dbase

3) svn co http://svn.php.net/repository/pecl/dbase/trunk dbase

Compile the Extension

4) cd /opt/php/php5-5.3.2/ext/dbase

5) phpize

6) ./configure

7) make

Copy the extension

8) cp  /opt/php/php5-5.3.2/ext/dbase/modules/dbase.so /usr/lib/php5/20090626+lfs/.

make clean

./configure –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –with-apxs2 –with-libdir=lib64 –with-mysql –with-mysqli –with-zlib –with-pdo-mysql –with-mysql-sock=/var/mysql/mysql.sock –with-pdo-sqlite –with-mcrypt=/usr/bin/mcrypt –enable-ftp –enable-zip –with-openssl –with-openssl-dir=/usr/bin

 

make
make test
make install

 

Restart Apache

/etc/init.d/apache2 restart

Posted in Apache | Leave a comment

Check Port 25 with the Telnet Command

You can check your SMTP Server on SMTP port 25 with the following Telnet command:

Open a command line and type

telnet smtp-server.domain.com 25

If your server is online a connection will be established on port 25 (SMTP).

An Exchange Server answers with the following output:

220- mailserver.domain.com ESMTP Exim 4.82 #2 Fri, 30 May 2014 21:24:45 -0400
220-We do not authorize the use of this system to transport unsolicited,
220 and/or bulk e-mail.

When you type the ‘help’ command the available commands are listed:

214-This server supports the following commands:
214 HELO EHLO STARTTLS RCPT DATA RSET MAIL QUIT HELP AUTH TURN ATRN ETRN BDAT VRFY

Try the following to send an eMail from the command line: 

220- mailserver.domain.com ESMTP Exim 4.82 #2 Fri, 30 May 2014 21:24:45 -0400
220-We do not authorize the use of this system to transport unsolicited,
220 and/or bulk e-mail.
helo myserver.domain.com
250 mailserver.domain.com Hello [10.1.11.133]
mail from:<myname@mydomain.com>
250 2.1.0 myname@mydomain.com….Sender OK
rcpt to:<recipientname@mydomain.com>
250 2.1.5 recipientname@mydomain.com
data
354 Start mail input; end with <CRLF>.<CRLF>
subject: This is a test mail
to: recipientname@mydomain.com
This is the text of my test mail.
.
250 2.6.0 <exchange.domain.com> Queued mail for delivery
quit

If the mailserver returns a “syntax error” after the “mail from:” command, you’ve probably forgotten to put the mail address in brackets <>

Posted in Linux, Mail server | Leave a comment

Deleting Linux Backup Easily With Rotation

Please create a cron job for the same and insert the following command.

find /data/backup/ -type f -ctime +90 -exec rm -f {} \; -print
find /data/backup/ -type d -ctime +90 -exec rm -rf {} \; -print

To check out the list of files

find /data/backup/ -ctime +90
Posted in Linux | Leave a comment

Reset Mysql Root Password

    •  Login to root prompt
    • Stop the mysql Server
# /etc/init.d/mysql stop
  •   start the Mysql server in safe mode
# /usr/local/mysql/bin/safe_mysqld –user=mysql –skip-grant-tables –skip-networking &
    •  Reset password
# /usr/local/mysql/bin/mysqladmin -u root flush-privileges password “newpassword”

sometime the above command would not work and password need to be reset with below given process

Login to mysql server, type the following command at shell prompt:

$ mysql -u root -p

Use mysql database (type command at mysql> prompt):

mysql> use mysql;

Change password for user root, enter:

mysql> update user set password=PASSWORD("NEWPASSWORD") where User='root';

Finally, reload the privileges:

mysql> flush privileges;
mysql> quit
  • Restart the Mysql Server
# /etc/init.d/mysql restart
Posted in MySQL | Leave a comment

All Email and its password Plesk

Login to the mysql database for Plesk

mysql -uadmin -p`cat /etc/psa/.psa.shadow` psa

select accounts.id, concat(mail.mail_name,’@’,domains.name), accounts.password from mail, domains, accounts  where domains.id = mail.dom_id and  mail.account_id = accounts.id;

Posted in Plesk | Leave a comment

Setup Master-Master Replication in MySQL Server

MySQL Master-Slave replication is to set up slave server to update immediately as soon as changes done in Master server. But it will not update Master if there are any changes done on slave server.

This article will help you to set up Master-Master replication between MySQL servers. In this setup if any changes made on either server will update on an other one.

Setup Details:

MySQL Master1 system : RHEL 5
Master1 IP Address : 192.168.1.250/24
MySQL Master2 system : RHEL 5
Master2 IP Address: 192.168.1.150/24

If you are using different – different versions of MySQL on either servers use this link to check compatibility.

Step 1. Set Up MySQL Master-1 Server

Edit MySQL configuration file and add the following lines under [mysqld] section.

# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog-do-db=unixmen
server-id=1

Restart MySQL server to changes take effect.

# service mysqld restart

Create an mysql account on Master-1 server with REPLICATION SLAVE privileges to which replication client will connect to master.

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%' IDENTIFIED BY 'secretpassword';
mysql> FLUSH PRIVILEGES;

Block write statement on all the tables, so no changes can be made after taking backup.

mysql> use unixmen;
mysql> FLUSH TABLES WITH READ LOCK;

Check the current binary log file name (File) and current offset (Position) value using following command.

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      332 | unixmen      |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

The above output is showing that the current binary file is using mysql-bin.000003 and offset value is 332. Note down these values to use on Master-2 server in next step.

Take a backup of database and copy it to another mysql server.

# mysqldump -u root -p unixmen > unixmen.sql
# scp unixmen.sql 192.168.1.150:/opt/

After completing backup remove the READ LOCK from tables, So that changes can be made.

mysql> UNLOCK TABLES;

Step 2. Setup MySQL Master-2 Server

Edit mysql Master-2 configuration file and add following values under [mysqld] section.

# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog-do-db=unixmen
server-id=1

server-id always be an non zero numeric value. These value will never be similar with other master or slave servers.

Restart MySQL server, If you had already configured replication use –skip-slave-start in start to not to immediate connect to master server.

# service mysqld restart

Restore database backup taken from master server.

# mysql -u root -p unixmen < /opt/unixmen.sql

Create an mysql account on Master-1 server with REPLICATION SLAVE privileges to which replication client will connect to master.

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'%' IDENTIFIED BY 'secretpassword';
mysql> FLUSH PRIVILEGES;

Check the current binary log file name (File) and current offset (Position) value using following command.

mysql > SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      847 | unixmen      |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

The above output is showing that the current binary file is using mysql-bin.000001 and offset value is 847. Note down these values to use in Step 3.

Setup option values on slave server using following command.

mysql>  CHANGE MASTER TO MASTER_HOST='192.168.1.250',
    -> MASTER_USER='repl_user',
    -> MASTER_PASSWORD='secretpassword',
    -> MASTER_LOG_FILE='mysql-bin.000003',
    -> MASTER_LOG_POS=332;

Step 3: Complete Setup on MySQL Master-1

Login to MySQL Master-1 server and execute following command.

mysql>  CHANGE MASTER TO MASTER_HOST='192.168.1.150',
     MASTER_USER='repl_user',
     MASTER_PASSWORD='secretpassword',
     MASTER_LOG_FILE='mysql-bin.000001',
     MASTER_LOG_POS=847;

Step 4: Start SLAVE on Both Servers

Execute following command on both servers to start replication slave process.

mysql> SLAVE START;

MySQL Master-Master Replication has been configured successfully on your system and in working mode. To test if replication is working make changes on either server and check if changes are reflecting on other server.

Posted in MySQL | Leave a comment

Setup Master-Slave Replication in MySQL Server

MySQL replication allows you to have multiple copies of data on many systems and data is automatically copied from one database (Master) to another database (Slave). If one server goes down, the clients still can access the data from another (Slave) server database.

In this article, let us see how to configure MySQL Master-Slave replication. I am using the following two systems to in this how-to:

MySQL Master system : RHEL 5

Master IP Address : 192.168.1.250/24

MySQL Slave system : RHEL 5

Slave IP Address: 192.168.1.150/24

Setting up MySQL Master

Now install MySQL packages using the following command:

[root@server1 ~]# yum install mysql-server mysql -y

Start mysqld service.

[root@server ~]# service mysqld start

[root@server ~]# chkconfig mysqld on

Configure MySQL Master

Open /etc/my.cnf file and add the following lines under [mysqld] section:

[root@server ~]# vi /etc/my.cnf

[mysqld]
server-id = 1
binlog-do-db=unixmen
expire-logs-days=7
relay-log = /var/lib/mysql/mysql-relay-bin
relay-log-index = /var/lib/mysql/mysql-relay-bin.index
log-error = /var/lib/mysql/mysql.err
master-info-file = /var/lib/mysql/mysql-master.info
relay-log-info-file = /var/lib/mysql/mysql-relay-log.info
log-bin = mysql-bin
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Here unixmen is the database name to be replicated to the Slave system.

Once you are done, restart MySQL service:

[root@server1 ~]# service mysqld restart

Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]

Now login to MySQL and create a Slave user and password. For instance, we will use slavek as Slave username and centoslave as password:

[root@server ~]# mysql -u root -p
Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> STOP SLAVE;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slavek'@'%' IDENTIFIED BY 'centoslave';

Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH TABLES WITH READ LOCK;

Query OK, 0 rows affected (0.00 sec)

mysql> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000001 | 106      | unixmen      |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.01 sec)

mysql> exit

Bye

Note down the file(mysql-bin.000001) and position number (106), you would need these values later while configuring slave servers.

Backup Master server database

Enter the following command to dump all Master databases and save them. We will transfer these databases to Slave server later:

[root@server ~]# mysqldump --all-databases --user=root --password --master-data > masterdatabase.sql

This will create a file called masterdatabase.sql. This will take some time depending upon the databases size.

Again login to MySQL as root user and unlock the tables:

[root@server ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye

Copy the masterdatabase.sql file to your Slave server. Here, I copy this file to /home folder. So the command should be:

[root@server ~]# scp masterdatabase.sql root@192.168.1.150:/home
root@192.168.1.150's password:
masterdatabase.sql                             100%    507KB    506.7KB/s    00:00

We have done Master side installation. Now we have to start on Slave side.

Setting up MySQL Slave
Install MySQL packages on Slave server:

[root@server1 ~]# yum install mysql-server mysql -y

Start mysqld service.

[root@server ~]# service mysqld start

[root@server ~]# chkconfig mysqld on

Seting up MySQL Root password:

Configure MySQL Slave

Open the file /etc/my.cnf and add the following entries under [mysqld] section as shown below. Replace the database name and master server IP Address with your own:

[root@server ~]# vi /etc/my.cnf
[mysqld]
server-id = 2
master-host=192.168.1.250
master-connect-retry=60
master-user=slavek
master-password=centoslave
replicate-do-db=unixmen
relay-log = /var/lib/mysql/mysql-relay-bin
relay-log-index = /var/lib/mysql/mysql-relay-bin.index
log-error = /var/lib/mysql/mysql.err
master-info-file = /var/lib/mysql/mysql-master.info
relay-log-info-file = /var/lib/mysql/mysql-relay-log.info
log-bin = mysql-bin
[...]

Here 192.168.1.250 is Master server IP address, slavek is Master server database user, centoslave is password of user slavek, unixmen is Master database name.

Save and exit the file.

Import the master database:

[root@server ~]# mysql -u root -p < /home/masterdatabase.sql
Enter password:

[root@server ~]# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]

Now log in to MySQL as root user and tell the Slave server to where to look for Master log file which is we have created on Master server using the command SHOW MASTER STATUS; (File – mysql-bin.000001 and Position – 106). Make sure that you changed the Master server IP address, username and password as your own:

[root@server ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SLAVE STOP;
Query OK, 0 rows affected (0.01 sec)

mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.250', MASTER_USER='slavek', MASTER_PASSWORD='centoslave', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=106;
Query OK, 0 rows affected (0.03 sec)

mysql> SLAVE START;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.250
                  Master_User: slavek
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 106
               Relay_Log_File: mysql-relay-bin.000003
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: unixmen
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 106
              Relay_Log_Space: 551
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
1 row in set (0.00 sec)

Test MySQL Replication

Master side:

[root@server ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database unixmen;
Query OK, 1 row affected (0.04 sec)

mysql> use unixmen;
Database changed

mysql> create table sample (c int);
Query OK, 0 rows affected (0.08 sec)

mysql> insert into sample (c) values (1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from sample;
+------+
| c    |
+------+
|    1 |
+------+
1 row in set (0.01 sec)

mysql>

Slave side:

[root@server ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.69-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use unixmen;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select * from sample;
+------+
| c    |
+------+
|    1 |
+------+
1 row in set (0.01 sec)

mysql>

That’s it. Now the tables created in the Master server are automatically replicated to the Slave server.

Posted in MySQL | Leave a comment

HTTP Cluster with heartbeat

 

Setup a two node high availability HTTP cluster with heartbeat. Both nodes use the Apache Web Server to serve the same content.

Pre-Configuration Requirement

  • Assign hostname NODE1 to Primary node with IP Address 10.10.10.1 to eth0.
  • Assign hostname NODE2 to Slave node with IP Address 10.10.10.2 to eth0.

On Node1

uname -n

must return NODE1.

On Node2

uname -n

must return NODE2.

10.10.10.3 is the virtual IP address that will be used for Apache webserver. (i.e. Apache would listen to 10.10.10.3 IP Address.)

Configuration

  • Download and install heartbeat package.

yum install heartbeat

or download packages :

heartbeat-2.08

heartbeat-pils-2.08

heartbeat-stonith-2.08

  • Now we need to configure heartbeat on our two node cluster.We will deal with three files and they are :

authkeys

ha.cf

haresources

  • In case your /etc/ha.d is empty you need to copy the files above said files to /etc/ha.d directory. In my case I would copy these files as given below

cp /usr/share/doc/heartbeat-2.1.2/authkeys /etc/ha.d/

cp /usr/share/doc/heartbeat-2.1.2/authkeys /etc/ha.d/

cp /usr/share/doc/heartbeat-2.1.2/authkeys /etc/ha.d/

  • First we would deal with authkeys file. We will use authentication method 2 (sha 1). For this we will make changes in the authkeys file.

vi /etc/ha.d/authkeys

and add following lines

auth 2

2 sha1 test-ha

change the permission of th authkeys file :

chmod 600 /etc/ha.d/authkeys

  • Now important file ha.cf. Add following lines in the ha.cf file :

vi /etc/ha.d/ha.cf

logfile /var/log/ha-log

logfacility local0

keepalive 2

deadtime 30

initdead 120

bcast eth0

udpport 694

auto_failback on

node NODE1

node NODE2

Note : NODE1 and NODE2 are the output generated by command uname -n on both system

  • Now update haresources file. This file contains information about resources which we want highly enable. In our case we want webserver(apache(httpd)) highly available.

vi /etc/ha.d/haresources

NODE1 10.10.10.3 httpd

  • Copy /etc/ha.d directory from NODE1 to NODE2

scp -r /etc/ha.d root@NODE2:/etc

  • As we want httpd highly enabled lets start configuring httpd

vi /etc/httpd/conf/httpd.conf

Listen 10.10.10.3:80

  • Copy /etc/httpd/conf/httpd.conf to NODE2

scp -r /etc/httpd/conf/httpd.conf root@NODE2:/etc/httpd/conf/

  • Create index files on both Nodes (NODE1 and NODE2)

on NODE1 :

vi /var/www/html/index.html

HTTP fron NODE1

on NODE2 :

vi /var/www/html/index.html

HTTP fron NODE2

  • Now start heartbeat on primary(NODE1) and slave(NODE2) Nodes./etc/init.d/heartbeat start
  • OPen web browser and type URL

http://10.10.10.3

It will show NODE1 web page

  • Now stop heartbeat deamon on NODE1

/etc/init.d/heartbeat stop

OPen web browser and type URL

http://10.10.10.3 and press enter.

It will show NODE2 web page.

  • We don’t need to create a virtual network interface and assign an IP address 10.10.10.3 to it. Heartbeat will do  this for you and start httpd service itself. So don’t worry about this.

Don’t use IP address 10.10.10.1 and 10.10.10.2 for services. These IP address are used by heartbeat for communication between NODE1 and NODE2. When any of them will be used for other services/resources, it will distrub heartbeat and will not work .

 

Posted in Apache | Leave a comment