Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Saturday, September 18, 2010

PHP Tutorial For Beginners

You can spruce up your site with some simple PHP. If you can copy and paste simple HTML and follow simple instructions than learning a little PHP just might be what the doctor ordered. Forget the notion that since you don't carry around a pocket calculator you shouldn't be messing with PHP. A PHP tutorial for beginners could add a dynamic dimension to your site and at the same time, save you a lot of time and money. After all, paying people to do this stuff doesn't come cheap. If you have this knowledge and can help others, you won't come cheap either if word gets out that you can help. Just don't be like me and do it all for free like some misguided Mr. Nice guy.

With a little bit of effort you may be surprised by what you can do. You know that sinking feeling you get when you drive your car away after paying a mechanic, or you regret the cash you parted with and gave to someone whose "skill" made you envious? Well, if you can learn PHP then you will be on the other side of that equation.

With so many people from outside America making money in lines of work we can't seem to get Americans to do, any American programmer is going to get a very good job in this economy or any other with some PHP knowledge. A solid knowledge in PHP and MySQL can end up getting you a great job or allowing you to work on your own. You have a lot to gain from gaining an understanding of PHP. It is estimated that 1.5 million related jobs will be opening up in the next 5-10 years.

Now if you're the type of person whose head starts to swim just thinking about pages of codes (sort of like me), I can' tell you that I've known some computer novices who have taken to PHP pretty well. Of course, even just learning snippets of it and hiring out people to do more complex jobs can also be a consideration, the choice is entirely up to you. It is my belief that anyone can do it completely with just a mediocre level of motivation.

If you're in a situation where you've grown sick and tired of paying programmers to install your PHP & CGI scripts, then learning PHP may be perfect for you. Paying people a lot of money just does something to you.

There are some internet marketers and affiliate marketers who learn PHP on a very simple level and it still benefits them even though they haven't grasped every nuance of PHP like a Computer Science graduate. A PHP tutorial for beginners is a perfect solution for people such as these.

If you've read this far then you probably have a pretty good idea of exactly what you want to do with your level of PHP knowledge. Whatever your choice, I wish you much success.

Monday, June 21, 2010

11 Easy Steps For Installing Apache ActiveMQ & PHP Application

Apache ActiveMQ is one good option for implementing message queue in your PHP application. It can be easily installed on your server and it’s web accessible admin interface really makes administrator’s life easy. It can be easily connected with PHP via STOMP. I will suggesst to use MySql for Data persistance and start ActiveMQ as unix service.

Basic requirements: java, php, mysql.


So lets starts with installation steps:



ActiveMQ installation

I am installing ActiveMQ in /usr/share/php directory.

cd /usr/share/php
wget http://mirror.its.uidaho.edu/pub/apache/activemq/apache-activemq/5.3.0/apache-activemq-5.3.0-bin.tar.gz
tar zxvf apache-activemq-5.3.0-bin.tar.gz
mv apache-activemq-5.3.0 activemq

We have renamed the directory to “activemq” for ease.

Run Apache ActiveMq

activemq/bin/activemq

Don’t close this terminal, open a new terminal for running below commands

Check running status of ActiveMq

netstat -an|grep 61616
ps -ef|grep activemq

ps command will show the process id (PID) of running ActiveMQ

ActiveMQ Web Admin interface

http://localhost:8161/admin/

Here you can monitor your current status of queues, topics, connections etc. You can also browse, delete, add data according to requirement.

Stop Apache AcitveMQ

kill -9 [PID]

where [PID] is the process id of the ActiveMQ process. Take it from ps command

ActiveMQ admin command

ActiveMQ is taking configuration details via simple xml (default:activemq.xml) file located in conf directory.

vi activemq/conf/activemq.xml

Make a small change in activemq.xml and your activemq-admin command will start working
Change the default createConnector=”false” to createConnector=”true” like below:





Now you can use below command for starting Apache ActiveMQ, stopping it and listing the connected brokers.

activemq/bin/activemq-admin start
activemq/bin/activemq-admin stop
activemq/bin/activemq-admin list

Configure STOMP transport connector, so that your PHP application can communicate with ActiveMQ

vi activemq/conf/activemq.xml

The default transport connector is openwire for native connectivity to Java and it will be available at tcp port 61616.

Change below code:









Data Persistence with MySql

Download latest MySQL Java Connector (mysql-connector-java-5.0.6-bin.jar) from MySql website. Copy the file into “activemq/lib/optional” folder

Launch the MySQL command line program and create a database for ActiveMQ

mysql> create database activemq;

vi activemq/conf/activemq.xml


The default persistence adapter is kahaDB which is fast but not very reliable. You can test it according to your condition or give MySql a change and take the better one.

Change below code:

















Start the ActiveMQ and check the MySql

mysql> use activemq; show tables;
Database changed
+--------------------+
| Tables_in_activemq |
+--------------------+
| ACTIVEMQ_ACKS |
| ACTIVEMQ_LOCK |
| activemq_msgs |
+--------------------+

Your MySql data persistence is working fine.

Installing PHP STOMP client

pecl install stomp-beta

It will generate a stomp.so in /usr/lib/php/modules/ directory. Open /etc/php.ini and load the extension like below

extension=stomp.so

Restart httpd service like below

service httpd restart

PHP message queue producer and consumer code

Now you have to make two php sample files, one for producting messages in queue and other for consuming those messages.

producer.php

getMessage());
}

for($i=1;$i<10;$i++) msg1 = "queue one my data">send($queue1, $msg1, array('persistent' => 'true'));
}

unset($stomp);
?>

In above code, I have generated a few messages via loop. In real senerio, it will be generated by events whenever something need to be processed in queue.

consumer.php

getMessage());
}

$stomp->subscribe($queue1);

while(1)
{
$frame = $stomp->readFrame();

if ( $frame != null)
{
echo $frame->body; //process your message
echo "\n";
$stomp->ack($frame);
}
}

unset($stomp);
?>

If you look at the code carefully, the consumption of messages are in a infinite loop. The consumer is ready to process all incoming messages.

You have to run the consumer code from your terminal like below:

nohup php consumer.php > consumer.log 2>&1 &

It will start consumer.php forever with nohup command, directs the messages in the consumer.log file

Configure ActiveMQ as Unix Service

ActiveMQ should be configured as Unix Service and it should start automatically if your server restarts.

Create ActiveMQ startup script /etc/init.d/activemqstart.sh with the following content:

#!/bin/bash
export JAVA_HOME=/usr
/usr/share/php/activemq/bin/activemq &

Create ActiveMQ stop script /etc/init.d/activemqstop.sh with the following content:

#!/bin/bash
export JAVA_HOME=/usr
/usr/share/php/activemq/bin/activemq-admin stop

Create ActiveMQ Linux service configuration script /etc/init.d/activemq with the following content:

#!/bin/bash
#
# activemq Starts ActiveMQ.
#
# chkconfig: 345 88 12
# description: ActiveMQ is a JMS Messaging Queue Server.
### BEGIN INIT INFO
# Provides: $activemq
### END INIT INFO

# Source function library.
. /etc/init.d/functions

[ -f /etc/init.d/activemqstart.sh ] || exit 0
[ -f /etc/init.d/activemqstop.sh ] || exit 0

RETVAL=0

umask 077

start() {
echo -n $"Starting ActiveMQ: "
daemon /etc/init.d/activemqstart.sh
echo
return $RETVAL
}
stop() {
echo -n $"Shutting down ActiveMQ: "
daemon su -c /etc/init.d/activemqstop.sh activemq
echo
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac

exit $?

Enable ActiveMQ service configuration as Linux Daemon:

chmod +x /etc/init.d/activemqstart.sh
chmod +x /etc/init.d/activemqstop.sh
chmod +x /etc/init.d/activemq
/sbin/chkconfig --add activemq
/sbin/chkconfig activemq on

Restart the server.

reboot

All done, Enjoy!
Cheers!!
Related Posts with Thumbnails