≡ Menu


No more php4 updates after 31st Dec 2007

There will be no updates after 31-Dec-2007 for PHP 4 version. According to the PHP development team ~ support for PHP 4 will continue until the end of this year only

You can easily upgrade to PHP 5 but please keep it mind that - few old php scripts may not work. Following application does works 100% with PHP 5 and MySQL 5:

a) WordPress blog software
b) Vbulletin and PHPBB forum software
c) Phpmyadmin MySQL admin software
d) Drupal 5.x CMS and many other softwares

Migrating from PHP 4 to PHP 5

FreeBSD Install Lighttpd Web Server

I’ve received couple of request about running Lighttpd web server under FreeBSD:

How do I install Lighttpd web server under FreeBSD along with MySQL 5 and PHP5?

lighttpd a secure, fast, compliant and very flexible web-server which has been optimized for high-performance environments. It has a very low memory footprint compared to other webservers and takes care of cpu-load. Its advanced feature-set (FastCGI, CGI, Auth, Output-Compression, URL-Rewriting and many more) make lighttpd the perfect webserver-software for every server that is suffering load problems. Installing lighttpd is quite simple under FreeBSD operating system using ports.

Lighttpd on FreeBSD

First update your port tree by typing following commands:
# portsnap fetch
# portsnap update

Install lighttpd web server under FreeBSD

Lighttpd port is located under /usr/ports/www/lighttpd:
# cd /usr/ports/www/lighttpd
Install lighttpd:
# make
# make install
# make clean

Install MySQL server

Install MySQL server:
# cd /usr/ports/databases/mysql50-server/
# make
# make install
# make clean

Also install mysql client support
# /usr/ports/databases/mysql50-client
# make
# make install
# make clean

Finally install mysql scripts:
# cd /usr/ports/databases/mysql50-scripts
# make
# make install
# make clean

Install php5 for lighttpd

Install PHP5 scripting:
# cd /usr/ports/lang/php5
# make
# make install
# make clean

Next you must install PHP5 extensions such as GB, mysql support and so on. At least select following extensions from menu:

  • ctype: The ctype shared extension for php
  • curl: The curl shared extension for php
  • dom: The dom shared extension for php
  • gd: The gd shared extension for php
  • imap: The imap shared extension for php
  • mbstring: The mbstring shared extension for php
  • mcrypt: The mcrypt shared extension for php
  • mysql: The mysql shared extension for php
  • mysqli: The mysqli shared extension for php
  • pcre: The pcre shared extension for php
  • posix: The posix shared extension for php
  • session: The session shared extension for php
  • simplexml: The simplexml shared extension for php
  • xml: The xml shared extension for php
  • xmlreader: The xmlreader shared extension for php
  • xmlwriter: The xmlwriter shared extension for php
  • zlib: The zlib shared extension for php

Type the following command:
# cd /usr/ports/lang/php5-extensions
# make
# make install
# make clean

Auto start lighttpd and mysql service

You need to enable both services. Open /etc/rc.conf:
# vi /etc/rc.conf
Append following two lines:

Start Mysql Server Under FreeBSD

To start MySQL server, enter:
# /usr/local/etc/rc.d/mysql-server start

Start Lighttpd Server Under FreeBSD

To start Lighttpd server, enter:
# /usr/local/etc/rc.d/lighttpd start

Verify that lighttpd is running:
# netstat -nat

FreeBSD Lighttpd configuration file location

  • Config file location: /usr/local/etc/lighttpd.conf
  • Default documentation root location : /usr/local/www/data/
  • Default username and groupname : www

See also:

Can someone steal my PHP script without hacking server?

Adarsh asks:

Can someone steal my PHP code or program without hacking my Linux box? Can someone snoop script over plain HTTP session?

Short answer is no. PHP is server side thingy.

However a misconfigured webserver can easily give out php file to all end users. You need to make sure that mod_php / mod_fastcgi loaded and correct MIME type is setup. To avoid such problem always test your server before moving to production environment. Most Linux distro configures both Apache and PHP out of box.

How do I stop downloading php source code?

The first step should be stopping a webserver.
# /etc/init.d/httpd stop
# /etc/init.d/lighttpd stop

If you are using Lighttpd...

Next bind webserver to for testing purpose. Open lighttpd websever config file and bind server address to
# vi /etc/lighttpd/lighttpd.conf
Bind to localhost/
server.bind = ""
Start lighttpd:
# /etc/init.d/lighttpd start
Now follow these instructions to configure php as fastcgi module. Now test your configuration using url PHP should work on server. If not working, refer to server log file.

If you are using Apache...

Open httpd.conf file and bind apache to
# vi httpd.conf
The Listen directive instructs Apache to listen to more than one IP address or port; by default it responds to requests on all IP interfaces, but only on the port given by the Port directive.
Start apache:
# /etc/init.d/httpd start
Now make sure php is installed use apt-get or rpm command to verify the same:
# rpm -qa | grep -i php
# dpkg --list | grep -i php
If PHP is not installed just follow these instructions to install PHP. Next make sure httpd.conf or php.conf has following directives:
LoadModule php4_module modules/libphp4.so
AddType application/x-httpd-php .php

Note: the path may differ in your setup. Now restart httpd:
# /etc/init.d/httpd restart
A sample php code:

<?php   phpinfo(); ?>

Finally when php started to work properly, make sure you bind back a server IP address from to public IP address.

Another option is keep your source code out of webroot and server all php requests from php application server using mod_proxy and multiple back-end servers.

How to program or integrate Paypal website payments using PHP 5

There is a nice and open source program called Website Payments Pro 4 Paypal.

From the project page:
It is an object-oriented PHP5 framework engineered to integrate easily with the Website Payments Pro API from Paypal. Complete the DoDirectPayment, SetExpressCheckout, GetExpressCheckoutDetails and DoExpressCheckoutPayment operations in just 3 lines or less.

Version 0.2.0 has been released.

=> Software name: Website Payments Pro 4 Paypal

=> License: BSD License

=> Download link: Click here to download

A great Zend Framework PHP Tutorial (PDF version)

PHP Logo

This tutorial is intended to give a very basic introduction to using the Zend Framework to write a very basic database driven application.

Zend Framework is a new open source effort aimed at producing a high-quality framework for developing modern, robust, secure web applications and web services in PHP 5.

This tutorial is constantly updated by author.

Download link

Download pdf version at Rob Allen's blog. [English version 83KB]

Also available in Simplified Chinese version, German version, Polish version, and Italian version.

Interesting PHP performance related articles roundup

PHP is a very fast programming language, but there is more to optimizing PHP than just speed of code execution. You need to tweak Apache, PHP and other things.

Here are some good articles about php (read as PHP tips) performance optimization.

=> How to install and configure Alternative PHP Cache - The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. It was conceived of to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

=> Cluesheet - Tips for PHP Developers - Excellent collection.

=> Getting Rich with PHP 5 OSCON 2006- Notes and slides

=> Accelerating PHP Applications - More notes and slides.

=> A HOWTO on Optimizing PHP - This article explain why optimizing PHP involves many factors which are not code related, and why tuning PHP requires an understanding of how PHP performs in relation to all the other subsystems on your server, and then identifying bottlenecks caused by these subsystems and fixing them.

=> 5 PHP Performance Tips You Probably Don’t Want To Hear

=> Howto: Apache and php web server by hiding version information.

PHP Send Email Using Authenticated SMTP Mail Server In Real Time

PHP has mail() function to send an email to users. However this mail() will not work:

=> If sendmail (or compatible binary) is not installed

=> If Apache Web server / Lighttpd running in chrooted jail

=> And your smtp server needs an authentication before sending an email

=> Or you just need to send email using PHP PEAR

In all these cases you need to use PHP PEAR's Mail:: interface. It defines the interface for implementing mailers under the PEAR hierarchy, and provides supporting functions which are useful in multiple mailer backends. In this tip you will learn about how to send an e-mail directly to client smtp server in real time.

PHP Pear's Mail.php is located in /usr/share/pear/ directory. Following is sample code to send an email via authenticated smtp server.

PHP send email using PHP SMTP mail Pear functions - Sample source code

Following code is well commented, you need to make necessary changes as per your setup.

/* mail setup recipients, subject etc */
$recipients = "feedback@yourdot.com";
$headers["From"] = "user@somewhere.com";
$headers["To"] = "feedback@yourdot.com";
$headers["Subject"] = "User feedback";
$mailmsg = "Hello, This is a test.";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "smtp.mycorp.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "smtpusername";
$smtpinfo["password"] = "smtpPassword";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);

Sending smtp email from chrooted Apache or Lighttpd webserver

Read following section, if you are running a secure chrooted Apache or Lighttpd web server. I have already written about setting php mail() function in chrooted jail. If you are using chrooted jail server setup, copy all files from /usr/share/pear directory to /chroot-directory/usr/share/pear directory. For example if lighttpd chrooted jail located in /webroot directory, you need to type following commands to install PHP pear support:
# mkdir -p /webroot/usr/share/pear
# cd /webroot/usr/share/pear
# cp -avr /usr/share/pear .

If PHP SAFE MODE is on, you must set /webroot/usr/share/pear directory permission to webserver username to allow access. Otherwise you will see error as follows:

1-Nov-2006 09:43:19] PHP Warning:  main(): SAFE MODE Restriction in effect.  The script whose uid is 506 is not allowed to access /usr/share/pear/PEAR.php owned by uid 0 in /usr/share/pear/Mail.php on line 636.

So if webserver username is lighttpd or apache use following command to setup correct ownership:
# chown lighttpd:lighttpd /webroot/usr/share/pear -ROR# chown apache:apache /webroot/usr/share/pear -R

You may also find modified wordpress WP-ContactForm plugin useful. It is a drop in form for users to contact you. It can be implemented on a page or a post. Original authored by Ryan Duff, which use php mail() function to send email. I have modified the same to send email via my ISP authenticated gateway using PHP PEAR's Mail:: interface :D