Netbox is a robust, open-source tool designed for managing IP addresses (IPAM) and data center infrastructure management (DCIM). Originally created by the network engineering team at DigitalOcean, Netbox is developed on the Django Python framework and uses PostgreSQL for data storage. Its main objective is to serve as a specialized source of truth for network operations, storing valuable information about networks, VMs, and inventory.
In this tutorial, we will guide you through the steps to install Netbox on Ubuntu 20.04 with Nginx acting as a reverse proxy.
Prerequisites
- Ubuntu 20.04 server setup.
- A configured root password on your server.
Getting Started
Before we begin, let’s install necessary dependencies required by Netbox. Execute the following command:
apt-get install nginx git gcc supervisor python3 python3-dev python3-pip python3-setuptools build-essential libxml2-dev libxslt1-dev libffi-dev graphviz libpq-dev libssl-dev zlib1g-dev -y
With all essential packages installed, we can move to the next step.
Install and Configure PostgreSQL Database
Netbox utilizes PostgreSQL for its database needs. Install it using:
apt-get install postgresql postgresql-contrib -y
Once PostgreSQL is set up, log in with:
su - postgres postgres@ubuntu2004:~$ psql
You’ll see:
psql (12.2 (Ubuntu 12.2-4)) Type "help" for help.
Create a database and a user for Netbox:
postgres=# CREATE DATABASE netbox; postgres=# CREATE USER netbox WITH PASSWORD 'password';
Grant privileges to the Netbox database:
postgres=# GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
Finally, exit the PostgreSQL shell:
postgres=# exit postgres@ubuntu2004:~$ exit
Install and Configure Netbox
Change to the /opt directory and download the Netbox source:
cd /opt/ git clone -b master https://github.com/digitalocean/netbox.git
Create a Python symbolic link:
ln -s /usr/bin/python3 /usr/bin/python
Generate a Django SECRET Key by navigating:
cd /opt/netbox/netbox/ ./generate_secret_key.py
Output will look like:
wcq@L2)eTDpo(k^f4Sm9bariUnK0syCPMGEIjW6XV_8l5xhB7z
Change directory and rename the configuration file:
cd netbox mv configuration.example.py configuration.py
Edit the Netbox configuration file:
nano configuration.py
Make the necessary changes:
ALLOWED_HOSTS = ['your-server-ip'] # PostgreSQL database configuration. See the Django documentation for a complete list of available parameters: # https://docs.djangoproject.com/en/stable/ref/settings/#databases DATABASE = { 'NAME': 'netbox', # Database name 'USER': 'netbox', # PostgreSQL username 'PASSWORD': 'password', # PostgreSQL password 'HOST': 'localhost', # Database server 'PORT': '', # Database port (leave blank for default) 'CONN_MAX_AGE': 300, # Max database connection age } SECRET_KEY = 'wcq@L2)eTDpo(k^f4Sm9bariUnK0syCPMGEIjW6XV_8l5xhB7z'
Save the file, then install Python dependencies:
pip3 install -r /opt/netbox/requirements.txt
Migrate the database:
cd /opt/netbox/netbox/ python3 manage.py migrate
Create an administrative user:
python3 manage.py createsuperuser
Follow the prompts to provide a username and password:
Username (leave blank to use 'root'): netboxadmin Email address: hitjethva@gmail.com Password: Password (again): Superuser created successfully.
Collect static files using:
python3 manage.py collectstatic
Output includes:
976 static files copied to '/opt/netbox/netbox/static'.
Install and Configure Gunicorn
Install Gunicorn for serving Netbox:
pip3 install gunicorn
Create a Gunicorn configuration file:
nano /opt/netbox/gunicorn_config.py
Add the following:
command = '/usr/local/bin/gunicorn' pythonpath = '/opt/netbox/netbox' bind = 'your-server-ip:8001' workers = 3 user = 'www-data'
Save your changes.
Install and Configure Supervisor
Use Supervisor to manage NetBox services. Create the configuration:
nano /etc/supervisor/conf.d/netbox.conf
Add these lines:
[program:netbox] command = gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi directory = /opt/netbox/netbox/ user = www-data
Save the file and restart Supervisor:
systemctl restart supervisor
Check the Supervisor status with:
systemctl status supervisor
Expected output:
? supervisor.service - Supervisor process control system for UNIX Loaded: loaded (/lib/systemd/system/supervisor.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-05-30 05:49:08 UTC; 14s ago Docs: http://supervisord.org Main PID: 550606 (supervisord) Tasks: 5 (limit: 4691) Memory: 184.3M CGroup: /system.slice/supervisor.service ??550606 /usr/bin/python3 /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf ??550626 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi ??550628 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi ??550629 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi ??550630 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi May 30 05:49:08 ubuntu2004 systemd[1]: Started Supervisor process control system for UNIX. May 30 05:49:08 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:08,664 CRIT Supervisor is running as root. Privileges were not dropped becau> May 30 05:49:08 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:08,664 INFO Included extra file "/etc/supervisor/conf.d/netbox.conf" during p> May 30 05:49:08 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:08,671 INFO RPC interface 'supervisor' initialized May 30 05:49:08 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:08,671 CRIT Server 'unix_http_server' running without any HTTP authentication> May 30 05:49:08 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:08,672 INFO supervisord started with pid 550606 May 30 05:49:09 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:09,676 INFO spawned: 'netbox' with pid 550626 May 30 05:49:11 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:11,060 INFO success: netbox entered RUNNING state, process has stayed up for
Configure Nginx for NetBox
Set up Nginx as a reverse proxy to serve Netbox on port 80. Create an Nginx configuration file:
nano /etc/nginx/sites-available/netbox.conf
Add this configuration:
server { listen 80; server_name your-server-ip; client_max_body_size 25m; location /static/ { alias /opt/netbox/netbox/static/; } location / { proxy_pass http://your-server-ip:8001; } }
Save and then enable the Nginx configuration:
ln -s /etc/nginx/sites-available/netbox.conf /etc/nginx/sites-enabled/
Verify Nginx for syntax errors:
nginx -t
If all is well, you’ll see:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart Nginx to apply changes:
systemctl restart nginx
Check the Nginx status:
systemctl status nginx
The expected output:
? nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Sat 2020-05-30 22:28:13 EST; 4min 14s ago Process: 984 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 982 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 980 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 985 (nginx) Tasks: 3 (limit: 25028) Memory: 5.5M CGroup: /system.slice/nginx.service ??985 nginx: master process /usr/sbin/nginx ??986 nginx: worker process ??987 nginx: worker process May 30 21:28:12 ubuntu2004 systemd[1]: Starting The nginx HTTP and reverse proxy server... Mar 30 21:28:12 ubuntu2004 nginx[982]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Mar 30 21:28:12 ubuntu2004 nginx[982]: nginx: configuration file /etc/nginx/nginx.conf test is successful Mar 30 21:28:13 ubuntu2004 systemd[1]: Started The nginx HTTP and reverse proxy server.
Access Netbox Web Interface
Open your browser and navigate to http://your-server-ip. You should see:
Click the Log in button to reach the login page:
Enter your admin username and password to see the dashboard:
Conclusion
We have successfully installed Netbox on Ubuntu 20.04 with Nginx. You can now use Netbox to document and manage your network infrastructure. For more details, consult the Netbox official documentation. Feel free to ask if you have any questions.
FAQ
- What is Netbox?
- Netbox is an open-source tool for IP address and data center infrastructure management, used to store information about network devices, IP addresses, and more.
- Why use PostgreSQL for Netbox?
- Netbox is built on the Django framework, which by default uses PostgreSQL for reliable and efficient data storage.
- Can I use a different forward proxy instead of Nginx?
- While Nginx is popular and recommended, you can configure other reverse proxies like Apache or Traefik to work with Netbox.
- Is it mandatory to use Supervisor for Netbox?
- Supervisor simplifies process management for Netbox, but other process control systems like systemd can also be used.
- Where can I find more information about configuring Netbox?
- Visit the Netbox documentation for an in-depth guide and tutorials.