Script to correct missing AWStats statistics on Plesk

The Plesk statistics system doesn’t use any log data from previous months. If you need to regenerate statistics for previous months, you can use this script. Make sure that the logs are uncompressed in the statistics/logs directory for the domain and then run this script from within the domain’s root directory (eg. /var/www/vhosts/example.com):

#!/bin/bash
# per http://kb.parallels.com/en/5550

# make sure to enter the actual domain name on the next line:
export vhost_name=ENTER-REAL-DOMAIN-NAME-HERE

export AWSTATS_BIN_D=`grep ^AWSTATS_BIN_D /etc/psa/psa.conf | awk '{print $2}'`
export HTTPD_VHOSTS_D=`grep ^HTTPD_VHOSTS_D /etc/psa/psa.conf | awk '{print $2}'`
export PRODUCT_ROOT_D=`grep ^PRODUCT_ROOT_D /etc/psa/psa.conf | awk '{print $2}'`
export awstats=${AWSTATS_BIN_D}/awstats.pl
export awstats_gen_opts="-staticlinks -configdir=${PRODUCT_ROOT_D}/etc/awstats -config=${vhost_name}-http"

find $HTTPD_VHOSTS_D/$vhost_name/statistics/webstat -name '*.txt' -exec rm -f '{}' \;

$awstats $awstats_gen_opts -LogFile=$HTTPD_VHOSTS_D/${vhost_name}/statistics/logs/access_log.processed

# for both of these loops, change the year range to match those that need to be regenerated:

for y in 2007 2008 2009; do for m in `seq 1 12` ; do mkdir ${HTTPD_VHOSTS_D}/${vhost_name}/statistics/webstat/$y-$(printf "%.2d" $m) ; done ; done

for y in 2007 2008 2009; do \
 for m in `seq -w 1 12` ; do \
     dest_dir=$HTTPD_VHOSTS_D/$vhost_name/statistics/webstat/$y-$m ; \
     $awstats $awstats_gen_opts -month=$m -year=$y -output > $dest_dir/awstats.${vhost_name}-http.html ; \
     ln -s $dest_dir/awstats.${vhost_name}-http.html $dest_dir/index.html ; \
     for output in alldomains allhosts lasthosts unknownip allrobots lastrobots session urldetail urlentry urlexit osdetail unknownos refererse refererpages keyphrases keywords errors404 ; do \
         $awstats $awstats_gen_opts -month=$m -year=$y -output=$output > $dest_dir/awstats.${vhost_name}-http.$output.html ; \
     done ; \
 done ; \
done

$PRODUCT_ROOT_D/admin/sbin/statistics --calculate-one --domain-name=$vhost_name

This is based on the information in this Parallels KB: http://kb.parallels.com/en/5550

Configuring Passenger (mod_rails) on cPanel Systems

cPanel has built-in support for Rails apps using Mongrel but this involves multiple servers and port forwarding and only allows one applicaiton per cPanel user. Phusion Passenger is an Apache (and Nginx) module that allows Rails applications to be run directly through the web server without further configuration. Normal installation of Phusion Passenger is very straight-forward but on cPanel servers, it must be installed in a cPanel-friendly way.

All of the following commands need to be run as root or with the use of sudo:

First, make sure Ruby is installed:

root@host:[~]$ /scripts/installruby

Next, install Passenger:

root@host:[~]$ gem install passenger

Then set the paths to Apache and the Apache dev headers and run the installer:

root@host:[~]$ APXS2=/usr/local/apache/bin/apxs PATH=$PATH:/usr/local/apache/bin passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v2.2.5.

This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.

Here's what you can expect from the installation process:

1. The Apache 2 module will be installed for you.
2. You'll learn how to configure Apache.
3. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.

Hit enter and the installer will compile the Apache 2 module. If this is successful you will get a message with directions for modifying the Apache conf:

The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5
PassengerRuby /usr/bin/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

Press ENTER to continue.

Copy the configuration directives and place them in /usr/local/apache/conf/includes/pre_main_global.conf. Do not directly edit the httpd.conf!

These are the lines that should be added (with server-specific version numbers):

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5
PassengerRuby /usr/bin/ruby

Add the following to the httpd.conf (passenger docs):

PassengerResolveSymlinksInDocumentRoot on

Run the httpd.conf config distiller to save these changes:

root@host:[~]$ /usr/local/cpanel/bin/apache_conf_distiller --update
Distilled successfully

Then rebuild the httpd.conf using the cPanel rebuild script. Stop and correct any errors before restarting Apache:

root@host:[~]$ /scripts/rebuildhttpdconf
Built /usr/local/apache/conf/httpd.conf OK

Restart Apache and verify Passenger is installed by running this command:

root@host:[~]$ apachectl -t -D DUMP_MODULES

Check for ‘passenger_module (shared)’.

By default, RailsAutoDetect is on so it will detect a directory that is a Rails application. If this is off, you will need to add a custom vhost configuration to let Apache know to load Passenger for a particular site. To do this add the following to the site’s vhost container:

RailsBaseURI /

(or whatever URI is the base of the application)

Directions for adding custom directives to a vhost in cPanel: Add Custom Apache Directives to a vhost in cPanel

If the Rails application will be the root of the site, you will need to create a symlink from the Rails application’s public directory to /home/[username]/public_html.

Redirecting MovableType URLs to WordPress using mod_rewrite / .htaccess

After following the steps outlined here to import the old MovableType content, I created the following mod_rewrite rules to redirect requests for old content to the new archive copy of the site:

RewriteEngine On
RewriteBase /
    # Redirects individual posts:
RewriteCond %{REQUEST_URI} ^/weblog/archives/[0-9]{6}\.html.*$
RewriteRule ^weblog/archives/([0-9]+)\.html.*$ http://archive.tylerdave.com/$1/ [R=301,L]
    # Redirects monthly archives:
RewriteCond %{REQUEST_URI} ^/weblog/archives/20[0-9]{2}\_[0-9]{2}\.html.*$
RewriteRule ^weblog/archives/20([0-9]{2})\_([0-9]{2})\.html.*$ http://archive.tylerdave.com/date/20$1/$2/ [R=301,L]
    # Redirects individual posts:
RewriteCond %{QUERY_STRING} ^entry_id=([0-9]{1})$
RewriteRule ^mt/mt-comments\.cgi http://archive.tylerdave.com/00000%1/? [R=301,L]

    # Redirects old comments URLs to the posts:
RewriteCond %{QUERY_STRING} ^entry_id=([0-9]{2})$
RewriteRule ^mt/mt-comments\.cgi http://archive.tylerdave.com/0000%1/? [R=301,L]

RewriteCond %{QUERY_STRING} ^entry_id=([0-9]{3})$
RewriteRule ^mt/mt-comments\.cgi http://archive.tylerdave.com/000%1/? [R=301,L]

    # Redirects other requests to me-comments.cgi to the archive's home page:
RewriteCond %{REQUEST_URI} ^/mt/mt-comments\.cgi.*$
RewriteRule . http://archive.tylerdave.com/ [R=301,L]

    # Redirects the index page::
RewriteCond %{REQUEST_URI} ^/weblog/archives/index.html$
RewriteRule . http://archive.tylerdave.com/ [R=301,L]

    # Redirects RDF feed:
RewriteCond %{REQUEST_URI} ^.*index.rdf$
RewriteRule . http://archive.tylerdave.com/feed/rdf/

These rules account for more than 95% of the traffic to the old site. I will continue to monitor the logs and update the rules as necessary.