NameVirtualHost *:80 has no VirtualHosts

February 21st, 2009 Category: Apache

If the message “NameVirtualHost *:80 has no VirtualHosts” appears during Apache start and no one of your virtual hosts is recognized  it can be solved by changing the file /etc/apache2/sites-enabled/000-default and /etc/apache2/ports.conf. Within both files I’ve changed declarations “*:80″ to “*”:

/etc/apache2/sites-enabled/000-default

<VirtualHost *>

/etc/apache2/ports.conf

NameVirtualHost *

Note that all your virtual host configurations also have to be defined with “<VirtualHost * >”.

Howto Restart Apache Graceful

January 24th, 2009 Category: Apache

Sometimes when changing settings in your Apache config, adding virtual hosts and so on it’s needed to reload/restart the Apache daemon. Usually this is done with:

/etc/init.d/apache2 restart

But there two big disadvantages using this method:

  1. If your config is invalid, Apache will shutdown and not start again
  2. Users currently accessing your server will recognize that Apache will not answer request for a short time

Indeed there is a much more better method to restart the Apache “gracefully”:

apache2ctl -t
Syntax OK


apache2ctl -k graceful

The first command will tell Apache to check your config. If everything is correct, you will get “Syntax OK”, otherwise the error will be reported but Apache will still run with the old configuration.

The second command will tell Apache to restart gracefully with the advantage that currently open connections are not aborted and the users will not be annoyed.

Howto Beautify Ugly .PHP URL’s

January 17th, 2009 Category: Apache, PHP/MySQL

You probably know this ugly .PHP links with many parameters like

http://technitip.net/test.php?param1=p1&param2=p2&param3=p3


I really don’t like this look. Doesn’t the following URL look much better?

http://technitip.net/test/p1/p2/p3


I think yes. So here is tutorial how you can beautify your links using Apache and mod_rewrite.

The Test Script

We assume that mod_rewrite is loaded in your Apache config and generate a simple PHP script called “test.php”:

<?php
echo "Parameter 1: " . $_REQUEST['param1'] . "<br />";
echo "Parameter 2: " . $_REQUEST['param2'] . "<br />";
echo "Parameter 3: " . $_REQUEST['param3'] . "<br />";
?>

This script gets the parameters param1, param2, etc. which have been given to the PHP page and prints them out. To check it we put the URL into the browser:

http://technitip.net/test.php?param1=p1&param2=p2&param3=p3

And get the result:

Parameter 1: p1
Parameter 2: p2
Parameter 3: p3

Simple Rewrite

Fine, passing parameters to our PHP script is working but still looking ugly. Now we generate a .htaccess file in the root directory of your web directory:

RewriteEngine On
RewriteBase /
RewriteRule ^(test)/?(.*)$ test.php [L,QSA,NC]

What will happen? test.php will be redirected to test and all parameters will be parsed like before, we can test it:

http://technitip.net/test?param1=p1&param2=p2&param3=p3&param4=p4

First Parameter Rewrite

Nice, but not yet what we really want. So we put another line into the .htaccess (before our first RewriteRule, not after! It’s important):

RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2 [L,QSA,NC]
RewriteRule ^(test)/?(.*)$ test.php [L,QSA,NC]

For every parameter we need a new expression /([a-z0-9]+)(/[^/]+)?/

This will convert the line /test/p1 into test.php?param1=p1. Note that only the characters a-z und 0-9 are allowed as parameters. For the first parameter $2 is used.

Second Parameter

We repeat this step for every further parameter we need to hand over, $4 is used for the second parameter (not $3!):

RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2&param2=$4 [L,QSA,NC]
RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2 [L,QSA,NC]
RewriteRule ^(test)/?(.*)$ test.php [L,QSA,NC]

Three Parameters

Or with 3 parameters. Note: Since the .htaccess is parsed from top to bottom the rewrite rule with the highest number of parameters must be located at the top of the .htaccess. $6 is used for the third parameter!

RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2&param2=$4&param3=$6 [L,QSA,NC]
RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2&param2=$4 [L,QSA,NC]
RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2 [L,QSA,NC]
RewriteRule ^(test)/?(.*)$ test.php [L,QSA,NC]

We test again with 3 parameters:

http://technitip.net/test/p1/p2/p3


Parameter 1: p1
Parameter 2: p2
Parameter 3: p3

Looks better, indeed.

Hint

Be sure you have enabled “Options FollowSymLinks” or Options “SymLinksIfOwnerMatch” in your Apache or virtual host config. Otherwise mod rewrite will not work!

Domain Redirects

January 16th, 2009 Category: Apache

Avoid Double Content

There are two reasons to redirect e.g. mydomain1.de to www.mydomain1.de:

  1. It avoids detecting search machines “double content”
  2. I personally prefer having one unique URL for a homepage, also if a user enters mydomain1.de he will be redirected to www.mydomain1.de
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^mydomain\.de$ [NC]
RewriteRule ^(.*) http://www.mydomain.de/$1 [L,R=301]

The redirect is defined as 301 “permanent”. Please note that these rewrite settings only work if the correct setting for “AllowOverride” is done within the Apache settings of your host or virtual host configuration.

Redirect Domain to another Domain

If you want to redirect a complete domain, here mydomain1.de including subdomains to another domain e.g. mydomain2.de put the following code into the .htaccess of the web root directory of mydomain1.de:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)mydomain1\.de$
RewriteRule ^(.*)$ http://www.mydomain2.de/$1 [R=301,L,NC]

Using this rewrite condition all hosts *.mydomain1.de including mydomain1.de will be redirected to www.mydomain2.de. Everything behind .de/ will be kept.

Furthermore the redirect is defined as 301 which means permanent redirect. This will avoid that search machines like Goole will treat you with “double content”.

Hint

Be sure you have enabled “Options FollowSymLinks” or Options “SymLinksIfOwnerMatch” in your Apache or virtual host config. Otherwise mod rewrite will not work!

Working with Apache/2.2.3

Apache Tuning Part 3

December 30th, 2008 Category: Apache

Some further settings which worked well for a server (1GB RAM, single CPU) with max. 120 users simultaneously and doing some video streaming. Settings are found in /etc/apache2/apache.conf

Timeout 30

KeepAlive On

MaxKeepAliveRequests 100

KeepAliveTimeout 5

<IfModule mpm_prefork_module>
StartServers         16
ServerLimit         512
MinSpareServers      16
MaxSpareServers      128
MaxClients          256
MaxRequestsPerChild 55555
</IfModule>

HostnameLookups Off

ServerTokens Prodme
ServerSignature Off
UseCanonicalName Off
TraceEnable Off

HostNameLookups Off are recommended in any case.