Installing Cacti on Ubuntu 22.04

Posted on 2nd of Apr, 2023

1) Update the system to get us to a solid start point

apt update && apt dist-upgrade -y

2) Install the required Cacti dependancies

sudo apt install -y apache2 php-mysql libapache2-mod-php php-xml php-ldap php-mbstring php-gd php-gmp snmp php-snmp rrdtool librrds-perl

3) Install a database server (both 'mysql-server' and 'mariadb-server' work, I'm using mysql out of personal preference)

apt install mysql-server -y

3.1. Modify the MySQL DB server to get the correct settings required for Cacti

nano /etc/mysql/mysql.conf.d/mysqld.cnf
collation-server = utf8mb4_unicode_ci
max_heap_table_size = 128M
tmp_table_size = 64M
join_buffer_size = 128M
innodb_buffer_pool_size = 1024M
innodb_flush_log_at_timeout = 3
innodb_read_io_threads = 32
innodb_write_io_threads = 16
innodb_buffer_pool_instances = 9
innodb_io_capacity = 5000
innodb_io_capacity_max = 10000

3.2. Restart the MySQL server for the changes above to take effect

service mysql restart

4) Edit the PHP configuration to set the correct timezone & allocate some more resources (both the below files need the same settings changed)

nano /etc/php/7.4/cli/php.ini
nano /etc/php/7.4/apache2/php.ini

4.1. The following settings need to be changed

date.timezone = Europe/London
memory_limit = 512M
max_execution_time = 60

5) Setup a database for Cacti to use

mysql -u root -p
> CREATE USER 'cacti'@'localhost' IDENTIFIED BY 'cactipassword';
> create database cacti;
> GRANT ALL PRIVILEGES ON cacti.* TO 'cacti'@'localhost';
> flush privileges;
> exit

6) Import timezone data into MySQL

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql

6.1. Grant cactiuser access to this timezone data

sudo mysql -u root -p
> GRANT SELECT ON mysql.time_zone_name TO cacti@localhost;
> flush privileges;
> exit

7) Grab the latest version of Cacti from their download site


7.1. Extract and move the download into the right location: '/opt/cacti'

tar -zxvf cacti-latest.tar.gz && mv cacti-1* /opt/cacti

8) Import the Cacti database into the new live database

mysql -u root -p cacti < /opt/cacti/cacti.sql

9) Edit the Cacti config file and set the right database settings

nano /opt/cacti/include/config.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "cactipassword";
$database_port = "3306";
$database_ssl = false;

10) Set the Cacti Cron (if you are looking for 1min polling you need to change the first value from '/5' to just '')

nano /etc/cron.d/cacti
* * * * * www-data php /opt/cacti/poller.php > /dev/null 2>&1

11) Reconfigure Apache2 to serve the correct content (drop the below content into the cacti.conf file)

nano /etc/apache2/sites-available/cacti.conf
<VirtualHost *:80>
        DocumentRoot /opt/cacti

        Alias /cacti /opt/cacti

        <Directory /opt/cacti>
                Options +FollowSymLinks
                AllowOverride None
        <IfVersion >= 2.3>
                Require all granted
        <IfVersion < 2.3>
                Order Allow,Deny
                Allow from all

        AddType application/x-httpd-php .php

        <IfModule mod_php.c>
                php_flag magic_quotes_gpc Off
                php_flag short_open_tag On
                php_flag register_globals Off
                php_flag register_argc_argv On
                php_flag track_vars On
                php_value mbstring.func_overload 0
                php_value include_path .

        DirectoryIndex index.php

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

11.1. Swap out the running config on Apache2 for the one we've just created

a2dissite 000-default.conf && a2ensite cacti.conf

11.2. Restart Apache2 for the config we've just changed to take effect

service apache2 restart

12) Create the required log files and set the correct permissions for the Cacti directory

touch /opt/cacti/log/cacti.log
chown -R www-data:www-data /opt/cacti/