NetBox is a free and open-source web application designed to manage and document computer networks. It offers a browser-based interface to manage IP addresses and data center infrastructure. Developed with the Django Python framework, NetBox uses PostgreSQL for its database. With it, you can centrally manage and document virtual machines, clusters, data circuits, network, console, and power connections.
This guide will walk you through the installation of NetBox on an Ubuntu 18.04 server.
Requirements
- An Ubuntu 18.04 server.
- Root access configured on your server.
Getting Started
First, update your system packages to their latest versions using the following commands:
apt-get update -y apt-get upgrade -y
After updating, restart your system to apply the changes.
Next, install the necessary dependencies for running NetBox:
apt-get install wget ca-certificates nginx supervisor git gcc python3 python3-dev python3-pip python3-setuptools build-essential libxml2-dev libxslt1-dev libffi-dev graphviz libpq-dev libssl-dev zlib1g-dev unzip -y
Install and Configure PostgreSQL
Since the latest PostgreSQL version isn’t available in the default Ubuntu 18.04 repository, you’ll first need to add the PostgreSQL repository:
Download and add the GPG key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
Add the repository:
nano /etc/apt/sources.list.d/postgres.list
Include this line:
deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main
After saving the file, update the repository and install PostgreSQL:
apt-get update -y apt-get install postgresql postgresql-contrib -y
Verify PostgreSQL’s status:
systemctl status postgresql
You should see something similar to:
? postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) Active: active (exited) since... ...
Create Database and Configure NetBox in PostgreSQL
Create a database and user for NetBox:
Log into the PostgreSQL shell:
su - postgres psql
Create a database and assign a user with permissions:
CREATE DATABASE netbox; CREATE USER netbox WITH PASSWORD 'password'; GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
Exit PostgreSQL:
\q exit
Install and Configure NetBox
Clone the NetBox repository to the /opt directory:
cd /opt git clone -b master https://github.com/digitalocean/netbox.git
Change to the NetBox directory and generate a Django secret key:
cd /opt/netbox/netbox/netbox/ ./generate_secret_key.py
You will see an output similar to:
+XHR3o&7K6isFk^DLc2%rIqCanlE@zTB(jwN#tfGbV=O1hgMU$
Rename the example configuration file:
mv configuration.example.py configuration.py
Edit the configuration file to set your database credentials and secret key:
nano configuration.py
Make appropriate changes:
ALLOWED_HOSTS = ['your-server-ip'] DATABASE = { 'NAME': 'netbox', 'USER': 'netbox', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '', } SECRET_KEY = '+XHR3o&7K6isFk^DLc2%rIqCanlE@zTB(jwN#tfGbV=O1hgMU$'
Save and close the file.
Install the required Python dependencies:
pip3 install -r /opt/netbox/requirements.txt
Migrate the database:
cd /opt/netbox/netbox/ python3 manage.py migrate
Create an administrative user for NetBox:
python3 manage.py createsuperuser
Follow the prompts to enter a username and password for the administrator account.
Move static files and load initial data:
python3 manage.py collectstatic python3 manage.py loaddata initial_data
Install and Configure Gunicorn for NetBox
Install Gunicorn using pip:
pip3 install gunicorn
Create a Gunicorn configuration file:
nano /opt/netbox/gunicorn_config.py
Add the following lines:
command = '/usr/local/bin/gunicorn' pythonpath = '/opt/netbox/netbox' bind = 'your-server-ip:8001' workers = 3 user = 'www-data'
Configure Supervisor for NetBox
Create a Supervisor configuration to manage NetBox:
nano /etc/supervisor/conf.d/netbox.conf
Add the following configuration:
[program:netbox] command = gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi directory = /opt/netbox/netbox/ user = www-data
Restart the Supervisor service and enable it at boot:
systemctl restart supervisor systemctl enable supervisor
Configure Nginx for NetBox
Create an Nginx configuration file for NetBox:
nano /etc/nginx/sites-available/netbox
Add the following lines:
server { listen 80; server_name your-domain-name; client_max_body_size 25m; location /static/ { alias /opt/netbox/netbox/static/; } location / { proxy_pass http://your-server-ip:8001; } }
Enable the virtual host and restart Nginx:
ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/ systemctl restart nginx
Access the NetBox Web Interface
NetBox is now ready. Access the web interface by navigating to http://your-domain.com in your browser. You should see the login page.
After logging in, you’ll be directed to the NetBox dashboard.
Congratulations! You’ve successfully installed and configured NetBox on Ubuntu 18.04. You can now manage your network resources centrally. If you have questions, feel free to ask.
FAQ
- What is NetBox?
- NetBox is an open-source web application designed to help manage and document computer networks using a browser interface. It is built on the Django Python framework and leverages PostgreSQL for database management.
- Why use Gunicorn with NetBox?
- Gunicorn is a Python WSGI HTTP server that allows you to run your NetBox application efficiently behind a web server, providing asynchronous processing capabilities that can manage multiple requests simultaneously.
- How do I change the NetBox admin password?
- You can change the admin password by accessing the Django management shell and using the createsuperuser command or by following Django’s documentation on updating user passwords from the command line.
- Can I use a different database instead of PostgreSQL?
- While NetBox is optimized for PostgreSQL, it may technically be possible to use other databases by modifying database backends in Django configurations. However, this will require significant effort and is not recommended or supported.
- How do I backup my NetBox data?
- You can back up your NetBox data by creating a dump of the PostgreSQL database using tools like `pg_dump` and ensuring the static file directories are also backed up.