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.

In this guide, we’ll show you how to install NetBox IRM software on Debian 12 server step-by-step. We’ll show you the installation of NetBox with PostgreSQL as the database server and Apache2 web server as a reverse proxy. You’ll also secure your NetBox installation with SSl/TLS certificates.

Prerequisites

Before proceeding, ensure you have the following:

  • A Debian 12 server.
  • A non-root user with administrator privileges.
  • A public or local domain name pointed to the server IP address.

Installing Dependencies

NetBox is a web application based on the Python Django web framework. It can be installed with the PostgreSQL database server and Redis server for cache management.

In the following step, you will install those dependencies that NetBox needs, you will also install the Apache2 web server that will be used as a reverse proxy for your NetBox installation.

To start, execute the following apt command to update your Debian repository.

sudo apt update

<img alt="update repo" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/1-update-repo.png651ae7198ea7b.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="161" loading="lazy" src="data:image/svg xml,” width=”702″>

Then, install package dependencies for your NetBox IRM installation.

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

Type y to proceed with the installation of dependencies such as Apache2 web server, PostgreSQL database server, Redis, Git, Python3 packages, and some additional system libraries.

<img alt="install dependencies" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/2-install-deps.png651ae719aafe4.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="159" loading="lazy" src="data:image/svg xml,” width=”750″>

After dependencies are installed, verify each dependency by executing the command below.

Verify the apache2 service to ensure that the service is enabled and running.

sudo systemctl is-enabled apache2

sudo systemctl status apache2

If apache2 is running and enabled, you should get an output like the following:

<img alt="check apache2" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/3-check-apache2.png651ae719c3f64.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="215" loading="lazy" src="data:image/svg xml,” width=”750″>

Verify the PostgreSQL service to ensure that the service is running and enabled.

sudo systemctl is-enabled postgresql

sudo systemctl status postgresql

The PostgreSQL service should be running and enabled like this:

<img alt="check postgresql" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/4-checking-postgresql.png651ae719ddab5.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="179" loading="lazy" src="data:image/svg xml,” width=”750″>

Now verify the Redis service to ensure that the service is running and enabled.

sudo systemctl is-enabled redis

sudo systemctl status redis

The Redis service should be running and enabled like the following

<img alt="check redis" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/5-checking-redis.png651ae71a05402.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="264" loading="lazy" src="data:image/svg xml,” width=”750″>

Lastly, verify the Python version using the command below. The latest version of NetBox IRM supports Python v3.9, 3.10, and 3.11.

python3 --version

You should see Python 3.11 is installed on your Debian machine.

<img alt="check python" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/6-check-python.png651ae71a3b077.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="90" loading="lazy" src="data:image/svg xml,” width=”371″>

Configuring PostgreSQL Server

After installing dependencies, you will create a new PostgreSQL database and user that NetBox will use. To do that, you must log in to the PostgreSQL server via psql command line.

Log in to the PostgreSQL server by executing the command below.

sudo -u postgres psql

Run the following queries to create a new user netbox with password p4ssw0rd. Then, create a new database netboxdb with the owner netbox.

CREATE USER netbox LOGIN CREATEDB PASSWORD 'p4ssw0rd';

CREATE DATABASE netboxdb OWNER netbox;

<img alt="create database user" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/7-create-user-database.png651ae71a51e7b.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="233" loading="lazy" src="data:image/svg xml,” width=”626″>

After that, verify the list of users and databases on your PostgreSQL by executing the command below.

l

du

You should see the database netboxdb and user netbox created on your PostgreSQL server.

<img alt="check database user" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/8-check-database-user.png651ae71bb6912.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="275" loading="lazy" src="data:image/svg xml,” width=”750″>

Type quit to exit from the PostgreSQL server.

Next, log in to PostgreSQL using the new user netbox to the database netboxdb. This will ensure that the user netbox can connect to the database netboxdb.

sudo -u postgres psql --username netbox --password --host localhost netboxdb

Once connected, verify your connection using the following query.

conninfo

In the following output, you should see that you’ve connected to the database netboxdb via user netbox.

<img alt="verify connection" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/9-check-postgres-user.png651ae71bd2af1.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="213" loading="lazy" src="data:image/svg xml,” width=”750″>

Type quit again to exit from your PostgreSQL server.

Configuring Redis Server

With the PostgreSQL database and user created, the next step is to configure your Redis server that will be used as cache management for NetBox. To do that, you will modify the Redis configuration /etc/redis/redis.conf and verify your changes via redis-cli.

Open the default Redis configuration /etc/redis/redis.conf using the following nano editor command.

sudo nano /etc/redis/redis.conf

Uncomment the option requirepass and input your password that will be used to secure your Redis server.

requirepass p4ssw0rdNetBox

When finished, save and exit the file.

Now run the following systemctl command to restart the redis service and apply the changes that you’ve made.

sudo systemctl restart redis

To ensure that everything is working, you can verify Redis via redis-cli. Access your Redis server using the redis-cli command below.

redis-cli

Authenticate to the Redis server using the following AUTH query and be sure to change the password.

AUTH p4ssw0rdNetBox

Once authenticated, you should get the output OK.

Now run the PING query below to ensure that your connection is successful.

PING

If successful, you should get the output PONG from the Redis server.

<img alt="configure redis" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/10-configure-redis.png651ae71be85b4.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="270" loading="lazy" src="data:image/svg xml,” width=”469″>

Installing NetBox IRM

In the following section, you will download and install NetBox IRM to your system. You will download the NetBox source code via Git, then configure it by modifying the NetBox configuration, adding the database PostgreSQL server and Redis, and then you will also create an administrator user for NetBox.

First, execute the command below to create a new systemd user netbox that will be used for running NetBox installation.

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

Download NetBox IRM source code via git and change the ownership of the /opt/netbox directory to user netbox.

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

sudo chown -R netbox:netbox /opt/netbox

<img alt="create user download netbox" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/11-create-user-download-netbox.png651ae71c077ac.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="219" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, move your working directory to /opt/netbox and generate the NetBox secret key via the script generate_secret_key.py. Be sure to copy the generated secret key that will be used for your NetBox installation.

cd /opt/netbox/netbox/netbox

sudo -u netbox python3 ../generate_secret_key.py

<img alt="generate secret" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/12-generate-secret.png651ae71c1c66e.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="131" loading="lazy" src="data:image/svg xml,” width=”750″>

Copy the default configuration configuration_example.py to configuration.py, then open the new file configuration.py using the following nano editor command.

sudo -u netbox cp configuration_example.py configuration.py

sudo -u netbox nano configuration.py

Within the ALLOWED_HOSTS section, add your domain name or your server IP address.

ALLOWED_HOSTS = ['netbox.hwdomain.io', '192.168.10.15']

Input your PostgreSQL database details to the DATABASE section, including the database name, user, password, host, and port.

# database configuration

DATABASE = {

    'NAME': 'netboxdb',               # Database name

    'USER': 'netbox',               # PostgreSQL username

    'PASSWORD': 'p4ssw0rd', # PostgreSQL password

    'HOST': 'localhost',            # Database server

    'PORT': '',                     # Database port (leave blank for default)

    'CONN_MAX_AGE': 300,            # Max database connection age (seconds)

}

Within the REDIS section, input details of your Redis server to both tasks and caching options.

# Redis cache configuration

REDIS = {

    'tasks': {

        'HOST': 'localhost',      # Redis server

        'PORT': 6379,             # Redis port

        'PASSWORD': 'p4ssw0rdNetBox',           # Redis password (optional)

        'DATABASE': 0,            # Database ID

        'SSL': False,             # Use SSL (optional)

    },

    'caching': {

        'HOST': 'localhost',

        'PORT': 6379,

        'PASSWORD': 'p4ssw0rdNetBox',

        'DATABASE': 1,            # Unique ID for second database

        'SSL': False,

    }

}

Lastly, input your secret key to the SECRET_KEY section.

# Secret key

SECRET_KEY = 'ZjYbgz$)j!NnqJcZLR!NB2BCz4(Yyk=o^Xr(1sTIrM)ZyiE%nk'

When you’re done, save and exit the file.

Next, execute the /opt/netbox/upgrade.sh script to start your NextBox installation. This will create a new Python virtual environment, install some Python packages and libraries, run database migration to your PostgreSQL server, also generate static files for NextBox.

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

Below is the similar output you will get during the process.

<img alt="netbox installation" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/13-install-netbox.png651ae71c57f9d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="515" loading="lazy" src="data:image/svg xml,” width=”750″>

The database migration process.

<img alt="database migration" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/14-database-migration.png651ae71c8d464.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="545" loading="lazy" src="data:image/svg xml,” width=”639″>

Generating static files process.

<img alt="generate static files" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/15-generating-static-files.png651ae71ca2282.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="237" loading="lazy" src="data:image/svg xml,” width=”750″>

Below is the output when the installation is finished.

<img alt="installation finished" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/16-installation-finished.png651ae71cd66ba.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="424" loading="lazy" src="data:image/svg xml,” width=”695″>

After NetBox is configured, you will create an administrator user for NetBox. To do that, log in to the Python virtual environment that is created using the following command.

source /opt/netbox/venv/bin/activate

Move to the /opt/netbox/netbox directory and run the manage.py script to create a NetBox administrator user.

cd /opt/netbox/netbox

python3 manage.py createsuperuser

When asked, input your admin email address, username, and password details.

<img alt="create superuser" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/17-create-superuser.png651ae71ce7d34.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="239" loading="lazy" src="data:image/svg xml,” width=”689″>

Next, execute the manage.py again to verify your NetBox installation. With this, you will run NetBox on your local IP address with port 8000.

python3 manage.py runserver 0.0.0.0:8000 --insecure

<img alt="running netbox" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/18-test-netbox.png651ae71d2c1a5.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="178" loading="lazy" src="data:image/svg xml,” width=”750″>

Open your web browser and visit your server IP address followed by port 8000, such as http://192.168.10.15:8000/. If your installation is successful, you should get the NetBox IRM index page, and from there, click the Login button at the top right.

<img alt="NETBOX HOME" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/19-netbox-home.png651ae71d8fd05.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="529" loading="lazy" src="data:image/svg xml,” width=”750″>

Input your admin user and password that you’ve created, then click Sign In.

<img alt="LOGIN NETBOX" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/20-netbox-login.png651ae71dc664b.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="413" loading="lazy" src="data:image/svg xml,” width=”518″>

If everything goes well, you should get the NetBox dashboard like the following:

<img alt="netbox daSHBOARD" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/21-netbox-adashboard.png651ae71e50dbd.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="495" loading="lazy" src="data:image/svg xml,” width=”750″>

Back to your terminal and press Ctrl c to terminate the process.

Running NetBox as a Systemd Service

At this point, you’ve installed NetBox IRM on your Debian machine. To make you easier to manage NetBox, you will be running NetBox as a systemd service, which allows you to control NetBox via the systemctl utility.

Copy the file /opt/netbox/contrib/gunicorn.py to /opt/netbox/gunicorn.py, then open the gunicorn.py file using the nano editor command below.

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

sudo -u netbox nano /opt/netbox/gunicorn.py

Change the bind option to the following. This will run your NetBox installation in localhost port 8001 via gunicorn.

bind = '127.0.0.1:8001'

Save and close the file when finished.

Next, copy the systemd service files for NetBox to the /etc/systemd/system/ directory. This will copy the service file netbox, netbor-rq, and netbook-housekeeping to /etc/systemd/system/ directory. Then, reload the systemd manager to apply the new changes on your system.

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

sudo systemctl daemon-reload

Now you can start and enable both netbox and netbox-rq service using the systemctl command below. After executing the command, your NetBox installation will be running in the background as a systemd service.

sudo systemctl start netbox netbox-rq netbox-housekeeping

sudo systemctl enable netbox netbox-rq netbox-housekeeping

Lastly, verify both netbox and netbox-rq service using the following command.

sudo systemctl status netbox

sudo systemctl status netbox-rq

The following output indicates that the netbox service is running and enabled.

<img alt="check netbox" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/23-check-netbox.png651ae71eaa8aa.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="315" loading="lazy" src="data:image/svg xml,” width=”750″>

The below output confirms that the netbox-rq service is running and enabled.

<img alt="netbox rq" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/24-check-netbox-rq.png651ae71ed357f.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="252" loading="lazy" src="data:image/svg xml,” width=”750″>

Configuring Apache as a Reverse Proxy

In the following step, you will configure Apache2 as a reverse proxy. Before that, ensure that you have a domain name pointed to your server IP address, you can also use a local domain name.

If you’re using a local domain, you can run the following openssl command to generate SSL certificates. If you’re using the public domain, you can use Certbot to generate SSL certificates from Letsencrypt.

openssl req -x509 -newkey rsa:4096 -sha256 -days 365 

  -nodes -keyout /etc/ssl/private/netbox.key -out /etc/ssl/certs/netbox.crt -subj "https://www.howtoforge.com/CN=netbox.hwdomain.io"

  -addext "subjectAltName=DNS:netbox.hwdomain.io,IP:192.168.10.15"

Now copy the Apache virtual host configuration example for NetBox to /etc/apache2/sites-available/netbox.conf. Then, modify the file /etc/apache2/sites-available/netbox.conf using the following nano editor command.

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

sudo nano /etc/apache2/sites-available/netbox.conf

Change the domain name with your domain and change the path of SSL/TLS certificates with the proper path file.



    # CHANGE THIS TO YOUR SERVER'S NAME

    ServerName netbox.hwdomain.io

    ...



    ProxyPreserveHost On

    # CHANGE THIS TO YOUR SERVER'S NAME

    ServerName netbox.hwdomain.io

    SSLEngine on

    SSLCertificateFile /etc/ssl/certs/netbox.crt

    SSLCertificateKeyFile /etc/ssl/private/netbox.key

    ...

Save and exit the file when finished.

Now run the following command to enable some Apache2 modules that are needed for NetBox.

sudo a2enmod ssl proxy proxy_http headers rewrite

After that, execute the following command activate the virtual host file netbox.conf and verify your Apache2 configurations to ensure that you’ve proper syntax.

sudo a2ensite netbox.conf

sudo apachectl configtest

If you’ve proper Apache2 syntax, you should get the output Syntax OK.

<img alt="apache2 netbox" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/26-apache-netbox.png651ae71ee601e.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="217" loading="lazy" src="data:image/svg xml,” width=”507″>

Now run the following command to restart the Apache2 service and apply the changes that you’ve made.

sudo systemctl restart apache2

Your NetBox installation should be accessible via the domain name.

Open up your web browser and visit the domain name of your NetBox installation, such as https://netbox.hwdomain.io/. If everything goes well, you should see the NetBox index page like the following:

<img alt="netbox home" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/27-netbox.png651ae71f890ae.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="530" loading="lazy" src="data:image/svg xml,” width=”750″>

After logging in, you should see the NetBox IRM dashboard running with the domain name under the Apache2 reverse proxy.

<img alt="netbox dashboard" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/10/echo/28-netbox-dashboard.png651ae71fa56e6.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="538" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

In conclusion, you’ve now installed NetBox IRM on the Debian 12 server with the PostgreSQL database server and Apache2 web server used as a reverse proxy. You’ve also secured your NetBox installation via SSL/TLS certificates.