Saturday 29 August 2009

Getting awstats working/perl running as CGI in Nginx

This morning I was trying to get awstats working on the web server. I had extracted the awstats folder to a web-accessible location on the web server, and also set up the awstats.mysite.conf file.

When I ran the update command line perl $HOME/path/to/awstats.pl -config=mysite -update, I got a message that my logs didn't match what I'd specified in the .conf file. So I checked and found that I had $http_x_forwarded_for on the end of each line of my log. So I removed that from the nginx log format, deleted all the logs, and restarted nginx last night.

Then this morning I tried running the command to update again perl $HOME/path/to/awstats.pl -config=mysite -update, but got the same message that the actual log format didn't match what I'd specified in the awstats site configuration file. Both what the log format should look like and what it did look like looked exactly the same to me, but after a bit of puzzling I realised that I had two spaces between the HTTP Status code and the bytes transferred value, when it should be just one space between them.

So I updated the log format in the nginx configuration again, deleted all the current logs again, restarted nginx, and then visited the website to get a log entry. Now finally the update command perl $HOME/path/to/awstats.pl -config=mysite -update would work.

I tried accessing http://mysite.com/awstats/wwwroot/cgi-bin/awstats.pl?config=mysite, but just got prompted to download the file.

So I searched to find out how to run perl scripts as CGI under nginx, and found a tutorial Nginx - Perl FastCGI How To. There is also an Nginx Wiki article - Nginx Simple CGI.

Following the tutorial tutorial Nginx - Perl FastCGI How To, I downloaded the perl FCGI wrapper, which requires the FCGI Perl module. So next I needed to check if the FCGI perl module was already installed. I found out how to do that at Checking to see if a Perl module is in your @INC path: perl -e 'use FCGI;' - if it prints nothing, the FCGI perl module is installed, if it says it can't locate the module, then it's not installed.

I found it wasn't installed, so I had to download and install it. I followed the guide Installing Perl Modules into a Nonstandard Directory. All you do is untar the tarball, cd into the directory, then run perl Makefile.PL PREFIX=$HOME/perlModules
make
make test
make install


Next I needed to modify the perl FastCGI wrapper script so that it would look in the correct location for the FCGI module, since I had installed it in a non standard location. I found a tutorial on how to do this: What to do when Perl modules aren't in their normal locations. All I needed to do was before use FCGI; I added use lib '/home/username/perlModules';

Except this didn't actually work. When I tried running the script, it still couldn't find the FCGI perl module. What I actually had to add was use lib '/home/username/perlModules/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi';, as this was where the FCGI.pm module had actually installed to.

After setting that to the correct value, and of course setting the correct location for the socket file to be created, I could now start the perl FCGI wrapper perl /path/to/fastcgi-wrapper.pl&

Next I needed to configure nginx, this was similar to what is specified in the Nginx - Perl FastCGI How To, except that my cgi-bin folder wasn't located at the root of my site, so I had to change
location ~ ^/cgi-bin/.*\.cgi$ {
to
location ~ /cgi-bin/.*\.cgi$ {

After restarting nginx I could finally access awstats, however I was currently accessing it via http://mysite.com/awstats/wwwroot/cgi-bin/awstats.pl?config=mysite, and wanted to try and get it so I could access it via http://mysite.com/awstats/awstats.pl?config=mysite, as is suggested in the guide to setting up awstats.

This meant implementing the equivalent of the
Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/"
Alias /awstatscss "/usr/local/awstats/wwwroot/css/"
Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/"
ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/"
Apache directives. I tried doing it with location:
location /awstatsclasses {
root /home/username/webapps/htdocs/admin/awstats/wwwroot/classes;
}
location /awstatscss {
root /home/username/webapps/htdocs/admin/awstats/wwwroot/css;
}
location /awstatsicons {
root /home/username/webapps/htdocs/admin/awstats/wwwroot/icon;
}
location /awstats {
root /home/username/webapps/htdocs/admin/awstats/wwwroot/cgi-bin;
}
But I just got a 404 if I tried to access http://mysite.com/awstats/awstats.pl?config=mysite.

After lunch I went on Animal Crossing, then I carried on trying to get awstats working on the webserver.

After reading a bit of the NGinx wiki I tried:
location /awstatsclasses/ {
alias /home/username/webapps/htdocs/admin/awstats/wwwroot/classes/;
}
location /awstatscss/ {
alias /home/username/webapps/htdocs/admin/awstats/wwwroot/css/;
}
location /awstatsicons/ {
alias /home/username/webapps/htdocs/admin/awstats/wwwroot/icon/;
}
location /awstats/ {
alias /home/username/webapps/htdocs/admin/awstats/wwwroot/cgi-bin/;
}
However, this meant I would get prompted to download the awstats.pl file again, as the location no longer contained '/cgi-bin/', so the script wasn't being passed to perl.

So finally I came up with this, which seems to work:

#awstats config
location /awstatsclasses/ {
alias /home/username/webapps/htdocs/admin/awstats/wwwroot/classes/;
}
location /awstatscss/ {
alias /home/username/webapps/htdocs/admin/awstats/wwwroot/css/;
}
location /awstatsicons/ {
alias /home/username/webapps/htdocs/admin/awstats/wwwroot/icon/;
}
location /awstats/ {
alias /home/username/webapps/htdocs/admin/awstats/wwwroot/cgi-bin/;
if (-e $request_filename)
{rewrite ^/awstats/(.*)$ /awstats/wwwroot/cgi-bin/$1 last;}
}


I spent most of the afternoon playing on the Kendo style game on Wii Sports Resort. First I got the maximum rating you can get in the duel (2500 points) using L's technique of pressing block just as you hit the other person, which seems to make it so you can hit them even when they're blocking.

Then I tried the challenge mode (think that's what it's called), and I got to the last person on the reverse castle level, and they only had one heart, but then they beat me. Most of the time I got beaten quite near the start of the level though.

After dinner we (me, Moccle, L and McRad) went out on walk between Gartree and Foxton.

I finished copying my pictures to my 2 new 1.5TB drives (I had been doing this all day, as takes a few hours to copy to each drive), and then swapped the 1TB drive in my PC that currently had my pictures for one of the new 1.5TB drives.

After getting that working I tried to see if there were any programs I could use to make sure no files had been corrupted when I copied them across before I format the old 1TB drive. Unfortunately I didn't find anything other than a perl script for checking jpegs in a specific folder and moving them to a folder called 'bad' if they are corrupted. Unfortunately the site that the perl script was hosted on doesn't seem to exist any longer, so I couldn't actually download it.

I also found a page that said most antivirus programs will let you know if they come across a corrupted file. So I might try doing a virus check on the drive tomorrow. Synkron doesn't complain about any corrupted files, but I don't know whether this means there aren't any or not.

The weather was a mixture of sun and cloud in the morning, then mostly cloudy in the afternoon, then a mixture of sun and cloud again in the evening. It looked like there was probably a decent sunset. It was quite windy all day, though nowhere near as windy as the last few days.

Food
Breakfast: Blackcurrant jam toast sandwich; cup o' tea.
Lunch: Mature cheddar cheese with little gem lettuce and sliced raddish sandwich; Fake Kettle chips sea salt and black pepper flavour; 3x strawberries; 2x plums; Chocolate Wafer biscuit; cup o' tea; 4x pieces Sainsburys Truffle Chocoate.
Dinner: 2x delee Chilli sausages; baked beans; mashed potato. Pudding was a strawberry yoghurt.
After walk snack: Coffee; 2x Shortbread fingers.

1 comment:

Hosting Nuggets said...

I went through the same problem and managed to get the Simple CGI working for awstats using the nginx documentation which is slightly bit shorter than yours ;-)