Apache/MySQL/PHP/phpMyAdmin on FreeBSD

1. Introduction:

This article describes how to setup Apache, MySQL, PHP and phpMyAdmin on a server running FreeBSD. The article was written for the software versions below but is likely to work on newer versions without too much difficulty.

 2. Software:

Operating System:        FreeBSD 7.0 for i386      Download

Apache:                           2.2.8                                Installed from Ports Collection

MySQL Server:            5.0.51a                             Installed from Ports Collection

PHP & Extensions:        5.2.5                                Installed from Ports Collection

phpMyAdmin:              2.11.5                               Installed from Ports Collection

3. Before you begin:

This article assumes you have a working install of FreeBSD 6.2 for i386 logged in as root (or another user in the wheel group and you have used “su”) with the ports collection installed. You can use sysinstall, cvsup or portsnap to install the ports distribution if you have not already done so. See point 2 below.

Update your ports collection (portsnap fetch, portsnap extract.) (See the FreeBSD Handbook Section 4.5.1)

4. Installing MySQL:

(1)  Go to the mysql50-server port directory by typing the command:

                 cd /usr/ports/databases/mysql50-server

 (2) Build the port by typing: (This takes AGES – good time for some food.)


(3) Install by typing:

             make install clean

(4)Open /etc/rc.conf with your favourite text editor and add the line shown below. This will ensure mysql is enabled and starts on boot.


 (5)Start mysql manually to avoid having to reboot now by typing:

         /usr/local/etc/rc.d/mysql-server start

 (6)Set a password for the MySQL root user by executing the command, subtituting your own password in place of new-password:

    /usr/local/bin/mysqladmin -uroot password ‘new-password’

  And you’re done! MySQL is installed.

5.Installing Apache

(1)Go to the apache22 port directory by typing the command:

cd /usr/ports/www/apache22

(2)Build and install the port by typing: (This takes a while, coffee time!)

make install clean

Note: You may want to disable the two DAV options if you don’t need them when prompted

(3)Open /etc/rc.conf with your favourite text editor and add the line shown below. This will ensure apache is enabled and starts on boot.


6.Installing PHP

(1)Go to the php5 port directory by typing the command:

cd /usr/ports/lang/php5

(2)Build and install the port by typing: (Just accept the default options, this takes a while, more coffee.)

make install clean

Make sure the APACHE (Build Apache module) option is ticked when configuring the build, leaving all other options as default, before selecting OK.

(3)Go to the php5-extentions meta port directory by typing the command:

cd /usr/ports/lang/php5-extentions

(4)Build and install the port by typing: (Just accept the defaults here, phpMyAdmin will install any other extensions required itself)

make install clean

(5)Install the php.ini file:

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




php.ini-development 开发用的,php.ini-produciton 生产机用的,如果做站的话,我觉得应该将php.ini-produciton改为php.ini,用php.ini-produciton可能会出现无法支持PHP,修改php.ini中的短标签开关short_open_tag=Off在作怪,改成On就行了

(6)Edit your Apache configuration file (/usr/local/etc/apache22/httpd.conf) and add the following lines to the end of the file:

AddType application/x-httpd-php .php

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

 Note: You can add additional extenions other than .php (eg .phtml) seperated by spaces.

Note: The second line is optional, it will show colour highlighted PHP source for .phps files.

(7)You should also search for the line that reads:

DirectoryIndex index.html

and change it to read:

DirectoryIndex index.php index.html

Note: You may also add index.phtml or any other default page if you added additional extentions in the previous step.

(8)Enable language settings by searching for the line:

#Include etc/apache22/extra/httpd-languages.conf

and removing the # comment mark so it reads:

Include etc/apache22/extra/httpd-languages.conf

(9)Edit the language settings file (/usr/local/etc/apache22/extra/httpd-languages.conf) and add the following line at the end of the file:

AddDefaultCharset On

(10)Start Apache using the startup script:

/usr/local/etc/rc.d/apache22 start

And you’re done! Apache with PHP is installed.

安装Zend Optimizer

cd /usr/ports/devel/ZendOptimizer/

make install clean

===> ZendOptimizer-3.3.0.a cannot install: doesn’t work with PHP version : 5 (Doesn’t support PHP 5).

*** Error code 1

Stop in /usr/ports/devel/ZendOptimizer.

注:如果你用的是FreeBsd8.0版本的可能会出现上面的情况,这里可以使用使用pkg_add命令来安装Zend Optimizer.

#pkg_add -r ZendOptimizer



Fetching ftp://ftp.freebsd.org/pub/FreeBSD/ports/i386/packages-8.0-release/Latest/ZendOptimizer.tbz… Done.

pkg_add: warning: package ‘ZendOptimizer-3.3.0.a’ requires ‘libxml2-2.7.5’, but ‘libxml2-2.7.7’ is installed

pkg_add: warning: package ‘ZendOptimizer-3.3.0.a’ requires ‘php5-5.2.11’, but ‘php5-5.3.2’ is installed




You have installed the ZendOptimizer package.


Edit /usr/local/etc/php.ini and add:









NOTE: PHP should be compiled in non-debug mode (default).





7.Installing phpMyAdmin

(1)Got to the phpmyadmin port directory by typing the command:

cd /usr/ports/databases/phpmyadmin

(2)Build and install the port by typing: (Just accept the default options)

make install clean

(3)phpMyAdmin is now installed in /usr/local/www/phpMyAdmin. To use it we need to create Alias and Directory entries in /usr/local/etc/apache22/httpd.conf. To do this, add the following lines to the <IfModule alias_module> section (just search for where all the other Alias commands are.)

Alias /phpmyadmin /usr/local/www/phpMyAdmin

(4)As /usr/local/www/phpMyadmin is outside of the Apache <DocumentRoot> you will have to make a <Directory> entry for it too. Add the following lines to the end of the <Directory> section: (just search for </Directory>)

<Directory “/usr/local/www/phpMyAdmin”>

Order allow,deny

Allow from all


 Note:You may prefer to put the Alias entry inside a <VirtualHost> entry if you are hosting multiple sites using name based virtual hosting and do not wish to enable phpMyAdmin on all the sites.

(5)Create a config directory for phpMyAdmin and make it globally read/write/executable by typing the commands:

cd /usr/local/www/phpMyAdmin

mkdir config

chmod 777 config

(6)Restart Apache so that the Alias and Directory entries take effect by typing:

/usr/local/etc/rc.d/apache22 restart

(7)Configure phpMyAdmin by going to http://hostname/phpmyadmin/scripts/setup.php in your browser and set at least the following:

  • Add Server
    • Change the “Authentication type” dropdown to http to have phpMyAdmin prompt you for a username and password.
    • Delete root from the “User for config” auth textbox so it is blank.
    • You can leave all other settings as they are (even if they are blank.)
    • Click the “Add” button to add the new server

Save the configuration using the Save button in the Configuration section.

(8)For the changes to take effect you must copy the generated config file from the phpMyAdmin/config directory to the phpMyAdmin directory by typing the following command: (Note the space dot at the end of the command)

cp config/config.inc.php .

(9)You can now delete the config directory you created earlier and reset the permissions on the config.inc.php file to read only typing the commands:

rm -rf config

chmod 444 config.inc.php

(10)That’s it! You can place your web site in /usr/local/www/data/ and access phpMyAdmin at http://hostname/phpmyadmin/ in your web browser and logging in using username root and the MySQL password you set earlier.



[Sat Jan 23 22:47:29 2010] [warn] (2)No such file or directory: Failed to enable the ‘httpready’ Accept Filter    解决方法是:

#kldload accf_http  或者将/boot/defaults/loader.conf==> accf_httpd_load=”YES”

2.apache22无法启动时,进入/var/logo查看httpd_error.log, 如果出现以下提示:

[alert] (EAI 8)hostname nor servname provided, or not known: mod_unique_id: unable to find IPv4 address of “shao  Configuration Failed

解决方法:进入/usr/local/etc/apache22    ee ./httpd.conf

将LoadModule unique_id_module libexec/apache2/mod_unique_id.so注释掉

然后启动/usr/local/etc/rc.d/apache22  start即可.



<Directory “/usr/home/www/jpeps.com”>

Options FollowSymLinks

AllowOverride None

Order allow,deny

Allow from all


<IfModule dir_module>

DirectoryIndex index.html index.htm index.php


添加php.ini 被禁用的函数(disable_functions)


Drop Your Comment