Install cacti on Ubuntu Server with Apache and PHP-FPM

Written by
Date: 2013-01-27 22:03:35 00:00


Introduction

If you need to monitor Linux servers, or routers, or any other SNMP enabled device or software, Cacti is maybe your best choice.

Cacti is a complete network graphing solution designed to harness the power of RRDTool's data storage and graphing functionality. Cacti provides a fast poller, advanced graph templating, multiple data acquisition methods, and user management features out of the box. All of this is wrapped in an intuitive, easy to use interface that makes sense for LAN-sized installations up to complex networks with hundreds of devices.

– Cacti Home page

Cacti on Ubuntu Server

The simplier and easiest way to install cacti on Ubuntu is to run:

sudo apt-get install cacti

The problem with this way is that, it is going to try to install Apache, with pre-fork MPM and run PHP as a module and Apache with MPM Worker is a lot more efficient.

Install Cacti on Ubuntu Server with Apache2 MPM worker and extarnal PHP/CGI PHP-FPM

We will need to Apache, MySQL, PHP-FPM. Once that is done we will continue to adapt the installation to work with cacti. Cacti will be downloaded from its page.

Prepare the server for cacti

This are packages need to support cacti. First install the dependencies needed.

sudo apt-get install dbconfig-common fontconfig libcairo2 libdatrie1 libdbi1 rrdtool php5-snmp php5-ldap snmp

Configure PHP

PHP needs to have some modules enabled for this work. Here is the list of needed modules:

  • mysql
  • SNMP
  • XML
  • Session
  • Sockets
  • LDAP
  • GD

You may run this command to be sure you have all modules available.

php -m

Configure Apache

I prefer to run cacti on its own server, or at least on its own virtual server, so I will create that virtual server.

mkdir -p /var/www/monitor.domain.com/{public_html,logs}

Then create the file in /etc/apache2/sites-available/ It will have this content::

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	ServerName monitor.domain.com

	DocumentRoot /var/www/monitor.domain.com/public_html
	<Directory />
		Options FollowSymLinks
		AllowOverride All
	</Directory>
	<Directory /var/www/monitor.domain.com/public_html/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

	ErrorLog /var/www/monitor.domain.com/logs/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog /var/www/monitor.domain.com/logs/access.log combined
	

	<IfModule mod_fastcgi.c>
	    AddHandler php5-fcgi .php
	    Action php5-fcgi /php5-fcgi
	    Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
	    # FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
	</IfModule>

</VirtualHost>

Now enable the site:

sudo a2ensite monitor.domain.com

Download Cacti

You have to download the latest version, which you can get from cacti download page. Once downloaded, uncompress it, and copy its contents to /var/www/monitor.domain.com/public_html/.

Configure MySQL

We need to create the database and prepare it for cacti, run all these commands, you will need the MySQL root password.

mysqladmin --user=root -p create cacti
mysql -u root -p cacti < cacti.sql

The file cacti.sql comes with your cacti package.

Now, get the MySQL command prompt.

mysql -u root -p

And there run these commands:

GRANT ALL ON cacti.* TO "cacti"@"localhost" IDENTIFIED BY 'your-strong-password';
FLUSH PRIVILGES;

Two more steps. Create a user that will run cacti, give him the ownership of some folders, and add a cronjob.

sudo adduser cactiuser
sudo chown -R cactiuser rra/ log/

The last command should be run in your cacti folder, in this case /var/www/monitor.domain.com/public_html/.

Finally add the cronjob:

sudo crontab -e

And add this line at the end::

*/5 * * * * cactiuser php /var/www/monitor.domain.com/public_html/poller.php > /dev/null 2>&1

Configure Cacti

Because I am running cacti in the root directory I need to change some defaults.

sudo vim /var/www/monitor.domain.com/public_html/include/config.php

And be sure you have these lines::

$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "your-strong-password";
$database_port = "3306";
$database_ssl = false;

$url_path = "/";

Conclusion

You are now ready to monitor your servers, and you have cacti running in an efficient server with Apache, and PHP-FPM as an external CGI instead of a module, which is less efficient.

Be sure to update cacti to the latest version, as soon as new ones are released. This is important in order to protect the security your information, and also the security of your server.