Sunday, January 9, 2011

How to Install fastcgi and Nginx on Centos

Install PHP & depends:

yum install gcc pcre-devel openssl-devel php-pear-Net-Socket php-pear php-common php-gd php-devel php php-mbstring php-pear-Mail php-cli php-imap php-snmp php-pdo php-xml php-pear-Auth-SASL php-ldap php-pear-Net-SMTP php-mysql


Download & install spawn-fcgi

cd /usr/local/src
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
tar -xzvf spawn-fcgi-1.6.3.tar.gz
cd spawn-fcgi-1.6.3
./configure
make & make install


add user nginx

useradd nginx


Create file fastcgi in /etc/init.d/ directory and put

#!/bin/bash
PHP_SCRIPT=/usr/local/bin/php-fastcgi
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php
RETVAL=$?
;;
restart)
killall -9 php
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL



change mode

chmod 755 fastcgi


Create file /usr/local/bin/php-fastcgi

#!/bin/sh
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -f /usr/bin/php-cgi


change mode

chmod 755 /usr/local/bin/php-fastcgi


Download and install nginx

cd /usr/local/src
wget http://sysoev.ru/nginx/nginx-0.8.54.tar.gz
tar -xzvf nginx-0.8.54.tar.gz
cd nginx-0.8.54

./configure --prefix=/usr --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access_log --error-log-path=/var/log/nginx/error_log --pid-path=/var/run/nginx.pid --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --with-md5-asm --with-md5=/usr/include --with-sha1-asm --with-sha1=/usr/include --with-http_realip_module --with-http_ssl_module --with-http_perl_module --with-http_stub_status_module

make & make install




Create configuration file for nginx /etc/nginx/nginx.conf

worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 10m;
sendfile on;
charset utf-8;
gzip on;
gzip_types text/plain application/xml text/javascript;
gzip_min_length 1000;

keepalive_timeout 65;



server {
listen 80;
server_name www.3nky.com;
server_tokens off;
root /home/3nky/public;
index index.php index.html index.htm;

access_log /var/log/nginx/3nky.com-access_log;
error_log /var/log/nginx/3nky.com-error_log;

#location / {
# try_files $uri /index.php;
#}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}


location / {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}

}
}


Thursday, January 6, 2011

How to install memcached on CentOS

yum -y install gcc gcc-c++


First, install the dependency libevent:

cd /usr/local/src
wget http://www.monkey.org/~provos/libevent-2.0.10-stable.tar.gz
tar -xzvf libevent-2.0.10-stable.tar.gz
cd libevent-2.0.10-stable
./configure
make
make install



cd /usr/local/src
wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
tar -xzvf memcached-1.4.5.tar.gz
cd memcached-1.4.5
./configure
make
make install


Locate the file surely that is in /usr/local/lib and make a symbolic link

ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5


run memcached

memcached -u root -d

Wednesday, December 22, 2010

Monday, December 20, 2010

Installing and configure Apache Solr with MySQL


Apache Solr™ is our high performance enterprise search server, with XML/HTTP and JSON/Python/Ruby APIs, hit highlighting, faceted search, caching, replication, distributed search, database integration, web admin and search interfaces.

1. Install Java

yum install java


2. Download & Extract Solr

cd /usr/local/src
wget http://www.apache.org/dist//lucene/solr/1.4.1/apache-solr-1.4.1.tgz
tar -xzvf apache-solr-1.4.1.tgz


3. Download MySQL Java connector

wget http://mysql.spd.co.il/Downloads/Connector-J/mysql-connector-java-3.1.14.zip
unzip mysql-connector-java-3.1.14.zip
cd mysql-connector-java-3.1.14
cp mysql-connector-java-3.1.14-bin.jar /usr/local/src/apache-solr-1.4.1/example/lib/


4. Configure MySQL Database
Create a new xml file called data-import.xml , change the obvious variables to suit your DB. In this example, I am indexing a own DB table called users.

cd /usr/local/src/apache-solr-1.4.1/example/solr/conf
nano data-import.xml

put follow text

<dataconfig>
<datasource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.1.7/mydatabase" user="myuser" password="mypassword">
<document name="content">
<entity name="node" query="select user_id AS id, mobile AS mobile, user_status AS status FROM users">
<field column="id" name="id">
<field column="mobile" name="mobile">
<field column="status" name="status">
</field></field></field></entity>
</document>
</datasource>
</dataconfig>



5. Configure solr .
Edit file solrconfig.xml which is located in /usr/local/src/apache-solr-1.4.1/example/solr/conf directory. Add the following requestHandler entry if not already existing.

<requesthandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requesthandler>


6. Now we will configure solr’s schema by editing schema.xml in /usr/local/src/apache-solr-1.4.1/example/solr/conf directory. Delete old tag and add the following fields as required. The xml format is self explanatory.

<fields>

<field name="id" type="string" indexed="true" stored="true" default="NOW" required="true">
<field name="mobile" type="text" indexed="true" stored="true" required="true">
<field name="status" type="text" indexed="false" stored="true" required="false">

</field></field></field></fields>

<uniquekey>id</uniquekey>

<!-- field for the QueryParser to use when an explicit fieldname is absent -->
<defaultsearchfield>mobile</defaultsearchfield>



7. Start SOLR

java -jar /usr/local/src/apache-solr-1.4.1/example/start.jar



Performing full or delta indexing
If everything works correctly, you can get solr to fully index the configured tables by accessing the following command via your browser. http://:8983/solr/dataimport?command=full-import

You can check the status of the command by accessing http://:8983/solr/dataimport

If everything works correctly, you can now search for data from http://:8983/solr/admin/ and you should now have data results in XML format.


To do an incremental or delta indexing of data since the last full or delta, increment, issue the command http://:8983/solr/dataimport?command=delta-import

You can now access these xml results from your web application. There are client api’s available for RoR, php, java etc.

References
http://www.cabotsolutions.com/2009/05/using-solr-lucene-for-full-text-search-with-mysql-db/

http://www.ipros.nl/2008/12/15/using-solr-with-wordpress/

http://wiki.apache.org/solr/DataImportHandler#head-df246a3aed0bb38297f3449bc35a0bdf38a272b5

http://lucene.apache.org/solr/tutorial.html

Monday, October 4, 2010

Resize image and set background






$filename = '/path/to/source/file/1.jpg';

// Set a maximum height and width
$width = 98;
$height = 100;

// set background color
$color_R = 0;
$color_G = 0;
$color_B = 0;

$worg=$width;
$horg=$height;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$src = imagecreatetruecolor($width, $height);

$image = imagecreatefromjpeg($filename);
imagecopyresampled($src, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);


// Create image instances

$dest = imagecreatetruecolor($worg, $horg);
$mycolor = imagecolorallocate($dest, $color_R, $color_G, $color_B);
imagefill($dest, 0, 0, $mycolor);

if ($width==$worg) {$start_x=0; $start_y=round(($horg-$height)/2);}
if ($height==$horg) {$start_y=0; $start_x=round(($worg-$width)/2);}

//echo $width." ".$height." ".$worg." ".$horg."
";
//echo $start_x;
//die;

// Copy
imagecopy($dest, $src, $start_x, $start_y, 0,0,$width, $height);
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($dest);

imagedestroy($dest);
imagedestroy($src);


Monday, July 5, 2010

Bash: Delete recursive duplicated folders

for i in `find /home/directory -type d`
do

cd $i
mydir=$(pwd|rev|awk -F \/ '{print $1}'|rev)
directory="$i/$mydir/$mydir"
dirtorm="$i/$mydir"
if [ -d "$directory" ]; then
echo "=======>$i"
rm -f -r $dirtorm
echo $directory
fi

done

Thursday, June 10, 2010

Bacula Backup - Building a File Daemon or Client

If you run the Director and the Storage daemon on one machine and you wish to back up another machine....

Install gcc & c++ compilers

yum install gcc gcc-c++


Download packages:


wget https://sourceforge.net/projects/bacula/files/bacula/5.0.2/bacula-5.0.2.tar.gz/download
tar -xzvf bacula-5.0.2.tar.gz

wget https://sourceforge.net/projects/bacula/files/depkgs/18Dec09/depkgs-18Dec09.tar.gz/download
tar -xzvf depkgs-18Dec09.tar.gz
mv depkgs bacula-5.0.2/



Compile it:

cd bacula-5.0.2/depkgs/
make
gmake mtx-install

cd ..



configure bacula


CFLAGS="-g -Wall" \
./configure \
--sbindir=$HOME/bacula/bin \
--sysconfdir=$HOME/bacula/bin \
--with-pid-dir=$HOME/bacula/bin/working \
--with-subsys-dir=$HOME/bacula/bin/working \
--enable-client-only \
--with-working-dir=$HOME/bacula/bin/working \
--with-dump-email=$USER



Install bacula

make install


How to start File daemon on start or add service

cd /root/bacula/bin

add content to file bacula

#! /bin/sh
# chkconfig: 2345 95 20
# description: Bacula Clien File Deamon
# processname: bacula



copy it to /etc/rc.d/init.d/

cp bacula /etc/rc.d/init.d/
chkconfig add bacula