Skip to content

May 8, 2010


Quick steps on setting up Apache + SSL + PHP + JSON on FreeBSD 8.0

by Joe Kuan

There are many webpages that offer comprehensive information on setting up Apache on FreeBSD, such as However, I can’t find any tutorial just list the quick steps to get me going in minutes. So here is one.

1. Install Apache 2.2 with ‘sysinstall’ utility.

All the apache configuration files will be installed in /usr/local/etc/apache22/.

2. Edit /boot/loader.conf and add the following lines:

(If you don’t do this step, you will get ‘http not ready’ and ‘data not ready’ error.)


3. Enable SSL

Edit /usr/local/etc/apache22/httpd.conf and uncomment the ‘Include …’ line.

# Secure (SSL/TLS) connections
Include etc/apache22/extra/httpd-ssl.conf

4. Enable Virtual Hosts

Edit httpd.conf and uncomment the line

# Virtual hosts
Include etc/apache22/extra/httpd-vhosts.conf

5. Redirect to SSL (Optional)

Add the following lines in extra/httpd-vhosts.conf. (Presume you know how to setup SSL Certificate)

<virtualhost *:80="">
RewriteEngine  on
RewriteCond    %{SERVER_PORT}  !^443$
RewriteRule    ^/(.*)          https://%{SERVER_NAME}/$1 [L,R,NC]

6. Enable PHP module

Edit httpd.conf and add the following lines:

# PHP5
LoadModule php5_module        libexec/apache22/

7. Install PHP Apache module

You won’t find the PHP5 apache module from sysinstall. The module can be only installed via directory /usr/ports/lang/php5

cd /usr/ports/lang/php5
make config
make install
cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini

8. Install PHP’s JSON package (Optional)

If you need to support web applications using Ajax and PHP, it is likely you need the PHP’s JSON package which is not included PHP5 standard installation. Use ‘sysinstall’ to install pear-Services_JSON-1.0.0. By installing this package doesn’t automatically provide json_encode and json_decode functions. See one of the contribution notes on the json_decode manual page. Here is the example.

if ( !function_exists('json_decode') ) {
  function json_decode($content, $assoc=false) {
    require_once 'JSON.php';
    if ( $assoc ) {
      $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
    } else {
      $json = new Services_JSON;
    return $json->decode($content);

if ( !function_exists('json_encode') ) {
  function json_encode($content) {
    require_once 'JSON.php';
    $json = new Services_JSON;
    return $json-&gt;encode($content);

9. Start Apache at system startup

Add the following line to /etc/rc.conf


I work for iTrinegy. Here are my other blogs on FreeBSD

2 Comments Post a comment
  1. Akzhan
    Oct 16 2010

    Simply install
    cd /usr/ports/lang/php5-extensions/
    make && make install

    and select JSON support.

  2. beginer
    Apr 3 2011

    Are the installations compiled from the source codes by the installing machine or they were post-compiled binaries from the “Packages” distribution list? I am asking this questions because I am a stern believer of make install clean” paradigm kind of guy :)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: