Install and Configure NetBox IPAM on Ubuntu

From SdNOG wiki
Revision as of 11:17, 16 August 2024 by Manhal.Mohamed (talk | contribs) (Prerequisites)
Jump to navigation Jump to search

Prerequisites

Ensure the following packages and dependencies are installed:

  • Python 3.8 or higher
  • PostgreSQL 12 or higher
  • Redis
  • Netbox 3.0 or higher
  • Nginx
  • Gunicorn

Create a non-root user with sudo access netboxuser

1. Install and configure PostgreSQL

Install PostgreSQL:

sudo apt install postgresql libpq-dev -y

Start the database server:

sudo systemctl start postgresql

Enable the database server to start automatically on reboot:

sudo systemctl enable postgresql

Change the default PostgreSQL password:

sudo passwd postgres

Switch to the postgres user:

su - postgres

Log in to PostgreSQL:

psql

Create the NetBox database:

CREATE DATABASE netbox;

Create the netbox user with a strong password (replace my_strong_password with a secure one):

CREATE USER netbox WITH ENCRYPTED PASSWORD 'my_strong_password';

Grant privileges to the netbox user on the netbox database:

GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;

Exit PostgreSQL:

\q

Return to your non-root sudo user account:

exit

2. Install Redis®

Redis® is an in-memory key-value store used by NetBox for caching and queuing.

Install Redis®:

sudo apt install -y redis-server

3. Install and configure NetBox

Install all required packages:

sudo apt install python3 python3-pip python3-venv python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev libpq-dev libssl-dev zlib1g-dev git -y

Update pip to the latest version:

sudo pip3 install --upgrade pip

Create the installation directory and change to it:

sudo mkdir -p /opt/netbox/ && cd /opt/netbox/

Clone NetBox from the official Git repository:

sudo git clone -b master https://github.com/netbox-community/netbox.git .

Create a system user named netbox:

sudo adduser --system --group netbox

Grant the netbox user ownership of the media directory:

sudo chown --recursive netbox /opt/netbox/netbox/media/

Navigate to the configuration directory:

cd /opt/netbox/netbox/netbox/

Copy the example configuration file:

sudo cp configuration_example.py configuration.py

Create a symbolic link for the Python binary:

sudo ln -s /usr/bin/python3 /usr/bin/python

Generate a random SECRET_KEY for the configuration:

sudo /opt/netbox/netbox/generate_secret_key.py

Copy the generated secret key and use it in the configuration file.

Edit the configuration file:

sudo nano /opt/netbox/netbox/netbox/configuration.py

Update the file with the following settings:

ALLOWED_HOSTS = ['*']

DATABASE = {
    'NAME': 'netbox',
    'USER': 'netbox',
    'PASSWORD': 'my_strong_password',
    'HOST': 'localhost',
    'PORT': '',
}

SECRET_KEY = '<generated_secret_key>'

Run the upgrade script:

sudo /opt/netbox/upgrade.sh Enter the Python virtual environment:

source /opt/netbox/venv/bin/activate Navigate to the NetBox directory:

cd /opt/netbox/netbox Create a superuser account:

python3 manage.py createsuperuser Reboot the system:

sudo reboot

4. Configure Gunicorn

Copy the Gunicorn configuration file:

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

5. Configure Systemd

Copy the systemd service files:

sudo cp /opt/netbox/contrib/*.service /etc/systemd/system/ Reload the systemd daemon:

sudo systemctl daemon-reload Start the NetBox services:

sudo systemctl start netbox netbox-rq Enable the services to start at boot:

sudo systemctl enable netbox netbox-rq

6. Configure Nginx Web Server

Install the Nginx web server:

sudo apt install -y nginx Copy the Nginx configuration file:

sudo cp /opt/netbox/contrib/nginx.conf /etc/nginx/sites-available/netbox Edit the configuration file:

sudo nano /etc/nginx/sites-available/netbox Replace the server name with your server's IP address:

server {
    listen 80;
    server_name 192.0.2.10;  # Update this with your server's IP

    client_max_body_size 25m;

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

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Delete the default Nginx configuration:

sudo rm /etc/nginx/sites-enabled/default Create a symbolic link for the NetBox configuration:

sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/netbox

Restart the Nginx service:

sudo systemctl restart nginx