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 * >”.
Written on February 21, 2009 | Posted in
Apache |
Leave a comment
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:
- If your config is invalid, Apache will shutdown and not start again
- 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.
Written on January 24, 2009 | Posted in
Apache |
Leave a comment
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¶m2=p2¶m3=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¶m2=p2¶m3=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¶m2=p2¶m3=p3¶m4=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¶m2=$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¶m2=$4¶m3=$6 [L,QSA,NC]
RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2¶m2=$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!
Written on January 17, 2009 | Posted in
Apache,
PHP/MySQL |
Leave a comment
January 16th, 2009 Category: Apache
Avoid Double Content
There are two reasons to redirect e.g. mydomain1.de to www.mydomain1.de:
- It avoids detecting search machines “double content”
- 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
Written on January 16, 2009 | Posted in
Apache |
Leave a comment
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.
Written on December 30, 2008 | Posted in
Apache |
Leave a comment
December 30th, 2008 Category: Apache
Let’s assume your web server has high disc IO. Every access to a .PHP, .HTML, .JPG, .GIF, etc wil cause a log entry which will be stored on hard disc. In such a case it helps to disable all apache loggings.
You can check the opened log file from apache using the command:
lsof |grep apache|grep log
Oh yes, if you serve lot’s of domains on your server this list will be quite long. To disable logging just uncomment the corresponding lines e.g. in your virtual host definition:
#ErrorLog /var/log/apachd2/mydonain.de-error_log
#CustomLog /var/log/apache2/mydomain.de-access_log combined
Afterwards check your apace configuration for errors:
apache2ctl -t
In case of syntax OK just restart your apache server:
apache2ctl -k graceful
This command is qute because it does a “graceful” restart without anoying users accesing your server right now.
Hint
How to check if my disc IO is high?
Use the command “vmstat 1″ and check the section “IO” (what else…). You will see bi and bo which means blocks received and blocks sent. In case the numbers are high, you will have high disc IO.
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 659476 41604 8732 347304 3 3 37 41 20 14 19 7 58 16
3 0 659476 41604 8732 347336 0 0 0 0 410 314 7 0 93 0
0 0 659476 41464 8740 347328 0 0 0 424 522 321 11 1 86 2
Written on December 30, 2008 | Posted in
Apache |
Leave a comment
December 30th, 2008 Category: Apache
This article applies to apache version 2.2.3 also it will work for other versions.
Apache needs a lot of memory, this will decrease performance on a machine with e.g. 1 GByte memory and about 60 users accessing the server simultaneously a lot. There is a simply method to save memory: disable all unused modules. Yes, it really helps!
Why does it help? Because every apache process started – and a lot will be started when several users are accessing the server – will load all enabled modules.
To get an overview how much memory is consumed by the modules use the command
lsof | grep apache | grep modules
Using this command you will also see which modules consumes much memory.
In my case only the following modules are used:
- actions.load
- authz_default.load
- authn_file.load
- auth_basic.load
- authz_user.load
- env.load
- mime.load
- rewrite.load
- userdir.load
- alias.load
- authz_host.load
- deflate.load
- expires.load
- negotiation.load
- setenvif.load
- headers.load
- speling.load
- cgi.load
- dir.load
- include.load
- php5.load
Modules which are not needed are disabled using the command
a2dismod <module_name>
Written on December 30, 2008 | Posted in
Apache |
Leave a comment