Web www.freebsdmadeeasy.com
Main Menu

Other Sites





Installing PHP 5 on FreeBSD

A tutorial on installing PHP from the FreeBSD ports for Apache and MySQL. What you need to add to the httpd.conf file and which of the PHP5 ports to choose.

Choosing which port to use

In the past there were several ports for PHP such as /www/mod-php5, /lang/php5-cli, and /lang/php5. Since the release of PHP 5.1.14 there is now only /lang/php5 This port now allows you to choose if you want to install the CLI, CGI, and Apache module.

CLI stands for command line interpreter. It is used for running PHP scripts from the command line and makes creating shell scripts very simple if you already know PHP. The Apache PHP Module is disabled by default, so make SURE that if you plan to use this for web work that you enable it.

Installing the port

Since all PHP ports are now combined you will need to configure it to be sure the parts you need are built.

# cd /usr/ports/lang/php5
# make config
# make install

When you run make config you will be shown a list of options. To use PHP with Apache make sure the Apache Module box is selected.

Once php has installed you will need to install the extra modules for things such as MySQL. These modules are all located in the ports. Some of the most common modules are

/usr/ports/databases/php5-mysql - MySQL Database
/usr/ports/www/php5-session	- Sessions
/usr/ports/graphics/php5-gd - Graphics Library

Adding the PHP 5 module to Apache

Apache needs the following lines in the httpd.conf file to use php. These lines should already be added by the port but if you have problems you should double check your httpd.conf file. Note that Apache 2.x does not need the AddModule line.

# Apache 1.3.x
LoadModule php5_module        libexec/apache/libphp5.so
AddModule mod_php5.c
# Apache 2.x
LoadModule php5_module        libexec/apache/libphp5.so

If you installed using the port and had apache installed already it should do this automatically for you.

Next find your DirectoryIndex section in your httpd.conf file. Apache is set up for PHP 4, but not PHP 5 currently so you will need to modify it and change the 4s to 5s like this.

<IfModule mod_dir.c>
    <IfModule mod_php3.c>
        <IfModule mod_php5.c>
            DirectoryIndex index.php index.php3 index.html
        </IfModule>
        <IfModule !mod_php4.c>
            DirectoryIndex index.php3 index.html
        </IfModule>
    </IfModule>
    <IfModule !mod_php3.c>
        <IfModule mod_php5.c>
            DirectoryIndex index.php index.html index.htm
        </IfModule>
        <IfModule !mod_php4.c>
            DirectoryIndex index.html
        </IfModule>
    </IfModule>
</IfModule>

This code is telling Apache to open index.php first you have the PHP 5 module loaded. You can change the order as you wish. Or if you just wanted to skip it you could simply add the following line to the httpd.conf file since you know you are going to have php 5.

DirectoryIndex index.php index.html index.htm

Now apache just needs to know what it should parse the PHP files with. These two lines should be added to the httpd.conf file, and can be put at the bottom if needed.

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

If want to use PHP code inside of .htm files you can just add on those extensions.

AddType application/x-httpd-php .php .htm .html

Configuring PHP

Settings for PHP are stored in /usr/local/etc/php.ini You will need to create this file by copying it from /usr/local/etc/php.ini-dist

# cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini

In this file you can set the memory limit for programs. Turn on global variables if you must, set the max file upload size, and everything else you need.

Testing PHP

Once you have restarted Apache so the changes take effect you are ready to test it. To test it run the following command to create a php file that you can attempt to run

# echo "<? phpinfo(); ?>" >> /usr/local/www/data/test.php

Then point your web browser to http://yourdomain.com/test.php and if it works you will see several pages of information on your PHP settings. If it did not work you will see only the text you typed in.


Comments

Submitted by Eddy on May 18, 2007
This is a general comment on this site:
 "THANK YOU SO MUCH" Installing and working on freebsd has never been this simple for me.
Submitted by Narendra on Oct 18, 2008
Hi,
 
 This helped me a lot...
 
 ThanQQQ very much......
Submitted by K-a-M-u-Z-u on May 28, 2007
What can i do when i get "error code 1" when installing php on FreeBSD?
Submitted by James on May 31, 2007
Error Code 1 is a generic "something went wrong" error code. Sometimes there is more information in the text above it. If you scroll back a bit you might be able to determine the cause. Sometimes I get an Error Code 1 when I am trying to install something that is already installed.
Submitted by jMCg on Jun 8, 2007
Seeing that there is the possibility to comment I‘d like to use this chance to provide a little correction from an Apache point of view.
 Using AddType to enable PHP [or any other script language] is a hack.
 Here‘s an explanation why:
 http://www.devside.net/articles/php
 
 [I‘m a regular of #Apache and PHP is generally a toothache for us, so we try to as hard as possible to raise people‘s awareness and correct those errors.]
Submitted by Joe on Jul 6, 2007
Hello,
 
 I have upgraded php4.4.4 to php5.2.3 but php -v shows 5.2.3 and phpinfo shows 4.4.4 can you please assist.
Submitted by Norsuzana Harun on Sep 7, 2007
Thank you for giving me a very simple guide to install PHP and enable it thru apache
Submitted by Cosmin C on Sep 10, 2007
This tutorial is very good with a little exception: For PHP5/Apache22 is required this line;
 AddModule php5_module libexec/apache22/libphp5.so
 not libexec/apache/libphp5.so.
 Thanks for ideea,
 Cosmin
Submitted by Khadija on Oct 19, 2007
Thanks for good explanation.
Submitted by Olav on Oct 19, 2007
Hi, I‘ve troubles installing php5.
 Actually a FreeBSD 6.2, Apache 2.2
 Mysql 4 and php4 run very well. But
 when I try an upgrade to php5, simply
 fails miserably. The new module
 crash httpd and let out php scripts.
 
 If I try an html page, theres no prob,
 but when I try (i.e. phpmysqladmin) one
 php script, simply fail and a httpd.core
 apear, when I try an analysis, I couldn‘t
 see something clear about what happen.
 
 Thanx in advance for your time.
 
 Olav.
Submitted by Armando Padilla on Feb 3, 2008
under FreeBSD.
 
 1. go to /usr/ports/lang/php5
 2. make deinstall
 3. make config
 4. select APACHE MODULES
 5. click ok
 6. make clean install
 7. read this page from AFTER the LoadModule section since the php install puts the proper LoadModule directive in there for us.
 
 
 Armando Padilla
Submitted by Nelson on Oct 30, 2007
Thanks! This document have been a great help.
Submitted by perk1z on Jan 15, 2008
Bravo .. thx
Submitted by betty on May 21, 2008
hi, i had install apache 2.2, php 5.2.6 and mysql 5 on freebsd. i had follow all the guidance, but php does not interpret any pages. when i load it from browser it ask me to download the file which is not file of php. any guideance would be appreciated..
 
 kind regards
Submitted by lol on Feb 6, 2009
you probably have the libphp5.so in a different directory, most likely in /usr/local/libexec/apache22/ instead of libexec/apache22/
Submitted by Ralph Branson on Jun 15, 2008
I followed the steps.
 No error messages.
 Started Apache and got the message that httpd could not be started.
 deinstalled and reinstalled a bunch of times.
 commented things out, etc.
 No error messages anywhere.
 httpd will not start.
 
 Ralph Branson
Submitted by I. Buettner on Oct 10, 2008
Hello,
 
 I had this problem on several installations (freebsd, windows, various linux) where it could be solved by NOT using mod_unique (comment out loading of module in httpd.conf or file included from there, depending on distribution). I have not investigated further since everything seemed OK then.
 
 Cheers
 
 Irene
Submitted by Pete on Apr 15, 2009
One thing that may help is doing the following:
 
 1. type ‘screen‘ (without ticks)
 2. CTRL-A C to open a second terminal
 3. In one terminal screen type ‘tail -f /var/log/httpd-error.log‘
 4. CTRL-A A
 5. type ‘apachectl start‘
 
 Go back to the tail screen and see what the output is and this should tell you the reason for apache‘s discontent.
Submitted by Abdul on Nov 5, 2008
Thank You Very much! Everything works!
Submitted by Jean Labac on Dec 15, 2008
Hey this one is really nice working tutorial
 thanks man
Submitted by androm31 on Feb 15, 2009
At first I think I thought the section only applied to apache 2.2 and neglected to change the 4‘s to 5‘s - that not-withstanding, I am now up and running thanks to your excellent advice.
Submitted by Kris on May 20, 2009
If apache won‘t start your probably missing
 apache22_enable="YES"
 
 in your /etc/rc.conf file
 
 Hope that helps
Submitted by rizky on Jun 5, 2009
I can‘t find php5 module libphp5.so at libexec/apache22/libphp5.so
 
 I‘ve installed apache22, mysql, php5, and php5 extension. What‘s wrong with that?
Submitted by Rich W on Aug 20, 2009
With the ".../lang/php5/make deinstall" steps described a few posts back, this still works great in BSD7.2, thanks!
Ask a question or add a comment here.