How to Install NetBox on Rocky Linux 9
NetBox is an Infrastructure Resource Modelling (IRM) software designed for network automation and infrastructure engineering. Initially, it was created by the DigitalOcean team, and now became an open-source project released under the Apache 2 License. NetBox was created in the Python Django Web framework with PostgreSQL as the default database, and the installation of NetBox is quite similar to other Python Django web applications.
NetBox helps you to manage your infrastructure, which includes:
- DCIM (Data Center Infrastructure Management)
- IPAM (IP Address Management)
- Data Circuits
- Connections (Network, console, and power)
- Equipment racks
- Virtualization
- Secrets
In this tutorial, you will install NetBox IRM (Infrastructure Resource Management) on a Rocky Linux 9 server. You'll set up NetBox with PostgreSQL as the database system and Apache/httpd as a reverse proxy on a Rocky Linux system. You'll also secure NetBox with SSL/TLS certificates via Certbot and Letsencrypt.
Prerequisites
Before you get started, ensure that you have the following requirements:
- A Rocky Linux 9 server - This example uses a Rocky Linux server with the hostname 'netbox-rocky'.
- A non-root user with sudo/root administrator privileges.
- An SELinux running in permissive mode.
- A domain name or sub-domain pointed to a server IP address - This example uses a sub-domain 'netbox.howtoforge.local' to run NetBox.
With these prerequisites in place, you're ready to install NetBox.
Installing and Configuring PostgreSQL
The NetBox IRM by default supports the PostgreSQL database server. At the time of this writing, it's required at least PostgreSQL v10 and above. By default, the Rocky Linux repository provides PostgreSQL server v13, which is suitable for the NetBox deployment.
In this step, you'll install the PostgreSQL database server, set up the password authentication, then create a new database and user that NetBox will use.
To start, run the below command to install the PostgreSQL server on the Rocky Linux server.
sudo dnf install postgresql-server
When prompted, input y to confirm and press ENTER to proceed.

After installing the PostgreSQL server, run the below command to initialize the PostgreSQL database and configuration.
sudo postgresql-setup --initdb
You should receive an output such as 'Initializing database in ...'.

With the PostgreSQL server initialized, you'll next set up the password encryption and authentication for PostgreSQL users.
Open the PostgreSQL config file '/var/lib/pgsql/data/postgresql.conf' using the below nano editor command.
sudo nano /var/lib/pgsql/data/postgresql.conf
Uncomment the parameter 'password_encryption' and change the value to 'scram-sha-256'. This will set up the default password encryption for PostgreSQL users to 'scram-sha-256'.
password_encryption = scram-sha-256
Save the file and exit the editor.
Next, open another PostgreSQL config file '/var/lib/pgsql/data/pg_hba.conf' using the below command. This file is where you can define authentication methods for your PostgreSQL.
sudo nano /var/lib/pgsql/data/pg_hba.conf
Change the default authentication methods for the host '127.0.0.1/32' and '::1/128' to 'scram-sha-256'. With this, you'll set up the default authentication methods for PostgreSQL users to 'scram-sha-256'.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
Save the file and exit the editor when finished.

Now run the below systemctl command utility to start and enable the PostgreSQL service.
sudo systemctl start postgresql
sudo systemctl enable postgresql
Then verify the PostgreSQL service using the below command.
sudo systemctl status postgresql
You should receive an output like this - The PostgreSQL service is running and it's enabled, which means the PostgreSQL will start automatically upon the bootup.

Now that you've configured the password authentication for the PostgreSQL server, it's now up and running. Next, you'll set up a new password for the default 'postgres' user and create a new database and user that NetBox will use.
Log in to the PostgreSQL shell via the below command.
sudo -u postgres psql
Run the below query to set up a new password for the default PostgreSQL user 'postgres'. Be sure to change the password in the following query.
ALTER USER postgres WITH PASSWORD 'PostgreSQLPass';

Next, run the below query to create a new PostgreSQL database and user. Also, be sure to change the default password in the following query.
In this example, you'll create a new database 'netboxdb' with the user 'netbox' that will be used for NetBox installation.
CREATE DATABASE netboxdb;
CREATE USER netbox WITH ENCRYPTED PASSWORD 'NetBoxRocks';
GRANT ALL PRIVILEGES ON DATABASE netboxdb TO netbox;
Now press Ctrl+d or type quit to exit.

Lastly, run the below command to log in to the PostgreSQL shell via the new user 'netbox' to the new database 'netboxdb'. When prompted for the password, input your password.
sudo -u postgres psql --username netbox --password --host localhost netboxdb
After logging in to the PostgreSQL shell, run the below query to verify your current connection.
\conninfo
You'll receive an output like this - You've connected to the PostgreSQL server via the 'netbox' user to the database 'netboxdb'.

With the PostgreSQL installed, the database, and the user created, you'll next install Redis which will be used as cache management on the NetBox web application.
Installing and Configuring Redis
Redis is a free and open-source key-value database that NetBox will use for cache management and queue management. At the time of this writing, NetBox required at least the Redis server v4, and the default Rocky Linux repository provides Redis v6 and is suitable for your NetBox deployment.
Install Redis to your Rocky Linux server via the below dnf command.
sudo dnf install redis
Input y when prompted and press ENTER to proceed.

After Redis is installed, open the Redis configuration file '/etc/redis/redis.conf' using the below nano editor command.
sudo nano /etc/redis/redis.conf
Uncomment the parameter 'requirepass' and input the new password for your Redis server.
requirepass RedisPasswordNetBox
Save the file and exit the editor when finished.
Next, run the below systemctl command to start the Redis server and enable it.
sudo systemctl start redis
sudo systemctl enable redis

Then verify the Redis server via the below systemctl command utility.
sudo systemctl status redis
In the output, you should see the Redis server is enabled and will be run automatically upon the bootup. And the status of the Redis server is running.

To verify your Redis installation, you will access Redis via the 'redis-cli' command below.
redis-cli
If you run the ping query, you should receive an output such as '(error) NOAUTH authentication required'. You need to be authenticated to run the 'ping' command.
ping
Execute the below Redis query to authenticate to the Redis server. Be sure to change the password. If authenticated, you should receive an output 'OK'.
AUTH RedisPasswordNetBox
Run the ping query again and you should get an output 'PONG", which means that the query executed successfully and you've authenticated to the Redis server.
ping

At this point, you've installed the PostgreSQL database server and the Redis key-value database on Rocky Linux. You're now ready to start NetBox installation.
Installing Netbox IRM
NetBox is a web application written with Python Django Framework. The current version of NetBox required at least Python 3.8, 3.9, 3.10, or 3.11. And the default Python on Rocky Linux 9 is Python 3.9, which is suitable for NetBox deployment.
To start, run the below dnf command to install package dependencies for NetBox. Input y when prompted and press ENTER to proceed.
sudo dnf install gcc libxml2-devel libxslt-devel libffi-devel libpq-devel openssl-devel redhat-rpm-config git

Next, run the below command to create a new system user 'netbox' with the default home directory '/opt/netbox'.
sudo useradd -r -d /opt/netbox -s /usr/sbin/nologin netbox
Create a new directory '/opt/netbox' and move your working directory into it. Then, download the NetBox source code via the git command. The directory '/opt/netbox' will be used as the main installation directory of NetBox.
mkdir -p /opt/netbox; cd /opt/netbox
sudo git clone -b master --depth 1 https://github.com/netbox-community/netbox.git .
Change the ownership of the NetBox installation directory '/opt/netbox' to the user and group 'netbox'. Then, move your working directory to '/opt/netbox/netbox/netbox'.
sudo chown -R netbox:netbox /opt/netbox
cd /opt/netbox/netbox/netbox
Next, run the below command to copy the default NetBox configuration to 'configuration.py'. Then, generate the SECRET_KEY via the Python script '../generate_secret_key.py'.
sudo -u netbox cp configuration_example.py configuration.py
sudo -u netbox python3 ../generate_secret_key.py
Now copy the generated SECRET_KEY. This will be used to set up the NetBox installation.

Open the NetBox config file 'configuration.py' using the below nano editor command.
sudo -u netbox nano configuration.py
Be sure to add your domain name to the 'ALLOWED_HOSTS' parameter, input details of the PostgreSQL database and user for NetBox, input the Redis password that you've configured, and paste the generated SECRET_KEY to the 'SECRET_KEY' parameter.
# domain and IP address
ALLOWED_HOSTS = ['netbox.howtoforge.local', '192.168.5.59']
# database configuration
DATABASE = {
'NAME': 'netboxdb', # Database name
'USER': 'netbox', # PostgreSQL username
'PASSWORD': 'NetBoxRocks', # PostgreSQL password
'HOST': 'localhost', # Database server
'PORT': '', # Database port (leave blank for default)
'CONN_MAX_AGE': 300, # Max database connection age (seconds)
}
# Redis cache configuration
REDIS = {
'tasks': {
'HOST': 'localhost', # Redis server
'PORT': 6379, # Redis port
'PASSWORD': 'RedisPasswordNetBox', # Redis password (optional)
'DATABASE': 0, # Database ID
'SSL': False, # Use SSL (optional)
},
'caching': {
'HOST': 'localhost',
'PORT': 6379,
'PASSWORD': 'RedisPasswordNetBox',
'DATABASE': 1, # Unique ID for the second database
'SSL': False,
}
}
# Secret key
SECRET_KEY = '-K0AV#USk(!-6hAEF-8NMgweJh6ex&+j0Kb$N7bi=*jsF9TOg*'
Save and exit the file when finished.
Now run the below script '/opt/netbox/upgrade.sh' to start the NetBox IRM installation.
sudo -u netbox /opt/netbox/upgrade.sh
This will install create Python virtual environment for the NetBox web application, install required Python dependencies via the PyPI repository, run the database migration for NetBox, and lastly generate static files for the NetBox web application.
Below is an output when the upgrade.sh script executed.

Below is the output message when the NetBox installation is finished.

At this point, you've installed the NetBox IRM in your system. But still, you need to set up your NetBox installation.
Configuring NetBox IRM
In this step, you'll set up NetBox IRM installation by creating an admin user for NetBox, setting up cron, and setting up systemd services for NetBox.
To start, run the below command to activate the Python virtual environment for your NetBox installation.
source /opt/netbox/venv/bin/activate
When activated, your prompt will become such as '(venv) root@hostname....'.
Next, move the working directory to '/opt/netbox/netbox' and run the Django script 'manage.py' to create a new NetBox admin user.
cd /opt/netbox/netbox
python3 manage.py createsuperuser
Input the new admin user, email, and password for your NetBox. You should receive an output 'Superuser created successfully.', which means the NetBox admin user is created.

Next, run the below command to set up cron that will be run on a daily basis. The script 'netbox-housekeeping.sh' is used to clean up your NetBox environment, this will remove expired tasks, old sessions, or any expired records.
sudo ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping
After configuring a cron for NetBox, you'll set up NetBox to run with Gunicorn.
Run the below command to copy the Guncorn configuration to '/opt/netbox/gunicorn.py'. Then, open the Gunicorn config file '/opt/netbox/gunicorn.py' using the below nano editor command.
sudo -u netbox cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
sudo -u netbox nano /opt/netbox/gunicorn.py
Change the 'bind' parameter with the following line. This will run the NetBox web application locally with port '8001'.
bind = '127.0.0.1:8001'
Save and xit the file when finished.

Next, run the below command to copy the default systemd services f