LESS CSS Apache module

Waleed Qadi

This is an Apache2 module that is written to compile LESS code inside the CSS files when they are requested so the process will be transparent from any script using CSS no matter what it’s language is.

This Apache2 module is written using C.

 

How it works:

 

Easy! When a request is sent to any CSS file this request will be handled by this module, the module will compile the CSS file and create another file from this CSS file with .less extension, then the module will pass the compiled version as a response to the request.

Here is an example:

Request: http://localhost/themes/css/style.css <=== this CSS file contains LESS codes inside it the module will compile style.css and save the compiled version to:
Response: http://localhost/themes/css/style.css.less <==== this is the compiled version from style.css then the module will return the compiled version as response for the file "style.css"

 

Dependencies:

 

Apache2

  • To install, use:
    sudo apt-get install apache2

Less CSS

  • To install, use:
    sudo apt-get install rubygems1.8​
    sudo gem install rubygems-update​
    sudo update_rubygems
    ​sudo gem install less

 

Installation:

Download the project from here: https://github.com/waleedq/libapache2-mod-less_beta1/tarball/master

copy /bin/mod_less.so to /usr/lib/apache2/modules/mod_less.so
chmod 644 /usr/lib/apache2/modules/mod_less.so
copy /bin/less.load to /etc/apache2/mods-available/less.load
copy /bin/less.conf to /etc/apache2/mods-available/less.conf

You can edit the less.conf file to change when this module is going to be invoked

For example: This conf with "\.css$" will invoke the module when any .css file is requested:

<FilesMatch "\.css$"> SetHandler less </FilesMatch> 

changing "\.css$" to "\.less$" will invoke the module only for requested files with .less extension.

 

Compile from source:

You can clone the code from git://github.com/waleedq/libapache2-mod-less_beta1.git

GitHub repo link: https://github.com/waleedq/libapache2-mod-less_beta1

Source can be found in /src/mod_less.c to compile it you will need the apxs2 tool which can be found in the apache2-prefork-dev package.

sudo apt-get install apache2-prefork-dev

After installing apxs2 tool, cd to the src directory and execute this command

sudo apxs2 -c -i mod_less.c 

Compiled file “mod_less.so” will be automatically copied to /usr/lib/apache2/modules/mod_less.so and chmoded. you only need to add the less.load and less.conf as mentioned in the Installation section above.


Note: feel free to fork and contribute to this project.