Posts Tagged ‘ PHP ’

ZendOptimizer+ For PHP 5.3.x

Zend Optimizer is available for php 5.3, but still no support in EasyApache. ZendGuardLoader and ZendOPtimizer are two different extensions and both of them can run Zend encoded files, but the difference is that “Optimizer” have additional opcode acceleration. ZendOptimizerPlus.so is used for PHP 5.3 encoding and is not officially released. If anyone need ZendOptimizer with PHP 5.3.x, we can install it using following steps.

1. Install p7zip

# yum install p7zipu

You may need to install RPMForge to work the above command

2. Download ZendServerCE, untar it and then unpack zend.7z:

# wget http://downloads.zend.com/zendserver/5.0.3/ZendServer-CE-php-5.3.3-5.0.3-linux-glibc23-i386.tar.gz
# tar xzvf ZendServer-CE-php-5.3.3-5.0.3-linux-glibc23-i386.tar.gz
# cd ZendServer*
# /usr/libexec/p7zip/7za e zend.7z -d zend.7z
for x64 use this file
# wget http://downloads.zend.com/zendserver/5.0.3/ZendServer-CE-php-5.3.3-5.0.3-linux-glibc23-x86_64.tar.gz

4. Now copy ZendOptimizerPlus.so to /usr/local/Zend53
mkdir /usr/local/Zend53
cp zend/lib/optimizerplus/php-5.3.x/ZendOptimizerPlus.so /usr/local/Zend53

4.Add below code to php.ini

[zend]
zend_extension=/usr/local/Zend53/ZendOptimizerPlus.so

; Optimizer+ On/Off switch. When set to Off, code is not optimized.
zend_optimizerplus.enable=1

; If set to On, use the current directory as a part of the script key
zend_optimizerplus.use_cwd=1

; If enabled, the Optimizer+ checks the file timestamps and updates the cache accordingly.
zend_optimizerplus.validate_timestamps=1

; How often to check file timestamps for changes to the shared memory storage allocation. (units: seconds)
zend_optimizerplus.revalidate_freq=2

; Enables or disables file search in include_path optimization
zend_optimizerplus.revalidate_path=0

; The verbosity of the Optimizer+ log
zend_optimizerplus.log_verbosity_level=1

; The Optimizer+ shared memory storage size. The amount of memory for storing precompiled PHP code in Mbytes. (units: MBytes)
zend_optimizerplus.memory_consumption=64

; The maximum number of keys (scripts) in the Optimizer+ hash table
zend_optimizerplus.max_accelerated_files=2000

; The maximum percentage of “wasted” memory until a restart is scheduled (units: %)
zend_optimizerplus.max_wasted_percentage=5

; Check the cache checksum each N requests
zend_optimizerplus.consistency_checks=0

; How long to wait (in seconds) for a scheduled restart to begin if the cache is not being accessed (units: seconds)
zend_optimizerplus.force_restart_timeout=180

; The location of the Optimizer+ blacklist file
zend_optimizerplus.blacklist_filename=

; If enabled, a fast shutdown sequence is used for the accelerated code
zend_optimizerplus.fast_shutdown=0

; A bitmask, where each bit enables or disables the appropriate Optimizer+ passes
zend_optimizerplus.optimization_level=0xfffffbbf

; Enables or disables the optimization passes that may take significant time, based on an internal runtime calculation
zend_optimizerplus.enable_slow_optimizations=1

And finally you get Zend Optimizer installed:

[~]# php -v
PHP 5.3.10 (cli) (built: Mar 27 2012 00:14:05)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
with the ionCube PHP Loader v4.0.12, Copyright (c) 2002-2011, by ionCube Ltd., and
with Zend Optimizer+ v4.1, Copyright (c) 1999-2010, by Zend Technologies

Advertisements

Script to find out PHP memory usage

Open a php file in the server and copy paste the following php code to into it. Access the php file via browser. You can see the memory usage in the web page.

<?php
// This is only an example, the numbers below will
// differ depending on your system
for ($i=1; $i<100; $i++) {
        //echo "used memory is " . memory_get_usage() . "<br />";
        $a = loadmem($i); 
        //echo "used memory after allocating ".$i."m is " . memory_get_usage() . "<br />"; // 57960
        //echo "used memory after unsetting allocated memory is " . memory_get_usage() . "<br />"; // 36744
        echo "You have allocated ". $i . "M (". memory_get_usage() . ") memory in this php script" . "<br />";
        unset($a);
}

function loadmem($howmuchmeg) {
        $a = str_repeat("0", $howmuchmeg * 1024 * 1024); // alocating 10 chars times million chars
        return $a;
}
?>


Installing snmp

SNMP installation steps

wget http://heanet.dl.sourceforge.net/sourceforge/net-snmp/

tar -zxvf ucd-snmp-4.2.6.tar.gz

(Check whether gcc is installed in the server, if not install gcc)

./configure

make

umask 022

make install

check with the command

snmpd -v


Install phpSHIELD Loaders

Installing phpShield Loaders
1) The first thing we need to do is check a couple of PHP settings. The easiest way to do this is with a phpinfo file.
2) You want to find/verify the following in your phpinfo.php file:

* Your PHP version
* Thread Safety is disabled
* enable_dl is set to on
* The path to your extension_dir(It will look something like this /usr/local/lib/php/extensions/no-debug-non-zts-20060613(example only))
Or
you can use following command on ssh shell to check extension library path
#php –i |grep extension_dir

* Path to your php.ini file

3) Now connect to your webserver using your favorite SSH client and login as root.

4)Create a new working directory then change directories:
# mkdir ~/phpshield

# cd ~/phpshield

5)Download the phpSHIELD loaders:
# wget http://phpshield.com/loadersphpshield.loaders.linux.zip

or if you have a 64 bit OS (most people will have a 32 bit OS so you will most likely use the code above)

# wget http://phpshield.com/loaders/phpshield.loaders.linux-64.zip

6) Extract the loaders:
# unzip phpshield.loaders.linux.zip

7)If you do a directory list:
# ls
you will see a bunch of files named phpshield.4.3.lin to phpshield.5.2.lin. What we want to do here is find the phpshield file with the number that matches your PHP version. You can find your PHP version at the very top of your phpinfo file from earlier.

8)Now we need to copy the appropriate phpshield loader file to your PHP extensions directory.

# cp ~/phpshield/phpshield.x.x.lin /path/to/your/php/extensions/directory

Replace x.x above with your PHP version and use the path to your PHP extensions directory (you should have found this in step 2 above.)

9) Open your php.ini file

#vi /usr/local/lib/php.ini

You should have found the path to your php.ini file in your phpinfo.php file from earlier.

10) Append the following to the Dynamic Extensions section of your php.ini file:

extension=”phpshield.x.x.lin”

Replace x.x with the number on the phpshield file you moved earlier.

# Finally, restart httpd:
# /etc/rc.d/init.d/httpd restart

php5 and php4 on same server as cgi

cd /usr/src

wget http://us.php.net/get/php-5.2.11.tar.gz/from/us.php.net/mirror

tar -zxf php-5.2.11.tar.gz
cd php-5.2.11
./configure –prefix=/usr/local/php5 –add_the_remaining_options_from_php4_expect_apxs
make
make install

check if binary is installed fine

/usr/local/php5/bin/php-cgi -v

 

Now run the following command

Code:
/usr/local/php5/bin/php-cgi  -v

It should show as cgi.

Now add the following lines on top of the httpd.conf file

Code:
ScriptAlias /php5/ /usr/local/php5/bin/
Action application/x-httpd-php5 /php5/php-cgi
<Directory "/usr/local/php5/bin/">
Allow from all
</Directory>

Save httpd.conf file and restart apache.

Open a .htaccess file inside the folder where you need php5 and add the following code to it. Make sure .htaccess support is enabled in your apache configuration.

Code:
AddHandler application/x-httpd-php5 .php

Make a phpinfo.php file in this directory and see what is shows when taken in browser. It should be php5.

See what it shows when the .htaccess line is commented. It should be php4 then.

RSS Parsing using Pear

Download the packages
wget http://download.pear.php.net/package/XML_RSS-0.9.10.tgz
wget http://download.pear.php.net/package/XML_Parser-1.3.0.tgz

Untar them.
Make a directory named XML inside the public_html directory of the user. Copy the file RSS.php from XML_RSS-0.9.10 and Parser.php from XML_Parser-1.3.0 to the directory XML. Open a test file and paste the php code below. Take in browser and see it works.
safe_mode should be off
==============================

If above does not work try below.

RSS Parsing using Pear
This tutorial will show you how to use Pear (PHP Extension and Application Repository) to parse an RSS feed and display it on your site.

Pear is one of the best hidden jewels inside PHP that programmers usually don’t take advantage of. The PHP Extension and Application Repository is located at here. Pear is described as “a structured library of open-sourced code for PHP users”. If you are running PHP on your box, you probably already have Pear installed! If you are not sure, ask your hosting provider or look at your PHP Info page for a PEAR installation. In this tutorial, we’re going to assume you already have Pear installed on your box.

RSS, or Real Simple Syndication, is a great way to distribute content via XML. For example, you can view the various RSS feeds Yahoo! provides here. In this tutorial, we will use the XML_RSS package from Pear to parse the top Yahoo! Technology News (located at http://rss.news.yahoo.com/rss/tech) and display it on an HTML page.

Step 1 – Install the XML_RSS package
The first thing to do is to install the XML_RSS package from Pear. For this, you will need to SSH or Telnet into your box, or get to a command line where your PHP is installed. If your hosting provider does not give you Telnet or SSH access, ask them to install the XML_RSS Pear package for you. For others who do have access, at the command line, type:

pear install XML_RSS

Executing the previous line will download the XML_RSS and the XML_PARSER modules from the PHP.net website and install it on your machine. You can learn about other PEAR functions at http://www.appelsiini.net/~tuupola/php/peardoc2/installation.cli.html. Now, we are ready to parse the RSS feed using PHP.

Open up a fresh PHP page and copy-paste the code provided below. The code presented below for the RSS feed is a slightly modified code then from the example given on the PEAR site located here:
[php]
<?
require_once “XML/RSS.php”;

$rss =& new XML_RSS(“http://rss.news.yahoo.com/rss/tech”);
$rss->parse();

echo “<ul>\n”;

foreach ($rss->getItems() as $item) {
echo “<li><a href=\”” . $item[‘link’] . “\”>” . $item[‘title’] . “</a></li>\n”;
}

echo “</ul>\n”;
?>
[/php]
The require once line loads the Pear XML_RSS module that we installed earlier. The next two lines loads the XML file from Yahoo and parses it. All the code for parsing the RSS is taken from the PEAR file, so we use it like a blackbox to parse the data. Next, we start the HTML list using the UL tag. The next two lines load each item from the XML file and posts the information on a list. Finally, the last line closes the list that we started. Save your file and point your browser to it. You can see an example of it working by clicking here.

The Pear modules really help you blackbox a lot of things and let you execute tough tasks like parsing an RSS file easily and effeciently. If you have any problems with the code, post about it on the discussion board and we’ll help you out!

PHP as CGI or Apache Module

PHP as Apache module

Running PHP as an Apache module requires web-server module apache mod_php

Advantages

* Faster performance since it is running as part of apache.
* Scripts are executed by server.
* A single config. file for all the scripts (php.ini).
* If need be, you can set PHP configuration variables in the web-server config. file or by using your .htaccess file to specify rules.

Disadvantages

* Scripts are being executed with web-server owners’ privileges, which might lead to some security related problems
* If the scripts run foreign applications (like mail distribution), the user will not be identified
* Apache loads servers, which might lead to the certain problems like to slow other statistics performance
* Errors in scripts may cause the whole web-server to crash

PHP as CGI

When PHP-interpreter.

Advantages

* Scripts will be executed with user privileges (www-domain owner)
* More than one PHP version can be run as CGI
* Needs less memory (RAM) than Apache module
* Errors in scripts do not cause the whole web-server to crash

Disadvantages

* Authorisation header not passed to PHP.