Ruby, Nginx, PHP and Python | Installation and configuration on Ubuntu

Oct/12

2

Setup AWStats, phpMyAdmin, phpPgAdmin With Nginx On Ubuntu



PHPMyAdminSo, you’ve already configured your linode, installed and configured nginx / php / ruby and setup automatic cloud backups. Now you are ready to install some extras like awstats, phpmyadmin and phppgadmin, right? If not, see previous articles.

Install and Configure AWStats

1. Install awstats from repository.

sudo apt-get install awstats

2. Create a php script to handle all requests to /cgi-bin.
AWStats is accessed by /cgi-bin/awstats.pl. We need to create a php script that is passed requests via php-fcgi.

sudo nano /etc/nginx/cgi-bin.php

Paste in the following:

<?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 = $_SERVER;
$newenv["SCRIPT_FILENAME"] = $_SERVER["X_SCRIPT_FILENAME"];
$newenv["SCRIPT_NAME"] = $_SERVER["X_SCRIPT_NAME"];

if (is_executable($_SERVER["X_SCRIPT_FILENAME"])) {
    $process = proc_open($_SERVER["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");
}
?>
sudo chmod +x /etc/nginx/cgi-bin.php

3. Change nginx’s log format to match apache so awstats can read it properly.

sudo nano /etc/nginx/nginx.conf

Find this line:

include       /etc/nginx/conf/mime.types;

Paste this on a line below it:

log_format main     '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

4. Setup each domain for usage.
You must perform the following steps for each domain that you wish to use awstats.

sudo cp /etc/awstats/awstats.conf /etc/awstats/awstats.domain.com.conf
sudo nano /etc/awstats/awstats.domain.com.conf

Now find and set the following settings to match.

LogFile="/home/user_name/public_html/domain.com/logs/access.log"
LogFormat=1
SiteDomain="domain.com"
HostAliases="www.domain.com localhost 127.0.0.1"

Now perform the initial generation for the domain.

sudo /usr/lib/cgi-bin/awstats.pl -config=domain.com -update

Add an entry in crontab to generate your stats every 10 mins. (or adjust to your preference)

sudo crontab -e

Add this line to the end:

*/10 * * * * /usr/lib/cgi-bin/awstats.pl -config=domain.com -update > /dev/null 2>&1

As the next step, you must modify the domains nginx config file and add the cgi-bin redirect so that it sends them to our php script from step 2.

sudo nano /etc/nginx/sites-available/domain.com

Find this line:

location ~ .php$ {

Paste this block of code before it:

location ^~ /awstats-icon {
    alias /usr/share/awstats/icon/;
    access_log off;
}

location ^~ /awstatscss {
    alias /usr/share/doc/awstats/examples/css/;
    access_log off;
}

location ^~ /awstatsclasses {
    alias /usr/share/doc/awstats/examples/classes/;
    access_log off;
}

# Configure /cgi-bin/scripts to go through php-fastcgi
location ~ ^/cgi-bin/.*\.(cgi|pl|py|rb) {
    gzip off;
    fastcgi_pass  127.0.0.1:49232;
    fastcgi_index cgi-bin.php;
    fastcgi_param SCRIPT_FILENAME    /etc/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;
}

Restart nginx and test the domain.

sudo /etc/init.d/nginx restart

In your browser visit the domain at… http://domain.com/cgi-bin/awstats.pl

When you installed AWStats it installed it’s own cron in your cron.d, we need to delete this as it will just continue to error (we are using separate configs per domain and don’t need the default.)

sudo rm /etc/cron.d/awstats

Reboot to make sure it takes effect, and you are set.

sudo reboot

Install and configure PhpMyAdmin

1. Install phpmyadmin from respository.

sudo apt-get install phpmyadmin

During the install it may ask you for passwords / current & new admin passwords. Remember these. (or use the same master password for everything). It may also ask if you wish to automatically configure your database, choose yes.

2. Create symbolic link to your web paths

Perform this step per each domain you wish to have access.

sudo ln -sf /usr/share/phpmyadmin /home/some_user/public_html/some_domain/public/

Test it at… http://www.domain.com/phpmyadmin

Install and configure PhpPgAdmin

1. Install phppgadmin from respository.

sudo apt-get install phppgadmin

During the install it may ask you for passwords / current & new admin passwords. Remember these. (or use the same master password for everything). It may also ask if you wish to automatically configure your database, choose yes.

2. Create symbolic link to your web paths

Perform this step per each domain you wish to have access.

sudo ln -sf /usr/share/phppgadmin /home/some_user/public_html/some_domain/public/

· · · ·

5 comments

  • Ltiisidii · December 12, 2012 at 1:42 pm

    I have this error:

    2012/12/12 08:27:11 [error] 2854#0: *1 FastCGI sent in stderr: “PHP message: PHP Notice: Undefined variable: pipes in /etc/nginx/cgi-bin.php on line 14
    PHP message: PHP Warning: proc_open() has been disabled for security reasons in /etc/nginx/cgi-bin.php on line 14″ while reading response header from upstream, client: xxx.xxx.181.145, server: mysite.com, request: “GET /cgi-bin/awstats.pl HTTP/1.1″, upstream: “fastcgi://unix:/tmp/php5-fpm.sock:”, host: “www.mysite.com”

    I understand it is to have disabled “proc_open” for security.

    understand it, is to have disabled “proc_open” for security, but there is no way to make it work without proc_open?

    Reply

    • Admin comment by kholland · February 7, 2013 at 9:08 pm

      Nope. you need to re-enable it to use these tutorials.

      Reply

  • rnic · February 20, 2013 at 9:12 pm

    Thanks for sharing! The only thing I had to change was the socket, since I had changed it in my /etc/php5/fpm/pool.d/www.conf file:

    from:
    fastcgi_pass 127.0.0.1:49232;
    to:
    fastcgi_pass unix:/var/run/php5-fpm.sock;

    Reply

  • Bogdan · April 13, 2013 at 10:00 am

    I followed your tutorial to enable awstats on nginx http server. Now I want to protect the link “www.domain.com/cgi-bin/awstats.pl” from the public with htpasswd. Where I should put the htpasswd file? I’m using Ubuntu 12.04 LTS. Thank you.

    Reply

  • Apurva Pandya · July 30, 2014 at 1:43 pm

    I am getting nginx 404 error message

    Reply

Leave a Reply

<<

>>