# Install and Configure NetBox IPAM on Ubuntu

<p class="callout info">This page was written by Manhal Mohamed, sdnog team, on 16 August 2024.</p>

Ensure the following packages and dependencies are installed:

- Python 3.8 or higher
- PostgreSQL 12 or higher
- Redis
- Netbox 3.0 or higher
- Nginx
- Gunicorn

Create a non-root user with sudo access **netboxuser**

#### <span class="mw-headline" id="bkmrk-install-and-configur-1">Install and configure PostgreSQL</span>

Install PostgreSQL:

`sudo apt install postgresql libpq-dev -y`

Start the database server:

`sudo systemctl start postgresql`

Enable the database server to start automatically on reboot:

`sudo systemctl enable postgresql`

Change the default PostgreSQL password:

`sudo passwd postgres`

Switch to the postgres user:

`su - postgres`

Log in to PostgreSQL:

`psql`

Create the NetBox database:

`CREATE DATABASE netbox;`

Create the netbox user with a strong password (replace my\_strong\_password with a secure one):

`CREATE USER netbox WITH ENCRYPTED PASSWORD 'my_strong_password';`

Grant privileges to the netbox user on the netbox database:

`GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;`

Exit PostgreSQL:

`\q`

Return to your non-root sudo user account:

`exit`

##### <span class="mw-headline" id="bkmrk-2.-install-redis%C2%AE-1">2. Install Redis®</span>

Redis® is an in-memory key-value store used by NetBox for caching and queuing.

Install Redis®:

`sudo apt install -y redis-server`

##### <span class="mw-headline" id="bkmrk-3.-install-and-confi-1">3. Install and configure NetBox</span>

Install all required packages:

`sudo apt install python3 python3-pip python3-venv python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev libpq-dev libssl-dev zlib1g-dev git -y`

Update pip to the latest version:

`sudo pip3 install --upgrade pip`

Create the installation directory and change to it:

`sudo mkdir -p /opt/netbox/ && cd /opt/netbox/`

Clone NetBox from the official Git repository:

`sudo git clone -b master <a class="external free" href="https://github.com/netbox-community/netbox.git" rel="nofollow">https://github.com/netbox-community/netbox.git</a> .`

Create a system user named netbox:

`sudo adduser --system --group netbox`

Grant the netbox user ownership of the media directory:

`sudo chown --recursive netbox /opt/netbox/netbox/media/`

Navigate to the configuration directory:

`cd /opt/netbox/netbox/netbox/`

Copy the example configuration file:

`sudo cp configuration_example.py configuration.py`

Create a symbolic link for the Python binary:

`sudo ln -s /usr/bin/python3 /usr/bin/python`

Generate a random SECRET\_KEY for the configuration:

`sudo /opt/netbox/netbox/generate_secret_key.py`

Copy the generated secret key and use it in the configuration file.

Edit the configuration file:

`sudo nano /opt/netbox/netbox/netbox/configuration.py`

Update the file with the following settings:

```
ALLOWED_HOSTS = ['*']

DATABASE = {
    'NAME': 'netbox',
    'USER': 'netbox',
    'PASSWORD': 'my_strong_password',
    'HOST': 'localhost',
    'PORT': '',
}

SECRET_KEY = '<generated_secret_key>'
```

Run the upgrade script:

`sudo /opt/netbox/upgrade.sh `Enter the Python virtual environment:

`source /opt/netbox/venv/bin/activate `Navigate to the NetBox directory:

`cd /opt/netbox/netbox `Create a superuser account:

`python3 manage.py createsuperuser `Reboot the system:

`sudo reboot`

##### <span class="mw-headline" id="bkmrk-4.-configure-gunicor-1">4. Configure Gunicorn</span>

Copy the Gunicorn configuration file:

`sudo cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py`

##### <span class="mw-headline" id="bkmrk-5.-configure-systemd-1">5. Configure Systemd</span>

Copy the systemd service files:

`sudo cp /opt/netbox/contrib/*.service /etc/systemd/system/ `Reload the systemd daemon:

`sudo systemctl daemon-reload `Start the NetBox services:

`sudo systemctl start netbox netbox-rq `Enable the services to start at boot:

`sudo systemctl enable netbox netbox-rq`

##### <span class="mw-headline" id="bkmrk-6.-configure-nginx-w-1">6. Configure Nginx Web Server</span>

Install the Nginx web server:

`sudo apt install -y nginx `Copy the Nginx configuration file:

`sudo cp /opt/netbox/contrib/nginx.conf /etc/nginx/sites-available/netbox`

Edit the configuration file:

`sudo nano /etc/nginx/sites-available/netbox`

Replace the server name with your server's IP address:

```
server {
    listen 80;
    server_name 192.0.2.10;  # Update this with your server's IP

    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-Forwarded-Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

Delete the default Nginx configuration:

`sudo rm /etc/nginx/sites-enabled/default`

Create a symbolic link for the NetBox configuration:

`sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/netbox`

Restart the Nginx service:

`sudo systemctl restart nginx`

access your url via the browser