Configure mod deflate for Apache 2.2.x

Mod Deflate comes built into Apache, but is not enabled by default. This tutorial will explain the simplest way of enabling it and setting which mime times to compress. Mod Deflate will increase your server load, but decreases the amount of time that clients are connected and can usually reduce the page size by 60 to 80 percent.

 

Loading Mod Deflate

First make sure that you are loading mod_deflate.so, this line should be at the top of your httpd.conf file and is usually loaded by default.

LoadModule deflate_module libexec/apache22/mod_deflate.so

Mod Deflate Settings

Second create a new config file to keep the deflate options in.

# ee /usr/local/etc/apache22/Include/mod_deflate.conf

This file will be included in the main httpd.conf file. Inside the file add the following:

AddOutputFilterByType DEFLATE text/html text/plain
#Highest 9 - Lowest 1
DeflateCompressionLevel 9

#Optional
#Skip browsers with known problems
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html

#Optional
#Logging
DeflateFilterNote ratio
LogFormat '"%r" %b (%{ratio}n) "%{User-agent}i"' deflate
CustomLog /usr/local/www/logs/deflate_log deflate

The compression level can be adjusted. 9 gives the highest compression, the smallest file sizes, and also uses the most CPU cycles.

Once you have this config file added restart apache.

# apachectl graceful

Testing Mod Deflate

To test your compression you can compare the file size by looking at the apache logs or a tool use as port80 tools.

Compressing additional types

You can compression additional mime types coming out of your server by adding them to the AddOutputFilterByType list in the mod_deflate.conf file. Some you might want to add are text/css, application/x-javascript, text/xml, and any others that are would benefit from being compressed.

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 extensions port.

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

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.

Installing and setting up a MySQL server

Installing the MySQL port

The MySQL database server can be installed with the following commands

# cd /usr/ports/databases/mysql41-server
# make install

A start up script is installed to /usr/local/etc/rc.d and the databases are stored in /var/db/mysql

Starting MySQL

/etc/rc.conf must contain the following line to allow the MySQL server to start

mysql_enable=”YES”

Once this line is there you can run the start up script with

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

Setting the root password

After MySQL has start you need to set passwords on both root accounts and the two anonymous accounts. By default these are left blank and give full access to the database server to anyone.

To set a password on the anonymous accounts use

# mysql -u root
# SET PASSWORD FOR ”@’localhost’ = PASSWORD(‘newpwd’);
# SET PASSWORD FOR ”@’host_name’ = PASSWORD(‘newpwd’);

To set a password for the root account use

# mysql -u root
# SET PASSWORD FOR ‘root’@'localhost’ = PASSWORD(‘newpwd’);
# SET PASSWORD FOR ‘root’@'host_name’ = PASSWORD(‘newpwd’);

For more information on setting up these initial accounts check out the MySQL manual.

Managing your MySQL server with phpMyAdmin

phpMyAdmin is an amazing tool for managing your new MySQL database. It has a web interface and allows you to perform SQL queries, create new databases, add users, change priveldges, and back up the database. Go here to learn about installing phpMyAdmin. It requires both the Apache Web Server and PHP.