Wednesday, April 25, 2012

Twitter Bootstrap, Less and PHP Setup on IIS 7.5 and Windows 7

It’s certainly not news to me that setting up any technology on Windows that’s typically associated with the LAMP stack can be a bit tricky. Today, I found myself wanting to run a Twitter Bootstrap site that was built in PHP. I use Windows 7 and IIS 7.5 on my laptop, so I had to get through a few minor setup considerations. Here are some of them—hopefully, this will help others get going quickly.

Essentially, you need to download PHP (http://windows.php.net/download or http://php.iis.net/), configure it to work with IIS. Download Twitter bootstrap, configure it to work with IIS… etc. etc. etc. Here are some quick tips for getting going. The PHP.net article on Enabling FastCGI support in IIS was helpful. There are some steps that are optional, so you might want to first check whether the handler mapping is already in IIS, but for the most part, it’s a useful page to follow. For example, installing the CGI component for IIS is required. Without that feature, you won’t be able to run any PHP code in IIS.

If you want to use the short form of the PHP server-side tag (“<? … ?>” instead of “<?php … ?>” ), you’ll need to turn the option on in your php.ini configuration file. Since this file is typically being used, you can’t just open it and save change. The easiest way to edit it is to search for the php.ini file, copy it to some other location, then open the file and search for the “short_open_tag =” line. Change it to On to use the short form and then copy the PHP.ini file back to it’s original location and restart IIS.

; This directive determines whether or not PHP will recognize code between
; <? and ?> tags as PHP source which should be processed as such. It's been
; recommended for several years that you not use the short tag "short cut" and
; instead to use the full <?php and ?> tag combination. With the wide spread use
; of XML and use of these tags by other languages, the server can become easily
; confused and end up parsing the wrong code in the wrong context. But because
; this short cut has been a feature for such a long time, it's currently still
; supported for backwards compatibility, but we recommend you don't use them.
; Default Value: On
; Development Value: Off
; Production Value: Off
;
http://php.net/short-open-tag
short_open_tag = On

To test whether PHP is working, you can run <?php phpinfo() ?> or <? phpinfo() ?> in a .php file under wwwroot. Call it whatever you want .php. Then open the page (e.g., http://localhost/phpinfo.php) and see what’s displayed. If everything is working, you’ll see a page that shows verbose information about your environment (see screen below). If you try to run the command with the short form notation, and you haven’t enabled that option, the page will be completely blank.

image
- PHPINFO() shown in Internet Explorer

If you have any issues getting Less or Bootstrap files to load, open up your site in Chrome or IE and use the developer tools to inspect what’s going on with the scripts.

image
- Chrome developer tools showing Resources (scripts)

For example, I had to add an IIS mime type for “.less” files to get Less to work on my machine (IIS 7.5). Without setting the mime type, I was getting a 404.3 error for the file bootstrap.less. This isn’t difficult to do, just open Internet Information Services (IIS) Manager and find the “Mime Types” feature under “HTTP Features.” Make sure you’ve selected the website in the left panel before you look for this option on the right side. I just add a new mime type for .less files and set it to “text/plain” and everything worked.

image