These forums are read-only!
Setup AWStats on my Nginx server
  • I'm trying to setup AWStats on my Nginx server. I can get over the log translation, but my main problem is with configuring the AWStats site to run under Nginx, rather than Apache. The provided configuration is Apache specific and will not work in Nginx. I don't want to have Apache running just for serving AWStats (thus eating more precious memory).

    If anyone managed to do so and don't mind sharing the server configuration file, I'll be grateful.

    Thanks,
    Zviki
  • The easiest solution is to run AWStats from cron to generate static reports every hour/day/however often you want, and then just host up that static HTML. Unfortunately the static reports aren't as nice as the dynamic script-generated ones, especially since there's no built-in way to manage the historical data from previous months.

    I ultimately got tired of not being able to run one-off scripts from nginx whenever I needed (such as Perl for my AWStats reporting). Since I already had a PHP FCGI pool running and proxied behind nginx for several PHP applications, I wrote a simple PHP script that uses "proc_open" to run any executable file it is provided and then pipe out the results. While not suitable for any end-user facing stuff, it's great for all my little one-off admin scripts, AWStats, etc. that are already well protected and that I need to run from time to time.

    The PHP (cgi-bin.php):


    <?php

    $descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to
    2 => array("pipe", "w") // stderr is a file to write to
    );

    $newenv = $_ENV;
    $newenv["SCRIPT_FILENAME"] = $_ENV["X_SCRIPT_FILENAME"];
    $newenv["SCRIPT_NAME"] = $_ENV["X_SCRIPT_NAME"];

    if (is_executable($_ENV["X_SCRIPT_FILENAME"])) {
    $process = proc_open($_ENV["X_SCRIPT_FILENAME"], $descriptorspec, $pipes, NULL, $newenv);
    if (is_resource($process)) {
    fclose($pipes[0]);
    $head = fgets($pipes[1]);
    while (strcmp($head, "\n")) {
    header($head);
    $head = fgets($pipes[1]);
    }
    fpassthru($pipes[1]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    $return_value = proc_close($process);
    }
    else {
    header("Status: 500 Internal Server Error");
    echo("Internal Server Error");
    }
    }
    else {
    header("Status: 404 Page Not Found");
    echo("Page Not Found");
    }
    ?>


    A relevant nginx config location using that:


    location ~ ^/cgi-bin/.*\.(cgi|pl|py|rb) {
    gzip off;
    fastcgi_pass 127.0.0.1:8080;
    fastcgi_index cgi-bin.php;
    fastcgi_param SCRIPT_FILENAME /etc/local/nginx/cgi-bin.php;
    fastcgi_param SCRIPT_NAME /cgi-bin/cgi-bin.php;
    fastcgi_param X_SCRIPT_FILENAME /usr/lib$fastcgi_script_name;
    fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE nginx;
    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param DOCUMENT_URI $document_uri;
    fastcgi_param DOCUMENT_ROOT $document_root;
    fastcgi_param SERVER_PROTOCOL $server_protocol;
    fastcgi_param REMOTE_ADDR $remote_addr;
    fastcgi_param REMOTE_PORT $remote_port;
    fastcgi_param SERVER_ADDR $server_addr;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_NAME $server_name;
    fastcgi_param REMOTE_USER $remote_user;
    }
  • Thanks.
    Sorry for the stupid question: how do you execute AWStat with that script?

    Anyway, does anyone have a simpler method?
  • Posted By: Zviki CohenThanks.
    Sorry for the stupid question: how do you execute AWStat with that script?


    Hitting the URL http://[host]/cgi-bin/awstats.pl would run it -- basically just as if it were on an Apache host with CGI exec setup. The big part left out here is the setup of the PHP FastCGI pool, which is being proxied in order to run the PHP. However, doing that is described in many places on the web as it's pretty standard stuff, e.g.: http://blog.kovyrin.net/2006/05/30/nginx-php-fastcgi-howto/
  • Thanks again. I already have the PHP/FastCGI running my Drupal, so this seems much simpler than I suspected.

    Let's assume I create a new site root with my cgi-bin.php installed there.
    Where should the AWStats go? Is it in the same folder with the cgi-bin.php?
  • It's up to you and what you set for these lines:


    fastcgi_param SCRIPT_FILENAME /etc/local/nginx/cgi-bin.php;
    fastcgi_param X_SCRIPT_FILENAME /usr/lib$fastcgi_script_name;


    The first points to wherever you want the cgi-bin.php to live, and can basically be anywhere. The second is where you want your CGI scripts to live, and that's where your awstats.pl would go. In this case it's /usr/lib/cgi-bin/ (the /cgi-bin/ part gets tacked on by that particular location statement, since it's matching /cgi-bin/[script name]).
  • It's still not working for me. My problem is with the definition of the location string in the nginx configuration. From some reason, I'm not getting to the PHP. If I point to it directly, it does work.

    I want the URLs to be under http://admin.example.com/awstats/cgi-bin/awstats.pl

    Thanks,
    Zviki
  • GOT IT!

    Had a wrong port. Works really nice. Thanks nezroy.
  • Thanks for the script nezroy. It came in very handily for me.

    I'll just add a small tip for others. To help lock things down, assuming that you don't want the world to access this, you can add something like the following to the nginx location directive:

    allow 127.0.0.1;
    deny all;

    Change the IP address to suit. Since I'm the only one with access to the server and normally run all of my work through a tunnel, this does the trick for me. If you want to go a step further, look into the HttpBasicAuth module (http://wiki.nginx.org/NginxHttpAuthBasicModule)
  • Hi - I feel like I'm 'almost there' with this, but I can't get it to work on my site that is running drupal fine.

    If I leave the line:

    @fastcgi_pass 127.0.0.1:8080;@

    in my virtual hosts file, I get 503-bad gateway. If I comment it out, I get 404, but it's not the 404 issued by the cgi-bin.php script.

    All locations & filenames are as Nezroy has suggested.

    Anyone have any ideas where to focus?
  • swduncan, set it to the address:port where your php-fcgi is listening. You probably have a similar directive for php files in general. Use the same address:port.

    http://wiki.nginx.org/NginxHttpFcgiModule#fastcgi_pass
  • I'm trying to get this working for a different cgi script (mailgraph http://mailgraph.schweikert.ch/)

    It pulls up the script (without any graphs showing) but my php-cgi processes are all left running at 100% cpu, and I eventually have to kill them.

    I see this happen sometimes when the php script tries to access something it doesn't have permisison for. If anyone has some insight I'd appreciate it, thanks!
    Brian
  • Awesome, thank you so much for this!

    One question. When i view my awstats all the images are broken. It's probably simple but how can i fix this?

    Thanks
  • So you are thinking about having your wedding during the winter?
    href="http://www.rs2guru.com/">unique wedding invitations

    If you want a winter wedding, there a few things that you going to need to account for.
    href="http://www.vponsale.com/">wedding dresses

    With the right planning, winter weddings can be just as beautiful as spring weddings.
    href="http://www.vponsale.com/">wedding gowns

    In my opinion, they can be more so.bridesmaid

    dresses

    However, with the wrong planning a winter wedding can be ten times more disastrous than the worst

    of springtime weddings. cheap wedding invitations
    Your wedding could be a winter wonderland or if you are not careful, a winter wasteland.
  • The following link was very helpful in my test setup for your issue. I wanted to try and duplicate the problem and this tutorial was instrumental in my setup:

    http://blog.katipo.co.nz/2007/05/10/howto-use-awstats-with-nginx-and-multiple-ruby-on-rails-apps-under-mongrel-clusters/

    The biggest trouble I ran into was the nginx specific configuration in replacement of apache.