Tag Archives: Apache

Redirect WWW to non-WWW using Apache (htaccess)

The easiest way to redirect all your traffic from a WWW address (like www.domain.com) to a non-WWW (like domain.com) is using the following code:

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

With this code, you don’t need to hard-code the name of the domain into the .htaccess file.

Apart from the fact that www is deprecated (more here), the main benefit of doing this (or redirect from non-WWW to WWW) is that Google and other search engines won’t penalize our site because of duplicated content on both versions of our domain (WWW and non-WWW).

Nginx newest version on Ubuntu Lucid 10.04 LTS

Some time ago I wrote a post regarding this same topic (installing an updated version of Nginx instead of the one that is on the official Ubuntu repositories) but for Hardy (8.04).

Now, it is the turn of the latest Long-Term-Support version of this Linux distribution.

In this case, we need to use this repository:

deb http://ppa.launchpad.net/nginx/stable/ubuntu lucid main

You can create a file like /etc/apt/sources.list.d/nginx-stable.list, and put that line inside.

We also need to add the key of this repository so Ubuntu won’t complain.

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 00A6F0A3C300EE8C

and finally, run:

apt-get upgrade && apt-get install nginx

Install updated version of nginx on Ubuntu Hardy 8.04LTS

Nginx is a great web server to handle tons of traffic with relative low resources, it beats Apache big time.

I have started using  it as a Reverse Proxy, Load Balancer, Web Cache and static files web server (like images, javascript and CSS) in front of Apache (for PHP processing). I’m more than amazed with the results.

The problem is that Ubuntu Hardy 8.04 comes with Nginx version 0.5 (version 1.0 has been recently released), and lacks of some features as Cache. An easy way to update the version to a more recent one (currently, to 0.8) without upgrading the whole Ubuntu version is using the following PPA repository: https://launchpad.net/~meto/+archive/nconfig

To add this repository to your system, you need to create a file inside /etc/apt/sources.list.d/ (for instance, nginx-stable.list) with the following content:

deb http://ppa.launchpad.net/meto/nconfig/ubuntu hardy main 
deb-src http://ppa.launchpad.net/meto/nconfig/ubuntu hardy main

Then, you need to add the key of the repository to your system:

apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ED283389

Finally, run:

apt-get update && apt-get upgrade

You should be prompted about a newer version of nginx, say yes, and that’s it, enjoy ;).

Forbid access to svn directories on Apache Web Server

Using Subversion to deploy and maintain updated web applications like WordPress, Joomla or even your development on a web server is very handy. One of the downsides I have found to this solution is that your web root will have hidden .svn directories all over the place (one per each directory on your application). This directories will not be hidden for the web server, so it is important to forbid browsers the access to them.

If you are using Apache, you can use the tips on this page.

If you use Apache 2.2 (I have not test it on other versions, but it should work also on 2.0 version) and have access to the configuration files, you can create the file /etc/apache2/conf.d/svn with the following content:

<Directory ~ "\.svn">
    Order allow,deny
    Deny from All

This will deny access to all the .svn directories on your web server. After you have create this file, do not forget to restart the web server so the changes can take effect.

Edit (26/May/2011): This can also be achieved using:

<DirectoryMatch .*\.svn/.*>
    Deny From All

Resolving troubleshooting with Munin

This are some problems that you may encounter after installing Munin:

Apache modules are not tracking, so they are not showing statistics, you may want to check Munin logs (specifically /var/log/munin/munin-node.log) to see what is going on.

If you got something like Can’t locate object method “new” via package “LWP::UserAgent”, you need to install the package libwww-perl which contains the LWP::UserAgent required. Thanks to Crowdway.

If you already have installed the libwww-perl package, but Munin is still not showing data, you may need to enable and configure the status module on Apache. Make sure you allow localhost (and the name of the server as well) to access the location server-status on apache2.conf or status.conf. Also, this plugins need the ExtendedStatus flag to be On.

You should have something like:

<IfModule mod_status.c>
# Allow server status reports generated by mod_status,
# with the URL of http://servername/server-status
# Uncomment and change the ".example.com" to allow
# access from other hosts.
ExtendedStatus On
<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from localhost ip6-localhost
    Allow from vps02.graphium.net
    #Allow from .example.com

Sometimes you can get permission errors on your logs, so the best way to get rid of them is to ensure that all involved files in Munin process are own by munin user and group. We can run as root:

 $ chown -R munin.munin /var/lib/munin /var/log/munin /var/www/munin

There are some plugins that require a parameter to work. For example, those that refer to networking (if_, if_err_, ip_) needs the network interface we want to track. In my case (VPS), this is venet0, but on a usual box it can be eth0.

We can list the available interfaces by running the command:

$ ifconfig

Then, to correctly enable this modules, we need to append the name of the interface:

$ ln -s /usr/share/munin/plugins/if_ /etc/munin/plugins/if_venet0

A convenient way to see the result of each module, we can run them independently:

$ munin-run apache_processes
busy80.value 7
idle80.value 1
$ munin-run if_venet0
down.value 7721322
up.value 37550800

By this command, we can ensure that the plugin is working fine and getting results.

Install Munin on Ubuntu Server

Munin is a small tool for monitoring resources on servers. I think it is very useful, specially on small VPS, that needs to save resources. Reports are written as HTML files, so we will need a Web Server like Apache to see this reports.

First, we install it and add some extra plugins:

$ sudo apt-get install munin munin-plugins-extra

Now, we can make some changes to the default configuration, located at /etc/munin/munin.conf. For example, we can change any of the paths where Munin works:

dbdir /var/lib/munin
htmldir /var/www/munin
logdir /var/log/munin
rundir /var/run/munin

Specially, the htmldir path, where all the reports are written to see through Apache or the one you are using. Remember to move the directory /var/www/munin to where you wanted if you change that configuration line. We can protect this directory with an htaccess file to only give access to some users.

We can configure email notifications if a change occur (like from a OK situation to a WARNING). To do this, we just need to uncomment or add the following line:

contact.someuser.command mail -s "Munin notification" your@email.com

By default, Munin will monitor localhost, but we can add other boxes (clients), these machines will only need to install munin-node package.

Then, we can enable some plugins (more plugins can be found here and here). To do this, we need to create a symbolic link per each plugin we want to activate. I’m going to enable apache and mysql modules, but you are free to enable the modules you need:

$ cd /etc/munin/plugins
$ sudo ln -s /usr/share/munin/plugins/apache_* .
$ sudo ln -s /usr/share/munin/plugins/mysql_* .

Each time a module is enable or disable, we need to restart the service, so we can do the following:

$ sudo /etc/init.d/munin-node restart

Also, it is recommended to reassign all files on the htmldir to munin user and group by doing:

$ sudo chown munin.munin -R /var/www/munin

And then, to avoid waiting 5 minutes until munin cron runs again, we force it by:

$ sudo /usr/bin/munin-cron --force-root

If we are not completely satisfied with the default template, we can modify it, they are HTML files (with some minor special template tags). Anyone with some knowledge of HTML and CSS can do that. We can even download other already created templates (I have found some errors on that template’s JavaScript, I hope I’ll get some time to post the modified template, in the meanwhile, if anyone need it, please drop me a line to send you the files).

Finally, as this post is not as complete as I would like, I leave some links that may help: