How to Install NetBox IRM (Infrastructure Resource Modelling Tool) on Ubuntu 24.04 Server

NetBox is an Infrastructure Resource Modelling (IRM) designed for network automation and infrastructure engineering. Initially, it was created by the DigitalOcean team, and now become an open-source project released under the Apache 2 License.

NetBox was created in the Python Django Web framework with PostgreSQL as the default database, and the installation of NetBox is quite similar to other Python Django web applications.

This tutorial will show you how to install Netbox IRM on the Ubuntu 24.04 server with PostgreSQL as the database and Apache as a reverse proxy. You'll also secure Netbox with HTTPS through Certbot and Letsencrypt.

Prerequisites

Before moving on, make sure you have the following:

  • An Ubuntu 24.04 server
  • A non-root user with administrator privileges
  • A domain name pointed to server IP address

Installing Dependencies

To install Netbox, you must ensure dependencies are installed on your Ubuntu server. This includes Python3, PostgreSQL server, Apache web server, Redis, and system libraries. In this section, you'll install those packages with the APT package manager.

First, run the following command to update your Ubuntu package index.

sudo apt update

Once updated, install dependencies for Netbox with the following command. In this example, you'll install PostgreSQL, Apache, Python, Redis, Certbot, build dependencies, and some libraries.

sudo apt install apache2 postgresql postgresql-common libpq-dev redis-server git certbot python3 python3-pip python3-venv python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev libssl-dev zlib1g-dev

Enter 'Y' to confirm the installation.

install deps

After the installation is finished, run the 'systemctl' command below to verify the Apache web server status.

sudo systemctl is-enabled apache2
sudo systemctl status apache2

In the following output, you can see that Apache is running and enabled.

check apache2

Lastly, check the PostgreSQL server status with the following:

sudo systemctl is-enabled postgresql
sudo systemctl status postgresql

In the output below, you can see that PostgreSQL is enabled and running with the status exited (the systemd cannot find the service to monitor).

check service postgresql

Setting up PostgreSQL

After dependencies are installed, you need to set up the PostgreSQL server by creating a new user and database that Netbox will use.

Log in to the PostgreSQL server using the command below.

sudo -u postgres psql

Now run the following queries to create a new user 'netbox' with the password 'netboxpassword' and the new database 'netboxdb'.

CREATE USER netbox LOGIN CREATEDB PASSWORD 'netboxpassword';
CREATE DATABASE netboxdb OWNER netbox;

Once the database and user are created, verify the list of databases and users on PostgreSQL with the following.

\l
\du

You can see below the database 'netboxdb' and user 'netbox' is created.

Type 'quit' to exit from the PostgreSQL server.

list databases

list users

Downloading Configuring Netbox

Now that the PostgreSQL database is ready, you'll download and install Netbox to your Ubuntu machine. You'll set up the domain name, secret key, and database for Netbox.

First, run the command below to create a new user 'netbox' on your Ubuntu machine.

sudo useradd -r -d /opt/netbox -s /usr/sbin/nologin netbox

Now run the command below to download the Netbox source code with the 'git' command, and change the ownership to the 'netbox' user.

sudo git clone -b master --depth 1 https://github.com/netbox-community/netbox.git /opt/netbox
sudo chown -R netbox:netbox /opt/netbox

Move to the '/opt/netbox/netbox/netbox' directory and execute the 'generate_secret_key.py' file. Copy the secret key to your note.

cd /opt/netbox/netbox/netbox
sudo -u netbox python3 ../generate_secret_key.py

Next, copy the 'configuration.py' file and modify it using the 'nano' editor.

sudo -u netbox cp configuration_example.py configuration.py
sudo -u netbox nano configuration.py

Add your local IP address and domain name to the 'ALLOWED_HOSTS'.

ALLOWED_HOSTS = ['netbox.howtoforge.local', '192.168.10.60']

Enter your PostgreSQL database name, user, and password in the 'DATABASE' section.

# database configuration
DATABASE = {
'NAME': 'netboxdb', # Database name
'USER': 'netbox', # PostgreSQL username
'PASSWORD': 'netboxpassword', # PostgreSQL password
'HOST': 'localhost', # Database server
'PORT': '', # Database port (leave blank for default)
'CONN_MAX_AGE': 300, # Max database connection age (seconds)
}

Paste your secret key to the 'SECRET_KEY' section.

SECRET_KEY = '98%D9uh8S%)T0QhQ@Z3zYf%Hik^CqWJ)Uwz%0J7TZUztmNiitG'

Save the file and exit the editor when finished.

Migrating Netbox and setting up administrator

Now that you've configured Netbox with the PostgreSQL server, you're ready to migrate the database and create an administrator user for Netbox installation.

Execute the 'upgrade.sh' script using the command below. This will create a Python environment, install dependencies for Netbox, migrate the Netbox database, connect to Redis, and then generate static files.

sudo -u netbox /opt/netbox/upgrade.sh

netbox configure

Below you can see the database migration for Netbox.

migrate

And below when 'manage.py' generates static files.

After the process is finished, you need to create a new admin user for Netbox through the 'manage.py' script.

static files

Run the following command to activate the Netbox virtual environment. Once activated, your shell will become such as '(venv) user@hostname'.

source /opt/netbox/venv/bin/activate

Go to the '/opt/netbox/netbox' directory and execute the 'manage.py' script like the following:

cd /opt/netbox/netbox
python3 manage.py createsuperuser

Enter your username, email address, and password for Netbox administrator.

create admin

Once complete, execute the 'deactivate' command to exit from the Netbox virtual environment.

Running Netbox as a service

In this section, you'll copy Netbox configurations that will be used to run Netbox as a systemd service. You need to ensure that Netbox running under Gunicorn, and then copy Netbox service files to the '/etc/systemd/system' directory.

First, copy the 'gunicorn.py' script to the '/opt/netbox' directory. This script contains the configuration for Netbox to run under the Python WSGI server.

sudo -u netbox cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py

Now run the command below to copy service files for Netbox to the '/etc/systemd/system' directory and reload the systemd manager.

sudo cp -v /opt/netbox/contrib/*.service /etc/systemd/system/
sudo systemctl daemon-reload

Once systemd is reloaded, run the 'systemctl' command below to start and enable Netbox services.

sudo systemctl enable --now netbox netbox-rq netbox-housekeeping

start and enabke

Lastly, verify Netbox services to ensure it is running with the following command.

sudo systemctl status netbox netbox-rq netbox-housekeeping

In the following output, you can see the 'netbox' service is running and enabled.

netbox

For the 'netbox-rq' service, you should get an output like this:

netbox-rq

Lastly, the 'netbox-housekeeping' with the status 'dead' because it will run as a timer.

netbox-housekeeping

Configuring Apache as a reverse proxy

At this point, Netbox is running in the background as a service under the Python WSGI server. To make it accessible from outside, you need to configure Apache as a reverse proxy. Before that, you also need to generate SSL certificates to secure Netbox installation.

First, run the command below to stop the 'apache2' service.

sudo systemctl stop apache2

Now generate SSL certificates from Letsencrypt with the 'certbot' command below. Make sure to change the domain name and email address with your information.

sudo certbot certonly --standalone --preferred-challenges http -d netbox.howtoforge.local

After the process is finished, your SSL certificates will be available in the '/etc/letsencrypt/live/domain.com' directory.

Next, copy the Apache configuration for Netbox to the '/etc/apache2/sites-available/netbox.conf' file and modify it using the 'nano' editor.

sudo cp /opt/netbox/contrib/apache.conf /etc/apache2/sites-available/netbox.conf
sudo nano /etc/apache2/sites-available/netbox.conf

Replace the domain name within the 'ServerName' option and change the path of your SSL certificates.

<VirtualHost *:80>
# CHANGE THIS TO YOUR SERVER'S NAME
ServerName netbox.howtoforge.local

...
</VirtualHost>

<VirtualHost *:443>
ProxyPreserveHost On

# CHANGE THIS TO YOUR SERVER'S NAME
ServerName netbox.howtoforge.local

SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/netbox.howtoforge.local/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/netbox.howtoforge.local/privkey.pem
...
</VirtualHost>

Save the file and exit the editor when done.

Now run the command below to activate Apache modules and the virtual host file 'netbox.conf'.

sudo a2enmod ssl proxy proxy_http headers rewrite
sudo a2ensite netbox.conf

After the virtual host is activated, run the following command to restart Apache and apply your changes. With this, your Netbox installation should be running under the Apache reverse proxy with HTTPS enabled.

sudo systemctl restart apache2

Lastly, visit https://netbox.howtoforge.local/ with your web browser and you'll be redirected to the Netbox login page. Enter your admin username and password, then click Sign In.

login

You'll see the following Netbox dashboard if you've proper admin credentials.

dashboard

Click on the Admin > System menu to see information on your Netbox installation. In the following output, you can see Netbox v4.1.7 is installed with the PostgreSQL database.

dashboard

Conclusion

Congratulations! You've finished the installation of Netbox IRM on the Ubuntu 24.04 server. You've Netbox up and running with PostgreSQL as the database server and Apache as a reverse proxy. You've also secured Netbox with HTTPS through Certbot.

Share this page:

0 Comment(s)