Apache mod_macro for Multi Vhost Develpment

To quickly set up virtual hosts in Apache mod_macro has prooven to be a little gem. As the name suggests it allows you to use macros in the Apache configuration files. With mod_macro enabled you can add a new vhost by simply adding a single line to the vhost config file.

Here a brief overview on how to get it working.

Install and enable the module (Ubuntu/Debian):

root@michael:~$ apt-get install libapache2-mod-macro
root@michael:~$ a2enmod libapache2-mod-macro

Set up the macro in the development config file (/etc/apache2/sites-enabled/development):

<Macro VHOST $host $path>
    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerAlias $host

        DocumentRoot $path

        <Directory $path>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/$host.error.log
        CustomLog ${APACHE_LOG_DIR}/$host.access.log combined

        LogLevel warn

        RewriteEngine On
        RewriteLog ${APACHE_LOG_DIR}/$host.rewrite.log
        RewriteLogLevel 3

    </VirtualHost>

    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
        ServerAlias $host

        DocumentRoot $path

        <Directory $path>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/$host.ssl.error.log
        CustomLog ${APACHE_LOG_DIR}/$host.ssl.access.log combined

        LogLevel warn

        RewriteEngine On
        RewriteLog ${APACHE_LOG_DIR}/$host.ssl.rewrite.log
        RewriteLogLevel 3

        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/server.crt
        SSLCertificateKeyFile  /etc/apache2/ssl/server.pem
    </VirtualHost>
</Macro>

Use VHOST foo.local /src/foo
Use VHOST bar.local /src/bar

The Use VHOST foo.local /src/foo line adds a new vhost to the config with the server alias foo.local and the document root set to /src/foo.

Add an entry for each vhost to your hosts file (Windows: %windir%\System32\drivers\etc, Linux: /etc/hosts).

192.168.0.1 foo.local
192.168.0.1 bar.local

After restarting Apache you should be able to access http://foo.local and http://bar.local from your browser.

Apache mod_macro for Multi Vhost Develpment

Apache mod_substitute for Dev Server Installs

I had to set up a development site for a client today were the URLs were hardcoded in the templates and CSS files. The client wasn’t willing to pay for making the base URL a config option. Adding the domain to the hosts file wasn’t an option because several people coming from different IPs needed to be able to use the dev site. To avoid people getting bounced back to the live site when clicking links and preventing them from loading resources from the live site via the hardcoded paths in the CSS files another solution was necessary. After looking around for a while I found mod_substitute which does exactly what I want. Before sending the output to the browser Apache replaces strings in the output via regular expressions. For performance reasons you don’t want to run this on your live site but it shouldn’t be noticeable during development.

To enable the substitution for HTML/CSS and JS files simply install mod_substitute and add this to your vhost:

<Location />
    AddOutputFilterByType SUBSTITUTE text/html
    AddOutputFilterByType SUBSTITUTE text/css
    AddOutputFilterByType SUBSTITUTE text/js
    Substitute s/www\.foo\.com/dev.foo.com/n
</Location>
Apache mod_substitute for Dev Server Installs

PHP Remote Debugging in VIM with XDebug

XDebugs remote debugging feature is very helpful when trying to trace code in an unknown application or debugging a tricky problem. It works with most IDEs but can also be set up to work with Vim.

To get it working you need to install the XDebug Apache module:

root@michael:~# apt-get install php5-xdebug

and configure the XDebug config (/etc/php5/conf.d/xdebug.ini) for remote debugging:

xdebug.remote_enable="1"
xdebug.remote_host="localhost"
xdebug.remote_port="9000"
xdebug.remote_mode="req"
xdebug.remote_autostart="0"
xdebug.remote_log="/tmp/xdebug_remote.log"

The remote_host needs to be the hostname or IP where Vim is running. The Vim debugging plugin listens on port 9000. Make sure your firewall settings allow for connections to this host and port. The req mode starts the debugger for every request.

Next you need to download and install the DBGp Remote Debugger Interface plugin for Vim. There are several different versions available. Make sure to use the latest version by Hadi Zeftin. To do so, download the file and copy its contents into your Vim plugin directory (~/.vim/plugin/). If you want to make the plugin available to all users on your system you can also copy the plugin files to the central vim plugin directory (/usr/share/vim/vim73/plugin/).

You can make various adjustments if you like. I prefer to set the debugger timeout to 20s and the array/object depth to 10. You can change this by adding the following line to your ~/.vimrc:

let g:debuggerTimeout = 20
let g:debuggerMaxDepth = 10

Next you need enable the debugging mode by calling your script with the ?XDEBUG_SESSION_START=1 query parameter.

http://www.example.com/?XDEBUG_SESSION_START=1

You only need to do this once and debugging will be enabled for 1 hour. You can change this time by setting a different xdebug.remote_cookie_expire_time in your xdebug.ini. There are also various browser plugins (Chrome, Firefox) available to enable and disable the debugging mode but in my opinion the query parameter is easy enough.

Once this is done you can open the file you want to debug in Vim and hit F5. You should see a status message like this:

waiting for a new connection on port 9000 for 20 seconds...

Next call your script in your browser and Vim should switch into debugging mode.

You can trace the code by using F2-F4. F12 lets you inspect the property under your cursor. Once you are in debug mode you can also open other files, set breakpoints and run the program till hitting the breakpoint by hitting F5. To set a breakpoint switch to command mode and enter Bp (:Bp).

PHP Remote Debugging in VIM with XDebug