Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Sunday, November 13, 2011

Installing php driver for MongoDB

PHP      &   mongoDB


This is the post where I will configure php to work with mongoDB. On my previous post I have put the teps I followed to install mongoDB on my Ubuntu [TurnKey Linux] running on VirtualBox.
1.Run:
sudo pecl install mongo
2.Open your php.ini file and add to it:
extension=mongo.so
It is recommended to add this to the section with the other "extensions", but it will work from anywhere within the php.ini file.
3.Restart your web server (Apache, nginx, etc.) for the change to take effect.
Note:
pecl requires that pear be installed. For those using apt-get, you may need to run the following:
sudo apt-get install php5-dev php5-cli php-pear
However, you can download the source for the driver and can compile it manually :
shell> tar -xzvf mongo-1.2.6.tar.gz
shell> cd mongo-1.2.6
shell> phpize
shell> ./configure
shell> make
shell> make install
Check with the phpinfo

Starting MongoDB and php on Ubuntu

Installing MongoDB on Ubuntu:
1. Add 10gen package to source.list 
The 10gen package contains the latest mongoDB version, append below line to the end of the file “/etc/apt/sources.list”
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
For example, you can vim the “source.list” and append the 10gen package like this :
$ sudo vim /etc/apt/sources.list
File : /etc/apt/sources.list
#...content omitted ...
## This software is not part of Ubuntu, but is offered by third-party
## developers who want to ship their latest software.
deb http://extras.ubuntu.com/ubuntu natty main
deb-src http://extras.ubuntu.com/ubuntu natty main
 #mongo repo ##########
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

 2. Update package
Update the modified “/etc/apt/sources.list” :
sudo apt-get update
Now, a new “mongodb-10gen – An object/document-oriented database” is available for install.
  3. Add GPG Key
10gen package required GPG key, import it :
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
   4. Install mongodb-10gen
Everything is ready, now you can Install the mongoDB package :
sudo apt-get install mongodb-10gen
    5. Post-InstallationVerification
Now, mongoDB is installed, started, and auto start mongoDB script is generated to “/etc/init/mongo” and “/etc/init.d/mongo“. In addition, all mongoDB files are copied to “/usr/bin” folder.

The main configuration file “mongodb.conf” is located at “/etc/mongodb.conf“, change the values to customize your mongoDB server.

File : mongodb.conf
# mongodb.conf
# Where to store the data.
# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/var/lib/mongodb
#where to log
logpath=/var/log/mongodb/mongodb.log logappend=true
#port = 27017
#......
   6. Verification
To verify it, just connect it with “mongo”
$ mongo
MongoDB shell version: 1.8.1 connecting to: test
>
on my next post I will try to connect php with this mongoDB.

Wednesday, April 6, 2011

Installing ffmpeg and other extensions and tools to use with php

Installing FFMpeg
yum install ffmpeg ffmpeg-devel
If you get package not found, then you will need to add few lines in the yum repository for dag packages installation. Create a file named dag.repo in /etc/yum.repos.d with the following contents on it
[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1
then
yum install ffmpeg ffmpeg-devel
If everything is fine, then the installation should proceed smoothly. If not you will get something like warning GPG public key missing .


Common Errors

To fix rpmforge GPG key warning:
rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
For more information refer to this faq depending on Centos version
Missing Dependency Error:
If you get missing dependency error like shown below, in the middle of ffmpeg installation
Error: Missing Dependency: libc.so.6(GLIBC_2.4) is needed by package ffmpeg
Error: Missing Dependency: libtheora.so.0(libtheora.so.1.0) is needed by package ffmpeg
Error: Missing Dependency: rtld(GNU_HASH) is needed by package ffmpeg
Error: Missing Dependency: libc.so.6(GLIBC_2.4) is needed by package imlib2
Error: Missing Dependency: rtld(GNU_HASH) is needed by package a52dec
Error: Missing Dependency: rtld(GNU_HASH) is needed by package imlib2
Error: Missing Dependency: rtld(GNU_HASH) is needed by package gsm
Error: Missing Dependency: libc.so.6(GLIBC_2.4) is needed by package x264
Error: Missing Dependency: rtld(GNU_HASH) is needed by package xvidcore
Error: Missing Dependency: libc.so.6(GLIBC_2.4) is needed by package lame
Error: Missing Dependency: libc.so.6(GLIBC_2.4) is needed by package a52dec
Error: Missing Dependency: rtld(GNU_HASH) is needed by package faad2
Error: Missing Dependency: rtld(GNU_HASH) is needed by package x264
Error: Missing Dependency: rtld(GNU_HASH) is needed by package lame
Error: Missing Dependency: libc.so.6(GLIBC_2.4) is needed by package xvidcore
Error: Missing Dependency: libc.so.6(GLIBC_2.4) is needed by package faac
Error: Missing Dependency: libc.so.6(GLIBC_2.4) is needed by package faad2
Error: Missing Dependency: libgif.so.4 is needed by package imlib2
Error: Missing Dependency: rtld(GNU_HASH) is needed by package faac
Error: Missing Dependency: libc.so.6(GLIBC_2.4) is needed by package gsm
Error: Missing Dependency: libpng12.so.0(PNG12_0) is needed by package imlib2
Error: Missing Dependency: rtld(GNU_HASH) is needed by package libmp4v2
Error: Missing Dependency: libc.so.6(GLIBC_2.4) is needed by package libmp4v2
then most commonly you have GLIB 2.3 installed instead of GLIB 2.4 version. To check the current GLIB version installed on your server. just use
yum list glib*
and it should list the latest GLIB package version.

The reason i was getting this error was my rpmforge packages was pointed to centos 5 versions instead of centos 4.6.


To fix dependency error:

To fix this error, you might need to check your rpmforge packages compatible to the release of your existing CentOS version.
Check the file /etc/yum.repos.d/rpmforge.repo and it should look like for Centos 4.6(Final). If you have lines like http://apt.sw.be/redhat/el5/en/mirrors-rpmforge you might need to make changes to therpmforge.repos like shown below

Note: Backup the original rpmforge.repo file before you edit its content.
[rpmforge]
name = Red Hat Enterprise $releasever - RPMforge.net - dag
#baseurl = http://apt.sw.be/redhat/el4/en/$basearch/dag
mirrorlist = http://apt.sw.be/redhat/el4/en/mirrors-rpmforge
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge
enabled = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1
To know what linux type and version you are running
cat /etc/redhat-release
Once this is done, do again yum install ffmpeg.
This trick resolved the problem in my linux box running Centos 4.6 and this is the only way i found to install ffmpeg using yum.
To check the FFmpeg working:

Finally, check the ffmpeg whether it is working or not.
ffmpeg
ffmpeg -formats
ffmpeg --help
// This lists path of mpeg, its modules and other path information
ffmpeg -i Input.file Output.file
To check what audi/video formats are supported
ffmpeg -formats > ffmpeg-format.txt
Open the ffmpeg-formats.txt to see the output
D means decode
E means encode
V means video
A means audio
T = Truncated


Install FFMPEG-PHP Extension
FFmpeg-php is a very good extension and wrapper for PHP which can pull useful information about video through API interface. Inorder to install it you will need to download the source file and then compile and install extension in your server. You can download the source tarball : http://ffmpeg-php.sourceforge.net/
wget /path/to/this/file/ffmpeg-php-0.6.0.tbz2
tar -xjf ffmpeg-0.6.0.tbz2
phpize
./configure
make
make install
Common Errors

1. If you get command not found error for phpize, then you will need to do yum install php-devel
2. If you get error like "ffmpeg headers not found" while configuring the source.
configure: error: ffmpeg headers not found. Make sure ffmpeg is compiled as shared libraries using the --enable-shared option
then it means you have not installed ffmpeg-devel packages.
To Fix: Just install ffmpeg-devel using
yum install ffmpeg-devel
3. If you get an error like shared libraries not found problem and the program halts in the middle, then you must specify the ffmpeg installed path explicitly to the ./configure.
configure: error: ffmpeg shared libraries not found. Make sure ffmpeg is compiled as shared libraries using the --enable-shared option
To Fix:
1. First find out the ffmpeg path with ffmpeg --help command. The prefix default path should be like /usr/local/cpffmpeg
2. Configure the FFmpeg-php with --with-ffmpeg option
./configure --with-ffmpeg=/usr/local/cpffmpeg
That should resolve the problem!


Issue that I faced while installing the ffmpeg-php of version 0.6.0 :
/tmp/ffmpeg-php-0.6.0/ffmpeg_frame.c: In function ‘zim_ffmpeg_frame_toGDImage’:
/tmp/ffmpeg-php-0.6.0/ffmpeg_frame.c:336: error: ‘PIX_FMT_RGBA32’ undeclared (first use in this function)
/tmp/ffmpeg-php-0.6.0/ffmpeg_frame.c:336: error: (Each undeclared identifier is reported only once
/tmp/ffmpeg-php-0.6.0/ffmpeg_frame.c:336: error: for each function it appears in.)
/tmp/ffmpeg-php-0.6.0/ffmpeg_frame.c: In function ‘zim_ffmpeg_frame_ffmpeg_frame’:
/tmp/ffmpeg-php-0.6.0/ffmpeg_frame.c:421: error: ‘PIX_FMT_RGBA32’ undeclared (first use in this function)
make: *** [ffmpeg_frame.lo] Erreur 1
Fix to this issue :
Just open the ffmpeg_frame.c file and replace the PIX_FMT_RGBA32 with PIX_FMT_RGB32. With vi you can do it as :
vi ffmpeg_frame.c
:%s/PIX_FMT_RGBA32/PIX_FMT_RGB32
:w
However I just opened it on a text editor on my windows and applied find and replace ;)
After the replacement is done you have surely you have to do the configure and make and make install as :
phpize
./configure
make
make install
[before performing a make this time it is recommended you do a make clean]

Editing php.ini
Once you have done that without any problems then you will see the php extension file /usr/lib/php/modules/ffmpeg.so and you will need mention that extension in php.ini file
vi /etc//php.ini
Put the below two lines at the end of the php.ini file
[ffmpeg]
extension=ffmpeg.so
Then restart the server service httpd restart. In my case I had a server from godaddy and I had to do the following to restart the webserver :
/usr/sbin/apachectl graceful
To check whether ffmpeg enabled with php, point your browser to test.php file. It should show the confirmation of installed ffmpeg php extension
// #test.php
          phpinfo()
?>
This is what you are suppose to get :
If any case the ffmpeg does not show in the phpinfo() test make sure that php.ini path to ffmpeg.so is correct. Still the problem occurs, the reason could be you might be using older versions of ffmpeg-php which is buggy. Just download the latest version of ffmpeg-php source then compile it.
Installing Mplayer + Mencoder
To know more about Mplayer
Just issue the following yum commands to install the rest of the packages.
yum install mplayer mencoder
Installing FlvTool2
Flvtool2 is a flash video file manipulation tool. It can calculate metadata and can cut and edit cue points for flv files.
If you are on Centos 5 try yum install flvtool2 [worked fine with me] with dag repository and if you get package not found you will need to manually download and compile the flvtool2. You can download latest version of flvtool2 here: http://rubyforge.org/projects/flvtool2/
wget
ruby setup.rb config
ruby setup.rb setup
sudo ruby setup.rb install
If you get command not found error, it probably means that you dont have ruby installed.
yum install ruby
Thats it! Once ffmpeg works fine with php extension, download a sample video, convert to .flv format in the command line and plug it to flowplayer to see it work on your web browser. Try also to download the video file offline and see whether the converted flv file works well with both audio and video.
Now following is how you might wanna play with this beautiful tool/toolset :
This is how ffmpeg actually works :

Here is a some tutorials to play with ffmepg.

http://www.phpro.org/tutorials/Video-Conversion-With-FFMPEG.html

Reference :

Sunday, January 16, 2011

Running CodeIgniter from shell on Cronjobs

I am actually tuklifying from the CodeIgniter wiki - just adding my experience - things I faced and solution to the issue.
Steps :
1.Make a directory for your cron scripts. I just used scripts and put it in the same directory that my CI system folder is in.
2. Copy the index.php file from your DOCUMENT_ROOT (the script that normally initiates a connection to CI from a web browser) to the scripts directory, and rename it to something meaningful, such as mailsender.php.
3. Add the following line at the very first line of the mailsender.php:
#!/usr/local/bin/php -q
4. Edit this file and make some slight adjustments. First, we don’t want the script to timeout during execution, so somewhere toward the top we add:
set_time_limit(0);
5. Set the path to the system folder, if it isn’t correct:
$system_folder = "../system";
6. We need to let CI know what controller/method we want to access, and any other URI variables we need. Normally this happens in the URL, so we emulate it by setting PATH_INFO manually:

$_SERVER['PATH_INFO'] = '/mailprocessor/sendmail';
$_GET = ''; // Required for some installations
However, if the config uri protocol is set to REQUEST_URI, then CI doesn’t use PATH_INFO, so instead, emulate REQUEST_URI manually:

$_SERVER['REQUEST_URI'] = '/mailprocessor/sendmail';
7. You create a mailprocessor.php controller (in the application/controllers dir) with a sendmail method, and make it do what it needs to do, load libraries/models/etc, execute them, then exit. What you don’t want to happen is output anything. Just make sure you don’t echo any content to stdout (echo, loading views, etc.) If you are sending emails, you would load a view file, but return the contents to a variable instead of displaying it (pass true as third param), then run through normal procedures for sending emails.
One thing to be aware of regarding security, you probably don’t want anyone accessing /mailprocessor/sendmail from a web browser[In my case I don put the scripts on the web directory- so no need to worry about ], so in the mailprocessor.php controller constructor, you may want to do some kind of check to make sure we are coming from a cron script. Probably the easiest method would be to test the SCRIPT_FILENAME variable:

if($_SERVER['SCRIPT_FILENAME'] != 'mailsender.php')
  exit; 
8.Once everything looks good, add your crontab entry to execute the script, and there you have it! Sample cronjob [from cPanel  can easy do that- you just need to put the script location to run]:
# execute cronjob every day at 2am
00 02 * * * /HOME/myusername/misc/scripts/mailsender.php
My addition : 
8. You need to edit the BASEPATH on the mailsender.php.  You need to put the absolute path instead of relative path.


Original post - You will get some other alternative ways to do the things :)
Update-05/16/2011: On version 2, CodeIgniter has implemented a feature to run sccript from CLI. :)