In this tutorial, we'll explain how to install and configure NetBox on Ubuntu 24.04.
NetBox is an open-source, web-based Infrastructure Resource Modeling (IRM) application designed to help manage and document networking operations. It includes features for IP address management, data center infrastructure modeling, and device inventory. This tutorial provides detailed instructions for installing and configuring NetBox on an Ubuntu server, using the latest tools and best practices.
Prerequisites
Before proceeding, ensure you have the following:
- A fresh Ubuntu 22.04 dedicated server or KVM VPS.
- A non-root user with sudo privileges.
- Python 3.10 or later installed.
Install and Configure NetBox on Ubuntu
Step 1: Update System Packages
Start by updating your system packages to ensure everything is up to date:
sudo apt update && sudo apt upgrade -y
Step 2: Install Required Dependencies
NetBox relies on Python, PostgreSQL, and other tools. Install the required dependencies:
sudo apt install -y git python3-pip python3-venv libpq-dev nginx
Step 3: Install and Configure PostgreSQL
Install PostgreSQL, Here we are installing default PostgreSQL which might be old version availabe in Ubuntu repo. If you want to install latest version of PostgreSQL here is the PostgreSQL official documentation to follow:
sudo apt install -y postgresql
Log in to the PostgreSQL shell:
sudo -u postgres psql
Create a database and user for NetBox:
CREATE DATABASE netbox;
CREATE USER netbox_user WITH PASSWORD 'securepassword';
GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox_user;
\q
Step 4: Install Redis
NetBox uses Redis for caching and queuing. Install Redis:
sudo apt install -y redis-server
Ensure Redis is running:
sudo systemctl enable redis --now
Step 5: Download NetBox
Clone the NetBox repository:
git clone -b master https://github.com/netbox-community/netbox.git /opt/netbox
Modify the owner of the /opt/netbox
directory and set to www-data
.
chown -R www-data:www-data /opt/netbox
Navigate to the NetBox directory:
cd /opt/netbox
Step 6: Create a Python Virtual Environment
Set up a Python virtual environment:
python3 -m venv venv
source venv/bin/activate
Install required Python packages:
pip install -r requirements.txt
Step 7: Configure NetBox
Generate a Django secret key:
python generate_secret_key.py
Copy the key into configuration.py under the SECRET_KEY
field.
Copy the example configuration file:
cp netbox/netbox/configuration_example.py netbox/netbox/configuration.py
Edit configuration.py:
nano netbox/netbox/configuration.py
Set the database and Add your domain in ALLOWED_HOSTS
also add SECRET_KEY:
ALLOWED_HOSTS = ['netbox.example.com']
CSRF_TRUSTED_ORIGINS = ['https://netbox.hnxcode.dev']
SECRET_KEY = '<secret key here>'
DATABASE = {
'NAME': 'netbox',
'USER': 'netbox_user',
'PASSWORD': 'securepassword',
'HOST': 'localhost',
'PORT': '',
}
Save and exit the file
Step 8: Initialize NetBox
Run the database migrations:
python manage.py migrate
Create a superuser:
python manage.py createsuperuser
Collect static files:
python manage.py collectstatic
Step 9: Configure Gunicorn
Create a systemd service file for Gunicorn:
sudo nano /etc/systemd/system/netbox.service
Add the following content:
[Unit]
Description=NetBox WSGI Service
After=network.target
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/opt/netbox/netbox
ExecStart=/opt/netbox/netbox/venv/bin/gunicorn --workers 3 --bind unix:/opt/netbox/netbox/netbox.sock netbox.wsgi
Restart=always
[Install]
WantedBy=multi-user.target
Start and enable the Gunicorn service:
sudo systemctl enable netbox --now
Step 10: Configure Nginx
Create an Nginx configuration file for NetBox:
sudo nano /etc/nginx/sites-available/netbox
Add the following configuration:
server {
listen 80;
server_name your_domain_or_ip;
location /static/ {
alias /opt/netbox/netbox/static/;
}
location / {
proxy_pass http://unix:/opt/netbox/netbox.sock;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl reload nginx
Step 11 Install Certbot SSL
To install an SSL certificate using Certbot, follow these steps:
Install Certbot:
sudo apt install -y certbot python3-certbot-nginx
Obtain an SSL Certificate: Run the following command, replacing your_domain_or_ip
with your actual domain name:
sudo certbot --nginx -d your_domain_or_ip
Set Up Auto-Renewal: Certbot includes a systemd timer to renew certificates automatically. Test the renewal process with:
sudo certbot renew --dry-run
Step 12: Access NetBox
Open your browser and navigate to https://your_domain. Log in with the superuser credentials created earlier.
Conclusion
In this tutorial, we've explained how to install and configure NetBox on Ubuntu 24.04. From here, you can explore its features to manage your networking infrastructure efficiently. Regularly update NetBox and its dependencies to ensure optimal performance and security.