Display all Apache configuration files

Various times I’ve had a problem with Apache and I’ve wanted to be able to display all of the configuration files as they are seen when parsed by Apache. As I can never remember now to do it I thought I would document it here.

The mod_info module can be used to do this. Just add the following to one of the hosts in your Apache configuration and then go to the /server-info address on your server.

SetHandler server-info
Order deny,allow
Deny from all
Allow from 192.168.0.1

Don’t forget to secure access to this location by IP address or other method (see deny and allow directives above).

qmail log files on Plesk CentOS server

After a big of digging around on a server today, I followed my nose to where the log files are held.

qmail is started from within /etc/init.d/qmail. Searching for qmail-start reveals two ways in which qmail can be started, the Debian way or the “other” way. As this is CentOS we need the “other” way, which is:

qmail-start "$defauldelivery" splogger qmail &

The splogger argument tells qmail to use the syslog for logging, and CentOS uses rsyslog so look in the file /etc/rsyslog.conf. There you will find the line:

mail.*                    -/usr/local/psa/var/log/maillog

which finally leads us to the path to the qmail log file:

/usr/local/psa/var/log/maillog

Phew, got there at last! 🙂

‘mod_alias’ and ‘mod_rewrite’

I spent a few hours recently trying to debug some Apache Rewrite rules on our development server.

We had a particular directory outside the root of the website that I was using an Alias command to make available. However, within this directory I also wanted to apply a couple of different Rewrite rules.

A simple cachebuster

One of the rules was basically to remove instances of the string nocache_XXXX_ where XXXX was a number which could change (this is usually a date-stamp, such as 20130131 if modified on 31st Jan, 2013). This allows us to easily change the number in the URL which will force the browser to reload the page, avoiding possible caching problems.

For example:
http://www.example.com/nocache_20130131_js/file.js
will be rewritten as
http://www.example.com/js/file.js
If the file.js changes we can modify the number in the url, which will force the browser to load the JavaScript again as it is a different URL, but the web server will return the current “file.js” inside the “js” directory, after the rewrite rule has been applied.

To cut a long story short, I found out that mod_alias and mod_rewrite cannot be used together. Therefore, the workaround in my case was to use just mod_rewrite and use a symbolic link instead of trying to use mod_alias. Note that to allow symlinks to be use, it is also necessary to specify Options FollowSymLinks within the Apache config for that site.

Maybe this will help someone else avoid wasting time trying to get mod_rewrite to work on top of mod_alias.

Executable PHP files

I have a PHP script that I will be running in one of three ways:

  • interactively, from the Linux command line
  • as a cron tab, on a Linux box
  • as a web page, with Apache and mod_php

In order to do this I found that I can do the following:

#!/usr/bin/php -q
<?php 
@ob_end_clean();

if (isset($argv)) {
	define("SHELL",true);

	$options = getopt("c");
	if (isset($options['c'])) {
		echo "Running as a cron job ...\n";
		define("INTERACTIVE",false);
	} else {
		echo "Running interactively ...\n";
		define("INTERACTIVE",true);
	}

} else {
	echo "Running via Apache...
";
	define("INTERACTIVE",false);
	define("SHELL",false);
}

The general idea is:

  1. Line 1: tell the Linux shell that the command, when executed directly, should be interpreted by php, and not the shell that you’re running in (“-q” tells PHP to suppress any HTTP headers).
  2. Line 3. If the script is running via Apache then tell it to remove the first line which has already been output. Use the @ to suppress warnings as ob_end_clean isn’t available when running from the CLI.
  3. Use the $argv array to determine if we are running via CLI, it’s not set when run through mod_php
  4. When executed from cron, we will pass a parameter “-c” to indicate it should run in “non-interactive” mode, use getopt to read the parameters and isset to check if the “-c” parameter was passed.
  5. Now, the SHELL and INTERACTIVE constants can be used to determine if we are in a shell or Apache, and interactive or cron-mode, respectively.

Finally, don’t forget to chmod the script to make it executable.

chmod 700 script.php

Skype command line switches

There are a few Skype command line switches documented on the Skype support site:
https://support.skype.com/en/faq/FA171/can-i-run-skype-for-windows-desktop-from-the-command-line

However, it seems that the list isn’t complete. From a post on the Skype community site (the post doesn’t seem to be available any more) I found the following additions:

This will start Skype with the specified account name and password:
"C:\Program Files\Skype\Phone\Skype.exe" /username:your user name /password:your password

This will start a “portable” Skype installed at “path to skype.exe” with all data being saved in the “Data” folder:
"path to skype.exe\Skype.exe" /datapath:”Data” /removable

Connect to IMAP server with telnet

Every so often I need to be able to check IMAP account settings. This can be done by manually connecting to the IMAP server with telnet.

Once connected, all IMAP commands are preceded with a word of your own choosing, which the server will respond with. This is to enable the client to recognise the reply to each command which it has sent (this would be useful where a real mail client sends various commands to the server without waiting for a reply after each one, and can later identify the reply to each command).

Some useful commands are:

LOGIN [username] [password]
LIST [flags] [folder separator] [search term]
STATUS [mailbox] [flags]
SELECT [mailbox]
FETCH [first]:[last] flags
FETCH [mail number] body[header]
FETCH [mail number] body[text]
LOGOUT

Here is an example of an IMAP conversation between telnet and the server:

telnet: > open imapserver.example.com imap
telnet: Trying 192.0.2.2...
telnet: Connected to imapserver.example.com.
telnet: Escape character is '^]'.
server: * OK Dovecot ready.
client: a1 LOGIN MyUsername MyPassword
server: a1 OK Logged in.
client: a2 LIST "" "*"
server: * LIST (\HasNoChildren) "." "INBOX"
server: a2 OK List completed.
client: a3 EXAMINE INBOX
server: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
server: * OK [PERMANENTFLAGS ()] Read-only mailbox.
server: * 1 EXISTS
server: * 1 RECENT
server: * OK [UNSEEN 1] First unseen.
server: * OK [UIDVALIDITY 1257842737] UIDs valid
server: * OK [UIDNEXT 2] Predicted next UID
server: a3 OK [READ-ONLY] Select completed.
client: a4 FETCH 1 BODY[]
server: * 1 FETCH (BODY[] {405}
server: Return-Path: sender@example.com
server: Received: from client.example.com ([192.0.2.1])
server:         by mx1.example.com with ESMTP
server:         id <20040120203404.CCCC18555.mx1.example.com@client.example.com>
server:         for <recipient@example.com>; Tue, 20 Jan 2004 22:34:24 +0200
server: From: sender@example.com
server: Subject: Test message
server: To: recipient@example.com
server: Message-Id: <20040120203404.CCCC18555.mx1.example.com@client.example.com>
server: 
server: This is a test message.
server: )
server: a4 OK Fetch completed.
client: a5 LOGOUT
server: * BYE Logging out
server: a5 OK Logout completed.

This post gives you more details on syntax and examples of more of the IMAP commands:
http://wiki.mediatemple.net/w/Email_via_IMAP_using_Telnet

CloudFlare

I came across a service called CloudFlare the other day when I was investigating Content Delivery Networks (CDNs). They seem to, at the basic level, offer a high-performance, CDN for websites with no coding required, and also give you protection from Distributed Denial Of Service (DDOS) attacks. There is a paid model to use too, but the basic free service looks like something that many websites could benefit from.

I’m still trying out the service, but at first glance it seems to be pretty good. I’ve already got www.andrewc.com running with it and I’m hoping to move some over sites over in the next few days.

Classic ASP with UTF-8

I had a problem today with a very old Classic ASP page which wasn’t dealing with accented characters properly. A quick StackOverflow question came up with this:

Put the following ASP code in your page:

Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=UTF-8"
Response.CodePage = 65001
Response.CharSet = "UTF-8"

And make sure that the charset in the meta tag on the page also specifies UTF-8:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />