Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Sunday, March 24, 2013

Install PHP Modules for a FreeBSD Production Web Server

php (1)Install PHP Modules for a FreeBSD Production Web Server

Let's say we want to build a FreeBSD Production Web Server with a list of php modules that will be installed after we install PHP, Apache and MySQL. Here is a script to do that, very usefull if we will install many servers in the future.
So in order to install a FreeBSD Production Web Server we will have to:

1. Install FreeBSD
---------------------
Get the last IMAGE of FreeBSD from ftp.freebsd.org. You can use USB version which is easely to install. Install it, then cvsup to the stable version.

You can do that using this tutorial: http://www.freebsdonline.com/content/view/460/476/


2. Install PHP and Appache
-----------------------------
  cd /usr/ports/www/apache22
make install clean

Add the following lines to /usr/local/etc/apache22/httpd.conf (to load index.php and support PHP)

(If you already have dir_module section, just add ther index.php)

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps


Then, compile php.

cd /usr/ports/lang/php5
make install clean


Don't forget check APACHE MODULE before compiling, then compile and install, so your Apache would be able to run PHP scripts.

You must configure Apache to run PHP.


3. Install MySQL Server and Client
-----------------------------------
To install MySQL Server and Client use:

cd /usr/ports/databases/mysql51-client
make install clean

cd /usr/ports/databases/mysql51-server
make install clean


4. Install PHP modules
--------------------------
Well this is the interesting part of this tutorial. We will use a list of PHP modules, saved in php_modules.conffile.
And then we will have a script: compile_php_modules.sh that will do the task of compiling php modules from ports and installing on our system.

# ------- php_modules.conf ------------

# php modules
php5-bz2
php5-bcmath
php5-ctype
php5-curl
php5-dom
php5-extensions
php5-filter
php5-gd
php5-gettext
php5-hash
php5-iconv
php5-json
php5-ldap
php5-mbstring
php5-mcrypt
php5-mysql
php5-openssl
php5-pdo
php5-pdo_sqlite
php5-posix
php5-session
php5-simplexml
php5-soap
php5-sockets
php5-sqlite
php5-tokenizer
php5-xml
php5-xmlreader
php5-xmlwriter
php5-zip
php5-zlib

# ------- eof php_modules.conf -------

And our script compile_php_modules.sh :

#!/bin/sh

MAKE_PARAMS=""

confFile="php_modules.conf"
confFileWithPath="php_modules_with_path.conf"
confLine=""
confLineWithPath=""
check_if_installed=""

rm php_modules_with_path.conf

while [ 1 ]
do
read confLine || break
if [ "`echo $confLine | cut -c1`" = "#" ] ; then
echo "Processing: "$confLine
else
echo "Generating path for:"$confLine
confLineWithPath=`whereis $confLine | cut -d" " -f2`
err=$?
if [ "$err" -ne 0 ]
then
echo "Error running make install, err:"$err"--->"$confLineWithPath
echo "Error running make install "$err"-"$confLineWithPath >>error_php_modules_with_path.log
exit 1;
fi
check_if_installed=`pkg_info | grep $confLine`
err=$?
if [ "$err" -ne 0 ]
then
echo "$confLineWithPath" >>php_modules_with_path.conf
else
echo "Package installed. Skipping... "$err"--->"$confLineWithPath
echo "Package installed. Skipping... "$err"-"$confLineWithPath >>error_php_modules_with_path.log
fi
fi
done < $confFile

while [ 1 ]
do
read confLine || break
if [ "`echo $confLine | cut -c1`" = "#" ] ; then
echo "$confLine"
else
cd $confLine
echo "Compile & Install: "$confLine
make  install $MAKE_PARAMS BATCH=yes
err=$?
if [ "$err" -ne 0 ]
then
echo "Error running make install, err:"$err"--->"$confLine
echo "Error running make install "$err"-"$confLine >>error_php_modules.log
exit 1;
fi
fi
done < $confFileWithPath


After installing PHP modules, don't forget to restart Apache.

If you want more modules you can add them to php_modules.conf file.

Read Files With PHP

php (1)Read Files With PHP

There are two useful methods to read files with PHP, that will be covered in this PHP Tutorial. The first method is using the fread function, this method requires a combination of the functions: fopen, fread, and fclose functions. The file_get_contents function is used in the second method that will be covered – it eliminates the need to call multiple functions to read a file in PHP.

When using both of the methods covered in this PHP Tutorial, the file will be read to a string – this can be saved in a variable for later use – reading files into variables is not always necessary, sometimes you can use the returned data from the file directly.

Read file in PHP – the Fread Function

As was mentioned earlier, the fread function uses a combination of: fopen, fread, and fclose. The file is first opened for reading using fopen with the r mode – this mode will open the file for reading, and place the pointer at the beginning of the file.
<?php
$filename = "My-File.txt";
$filesize = filesize($filename);
$handle = fopen($filename, "r");
$contents = fread($handle, $filesize);
fclose($handle);
echo $contents;
?>

The second argument in the fread function, is the number of bytes where we want PHP to stop reading – in this case, the file size of the file we are going to read.


Get Content of file in PHP - the File_get_contents Function


Using the file_get_contents function to read files with may be easier than using fread and its accompanied functions.
<?php
$file = "my-beloved-text-file.txt";
$FileContent = file_get_contents($file)

echo $FileContent;
?>

Create the custom post type

php (1)Create the custom post type.

Today I want to show you how to create a custom post type for wordpress and then write code to post directly to the custom post type from the front end of your website.

First thing we need to do is create a function called ty_post_type_init(). We will then add the custom post type code as follows.

First we need to create the labels for the post type.

$labels = array(
'name' => _x('Custom Posts', 'the custom posts', 'your_text_domain'),

'singular_name' => _x('Custom Post', 'the custom post', 'your_text_domain'),

'add_new' => _x('Add New', 'custom-post', 'your_text_domain'),

'add_new_item' => __('Add New Custom Post', 'your_text_domain'),

'edit_item' => __('Edit Custom Post', 'your_text_domain'),

'new_item' => __('New Custom Post', 'your_text_domain'),

'all_items' => __('All Custom Posts', 'your_text_domain'),

'view_item' => __('View Custom Post', 'your_text_domain'),

'search_items' => __('Search Custom Post', 'your_text_domain'),

'not_found' =>  __('No custom posts found', 'your_text_domain'),

'not_found_in_trash' => __('No custom posts found in Trash', 'your_text_domain'),

'parent_item_colon' => '',

'menu_name' => __('Custom Posts', 'your_text_domain')


);




The next step is to add the arguments code; this is where we control certain aspects of the custom post type. For example you can add support for the title, editor, categories, thumbnails and more. You can also set things like the slug for the URL and the menu position.

Here is the arguments code.










1

2

3

4

5

6

7

8

9

10

11

12

13

14


$args = array(

'labels' => $labels,

'public' => true,

'publicly_queryable' => true,

'show_ui' => true,

'show_in_menu' => true,

'query_var' => true,

'rewrite' => array( 'slug' => _x( 'custom-post', 'URL slug', 'your_text_domain' ) ),

'capability_type' => 'post',

'has_archive' => true,

'hierarchical' => false,

'menu_position' => null,

'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )

);




Next we create the custom post type with the following function.










1


register_post_type('custom_posts', $args);




Now all that’s left for this part is to add an action to register the post on init.










1


add_action( 'init', 'ty_post_type_init' );




This is what the code looks like so far.










1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35


// register post type for custom posts

function ty_post_type_init() {

$labels = array(

'name' => _x('Custom Posts', 'the custom posts', 'your_text_domain'),

'singular_name' => _x('Custom Post', 'the custom post', 'your_text_domain'),

'add_new' => _x('Add New', 'custom-post', 'your_text_domain'),

'add_new_item' => __('Add New Custom Post', 'your_text_domain'),

'edit_item' => __('Edit Custom Post', 'your_text_domain'),

'new_item' => __('New Custom Post', 'your_text_domain'),

'all_items' => __('All Custom Posts', 'your_text_domain'),

'view_item' => __('View Custom Post', 'your_text_domain'),

'search_items' => __('Search Custom Post', 'your_text_domain'),

'not_found' =>  __('No custom posts found', 'your_text_domain'),

'not_found_in_trash' => __('No custom posts found in Trash', 'your_text_domain'),

'parent_item_colon' => '',

'menu_name' => __('Custom Posts', 'your_text_domain')


);

$args = array(

'labels' => $labels,

'public' => true,

'publicly_queryable' => true,

'show_ui' => true,

'show_in_menu' => true,

'query_var' => true,

'rewrite' => array( 'slug' => _x( 'custom-post', 'URL slug', 'your_text_domain' ) ),

'capability_type' => 'post',

'has_archive' => true,

'hierarchical' => false,

'menu_position' => null,

'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )

);

register_post_type('custom_posts', $args);

}

add_action( 'init', 'ty_post_type_init' );




Create the form for the front end posting.


We need to create the form here to be able to post in the front end. We will be using this in our template files or as a short code, depending on the style of coding.










1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29


    <form id="custom-post-type" name="custom-post-type" method="post" action="">


<p><label for="title">Post Title</label><br />


<input type="text" id="title" value="" tabindex="1" size="20" name="title" />


</p>


<p><label for="description">Post Description</label><br />


<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>


</p>


<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>


<p><label for="post_tags">Post Tags</label>


<input type="text" value="" tabindex="5" size="16" name="post-tags" id="post-tags" /></p>


<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>


<input type="hidden" name="post-type" id="post-type" value="custom_posts" />


<input type="hidden" name="action" value="custom_posts" />


<?php wp_nonce_field( 'name_of_my_action','name_of_nonce_field' ); ?>


</form>




You will notice the wp_dropdown_categories() function. This is used to be able to select categories in the front end via a dropdown menu.

Next we need to add a nonce field; this will be used for security checks so that we know the posted data came from our form. We do this by using the function wp_nonce_field(). You can read more about this here http://codex.wordpress.org/Function_Reference/wp_nonce_field

Next I’m just checking if the the form was posted, if it was run the function ty_save_post_data().










1

2

3


if($_POST){

ty_save_post_data();

}




Our complete code for this function looks like this.










1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40


function ty_front_end_form() {


?>

<form id="custom-post-type" name="custom-post-type" method="post" action="">


<p><label for="title">Post Title</label><br />


<input type="text" id="title" value="" tabindex="1" size="20" name="title" />


</p>


<p><label for="description">Post Description</label><br />


<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>


</p>


<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>


<p><label for="post_tags">Post Tags</label>


<input type="text" value="" tabindex="5" size="16" name="post-tags" id="post-tags" /></p>


<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>


<input type="hidden" name="post-type" id="post-type" value="custom_posts" />


<input type="hidden" name="action" value="custom_posts" />


<?php wp_nonce_field( 'name_of_my_action','name_of_nonce_field' ); ?>


</form>

<?php


if($_POST){

ty_save_post_data();

}


}

add_shortcode('custom-post','ty_front_end_form');




Now we need to add a function with code to handle the posted form data and then create the new post for us.

The first code for our function looks like this.










1

2

3

4

5

6


if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') )

{

print 'Sorry, your nonce did not verify.';

exit;


}




In the code above we check for an empty post variable and check that our nonce is correct. If it fails we give them a failure message.

Next we do some really minor form validation just to check that the fields are set before creating the post. We don’t want those fields really to be empty especially the title and description part.










1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19


// Do some minor form validation to make sure there is content

if (isset ($_POST['title'])) {

$title =  $_POST['title'];

} else {

echo 'Please enter a title';

exit;

}

if (isset ($_POST['description'])) {

$description = $_POST['description'];

} else {

echo 'Please enter the content';

exit;

}


if(isset($_POST['post_tags'])){

$tags = $_POST['post_tags'];

}else{

$tags = "";

}




Next we create an array with the necessary types for the post to be created.










1

2

3

4

5

6

7

8

9


// Add the content of the form to $post as an array

$post = array(

'post_title' => wp_strip_all_tags( $title ),

'post_content' => $description,

'post_category' => $_POST['cat'],  // Usable for custom taxonomies too

'tags_input' => $tags,

'post_status' => 'publish',            // Choose: publish, preview, future, etc.

'post_type' => $_POST['post-type']  // Use a custom post type if you want to

);




You will see above for the title part we are striping all tags. We don’t want html or any JavaScript tags in the title.

We then just use the wp_insert_post() function to create the actual post.

All that’s left is to set the location, in our case the home URL and then redirect there when we are done with the post.










1

2

3


$location = home_url(); // redirect location, should be login page


echo "<meta http-equiv='refresh' content='0;url=$location' />"; exit;




Here is what the completed function looks like now.










1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46


function ty_save_post_data() {


if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') )

{

print 'Sorry, your nonce did not verify.';

exit;


}else{


// Do some minor form validation to make sure there is content

if (isset ($_POST['title'])) {

$title =  $_POST['title'];

} else {

echo 'Please enter a title';

exit;

}

if (isset ($_POST['description'])) {

$description = $_POST['description'];

} else {

echo 'Please enter the content';

exit;

}


if(isset($_POST['post_tags'])){

$tags = $_POST['post_tags'];

}else{

$tags = "";

}


// Add the content of the form to $post as an array

$post = array(

'post_title' => wp_strip_all_tags( $title ),

'post_content' => $description,

'post_category' => $_POST['cat'],  // Usable for custom taxonomies too

'tags_input' => $tags,

'post_status' => 'publish',            // Choose: publish, preview, future, etc.

'post_type' => $_POST['post-type']  // Use a custom post type if you want to

);



$location = home_url(); // redirect location, should be login page


echo "<meta http-equiv='refresh' content='0;url=$location' />"; exit;

} // end IF


}




You can download the complete plugin,  just fill in your email below. Use the following shortcode [custom-post] to add the form on a page.

Please Like the article and look out for more awesome tutorials coming soon. :)

Cookies In Php

php (1)Cookies In Php

Cookies are stored by browser on user's computer. Cookies store small information.The information are sent to the server by the browser with each request.

In cookies different types of infomation can be stored like user-id,last visit date etc. In php, cookies are created by setcookie() method OR with $_COOKIE array.

<?php

setcookie("cookie_name","value","time","path","domain","secure");
?>
































ParameterDescription
cookie_nameName of the cookie is required.
valueInformation which we want to store in cookie.
timeLife time of cookies in seconds. This is optional, if value is not set, by default cookie expires in 30 days.
pathThis is the path where cookie is availed. If "/" is set, the cookie will be availed to all scripts through out entire domain.
domainThis is optional. Here sub domain is metioned where cookie is availed. Use ".yourdomain.com", If want on all subdomains
secureThis is optional.If we want to use cookies only with https (secure mode) set this option true, by default it is false.

For example if we want to record page preview by a visitor in a month, we will do something like given below:
<?php

$count=1;

if(isset($_COOKIE["count"])){

$count=$_COOKIE["count"]+1; $time=time()+(60 * 60 * 24 * 30)); setcookie("count",$count,$time);
}
?>

Cookies are deleted by browser after expiry time. But we can delete cookies if we set past time as expiry of cookie.
<?php
$time=time()-60;
setcookie("count",$count,$time);

 
}
?>

While using cookies, keep following limitations of cookies in mind.

1. If cookies are disabled in user's browser. You can't use.
2. Cookies store small data approx 4 KB
3. User can delete cookies any time.

Performing HTTP GET Requests in PHP

php (1)Performing HTTP GET Requests in PHP


This PHP Tutorial focuses on the use of fread and file_get_contents to download content from the web – in this Tutorial we will focus on the HTTP protocol, but it may also work for HTTPs – Using the methods covered in this Tutorial, requires that the fopen wrappers has been enabled, enabling the wrappers can be done by setting allow_url_fopen in php.ini toOn.

Performing HTTP requests can be useful for many things. Logging in on other websites, simply getting the content of a webpage, or checking for new versions of a PHP application. This PHP Tutorial will show you how to perform simple requests over HTTP, we will show how to set request headers in a later Tutorial.


Using Fread to Download of get Files Over the web


It is also possible to read a web page, just remember that reading will stop after a packet is available, so you either have to use a function like stream_get_contents – similar to file_get_contents – or you can use a while loop to read the content in smaller chunks until the end of the file has been reached.
<?php
$handle = fopen("http://brugbart.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>

<?php
$handle = fopen("http://brugbart.com/", "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
echo $contents;
?>

In this case the last argument in the fread function is equal to the chunk size, this is usually not larger than 8192 (8*1024). Keep in mind that this can be larger or smaller, but may also be limited by the system PHP is running on – you can likely optimize your PHP scripts by not using a larger chunk-size than that of your storage device.


Using File_get_contents to get a Website URL


It may also be easier to use this method when reading a file over HTTP, as you do not have to think about reading in chunks – everything is handled by PHP.
<?php
$homepage = file_get_contents('http://brugbart.com/');
echo $homepage;
?>

PHP OOP Tutorial

php (1)PHP OOP Tutorial

This is an Object-oriented PHP Tutorial, we will briefly cover how to use classes and objects so most will understand it, and how to use OOP in real situations.

OOP can sometimes be useful if you have a block of code, that you would otherwise have to repeat a lot of places, across multiple files or projects.

In situations where you have a lot of repeated database connections, such as across multiple files, it can be a good idea to make a function, and then simply call that whenever you need to connect to the database. But when you also got a lot of database queries, it may be a good idea to simply group all the functions together in a single class.

OOP is about maintainability and scalability. OOP makes it easier to maintain larger projects and to reuse code in other files and projects.


Classes and Objects in PHP


A class in PHP is a template used to create objects. Classes can include Properties and Methods (variables and functions).


OOP PHP Properties Example


The below is a simple PHP class, containing two variables.


class MyClassName {
public $Property1 = 'Hallo hallo!';
public $MyVariableName = 'Hallo dude!'; // Another Property
}

To access these variables, we first need to create an object, which is basically just en instance of the class.
$MyObject = new MyClassName(); // Creates an object from the class

We can then output or change the variable values as we wish.
echo $MyObject->Property1 ."<br>";
$MyObject->MyVariableName = 'My House'; // Changes the variable value


OOP PHP Methods Example


You can also include functions in your classes, when this is done the functions are referred to as methods. A simple method that everyone should be able to understand is shown below.
class MyClassName {
function SayHallo() {
return 'Hallo';
}
}

$MyObject = new MyClassName();
echo $MyObject->SayHallo();

How to Loop Through an Array in PHP

php (1)

How to Loop Through an Array in PHP

This PHP tutorial will teach you how you can easily loop through an array. In the first example script, we will be looping through our array with a normal while loop. Using this method may be faster than using a foreach loop, but the foreach loop is generally easier to use.

In this first example, we first count the elements in our PHP array, then we run the while loop until our counter reached the number of elements in the array – example below:
$aMyArray['0'] = 'One';
$aMyArray['1'] = 'Two';

$n = count($aMyArray); // Count the number of Elements in array

$counter = 0; // Initialize the counter variable
while ($counter < $n) {
echo $aWords["$counter"] . '<br>';
++$counter; // Increment the counter by 1
}

PHP loop trough array with foreach


The PHP foreach loop deserves its own section – this loop is intended to be used with arrays and objects – but it's also slower than loops where you first count the number of elements present in the array, presumably because it checks if the last element has been reached over and over again.

Do not be frightened by this however, as normally this won't be a problem – so if you think the foreach PHP loop is easier to work with, you shouldn't hesitate to use it – example below:
<?php
$aMyArray['0'] = 'One';
$aMyArray['1'] = 'Two';

foreach ($aMyArray as &$value) {
echo $aMyArray . '<br>';
}
?>

Friday, March 15, 2013

Authenticating in PHP using SSH



Authenticating in PHP using SSH


For a good couple of years now I've wondered if there was a way to write an authentication system in PHP that utilized SSH instead of the widely-breakable database and flatfile methods. After doing some research I found its possible after installing a PHP extension. This guide will detail the methods used to do this, with the intent of hopefully having this a more versatile option.

Requirements
Before we begin, there are a few things that are needed. The first is obviously PHP, with this guide written for 5.3.6. My system has Suhosin already installed and it causes no issues so you should be fine too if you have those patches installed. The next thing you'll need is the PECL extension SSH2. There other libraries out there that you can use that people have written but I decided to stick with officially supported releases. You can install this by simply running the following command:
pecl install ssh2

 


I cannot guarantee this but it is better safe than sorry, your PHP should be compiled with OpenSSH libraries.


Configuration File
I've already written all the code for both the configuration and actual authentication, and I'll provide those links at the bottom of this. But, basically the configuration file will store this information:

Host - The FQDN or IP of the SSH server (usually localhost is okay)
Port - The port which the host is listening on (standard is 22)
Key path - The path to the private key (used for public key authentication only)
Fingerprint - The fingerprint, or unique ID, of the host. If the fingerprint doesn't match the server's, authentication will be refused regardless
Authentication mode - Tells the system whether to use password or public key (default)

Now, if you're wondering why the fingerprint is so important, here's a scenario. Let's say you are using this to authenticate users for a banking system. Someone compromises the server and reroutes incoming port 22 packets to a different server (thus with a different fingerprint). If the fingerprint in the config file does not match the fingerprint of the attacker's SSH server when the script tries to establish a connection, the connection will die. This ensures that accounts are not compromised and sensitive data is not stolen. We add an extra layer of security to this by enforcing public key by default.

Authentication Class
I'm sure this is what you're probably wondering, so I'll address this first. There is a function available (SSH_GetFP()) that lets you get the fingerprint of the server. Here is a list of the functions available and what they do (all prefixed with SSH_):

Conn() - Creates a connection to the SSH server based on host & port.
Exists() - Determines whether the extension is installed or not.
FP() - This does the fingerprint checking of the config file against the server.
GetFP() - Function to get the fingerprint of the server (should ONLY be used to get the fingerprint...and never used in production)
AuthKey() - Authenticates a user by public key. Takes 3 arguments: username, public key file and the passphrase. If no passphrase is provided the function will fail.
AuthPass() - Authenticates a user by password.

Using These Files
After you properly set up the configuration file, do this to get the fingerprint of the server:
require_once('auth_ssh.php');

$ssh = new Auth_SSH();
echo($ssh->SSH_GetFP());

 


Every function calls SSH_Conn() due to a connection resource being needed, so you do not have to call it directly. As long as your host and port are correct, Get_FP() will establish the connection and get the fingerprint for you. Now you just need to add that to your configuration file.


To authenticate a user, its pretty easy. For passwords:
if(!$ssh->SSH_AuthPass("username", "password")){
echo("Fail");
} else{
echo("Success.");
}

To use the public key method, its a little bit trickier. You can either have the person upload their public key to the server or store them on the server along with the private key. My recommendation on this would be to have them upload their public key. To use this method, call it like so:
if(!$ssh->SSH_AuthKey("username", "/path/to/pub/key/file.pub", "passphrase_for_keyfile")){
echo("Fail");
} else{
echo("Success");
}

The reason why the passphrase is mandatory is, besides it being good security practice, is that in case someone does compromise the server and steals the public key, they still cannot log in without the passphrase.

Conclusion
Is this a flawless method? No. However, I strongly believe two-factor authentication is the wave of the future (which public key authentication is when provided with a passphrase). The source for these files are open to the public. If you use it, credit is welcome but not mandatory.

Also, you'll see looking through auth_ssh.php that $this->msg is filled a bit. This is used for debugging purposes. Functions themselves only return true or false (except for SSH_Conn() which returns a resource ID). While I try my best, I cannot guarantee this will work for you, but it does work for me.

FIles
auth_ssh.php - https://github.com/SecurityForUs/CodeDump/blob/master/auth_ssh.php
auth_ssh_config.php - https://github.com/SecurityForUs/CodeDump/blob/master/auth_ssh_config.php

Thursday, March 14, 2013

How To Use Multiple PHP Versions PHP-FPM & FastCGI With ISPConfig 3 CentOS 6.3


How To Use Multiple PHP Versions PHP-FPM & FastCGI With ISPConfig 3 CentOS 6.3


 


Since ISPConfig 3.0.5, it is possible to use multiple PHP versions on one server and select the optimal PHP version for a website. This feature works with PHP-FPM (starting with PHP 5.3) and FastCGI (all PHP 5.x versions). This tutorial shows how to build PHP 5.3 and PHP 5.4 as a PHP-FPM and a FastCGI version on a CentOS 6.3 server. These PHP versions can be used together with the default PHP (installed through yum) in ISPConfig.

I do not issue any guarantee that this will work for you!


1 Preliminary Note


I will install PHP 5.3.22 and PHP 5.4.12, the latest PHP 5.3 and 5.4 versions at the time of this writing. I will also show how to build some additional PHP extensions such as APC, memcache, memcached, and ioncube.

Please note that PHP-FPM can be used on both Apache and nginx servers, while FastCGI is available only for Apache servers.

PHP-FPM and FastCGI are mutually exclusive in PHP 5.3 and 5.4, that's why I show two ways of building PHP, one for PHP-FPM, one for FastCGI, however you can compile PHP twice to get both, one time with --enable-fpm and one time with --enable-cgi (make sure to use different base directories, such as /opt/php-5.3.22 for PHP-FPM and /opt/phpfcgi-5.3.22 for FastCGI).


2 Building PHP 5.3.22 (PHP-FPM)


Download and extract PHP 5.3.22:

mkdir /opt/php-5.3.22
mkdir /usr/local/src/php5-build
cd /usr/local/src/php5-build
wget http://de.php.net/get/php-5.3.22.tar.bz2/from/this/mirror -O php-5.3.22.tar.bz2
tar jxf php-5.3.22.tar.bz2

cd php-5.3.22/

Install the prerequisites for building PHP5:

yum groupinstall 'Development Tools'

yum install libxml2-devel libXpm-devel gmp-devel libicu-devel t1lib-devel aspell-devel openssl-devel bzip2-devel libcurl-devel libjpeg-devel libvpx-devel libpng-devel freetype-devel readline-devel libtidy-devel libxslt-devel libmcrypt-devel pcre-devel curl-devel mysql-devel ncurses-devel gettext-devel net-snmp-devel libevent-devel libtool-ltdl-devel libc-client-devel postgresql-devel

Configure and build PHP 5.3.22 as follows (you can adjust the ./configure command to your needs, take a look at

./configure --help

to see all available options; if you use a different ./configure command, it is possible that additional libraries are required, or the build process will fail):

./configure \
--prefix=/opt/php-5.3.22 \
--with-pdo-pgsql \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-soap \
--enable-calendar \
--with-curl \
--with-mcrypt \
--with-zlib \
--with-gd \
--with-pgsql \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-mysql \
--with-pdo-mysql \
--with-mysqli \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--enable-gd-native-ttf \
--with-openssl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-libdir=lib64 \
--enable-ftp \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-gettext \
--enable-fpm

The last switch (--enable-fpm) makes sure this PHP version will work with PHP-FPM. If you want to use this PHP-FPM version with Apache, please use --with-fpm-user=apache and --with-fpm-group=apache; if you want to use this PHP-FPM version with nginx, please use --with-fpm-user=nginx and --with-fpm-group=nginx.

make
make install

Copy php.ini and php-fpm.conf to the correct locations:

cp /usr/local/src/php5-build/php-5.3.22/php.ini-production /opt/php-5.3.22/lib/php.ini

cp /opt/php-5.3.22/etc/php-fpm.conf.default /opt/php-5.3.22/etc/php-fpm.conf

Open /opt/php-5.3.22/etc/php-fpm.conf and adjust the following settings - in the listen line you must use an unused port (e.g. 8999; port 9000 might be in use by the default CentOS PHP-FPM already), and you must add the line include=/opt/php-5.3.22/etc/pool.d/*.conf at the end. If you want to use this PHP-FPM version with Apache, please use user = apache and group = apache; if you want to use this PHP-FPM version with nginx, please use user = nginx andgroup = nginx.

vi /opt/php-5.3.22/etc/php-fpm.conf








[...]
pid = run/php-fpm.pid
[...]
user = nginx
group = nginx
[...]
listen = 127.0.0.1:8999
[...]
include=/opt/php-5.3.22/etc/pool.d/*.conf


Create the pool directory for PHP-FPM:

mkdir /opt/php-5.3.22/etc/pool.d

Next create an init script for PHP-FPM:

vi /etc/init.d/php-5.3.22-fpm








#! /bin/sh
### BEGIN INIT INFO
# Provides: php-5.3.22-fpm
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-5.3.22-fpm
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO
php_fpm_BIN=/opt/php-5.3.22/sbin/php-fpm
php_fpm_CONF=/opt/php-5.3.22/etc/php-fpm.conf
php_fpm_PID=/opt/php-5.3.22/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF"

wait_for_pid () {
try=0
while test $try -lt 35 ; do
case "$1" in
'created')
if [ -f "$2" ] ; then
try=''
break
fi
;;
'removed')
if [ ! -f "$2" ] ; then
try=''
break
fi
;;
esac
echo -n .
try=`expr $try + 1`
sleep 1
done
}
case "$1" in
start)
echo -n "Starting php-fpm "
$php_fpm_BIN $php_opts
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
fi
wait_for_pid created $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Gracefully shutting down php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -QUIT `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed. Use force-exit"
exit 1
else
echo " done"
echo " done"
fi
;;
force-quit)
echo -n "Terminating php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -TERM `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
echo -n "Reload service php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -USR2 `cat $php_fpm_PID`
echo " done"
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload}"
exit 1
;;
esac


Make the init script executable and create the system startup links:

chmod 755 /etc/init.d/php-5.3.22-fpm
chkconfig --levels 235 php-5.3.22-fpm on

Finally start PHP-FPM:

/etc/init.d/php-5.3.22-fpm start

As long as there are no pools in /opt/php-5.3.22/etc/pool.d, you will get this warning which you can ignore:

[root@server1 php-5.3.22]# /etc/init.d/php-5.3.22-fpm start
Starting php-fpm [05-Mar-2013 17:34:54] WARNING: Nothing matches the include pattern '/opt/php-5.3.22/etc/pool.d/*.conf' from /opt/php-5.3.22/etc/php-fpm.conf at line 511.
done
[root@server1 php-5.3.22]#

That's it - if you like, you can now install some additional modules like APC, memcache, memcached, and ioncube.

The APC, memcache, and memcached modules can be installed through PEAR which we must install and initialize first:

 


yum install php-pear

cd /opt/php-5.3.22/etc

pecl -C ./pear.conf update-channels

APC can now be installed as follows:

pecl -C ./pear.conf install apc

Accept all default values. Afterwards, open /opt/php-5.3.22/lib/php.ini...

vi /opt/php-5.3.22/lib/php.ini

... and add the line extension=apc.so at the end of the file (you can also configure some additional APC settings):








[...]
extension=apc.so
apc.enabled=1
apc.shm_size=128M
apc.ttl=0
apc.user_ttl=600
apc.gc_ttl=600
apc.enable_cli=1
apc.mmap_file_mask=/tmp/apc.XXXXXX
;apc.mmap_file_mask=/dev/zero
;apc.shm_segments = 5


The memcache extension can be installed as follows:

pecl -C ./pear.conf install memcache

Open /opt/php-5.3.22/lib/php.ini...

vi /opt/php-5.3.22/lib/php.ini

... and add the line extension=memcache.so at the end of the file:








[...]
extension=memcache.so


The memcached extension can be installed as follows:

yum install libmemcached-devel

pecl -C ./pear.conf install memcached

Open /opt/php-5.3.22/lib/php.ini...

vi /opt/php-5.3.22/lib/php.ini

... and add the line extension=memcached.so at the end of the file:








[...]
extension=memcached.so


The ionCube Loader can be installed as follows:

cd /tmp

Next download and unpack the correct ionCube Loader package for your architecture (x86_64 or x86).

For x86_64:

wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz
tar xfvz ioncube_loaders_lin_x86-64.tar.gz

For x86:

wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86.tar.gz
tar xfvz ioncube_loaders_lin_x86.tar.gz

Proceed as follows:

cp ioncube/ioncube_loader_lin_5.3.so /opt/php-5.3.22/lib/php/extensions/no-debug-non-zts-20090626/ioncube.so
vi /opt/php-5.3.22/lib/php.ini

Add the line zend_extension = /opt/php-5.3.22/lib/php/extensions/no-debug-non-zts-20090626/ioncube.so right at the beginning of the file (before the[PHP] line):








zend_extension = /opt/php-5.3.22/lib/php/extensions/no-debug-non-zts-20090626/ioncube.so
[PHP]
[...]


Reload PHP-FPM afterwards:

/etc/init.d/php-5.3.22-fpm reload

In ISPConfig 3.0.5, you can configure the new PHP version under System > Additional PHP Versions. On the Name tab, you just fill in a name for the PHP version (e.g. PHP 5.3.22) - this PHP version will be listed under this name in the website settings in ISPConfig:


Click to enlarge


Go to the PHP-FPM Settings tab (the FastCGI Settings tab can be left empty) and fill out the fields as follows:


Click to enlarge

 


3 Building PHP 5.3.22 (FastCGI)


 


mkdir /opt/phpfcgi-5.3.22
mkdir /usr/local/src/php5-build
cd /usr/local/src/php5-build
wget http://de.php.net/get/php-5.3.22.tar.bz2/from/this/mirror -O php-5.3.22.tar.bz2
tar jxf php-5.3.22.tar.bz2

cd php-5.3.22/

Install the prerequisites for building PHP5:

yum groupinstall 'Development Tools'

yum install libxml2-devel libXpm-devel gmp-devel libicu-devel t1lib-devel aspell-devel openssl-devel bzip2-devel libcurl-devel libjpeg-devel libvpx-devel libpng-devel freetype-devel readline-devel libtidy-devel libxslt-devel libmcrypt-devel pcre-devel curl-devel mysql-devel ncurses-devel gettext-devel net-snmp-devel libevent-devel libtool-ltdl-devel libc-client-devel postgresql-devel

Configure and build PHP 5.3.22 as follows (you can adjust the ./configure command to your needs, take a look at

./configure --help

to see all available options; if you use a different ./configure command, it is possible that additional libraries are required, or the build process will fail):

./configure \
--prefix=/opt/phpfcgi-5.3.22 \
--with-pdo-pgsql \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-soap \
--enable-calendar \
--with-curl \
--with-mcrypt \
--with-zlib \
--with-gd \
--with-pgsql \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-mysql \
--with-pdo-mysql \
--with-mysqli \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--enable-gd-native-ttf \
--with-openssl \
--with-fpm-user=apache \
--with-fpm-group=apache \
--with-libdir=lib64 \
--enable-ftp \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-gettext \
--enable-cgi

The last switch (--enable-cgi) makes sure this PHP version will work with FastCGI.

make
make install

Copy php.ini to the correct location:

cp /usr/local/src/php5-build/php-5.3.22/php.ini-production /opt/phpfcgi-5.3.22/lib/php.ini

That's it - if you like, you can now install some additional modules like APC, memcache, memcached, and ioncube.

The APC, memcache, and memcached modules can be installed through PEAR which we must install and initialize first:

yum install php-pear

cd /opt/phpfcgi-5.3.22/etc

pecl -C ./pear.conf update-channels

APC can now be installed as follows:

pecl -C ./pear.conf install apc

Accept all default values. Afterwards, open /opt/phpfcgi-5.3.22/lib/php.ini...

vi /opt/phpfcgi-5.3.22/lib/php.ini

... and add the line extension=apc.so at the end of the file (you can also configure some additional APC settings):








[...]
extension=apc.so
apc.enabled=1
apc.shm_size=128M
apc.ttl=0
apc.user_ttl=600
apc.gc_ttl=600
apc.enable_cli=1
apc.mmap_file_mask=/tmp/apc.XXXXXX
;apc.mmap_file_mask=/dev/zero
;apc.shm_segments = 5


The memcache extension can be installed as follows:

pecl -C ./pear.conf install memcache


Open /opt/phpfcgi-5.3.22/lib/php.ini...

vi /opt/phpfcgi-5.3.22/lib/php.ini

... and add the line extension=memcache.so at the end of the file:








[...]
extension=memcache.so


The memcached extension can be installed as follows:

yum install libmemcached-devel

pecl -C ./pear.conf install memcached

Open /opt/phpfcgi-5.3.22/lib/php.ini...

vi /opt/phpfcgi-5.3.22/lib/php.ini

... and add the line extension=memcached.so at the end of the file:








[...]
extension=memcached.so


The ionCube Loader can be installed as follows:

cd /tmp

Next download and unpack the correct ionCube Loader package for your architecture (x86_64 or x86).

For x86_64:

wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz
tar xfvz ioncube_loaders_lin_x86-64.tar.gz

For x86:

wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86.tar.gz
tar xfvz ioncube_loaders_lin_x86.tar.gz

Proceed as follows:

cp ioncube/ioncube_loader_lin_5.3.so /opt/phpfcgi-5.3.22/lib/php/extensions/no-debug-non-zts-20090626/ioncube.so
vi /opt/phpfcgi-5.3.22/lib/php.ini

Add the line zend_extension = /opt/phpfcgi-5.3.22/lib/php/extensions/no-debug-non-zts-20090626/ioncube.so right at the beginning of the file (before the[PHP] line):








zend_extension = /opt/phpfcgi-5.3.22/lib/php/extensions/no-debug-non-zts-20090626/ioncube.so
[PHP]
[...]


In ISPConfig 3.0.5, you can configure the new PHP version under System > Additional PHP Versions. On the Name tab, you just fill in a name for the PHP version (e.g. PHP 5.3.22) - this PHP version will be listed under this name in the website settings in ISPConfig:


Click to enlarge


Go to the FastCGI Settings tab (the PHP-FPM Settings tab can be left empty) and fill out the fields as follows:


Click to enlarge

 


4 Building PHP 5.4.12 (PHP-FPM)


 


mkdir /opt/php-5.4.12
mkdir /usr/local/src/php5-build
cd /usr/local/src/php5-build
wget http://de.php.net/get/php-5.4.12.tar.bz2/from/this/mirror -O php-5.4.12.tar.bz2
tar jxf php-5.4.12.tar.bz2

cd php-5.4.12/

Install the prerequisites for building PHP5:

yum groupinstall 'Development Tools'

yum install libxml2-devel libXpm-devel gmp-devel libicu-devel t1lib-devel aspell-devel openssl-devel bzip2-devel libcurl-devel libjpeg-devel libvpx-devel libpng-devel freetype-devel readline-devel libtidy-devel libxslt-devel libmcrypt-devel pcre-devel curl-devel mysql-devel ncurses-devel gettext-devel net-snmp-devel libevent-devel libtool-ltdl-devel libc-client-devel postgresql-devel

Configure and build PHP 5.4.12 as follows (you can adjust the ./configure command to your needs, take a look at

./configure --help

to see all available options; if you use a different ./configure command, it is possible that additional libraries are required, or the build process will fail):

./configure \
--prefix=/opt/php-5.4.12 \
--with-pdo-pgsql \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-soap \
--enable-calendar \
--with-curl \
--with-mcrypt \
--with-zlib \
--with-gd \
--with-pgsql \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-mysql \
--with-pdo-mysql \
--with-mysqli \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--enable-gd-native-ttf \
--with-openssl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-libdir=lib64 \
--enable-ftp \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-gettext \
--enable-fpm

The last switch (--enable-fpm) makes sure this PHP version will work with PHP-FPM. If you want to use this PHP-FPM version with Apache, please use --with-fpm-user=apache and --with-fpm-group=apache; if you want to use this PHP-FPM version with nginx, please use --with-fpm-user=nginx and --with-fpm-group=nginx.

make
make install

Copy php.ini and php-fpm.conf to the correct locations:

cp /usr/local/src/php5-build/php-5.4.12/php.ini-production /opt/php-5.4.12/lib/php.ini

cp /opt/php-5.4.12/etc/php-fpm.conf.default /opt/php-5.4.12/etc/php-fpm.conf

Open /opt/php-5.4.12/etc/php-fpm.conf and adjust the following settings - in the listen line you must use an unused port (e.g. 8998; port 9000 might be in use by the default CentOS PHP-FPM already), and you must add the line include=/opt/php-5.4.12/etc/pool.d/*.conf at the end. If you want to use this PHP-FPM version with Apache, please use user = apache and group = apache; if you want to use this PHP-FPM version with nginx, please use user = nginx andgroup = nginx.

vi /opt/php-5.4.12/etc/php-fpm.conf








[...]
pid = run/php-fpm.pid
[...]
user = nginx
group = nginx
[...]
listen = 127.0.0.1:8998
[...]
include=/opt/php-5.4.12/etc/pool.d/*.conf


Create the pool directory for PHP-FPM:

mkdir /opt/php-5.4.12/etc/pool.d

Next create an init script for PHP-FPM:

vi /etc/init.d/php-5.4.12-fpm








#! /bin/sh
### BEGIN INIT INFO
# Provides: php-5.4.12-fpm
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-5.4.12-fpm
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO
php_fpm_BIN=/opt/php-5.4.12/sbin/php-fpm
php_fpm_CONF=/opt/php-5.4.12/etc/php-fpm.conf
php_fpm_PID=/opt/php-5.4.12/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF"

wait_for_pid () {
try=0
while test $try -lt 35 ; do
case "$1" in
'created')
if [ -f "$2" ] ; then
try=''
break
fi
;;
'removed')
if [ ! -f "$2" ] ; then
try=''
break
fi
;;
esac
echo -n .
try=`expr $try + 1`
sleep 1
done
}
case "$1" in
start)
echo -n "Starting php-fpm "
$php_fpm_BIN $php_opts
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
fi
wait_for_pid created $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Gracefully shutting down php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -QUIT `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed. Use force-exit"
exit 1
else
echo " done"
echo " done"
fi
;;
force-quit)
echo -n "Terminating php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -TERM `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
echo -n "Reload service php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -USR2 `cat $php_fpm_PID`
echo " done"
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload}"
exit 1
;;
esac


Make the init script executable and create the system startup links:

chmod 755 /etc/init.d/php-5.4.12-fpm
chkconfig --levels 235 php-5.4.12-fpm on

Finally start PHP-FPM:

/etc/init.d/php-5.4.12-fpm start

As long as there are no pools in /opt/php-5.4.12/etc/pool.d, you will get this warning which you can ignore:

[root@server1 php-5.4.12]# /etc/init.d/php-5.4.12-fpm start
Starting php-fpm [05-Mar-2013 19:37:55] WARNING: Nothing matches the include pattern '/opt/php-5.4.12/etc/pool.d/*.conf' from /opt/php-5.4.12/etc/php-fpm.conf at line 511.
done
[root@server1 php-5.4.12]#

That's it - if you like, you can now install some additional modules like APC, memcache, memcached, and ioncube.

The APC, memcache, and memcached modules can be installed through PEAR which we must install and initialize first:

yum install php-pear

cd /opt/php-5.4.12/etc

pecl -C ./pear.conf update-channels

APC can now be installed as follows:



pecl -C ./pear.conf install apc

Accept all default values. Afterwards, open /opt/php-5.4.12/lib/php.ini...

vi /opt/php-5.4.12/lib/php.ini

... and add the line extension=apc.so at the end of the file (you can also configure some additional APC settings):








[...]
extension=apc.so
apc.enabled=1
apc.shm_size=128M
apc.ttl=0
apc.user_ttl=600
apc.gc_ttl=600
apc.enable_cli=1
apc.mmap_file_mask=/tmp/apc.XXXXXX
;apc.mmap_file_mask=/dev/zero
;apc.shm_segments = 5


The memcache extension can be installed as follows:

pecl -C ./pear.conf install memcache

Open /opt/php-5.4.12/lib/php.ini...

vi /opt/php-5.4.12/lib/php.ini

... and add the line extension=memcache.so at the end of the file:








[...]
extension=memcache.so


The memcached extension can be installed as follows:

yum install libmemcached-devel

pecl -C ./pear.conf install memcached

Open /opt/php-5.4.12/lib/php.ini...

vi /opt/php-5.4.12/lib/php.ini

... and add the line extension=memcached.so at the end of the file:








[...]
extension=memcached.so


The ionCube Loader can be installed as follows:

cd /tmp

Next download and unpack the correct ionCube Loader package for your architecture (x86_64 or x86).

For x86_64:

wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz
tar xfvz ioncube_loaders_lin_x86-64.tar.gz

For x86:

wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86.tar.gz
tar xfvz ioncube_loaders_lin_x86.tar.gz

Proceed as follows:

cp ioncube/ioncube_loader_lin_5.4.so /opt/php-5.4.12/lib/php/extensions/no-debug-non-zts-20100525/ioncube.so
vi /opt/php-5.4.12/lib/php.ini

Add the line zend_extension = /opt/php-5.4.12/lib/php/extensions/no-debug-non-zts-20100525/ioncube.so right at the beginning of the file (before the[PHP] line):








zend_extension = /opt/php-5.4.12/lib/php/extensions/no-debug-non-zts-20100525/ioncube.so
[PHP]
[...]


Reload PHP-FPM afterwards:

/etc/init.d/php-5.4.12-fpm reload

In ISPConfig 3.0.5, you can configure the new PHP version under System > Additional PHP Versions. On the Name tab, you just fill in a name for the PHP version (e.g. PHP 5.4.12) - this PHP version will be listed under this name in the website settings in ISPConfig:


Click to enlarge


Go to the PHP-FPM Settings tab (the FastCGI Settings tab can be left empty) and fill out the fields as follows:


Click to enlarge






Monday, September 21, 2009

FFmpeg-PHP Installation in Linux



How to install ffmpeg, ffmpeg-PHP, Mplayer, Mencoder, flv2tool, LAME MP3 Encoder, and Libog from source
This article shows how to install ffmpeg, ffmpeg-PHP, Mplayer, Mencoder, flv2tool, LAME MP3 Encoder, and Libog from source. It should work on most systems, however may need some tweaking from system to sytem.

cd /usr/local/src

Download source tarballs:

wget http://rubyforge.org/frs/download.php/9225/flvtool2_1.0.5_rc6.tgz
wget http://easynews.dl.sourceforge.net/sourceforge/lame/lame-3.97.tar.gz
wget http://superb-west.dl.sourceforge.net/sourceforge/ffmpeg-php/ffmpeg-php-0.5.0.tbz2
wget http://downloads.xiph.org/releases/ogg/libogg-1.1.3.tar.gz
wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.1.2.tar.gz
wget http://www4.mplayerhq.hu/MPlayer/releases/codecs/essential-20061022.tar.bz2

Extract the tarballs:

tar zxvf lame-3.97.tar.gz
tar zxvf libogg-1.1.3.tar.gz
tar zxvf libvorbis-1.1.2.tar.gz
tar zxvf flvtool2_1.0.5_rc6.tgz
tar jxvf essential-20061022.tar.bz2
tar jxvf ffmpeg-php-0.5.0.tbz2
tar zxvf flvtool2_1.0.5_rc6.tgz


Make a directory to store all the codecs:

mkdir /usr/local/lib/codecs/


Install any necessary packages:

yum install gcc gmake make libcpp libgcc libstdc++ gcc4 gcc4-c++ gcc4-gfortran
yum install subversion ruby ncurses-devel

svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg
svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer
cd /usr/local/src/mplayer
svn update
cd /usr/local/src

Copy codecs for mplayer:

mv /usr/local/src/essential-20061022/* /usr/local/lib/codecs/
chmod -R 755 /usr/local/lib/codecs/

If you are using secure tmp:

mkdir /usr/local/src/tmp
chmod 777 /usr/local/src/tmp
export TMPDIR=/usr/local/src/tmp

Installing Lame:

cd /usr/local/src/lame-3.97
./configure
make && make install

Installing Libogg:

cd /usr/local/src/libogg-1.1.3
./configure && make && make install

Installing libvorbis:

cd /usr/local/src/libvorbis-1.1.2
./configure && make && make install

Installing flvtool2:

cd /usr/local/src/flvtool2_1.0.5_rc6/
ruby setup.rb config
ruby setup.rb setup
ruby setup.rb install

cd /usr/local/src/mplayer
./configure && make && make install

Installing ffmpeg:

cd /usr/local/src/ffmpeg/
./configure --enable-libmp3lame --enable-libogg --enable-libvorbis --disable-mmx --enable-shared
make && make install

ln -s /usr/local/lib/libavformat.so.50 /usr/lib/libavformat.so.50
ln -s /usr/local/lib/libavcodec.so.51 /usr/lib/libavcodec.so.51
ln -s /usr/local/lib/libavutil.so.49 /usr/lib/libavutil.so.49
ln -s /usr/local/lib/libmp3lame.so.0 /usr/lib/libmp3lame.so.0
ln -s /usr/local/lib/libavformat.so.51 /usr/lib/libavformat.so.51

Installing ffmpeg-php:

cd /usr/local/src/ffmpeg-php-0.5.0/
phpize
./configure
make
make install

Note: Make sure this is the correct php.ini for this server.

echo 'extension=/usr/local/lib/php/extensions/no-debug-non-zts-20020429/ffmpeg.so' >> /usr/local/Zend/etc/php.ini

Restart apache:

service httpd restart

Friday, August 21, 2009

how to install imagemagick on linux server



How to install imagemagick on linux server


Here is a step-by-step guide on installing ImageMagick on most RedHat-based servers (RHEL, CentOS, Fedora, etc.):


You can install ImageMagick from source. However, if don't have a proper development environment or if you're anxious to get started, download a ready-to-run Unix or Windows executable. Before you download, you may want to review recent changes to the ImageMagick distribution.

You Can Download ImageMagick Rpm from the following Link
http://www.imagemagick.org/download/linux/fedora/i386/ImageMagick-6.5.4-10.i386.rpm
ImageMagick RPM's are self-installing. Simply type the following command and you're ready to start using ImageMagick:
$magick> rpm -Uvh ImageMagick-6.5.4-10.i386.rpm

For other systems, create (or choose) a directory to install the package into and change to that directory, for example:
$magick>
cd $HOME

Next, extract the contents of the package. For example:

$magick> tar xvfz ImageMagick.tar.gz

Set the MAGICK_HOME environment variable to the path where you extracted the ImageMagick files. For example:

$magick> export MAGICK_HOME="$HOME/ImageMagick-6.5.4"

If the bin subdirectory of the extracted package is not already in your executable search path, add it to your PATH environment variable. For example:

$magick> export PATH="$MAGICK_HOME/bin:$PATH"

On Linux and Solaris machines add $MAGICK_HOME/lib to the LD_LIBRARY_PATH environment variable:

$magick>LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$MAGICK_HOME/lib" $magick> export LD_LIBRARY_PATH

Finally, to verify ImageMagick is working properly, type the following on the command line:

$magick> convert logo: logo.gif $magick> identify logo.gif $magick> display logo.gif

Congratulations, you have a working ImageMagick distribution under Unix or Linux and you are ready to use ImageMagick to convert, compose, or edit your images or perhaps you'll want to use one of the Application Program Interfaces for C, C++, Perl, and others.

Or

yum install ImageMagick

after you send this shell command you will be ask this.. just say yes
Total download size: 15 M
Is this ok [y/N]: y