Installing NetBox on Ubuntu

NetBox is a web application for documenting and management of computer networks.
With a large number of networks, devices, it allows you to visually place it in racks, maintain a database, etc.
For example, I will perform a test installation of NetBox on Ubuntu 18.04.

Switch to root user:

sudo -i

Install the PostgreSQL database server:

apt update
apt install postgresql libpq-dev
systemctl status postgresql
systemctl is-enabled postgresql
systemctl enable postgresql
systemctl restart postgresql

Let’s create a database and a user:

sudo -u postgres psql
CREATE DATABASE netbox;
CREATE USER netbox WITH PASSWORD 'ixnfo.com';
GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
\q

Let’s make a test connection to the database and disconnect:

psql -U netbox -W -h localhost netbox
\q

Install Redis:

apt install redis-server
systemctl is-enabled redis
systemctl enable redis
systemctl restart redis
systemctl status redis

Let’s check (the answer should display “PONG”):

redis-cli ping

Let’s install other necessary components:

apt install python3.7 python3-pip python3.7-venv python3.7-dev build-essential libxml2-dev libxslt1-dev libffi-dev libpq-dev libssl-dev zlib1g-dev supervisor

Copy the link to the latest version of NetBox here https://github.com/netbox-community/netbox/releases and download, for example:

cd /opt/
wget https://github.com/netbox-community/netbox/archive/v3.0.2.tar.gz
tar -xzf v3.0.2.tar.gz -C /opt
ln -s netbox-3.0.2/ netbox
ls -l /opt | grep netbox
cd /opt/netbox/

Let’s create a system user:

adduser --system --group netbox
chown --recursive netbox /opt/netbox/netbox/media/

Install Netbox dependencies:

python3.7 -m pip install -r /opt/netbox/requirements.txt
python3.7 -m pip install django psycopg2

Let’s create a configuration file from the example:

cd /opt/netbox/netbox/netbox/
cp configuration.example.py configuration.py
nano /opt/netbox/netbox/netbox/configuration.py

We indicate the previously created user to access the database, in ALLOWED_HOSTS we indicate the domain of your server or all *:

DATABASE = {
    'NAME': 'netbox',
    'USER': 'netbox',
    'PASSWORD': 'ixnfo.com',
...
ALLOWED_HOSTS = ['ixnfo.com', 'test.ixnfo.com']
#ALLOWED_HOSTS = ['*']

We generate a secret key:

python3 -V
cd /opt/netbox/netbox/
python3.7 ./generate_secret_key.py

Let’s specify it in the configuration file, for example:

nano /opt/netbox/netbox/netbox/configuration.py
SECRET_KEY = 'XeV@7#NzCRRw-SPjI6JJi3Rh##qqcU2JpOA%z0b_PY1-So7X5Q'

Let’s configure gunicorn:

pip3 install gunicorn

nano /opt/netbox/gunicorn_config.py

command = '/usr/local/bin/gunicorn'
pythonpath = '/opt/netbox/netbox'
bind = '192.168.24.73:8001'
workers = 3
user = 'www-data'

Let’s create a supervisor configuration file:

nano /etc/supervisor/conf.d/netbox.conf

[program:netbox]
command = gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
directory = /opt/netbox/netbox/
user = www-data
systemctl restart supervisor.service
systemctl is-enabled supervisor.service
systemctl enable supervisor.service
systemctl status supervisor.service

Let’s transfer the data to the database and static files:

cd /opt/netbox/netbox/
python3.7 manage.py migrate
python3.7 manage.py collectstatic
python3.7 manage.py loaddata initial_data

Let’s create an administrator:

python3.7 manage.py createsuperuser

Let’s update:

/opt/netbox/upgrade.sh

Let’s set up a web configuration, for example:

nano /etc/nginx/conf.d/netbox.conf

server {
    listen 80;
    server_name test.ixnfo.com;
    client_max_body_size 25m;

    location /static/ {
        alias /opt/netbox/netbox/static/;
    }

    location / {
        proxy_pass http://192.168.24.73:8001;
    }
}

Let’s check the nginx web configuration and restart it:

nginx -t
systemctl restart nginx

After that, you can open NetBox in a browser and log in under the previously created administrator.

Leave a comment

Leave a Reply