VnStat PHP: A Web Based Interface for Monitoring Network Bandwidth Usage

VnStat PHP a graphical interface application for most famous console mode network logger utility called “vnstat“. This VnStat PHP is a graphical frontend to VnStat, to view and monitor network traffic bandwidth usage report in nicely graphical format. It displays IN and OUT network traffic statistics in hourly, days, months, or full summary.

This article shows you how to install VnStat and VnStat PHP  in Linux systems.

VnStat PHP Prerequisites

You need to install the following software packages on your system.

  • VnStat: A command-line network bandwidth monitoring tool, must be installed, configured, and should collect network bandwidth statistics.
  • Apache: A Web Server to serve web pages.
  • PHP: A server-side scripting language for executing php scripts on the server.
  • php-gd extension: A GD extension for serving graphic images.

Step 1: Installing and Configuring VnStat Command Line Tool

VnStat is a command-line network bandwidth monitoring utility that counts bandwidth (transmit and received) on network devices and keeps the data in its own database.

Vnstat is a third-party tool and can be installed via enabling epel repository under Red Hat-based systems. Once you’ve enabled, you can install it using the yum command as shown below.

# yum install vnstat              [On CentOS/RHEL]
$ sudo apt-get install vnstat     [On Debian/Ubuntu]

To install the most latest version of VnStat, follow this article – How to Install vnStat to Monitor Network Traffic in Linux.

As I said Vnstat maintains its own database to keep all network information. To create a new database for network interface called “eth0“, issue the following command. Make sure to replace the interface name as per your requirements.

# vnstat -i eth0

Error: Unable to read database "/var/lib/vnstat/eth0".
Info: -> A new database has been created.

If you get the above error, don’t worry about such error, because you are executing the command first time. So, it creates a new database for eth0.

Now run the following command to update all enabled databases or only specific interface with -i parameter as shown. It will generate traffic statistics of IN and OUT of an eth0 interface.

# vnstat -u -i eth0

Next, add a crontab that runs every 5min and update the eth0 database to generate traffic statistics.

*/5 * * * * /usr/bin/vnstat -u >/dev/null 2>&1

Step 2: Installing Apache, Php, and Php-gd Extension

Install the following software packages with the help of a package manager tool called “yum” for Red Hat-based systems and “apt-get” for Debian based systems.

On RHEL/CentOS and Fedora
# yum install httpd php php-gd

Turn on Apache at system start-up and start the service.

# chkconfig httpd on
# service httpd start

Run the following “iptables” command to open Apache port “80” on the firewall and then restart the service.

# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# service iptables restart
On Debian/Ubuntu and Linux Mint
$ sudo apt-get install apache2 php php-gd
$ sudo /etc/init.d/apache2 start

Open port 80 for Apache.

$ sudo ufw allow 80

Step 3: Downloading VnStat PHP Frontend

Download the latest VnStat PHP source tarball file using “wget command” as shown below or visit THIS PAGE to grab the latest version.

# cd /tmp
# wget http://www.sqweek.com/sqweek/files/vnstat_php_frontend-1.5.1.tar.gz

Extract the source tarball file, using “tar command” as shown given.

# tar xvf vnstat_php_frontend-1.5.1.tar.gz

Step 4: Installing VnStat PHP Frontend

Once extracted, you will see a directory called “vnstat_php_frontend-1.5.1“. Copy the contents of this directory to the webserver root location as directory vnstat as shown below.

On RHEL/CentOS and Fedora
# cp -fr vnstat_php_frontend-1.5.1/ /var/www/html/vnstat

If SELinux enabled on your system, run the “restorecon” command to restore files default SELinux security contexts.

# restorecon -Rv /var/www/html/vnstat/
On Debian/Ubuntu and Linux Mint
# cp -fr vnstat_php_frontend-1.5.1/ /var/www/vnstat

Step 5: Configuring VnStat PHP Frontend

Configure it to match your setup. To do open the following file with VI editor and change the parameters as shown below.

On RHEL/CentOS and Fedora
# vi /var/www/html/vnstat/config.php
On Debian/Ubuntu and Linux Mint
# vi /var/www/vnstat/config.php

Set your default, Language.

// edit these to reflect your particular situation
$locale = 'en_US.UTF-8';
$language = 'en';

Define your network interfaces to be monitored.

// list of network interfaces monitored by vnStat
$iface_list = array('eth0', 'eth1');

You can set custom names for your network interfaces.

// optional names for interfaces
// if there's no name set for an interface then the interface identifier.
// will be displayed instead
$iface_title['eth0'] = 'Internal';
$iface_title['eth1'] = 'External';

Save and close the file.

Step 6: Access VnStat PHP and View Graphs

Open your favorite browser and navigate to any of the following links. Now you will see a fancy network graph that shows you a summary of network bandwidth usage in hours, days, and months.

http://localhost/vnstat/
http://your-ip-address/vnstat/
Sample Output
Install Vnstat PHP in Linux
VnStat PHP Network Summary

Reference Link

VnStat PHP Homepage

If you read this far, tweet to the author to show them you care. Tweet a thanks
Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

11 thoughts on “VnStat PHP: A Web Based Interface for Monitoring Network Bandwidth Usage”

  1. Everything works to install the vnstat, I did it previously, I got the front end installed in the web server directory but in the interface, none of the data is populated. vnstat on the command line works perfectly.

    Reply
    • I have the same issue. It seems to be caused by the change in vnstat version 2.0 and newer, change to SQLite database. I don’t think the PHP frontend will work with that format as it has not been updated since 2010. I will probably have a downgrade to vnstat 1.18.

      Reply
  2. On Linux Mint 17.3 only need to change this command:

    # cp -fr vnstat_php_frontend-1.5.1/ /var/www/vnstat

    into:

    $ sudo cp -fr vnstat_php_frontend-1.5.1/ /var/www/html/vnstat

    Reply
  3. i am new to linux i am running linux mint 17.3 i can run vnstat in the terminal and it tells me data usage but i cant get the web browser to work i want to know how much data i am using

    Reply
    • @Michael,

      Have you installed Apache with all required modules? have you added correct settings about your interfaces in vnstat config.php file? have you opened port 80 on the firewall to access to the vnstat over web? please check these things again and let me know what error you still getting on vnstat web?

      Reply
  4. Pretty good article.

    Thanks

    Just want to know when eth0 database is created, which type of database it is & if we want to open in another application how can we use it ?

    Thanks in advance

    Regards,

    Sandeep

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.