This article provides a step-by-step guide to install NetBox on Ubuntu VPS server.
What is NetBox?
NetBox is a powerful and versatile open-source tool that allows you to efficiently manage your network infrastructure. In this comprehensive guide, we will walk you through the process of installing NetBox on your Ubuntu VPS. By following these step-by-step instructions, you will be able to seamlessly deploy NetBox and take full advantage of its features on your Ubuntu VPS server.
Step-by-Step Guide to Install NetBox on Ubuntu VPS
This guide will walk you through installing NetBox, a popular open-source IP Address Management (IPAM) and Data Center Infrastructure Management (DCIM) tool, on a fresh Ubuntu VPS.
NetBox uses Python, PostgreSQL, and other dependencies, which need to be properly set up before you can run the software.
Prerequisites:
- A fresh Ubuntu VPS (tested on Ubuntu 22.04 LTS)
- Basic knowledge of the Linux command line
- A user with sudo privileges
- Minimum of 2GB RAM for smoother operation
- 4GB of swap space if less than 2GB RAM is available
Step 1: Update the System
Before installing any new packages, it’s essential to ensure your server is up to date. Run the following commands to update your system.
sudo apt update && sudo apt upgrade -y
Once the update process is complete, reboot the server:
sudo reboot
Step 2: Install Required Dependencies
NetBox relies on several core components such as Python, PostgreSQL, Redis, and some additional tools like Git. Install these dependencies using the following commands:
- Install PostgreSQL (Database)
sudo apt install -y postgresql libpq-dev
- Install Redis (Caching Backend)
sudo apt install -y redis-server
- Install Python 3 and other dependencies
sudo apt install -y python3-pip python3-dev python3-venv build-essential libssl-dev libffi-dev libxml2-dev libxslt1-dev zlib1g-dev
- Install Git
sudo apt install -y git
Step 3: Set up PostgreSQL Database
- Switch to the
postgres
user:
sudo -u postgres psql
- Create a database and user for NetBox. Replace
netbox
andpassword
with your preferred database name and password.
CREATE DATABASE netbox; CREATE USER netbox WITH PASSWORD 'password'; ALTER ROLE netbox SET client_encoding TO 'utf8'; ALTER ROLE netbox SET default_transaction_isolation TO 'read committed'; ALTER ROLE netbox SET timezone TO 'UTC'; GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox; \q
Step 4: Create a Directory for NetBox
- Clone the NetBox repository into
/opt/netbox/
:
sudo git clone -b master https://github.com/netbox-community/netbox.git /opt/netbox/
- Change into the NetBox directory:
cd /opt/netbox/
Step 5: Configure Python Virtual Environment
- Set up a Python virtual environment for NetBox. Virtual environments help manage dependencies locally for the application without affecting the system Python installation.
sudo python3 -m venv /opt/netbox/venv
- Activate the virtual environment:
source /opt/netbox/venv/bin/activate
- Install the required Python dependencies:
pip install -r /opt/netbox/requirements.txt
Step 6: Configure NetBox
- Copy the example configuration file to
configuration.py
:
cd /opt/netbox/netbox/netbox/ sudo cp configuration_example.py configuration.py
- Open
configuration.py
for editing:
sudo nano /opt/netbox/netbox/netbox/configuration.py
- Modify the configuration:
- Database Settings: Update the database settings to reflect the PostgreSQL database and user you created in Step 3.
DATABASE = { 'NAME': 'netbox', # Database name 'USER': 'netbox', # PostgreSQL username 'PASSWORD': 'password', # PostgreSQL password 'HOST': 'localhost', # Database host 'PORT': '', # Leave blank for default }
- Secret Key: Generate a random secret key to use for NetBox. You can use the following Python command to generate one:
python3 -c "import secrets; print(secrets.token_urlsafe(50))"
Paste this key in the SECRET_KEY
field in the configuration.py
file.
SECRET_KEY = 'your_generated_secret_key_here'
- ALLOWED_HOSTS: Set this to your server’s domain name or IP address.
ALLOWED_HOSTS = ['your_server_ip_or_domain']
Step 7: Run Database Migrations
To set up the database schema, you need to run the NetBox migrations.
cd /opt/netbox/netbox/ python3 manage.py migrate
Step 8: Create a Superuser
Create a superuser to access the NetBox web interface:
python3 manage.py createsuperuser
Follow the prompts to set up your admin account.
Step 9: Collect Static Files
NetBox uses static files (CSS, JavaScript, etc.), and you need to gather these files using the following command:
python3 manage.py collectstatic
You will be prompted to confirm the collection process. Type yes
when asked.
Step 10: Set Up Gunicorn
NetBox uses Gunicorn as the application server. To configure Gunicorn:
- Install Gunicorn:
pip install gunicorn
- Create a Gunicorn systemd service:
sudo nano /etc/systemd/system/netbox.service
Add the following configuration:
[Unit] Description=NetBox WSGI Service After=network.target [Service] User=root Group=www-data WorkingDirectory=/opt/netbox/netbox ExecStart=/opt/netbox/venv/bin/gunicorn --workers 3 --bind 127.0.0.1:8001 netbox.wsgi [Install] WantedBy=multi-user.target
Save and exit the file. Reload the systemd service configuration:
sudo systemctl daemon-reload
- Start and enable the NetBox service:
sudo systemctl start netbox sudo systemctl enable netbox
Step 11: Set Up Nginx
NetBox uses Nginx as a reverse proxy to route traffic to Gunicorn. Install Nginx:
sudo apt install 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_server_ip_or_domain; 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-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Save the file and create a symbolic link to enable the site:
sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/
Check the Nginx configuration for syntax errors:
sudo nginx -t
Restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 12: Enable HTTPS (Optional)
If you plan to serve NetBox over HTTPS (highly recommended for production environments), you can use Let’s Encrypt to obtain an SSL certificate.
- Install Certbot:
sudo apt install certbot python3-certbot-nginx
- Obtain an SSL certificate:
sudo certbot --nginx -d your_domain_name
- Follow the prompts to install the SSL certificate and configure automatic redirection.
Step 13: Access NetBox
Now that the setup is complete, you can access your NetBox installation through a web browser. Navigate to:
http://your_server_ip_or_domain/
Log in with the superuser credentials you created.
Step 14: Automate Background Tasks
NetBox relies on background tasks that can be managed using systemd
. To automate them, create the following service and timer files:
- Create the NetBox RQ worker service:
sudo nano /etc/systemd/system/netbox-rq.service
Add the following:
[Unit] Description=NetBox RQ Worker After=netbox.service [Service] User=root Group=www-data WorkingDirectory=/opt/netbox/netbox/ ExecStart=/opt/netbox/venv/bin/python3 /opt/netbox/netbox/manage.py rqworker [Install] WantedBy=multi-user.target
- Enable and start the service:
sudo systemctl enable netbox-rq sudo systemctl start netbox-rq
Conclusion
You have successfully completed the step-by-step guide to install NetBox on Ubuntu VPS server. You can now manage your IP addresses, racks, devices, and more using the web-based interface.
Make sure to regularly update NetBox and its dependencies to ensure security and performance.