LibreNMS - Install Script On Ubuntu 24.4

From IT-Arts.net


Return to Wiki Index


install_librenms.sh

#!/bin/bash

# This script installs LibreNMS on Ubuntu 24.04 LTS

# Update system and install essential utilities
echo "Updating system and installing essential utilities..."
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl wget gnupg2 lsb-release ca-certificates software-properties-common

# Install required dependencies
echo "Installing required dependencies..."
sudo apt install -y apache2 mariadb-server php php-cli php-mbstring php-gd php-xml php-json php-zip php-mysql php-curl php-bcmath git unzip snmp fping mtr whois

# Enable Apache and MariaDB services
echo "Enabling Apache2 and MariaDB services to start on boot..."
sudo systemctl enable apache2
sudo systemctl enable mariadb

# Start Apache2 and MariaDB services
echo "Starting Apache2 and MariaDB services..."
sudo systemctl start apache2
sudo systemctl start mariadb

# Set up MySQL database for LibreNMS
echo "Setting up MariaDB for LibreNMS..."

# Secure the MariaDB installation
sudo mysql_secure_installation

# Log into MariaDB as root and create the LibreNMS database and user
sudo mysql -u root -e "CREATE DATABASE librenms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
sudo mysql -u root -e "CREATE USER 'librenms'@'localhost' IDENTIFIED BY 'librenmspassword';"
sudo mysql -u root -e "GRANT ALL PRIVILEGES ON librenms.* TO 'librenms'@'localhost';"
sudo mysql -u root -e "FLUSH PRIVILEGES;"

# Install LibreNMS from GitHub
echo "Cloning LibreNMS repository..."
cd /opt
sudo git clone https://github.com/librenms/librenms.git
sudo chown -R www-data:www-data /opt/librenms
sudo chmod -R 755 /opt/librenms

# Configure the web server
echo "Configuring Apache for LibreNMS..."

# Create a new Apache configuration file for LibreNMS
sudo bash -c 'cat <<EOF > /etc/apache2/sites-available/librenms.conf
<VirtualHost *:80>
    DocumentRoot /opt/librenms
    ServerName librenms.local

    <Directory /opt/librenms>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF'

# Enable the LibreNMS site and rewrite module
sudo a2ensite librenms.conf
sudo a2enmod rewrite

# Restart Apache to apply the changes
echo "Restarting Apache2..."
sudo systemctl restart apache2

# Set up the LibreNMS configuration file
echo "Setting up LibreNMS configuration file..."

# Copy the default config file
sudo cp /opt/librenms/config.php.default /opt/librenms/config.php

# Set permissions for LibreNMS
echo "Setting proper permissions for LibreNMS..."
sudo chown -R www-data:www-data /opt/librenms
sudo chmod -R 755 /opt/librenms

# Install Composer (PHP dependency manager)
echo "Installing Composer..."
sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

# Install PHP dependencies using Composer
echo "Installing PHP dependencies..."
cd /opt/librenms
sudo -u www-data /usr/local/bin/composer install --no-dev

# Set up the LibreNMS database
echo "Setting up the LibreNMS database..."
sudo -u www-data php /opt/librenms/scripts/composer_wrapper.php migrate

# Set up cron jobs for LibreNMS
echo "Setting up cron jobs for LibreNMS..."
sudo cp /opt/librenms/librenms.cron /etc/cron.d/librenms
sudo chmod 644 /etc/cron.d/librenms

# Finish installation by running the web-based setup
echo "LibreNMS should now be accessible at http://librenms.local or http://<your-server-ip>"
echo "Access the web interface and follow the instructions to finish the setup."

exit 0