About Jeffrey Barke

Web worker

How to make MAMP recognize the .shtml extension and read server-side includes

Update (24 August 2011): Due to my recent “restart,” I wanted to see if these instructions still work with MAMP 2.0.1. I followed them as written and was able to get server-side includes working the first time with no issue.

Making MAMP recognize server-side includes is simple:

  1. Stop the MAMP servers.
  2. Open MAMP's httpd.conf (located at /Applications/MAMP/conf/apache/httpd.conf) in a plain text editor.
  3. Find the line DirectoryIndex index.html index.php and change it to DirectoryIndex index.shtml index.html index.php
  4. Uncomment the following two lines: AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
  5. Find the lines <Directory />
    Options Indexes FollowSymLinks
    AllowOverride All
    </Directory>
    and change to <Directory />
    Options Indexes FollowSymLinks
    Options +Includes
    AllowOverride All
    </Directory>
  6. Restart the MAMP servers.

Using the PHP Fileinfo extension

In my last post, I described how to install the Fileinfo PECL module on a Media Temple (dv) server, but I didn't really talk about what it does, why one might want to install it and how to use it.

The methods/functions in the Fileinfo PHP extension "try to guess the content type and encoding of a file by looking for certain magic byte sequences at specific positions within the file. While this is not a bullet proof approach, the heuristics used do a very good job."1 (Note that this module replaces the Mimetype module.)

Continue reading

Installing the Fileinfo PECL module on a Media Temple dedicated virtual (dv) server

This post describes how to install the PECL Fileinfo extension on a Media Temple dedicated virtual (dv) 3.5 server. A future post will describe what the Fileinfo extension does and why you might want to install it.

As of PHP 5.3.0, the Fileinfo extension is enabled by default and is no longer maintained at PECL. However, the Media Temple (dv) 3.5 service only includes PHP 5.2.6, so we’ll need to manually install it.

Please note that prior to installing Fileinfo, you’ll need to make sure that root access has been enabled on your (dv) and that the developer tools have been installed. Both of these actions can be completed from the Media Temple AccountCenter. For more information on enabling root access, please review the first two articles in the references/more info section below. The third link is to an article on the developer tools.

Once you’re sure that you have root access and that the developer tools are installed, SSH into your (dv). If Media Temple had the PECL binary installed, we could easily install Fileinfo by using the following statement from the command line: pecl install fileinfo.

Since we can’t, we’ll need to install Fileinfo from source. As the root user, enter the following statements into the terminal:

mkdir /var/pecl-install
cd /var/pecl-install
wget http://pecl.php.net/get/Fileinfo-1.0.4.tgz
tar -zxf Fileinfo-1.0.4.tgz
cd Fileinfo-1.0.4
phpize
./configure
make
make install
cd /var
rm -f -r pecl-install

First, we download and unpack the extension in our working directory /var/pecl-install. Next we phpize the contents (i.e. prepare the build environment for a PHP extension). Once that’s done, we perform a standard configure and make routine to install the module. The final two commands remove our working directory from the server.

The outcome is that our extension, fileinfo.so, should be created and exist in /usr/lib/php/modules. However, PHP still doesn’t know about it. To activate the extension we need to add a line to our php.ini file.

From the command line, enter vi /etc/php.ini. Scroll down until you hit the extensions section of the file and then press i to enter vi’s insert mode. Add extension=fileinfo.so and then press <esc> to exit insert mode and return to command mode. Type :wq<return> to exit vi and save your changes.

The final step to activate the Fileinfo extension is to restart the web server. From the command line enter:

/etc/init.d/httpd stop
/etc/init.d/httpd start

At this point, if you were to run phpinfo() on your server, you’d see that fileinfo is listed among the loaded modules, and if you tried to use any of the Fileinfo functions, you’d find they’d run without error.

However, if you actually try to use Fileinfo to determine the MIME type of a file, you’ll discover it still doesn’t work! The reason why is that Fileinfo depends on a "magic" file to determine the MIME type and, on a Media Temple (dv), it cannot access this file without our next (and final) step.

By default, Media Temple restricts PHP from accessing files outside of httpdocs using the open_basedir directive. However, to use Fileinfo, we have to let PHP access the directory that contains the "magic" file, in this case /usr/share/file/.

So, from the command line as root, enter (replacing your-domain.com with your actual domain!):

cd /var/www/vhosts/your-domain.com/conf/
vi vhost.conf

Enter vi's insert mode and add the following lines to the empty vhost.conf file:

<Directory "/var/www/vhosts/your-domain.com/httpdocs">
php_admin_value open_basedir "/var/www/vhosts/your-domain.com/httpdocs:/usr/share/file:/tmp"
</Directory>

Exit vi (saving your changes!) and reconfigure the server to look for the new vhost.conf file by entering:

/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=your-domain.com

Restart the web server once again:

/etc/init.d/httpd stop
/etc/init.d/httpd start

And that's it! Fileinfo should be now be successfully installed and activated! For more info on changing the open_basedir configuration, please see the last link in the resources/more info section below.

References/More info

  1. Media Temple KB. (dv) An introduction to the root user
  2. Media Temple KB. Disabling SSH Login for root user
  3. Media Temple KB. (dv) 3.5 Tech Specs – Developer’s Tools package listing.
  4. Jelly and Custard. Installing PECL Modules
  5. Media Temple KB. How can I edit php.ini on the (dv) & (dpv) Dedicated-Virtual Servers?
  6. Media Temple KB. (dv) HOWTO: Enable PEAR/Set open_basedir and include_path.

CAST()ing a sort in MySQL

I was asked to sort a table of lease data by floor in descending order today—simple, right? But after updating the query with ORDER BY floor DESC, I noticed the results were wrong. The 9th floor was always at the top and the 10th floor and above were between the second and first floors.

It was immediately obvious—the floor field was not stored as numeric data, but as character data. This struck me as odd, so I investigated the DB structure and values. The floor field was definitely being stored as character data, but why? The reason: the client wanted to store certain floors as LL.

So given this structure, how could I quickly and easily sort the floors? The answer: MySQL’s CAST function. By casting the floor field as an integer, the numeric floors would sort correctly. Even better, the character data LL would cast to 0, preserving a correct sort.

I updated the query with ORDER BY CAST(floor as UNSIGNED) DESC and obtained the desired results. Learn more about MySQL’s cast functions and operators.