How to install and configure Piwik under Apache or Lighttpd on Linux

Installing Piwki from Git

We run master, it's the only way to go although once or twice we've have to wait as much as an hour for a bug fix.

git clone /opt/piwik
cd /opt/piwik
./composer.phar install --no-dev

Dependencies / Requirements

Any web-sever will do, Piwik runs nicely under Apache, Lighttpd or Nginx.

Directory Permissions

We think the default permissions defined by Piwik are too open, so we just do this. Pick one of these, with the proper user for your web-server process.

chown -R apache:apache config tmp
chown -R lighttpd:lighttpd config tmp
chown -R www-data:www-data config tmp

You may be warned by Piwik that it cannot update itself due to restricted permissions, this is a good thing.

Apache Configuration

This example configuration makes Piwik function in a sub-path, you can easily redefine this as a virtual host.

# Piwik - Logs
Alias /piwik /opt/piwik
<Directory /opt/piwik>

    Order allow,deny
    Allow from all
    AllowOverride None
    Options Indexes FollowSymLinks

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* index.php [L,QSA]


Restart Apache, then checkout /piwik on your server, it should prompt you for the setup.

Lighttpd Configuration

Running Piwik under Lighttpd is recommended for high-volume sites when using the real-time JavaScript. For sure you should have one machine dedicated as the Piwik web-application and another system to function as the database.

cd /var/www
ln -s /opt/piwik log.domain.tld

Use your site name for this logging server directory.

Piwik & Lighttpd

Using lighttpd is important for high (or even medium) volume websites.

$SERVER["socket"] == ":443" {
    ssl.engine = "enable"
    ssl.pemfile = "/etc/lighttpd/log.domain.tld.pem" = "/etc/lighttpd/log.domain.tld-ca.crt"

$HTTP["host"] == "log\.domain\.tld" {

    server.document-root = "/opt/piwik/"

    $HTTP["url"] !~ "^/piwik\.(js|php)|robots\.txt$" {
        auth.backend = "htpasswd"
        auth.backend.htpasswd.userfile = "/etc/lighttpd/htpasswd"
        auth.require = ( "/" => ( "method" => "basic", "realm" => "admin", "require" => "valid-user" ) )


Configuring Piwik

Now that Piwik is installed, visit the home page, like http://log.domain.tld/ Piwik has a guided installation that should complete w/o issues.

Importing Log Files

python /opt/piwik/misc/log-analytics/ \
    --debug --debug \
    --url= \
    --add-sites-new-hosts \
    --enable-http-errors \
    --enable-http-redirects \
    --enable-static \
    --enable-bots \
    --strip-query-string \
    --show-progress \
    --recorders=2 \

We maintain a Piwik Import shellscript

Processing Archives

Update the settings to disable the browser based triggering, using SQL

UPDATE piwik_option SET option_value = 0 WHERE option_name = 'enableBrowserTriggerArchiving';
# php /opt/piwik.git/misc/cron/archive.php  -- url=http://log.domain.tld/

[2013-04-19 04:59:31] [3a7691aa] NOTE: if you execute this script at least once per hour (or more often) in a crontab, you may disable 'Browser trigger archiving' in Piwik UI > Settings > General Settings. 
[2013-04-19 04:59:31] [3a7691aa]       see doc at:
[2013-04-19 04:59:31] [3a7691aa] ---------------------------
[2013-04-19 04:59:31] [3a7691aa] INIT
[2013-04-19 04:59:31] [3a7691aa] Querying Piwik API at: http://log.domain.tld/index.php
[2013-04-19 04:59:31] [3a7691aa] Running Piwik 1.12-b9 as Super User:
[2013-04-19 04:59:32] [3a7691aa] Notes
[2013-04-19 04:59:32] [3a7691aa] - Reports for today will be processed at most every 10 seconds. You can change this value in Piwik UI > Settings > General Settings.
[2013-04-19 04:59:32] [3a7691aa] - Reports for the current week/month/year will be refreshed at most every 3600 seconds.
[2013-04-19 04:59:32] [3a7691aa] --force-all-periods was detected: we will process websites with visits in the last 7 days 0 hours
[2013-04-19 04:59:34] [3a7691aa] Will process 68 websites with new visits since 7 days 0 hours , IDs: 1, N+
[2013-04-19 04:59:34] [3a7691aa] Will process 90 other websites because some old data reports have been invalidated (eg. using the Log Import script) , IDs: 1, N+
[2013-04-19 04:59:34] [3a7691aa] -----------------------

Allow write access to the themes directory, upload the photo and then revert (root in our case).

chown lighttpd:lighttpd ./themes
# UPload
chown root:root themes/ themes/logo-header.png themes/logo.png

Purging Data

Maybe after a trial run you want to purge your data.

DELETE FROM piwik_log_action;
DELETE FROM piwik_log_conversion;
DELETE FROM piwik_log_conversion_item;
DELETE FROM piwik_log_link_visit_action;
DELETE FROM piwik_log_profiling;
DELETE FROM piwik_log_visit;
DELETE FROM piwik_logger_api_call;
DELETE FROM piwik_logger_error;
DELETE FROM piwik_logger_exception;
DELETE FROM piwik_logger_message;
DROP TABLE piwik_archive_blob_*;
DROP TABLE piwik_archive_numeric_*;

Upgrading Piwik

When upgrading from the git or if using the latest stable, after uploading you have to run the CoreUpdater

cd /opt/piwik
git pull --rebase
./composer.phar self-update
./composer.phar install --no-dev

# For older Piwik
php ./index.php -- "module=CoreUpdater"

# For newer Piwik
./console core:update

See Also