Katipo
Search  
Site Blog
  About  
  Home
About Portfolio Solutions Client Area Contact Us
: : About Us
Awards
Jobs
Our People
What Is A ... ?
Working From Home
News
Photo Gallery
Katipo Blog


Howto Use Awstats with Nginx and Multiple Ruby on Rails Apps Under Mongrel Clusters

I have a host where Nginx listens on port 80 and dishes out requests to various apps depending on what their host name is. It also does appropriate load balancing. Primarily I use this for hosting various instances of Kete Ruby on Rails apps, but I also use it for supporting the subversion repository under Apache and a few other RoR apps, too. I wanted to setup up web log statistics for all of the above and I wanted to do it in such a way that it’s easy to add another web app to view statistics for.

Awstats is wicked easy to setup if you live in the LAMP world, but I could find a decent tutorial that covered my setup. So here goes…

Requirements

Besides the software listed in the title, this tutorial also assumes that you are running Debian Etch and have Apache2 setup (although not on port 80).

Install Awstats

On Debian Etch this is a one liner as root.

apt-get install awstats

Set up Apache for Awstats

cd /usr/share/doc/awstats/examples/
cp apache.conf /etc/apache2/sites-available/awstats.conf
ln -s /etc/apache2/sites-available/awstats.conf /etc/apache2/sites-enabled/

Configure A Domain

You’ll need to do this for every domain you want to get statistics for. This assumes you are still root and in the directory from the last section.

gzip -d awstats.model.conf.gz
cp awstats.model.conf /etc/awstats/awstats.your_domain.conf

Now open /etc/awstats/awstats.your_domain.conf with your favorite text editor, as root. This is where we get into the Nginx specific stuff comes in. Change the LogFile parameter like so (based on using the default Nginx storage of log files):

LogFile=”/usr/local/nginx/logs/access.your_domain.log”

there are few more that you need to change to match your domain:

SiteDomain=”your_full_domain”

and

HostAliases=”localhost 127.0.0.1 REGEX[your_domain\.com$]“

and

DirData=”/some/dir/where/www-data/has/read-and-write/your_domain”

You’ll need to do the following to make the matching directory that you specified in the DirData line:

mkdir /some/dir/where/www-data/has/read-and-write/your_domain

In my case, I had to also chmod and chown the new directory, too.

Important Note: I have left the ability to update statistics off from the web, but allowed anyone to read statistics. This is fine for my clients, but may not be for yours. There are additional steps involved to lock it down, if needed

Again, note that you have to repeat the configuration for every domain.

Configure Nginx for the Awstats Web Server

Don’t forget to add DNS for whatever domain name you decide to go with.

In my case, I already had Nginx pointing at Apache running on port 8000 for HTTP access to my SVN repository. I simply tacked “stats.your_domain” onto my server_name line in /usr/local/nginx/conf/nginx.conf and restarted Nginx with /etc/init.d/nginx stop and start.

See this post for details.

Set up Logrotate for Nginx

cd /etc/logrotate.d/
cp apache2 nginx

Now edit /etc/logrotate.d/nginx. Change “/var/log/apache2/” to “/usr/local/nginx/logs/” and “/var/run/apache2.pid” to “/usr/local/nginx/logs/nginx.pid”. Also “/etc/init.d/apache2 restart” to “/etc/init.d/nginx restart” and “create 640 root adm” to “create 664 root staff”. Awstats user, www-data, needs permission to read the logs.

We also want to update our stats just before we run the weekly logrotate. Thus we add this before the postrotate line:

prerotate
/usr/lib/cgi-bin/awstats.pl -config=your-domain-name -update
endscript

Your will need to add a awstats.pl update call for each domain here, between prerotate and endscript.

Run Awstats on Existing Log

If you haven’t already, you probably want to do this:

chmod 644 /usr/local/nginx/logs/*.log

For each of your domains, do this:

/usr/lib/cgi-bin/awstats.pl -config=your_domain -update

Set up Cronjob of Domain

Edit /etc/cron.d/awstats. Replace what is in there with this:

0,10,20,30,40,50 * * * * www-data /usr/lib/cgi-bin/awstats.pl -config=horowhenua -update >/dev/null

This will update your stats every ten minutes.

Rails Production Log Rotation

Lastly, for the domains that are Ruby on Rails apps in production, we’ll want to rotate their Rails production log. http://nubyonrails.com/articles/2007/01/03/a-hodel-3000-compliant-logger-for-the-rest-of-us

Follow the instructions up to “Once More with Feeling” then add crob job –>

I’m running a small variant of what you can find here: rotate production logs

Ok, just repeat steps necessary to add domains.

Cheers,
Walter

5 Responses to “Howto Use Awstats with Nginx and Multiple Ruby on Rails Apps Under Mongrel Clusters”

  1. walter Says:

    This is a rapidly evolving stack. People are working on dynamically adding mongrels to the cluster as needed, for example. You’ll want to monitor changes closely. I would say the authoritative blog on this stack is http://brainspl.at/.

    Cheers,
    Walter

  2. TestBlog » awstats installieren Says:

    [...] http://blog.katipo.co.nz/?p=34 [...]

  3. irishfighter Says:

    Hi,

    Is it possible to configure awstats to run on nginx alone without the need to have apache installed?

    Regards,

    Darren

  4. walter Says:

    It may be possible, but I’m aware of how to do it. Please post back here if you find a way.

    Cheers,
    Walter

  5. walter Says:

    Further info on adding records from old logs…

    If you have a set of old logs that have been gzip’d (on Debian Etch), you can use this in your domain’s awstats config, but be sure to do it first thing and probably to exclude the unzipped latest access log.

    LogFile=”perl /usr/share/doc/awstats/examples/logresolvemerge.pl /path/to/old_logs/access.your_domain.log* |”

    Cheers,
    Walter

Leave a Reply

You must be logged in to post a comment.


Katipo
Rachel Snowboarding