How to Install Piwik on Ubuntu

Posted by JeremyL on Thu 07 November 2013

Piwik offers a Free (GPL licensed) analytics platform were you control the data. Providing detailed reports for your sites and user-base. You will get information regarding the search engines and keywords used. Though do be aware because of current changes in Google this will not solve Keyword not Defined. You can also get data regarding the user-base language, specs on their computer, pages they enjoyed, downloaded files and much more. It is written in PHP5 and has many easy method to customize and expand to fit exactly into your sites needs. Piwik is the best alternative to Google Analytics, best of all it's Open Source. To find out about all the great features of Piwik check out this page.

To complete this you're going to need a running Ubuntu Server with either a LEMP or LAMP stack. If not please set that up now.

Installing Piwik on Ubuntu

First off we are going to want to make sure that the box is completely up to date. Remember if there were any kernel updates you should reboot, if you just want to reboot before continuing that is fine as well. Will make sure any new/updated services will also restart.

sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade
Piwik on LAMP stack

There are some prerequisites that you need.

sudo apt-get install php5-gd unzip

You may need to uncomment things like iconv in your php.ini file.

Let's move into the web directory

cd /var/www

Now we will need to download the most recent Piwik.

wget http://builds.piwik.org/latest.zip

Let's unzip it

unzip latest.zip

You can clean up the unneeded files

rm latest.zip

cd piwik
rm README.md LEAGALNOTICE

cd misc
rm How\ to\ install\ Piwik.html gpl-3.0.txt
cd ..

Now you will need to give the proper owner and group to the piwik folder.

 sudo chown -R www-data:www-data /var/www/piwik

Now lets create a new database for Piwik to use before continuing on with the process.

mysql -u root -p

You do not have to name your database what is used in this example.

CREATE DATABASE piwik_db;
CREATE USER 'piwik_user'@'localhost' IDENTIFIED BY 'YourSecurePassword';
GRANT ALL PRIVILEGES ON piwik_db.* TO 'piwik_user'@'localhost' WITH GRANT OPTION;

Your newly created user should have the following access to your Piwik database

SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, LOCK.

Now let's go ahead and restart Apache

sudo service apache2 restart

Go ahead and go to the Finish Installing portion now.

Piwik on LEMP stack

There are some prerequisites that you need.

sudo apt-get install php5-gd php5-fpm php5-cgi unzip

You may need to uncomment things like iconv in your php.ini file.

Let's move into the web directory

cd /var/www

Now we will need to download the most recent Piwik.

wget http://builds.piwik.org/latest.zip

Let's unzip it

unzip latest.zip

You can clean up the unneeded files

rm latest.zip

cd piwik
rm README.md LEAGALNOTICE

cd misc
rm How\ to\ install\ Piwik.html gpl-3.0.txt
cd ..

Now you will need to give the proper owner and group to the piwik folder.

 sudo chown -R www-data:www-data /var/www/piwik

Now lets create a new database for Piwik to use before continuing on with the process.

mysql -u root -p

You do not have to name your database what is used in this example.

CREATE DATABASE piwik_db;
CREATE USER 'piwik_user'@'localhost' IDENTIFIED BY 'YourSecurePassword';
GRANT ALL PRIVILEGES ON piwik_db.* TO 'piwik_user'@'localhost' WITH GRANT OPTION;

Your newly created user should have the following access to your Piwik database

SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, LOCK.

From here you will need to configure your NGINX install for your Piwik site. There is a great config setup available on GitHub that takes most of the pain out of it. You can find it HERE.

Finish Installing Piwik

From here you will complete the install process through your web browser of choice. Depending on how you put the directory you would access the install as so.

https://YourDomain.tld/piwik/

You will be greeted with the Welcome Screen, you will just need to press Next on this step.
Piwik Ubuntu Linux LAMP
LEMP

You will now be on the System Check page, this page should show you all green checks. If there are any red X's you need to solve those before carrying on to the next step. There should be no red checks. Now go ahead and press Next.
Piwik Ubuntu Linux LAMP
LEMP

Now we are at the Database Setup portion. You are going to need the information you used from the database setup above. You will want to set the database server section with 127.0.0.1 your localhost. For the login in name it is recommended to not use root for the username this is also the username you created when making the database for Piwik, make sure they match. Set a strong password. Enter your created database name from above. You can change the table prefix to anything you would like, it is recommended to change this. An example would be

pi_45_wik

You will want to make sure that the PDO_MYSQL adapter is selected and then press next.
Piwik Ubuntu Linux LAMP
LEMP

On the next screen you will be on the Creating Tables portion. Here you should see a screen that says tables created with success. If you do not see this you will need to go back and double check your database settings. You could also try logging in as the user you created for your Piwik install to make sure it is working.
Piwik Ubuntu Linux LAMP
LEMP

Now we need to setup the Super User. You will want to give a username and secure password, it is recommended to not use root, it can be anything you like. Make sure you put in a valid email address as it will be used if you ever loose your password and will be used to email reports if you select that option here or later after setup.
Piwik Ubuntu Linux LAMP
LEMP

Next we are going to setup an initial website that you want to have analytics on. Make sure you set the timezone that your site is based off. Such as if you site is an affiliate site and your affiliates days end in a different timezone you can match the site stats to the same that your affiliate company uses to better coincide the data. Select if you have an E-Commerce site or not. Once done press Next.
Piwik Ubuntu Linux LAMP
LEMP

You will now be on tracking code screen. This will be the JavaScript tracker you will be inserting into you code. It is recommended to not effect your sites speed to put the tracking code just before the

tag. This way it will not load until after your content and will not effect the end users experience.
If you're on something like Wordpress or Drupal you can find plugins to help ease the pain of putting in your tracking code. You can also manually do it if you wish. Press Next.
Piwik Ubuntu Linux LAMP
LEMP

You should now see the Congratulations page.
Piwik Ubuntu Linux LAMP
LEMP

Let's go ahead and confirm it is all working by trying to login with the user you created.

https://YourPiwikInstall.tld/piwik

Or whatever is your installs domain. You should see the login and screen and be able to successfully login.
Piwik Ubuntu Linux LAMP
LEMP

That is it, you now have a basic install of Piwik on your LAMP or LEMP stack. Feel free to share what tweaks and tracking methods you're using. Stay tuned for more posts to further your tracking abilities with Piwik.