Enable .phar handling in your web server

PHP allows us to pack up whole applications in a single .phar file, but no web server software today lets PHP handle .phar files, giving you a download dialog or displaying the text contents of the file. ​

News about this topic in 2017: Webserver .phar handling lands in distributions.

mod_php on Apache​

Debian​

Open /etc/apache2/mods-enabled/php5.conf and change the line ​

<FilesMatch ".+\.ph(p[345]?|t|tml)$">

into ​

<FilesMatch ".+\.ph(ar|p[345]?|t|tml)$">

Corresponding bug report and patch: #639268 ​

Fedora​

Edit /etc/httpd/conf.d/php.conf and add the line ​

AddHandler php5-script .phar

Corresponding bug report: #1117140 ​

Mac OS X​

Open /etc/apache2/other/php5.conf and add the line ​

AddType application/x-httpd-php .phar

below the similar line ending with .php. ​

PHP-FPM​

nginx​

Use the following configuration to make PHP-FPM handle .php and .phar files: ​

location ~ \.(php|phar)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
    set $path_info $fastcgi_path_info;
    fastcgi_param PATH_INFO $path_info;
    ...
}

Be aware of PHP bug #67587. You will get an endless redirection loop for all requests with a non-empty PATH_INFO (file.phar/foo). The fix is already committed and will released with PHP 5.6.0, 5.5.15 and 5.4.31. ​

Written by Christian Weiske.

Comments? Please send an e-mail.