Skip to content

Instantly share code, notes, and snippets.

@ericmagnuson
Last active March 29, 2021 01:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ericmagnuson/3846371 to your computer and use it in GitHub Desktop.
Save ericmagnuson/3846371 to your computer and use it in GitHub Desktop.
Compiling and serving LESS on-the-fly with PHP & Apache
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*\.min.css) less.php?min=yes&path=$1
RewriteRule (.*\.css) less.php?min=no&path=$1
</IfModule>
<?php
// Path to your lessc executable. This can be installed
// easily using npm. Just run `npm install less`. See
// the LESS site for more info: http://lesscss.org
$path_to_lessc = exec('which lessc');
// Enable minified output for file requests ending
// in '.min.css'. If disabled, files ending in '.min.css'
// will still compile – just not as a minified version
$enable_minified = TRUE;
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
$output = $exit_status = NULL;
$css = $_GET['path'];
$min = $_GET['min'];
$less = str_replace(".css", ".less", $css);
$less = str_replace(".min", "", $less);
if (file_exists($less))
{
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
header("Content-type: text/css");
// Minify if requested
if ($enable_minified && ($min == 'yes'))
$less_command = $path_to_lessc . " -x " . realpath($less) . ' 2>&1';
else
$less_command = $path_to_lessc . " " . realpath($less) . ' 2>&1';
// Run the command and catch the output and the error code
exec($less_command, $output_lines, $exit_status);
$was_successful = ($exit_status == 0) ? TRUE : FALSE;
if ($was_successful)
{
// Just output the CSS
foreach ($output_lines as $output_line) echo $output_line . "\n";
}
else
{
// Output the errors while stripping out any command line color codes
$output_lines = preg_replace('/\[[0-9][0-9]?m/', '', $output_lines);
foreach ($output_lines as $output_line) echo $output_line . "\n";
}
}
else if (file_exists($css))
{
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
header("Content-type: text/css");
echo file_get_contents($css);
}
else
{
header("HTTP/1.0 404 Not Found");
echo "Not Found";
}
?>
@ericmagnuson
Copy link
Author

Install

  1. Upload the two files, less.php and .htaccess, to your public styles directory (e.g., /css/).
  2. In less.php, set the correct path to the lessc LESS compiler installed on your server.

Usage

Requests sent to CSS files (http://example.com/css/yourstyles.css) will be forwarded to the script, and if a LESS file exists in the same directory, with the same name but ending with a .less extension instead (http://example.com/css/yourstyles.less), it will be compiled and served as CSS.

If there is no matching LESS file found, the script will instead look for and serve the ordinary CSS file as requested. This allows you to store both normal CSS files and LESS files in your styles directory.

Requirements

  • mod_rewrite
  • PHP
  • lessc LESS compiler (If you installed LESS server-side using npm install less, you have it – see lesscss.org for more info.)

@ericmagnuson
Copy link
Author

Note:

If using MAMP, there are a few problems executing lessc
through PHP. You may need to do the following:

  1. To fix envvar problems, open the "envvars" file in
    /Applications/MAMP/Library/bin/, comment out the two
    lines (DYLD_LIBRARY_PATH), and add PATH=${PATH}:/usr/local/bin
    to the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment