FreeBSD Configure Apache PHP with mod_fastcgi Module

Q. How do I configure and Install Apache web server with PHP5 and mod_fastcgi to get faster PHP access under FreeBSD server?

A.mod_fastcgi is a cgi-module for Apache web server.

FastCGI is a language independent, scalable, open extension to CGI that provides high performance without the limitations of server specific APIs.

Red Hat Linux / CentOS Linux specific mod_fastcgi tutorial is here.

FreeBSD Install Apache Web server

Install apache using FreeBSD ports collection:
# cd /usr/ports/www/apache22
# make install clean
# echo 'apache22_enable="YES"' >> /etc/rc.conf

FreeBSD Install PHP5

Make sure php-cgi binary exists and it is compiled with fastcgi support:
# /usr/local/bin/php-cgi -v
Output

PHP 5.2.6 with Suhosin-Patch 0.9.6.2 (cgi-fcgi) (built: Sep 10 2008 19:07:02)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
    with XCache v1.2.2, Copyright (c) 2005-2007, by mOo

If you do not see word cgi-fcgi, recompile php with fastcgi support by visiting /usr/ports/lang/php5:
# cd /usr/ports/lang/php5
# make config
# make install clean

Fig.01 Enable FastCGI support by selecting FastCGI option

Make sure you install php-gd, php-mysql and other extensions, enter:
# cd /usr/ports/lang/php5-extensions/
# make install clean

Fig.02: Configure FreeBSD PHP5 extensions

Configure FreeBSD Apache mod_fastcgi

Install mod_fastcgi, enter:
# cd /usr/ports/www/mod_fastcgi
# make install clean

Open /usr/local/etc/apache22/httpd.conf file, enter:
# vi /usr/local/etc/apache22/httpd.conf
Make sure following line exists:

LoadModule fastcgi_module     libexec/apache22/mod_fastcgi.so

Create virtual hosting configurations

Your sample setup:

  • Apache share IP address: 74.86.49.131
  • Domain name: theos.in
  • DocumentRoot Directory: /websites/home/theos.in/http
  • Log Directory: /websites/home/theos.in/logs
  • PHP cgi-bin Directory: /websites/home/theos.in/cgi-bin/

Create directory structure as above:
# mkdir -p /websites/home/theos.in/{http,https,logs,cgi-bin,images,private,stats}
# chown -R theosftpuser:theosftpgroup /websites/home/theos.in/

Replace username, groupname and directories as per your setup.

Append following configuration directives to httpd.conf virtual hosting section:

<VirtualHost *:80>
        ServerAdmin webmaster@theos.in
        DocumentRoot "/websites/home/theos.in/http"
        ServerName theos.in
        ServerAlias www.theos.in
        ErrorLog "/websites/home/theos.in/logs/error.log"
        CustomLog "/websites/home/theos.in/logs/access.log" common
        ScriptAlias /cgi-bin/ "/websites/home/theos.in/cgi-bin/"
 
	<Directory "/websites/home/theos.in/http">
		Options -Indexes FollowSymLinks +ExecCGI
		AllowOverride AuthConfig FileInfo
		AddHandler php5-fastcgi .php
		Action php5-fastcgi /cgi-bin/php.cgi
		Order allow,deny
		Allow from all
	</Directory>
 
	<Directory "/websites/home/theos.in/cgi-bin">
		AllowOverride None
		Options None
		Order allow,deny
		Allow from all
	</Directory>
 
</VirtualHost>

Save and close the file.

Create php.cgi script

Create a shell script as follows (/websites/home/theos.in/cgi-bin/php.cgi):

#!/bin/sh
# Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
# Tested under FreeBSD 6.x and 7.x
### Set PATH ###
PHP_CGI=/usr/local/bin/php-cgi
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
### no editing below ###
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec $PHP_CGI

Copy above shell script to /websites/home/theos.in/cgi-bin/ as php.cgi. Set permissions:
# chmod +x /websites/home/theos.in/cgi-bin/php.cgi

Restart Apache

Type the following command to restart Apache:
# /usr/local/etc/rc.d/apache22 restart

Test your setup

Place following code at /websites/home/theos.in/http/test.php:

<?php
phpinfo();
?>

Optional: Open port 80 using FreeBSD PF firewall

Add following PF firewall rule to /etc/pf.conf file:

pass in on $ext_if proto tcp from any to 74.86.49.131 port 80 flags S/SA synproxy state

Close and save the file. Reload pf firewall:
# /etc/rc.d/pf restart

Further configuration:

I also recommend installing xcache opcode php cacher and MySQL database server to host dynamic web sites.

Once installed you can upload open source scripts / software such as wordpress, phpBB and others at /websites/home/theos.in/http directory.

Further readings:

  • Apache documentation
  • man pages: ports, make, httpd.conf

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 6 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • Raj Oct 8, 2008 @ 16:13

    Excellent guide.

    Can you provide info for RHEL / CentOS Linux?

  • Brad Mar 25, 2009 @ 0:32

    THIS IS A NIGHTMARE INSTALL ON FREEBSD

    Don’t even try it.
    You will get stuck on Firebird.
    I hate this flippn bird!
    Kill this bird!
    can’t do the install as root because of the bird.
    Can’t do t without this flippn bird.
    KILL FIREBIRD!

  • SoniXAnT May 24, 2009 @ 18:53

    Great guide!!!!

  • Borg Jun 11, 2009 @ 7:52

    /usr/ports/www/mod_fcgid (great port :) )

  • Godnet Sep 22, 2010 @ 15:18

    hi! Can you provide a guide install apache with suphp on freebsd?
    Thanks!

  • Silvio Feb 22, 2011 @ 22:17

    Hello,

    i search a way how can use fastcgi with php-fpm. Is same like spawn-cgi only with php-fpm. With lighttpd is running good, only apache want not really run.

    Has someone a idea here?

    Silvio

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum