Difference between revisions of "Install and Configure NetBox IPAM on Ubuntu"

From SdNOG wiki
Jump to navigation Jump to search
(Created page with "Certainly, I can help you convert this content into wiki page syntax. Here's the content formatted for a wiki page: = Prerequisites = * Deploy a fully updated Ubuntu 20.04 LT...")
 
(6. Configure Nginx Web Server)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
Certainly, I can help you convert this content into wiki page syntax. Here's the content formatted for a wiki page:
+
= Prerequisites =
 +
Ensure the following packages and dependencies are installed:
  
= Prerequisites =
+
* Python 3.8 or higher
* Deploy a fully updated Ubuntu 20.04 LTS at Vultr with at least 2GB of RAM and 1 vCPU cores.
+
* PostgreSQL 12 or higher
* Create a non-root user with sudo access.
+
* Redis
 +
* Netbox 3.0 or higher
 +
* Nginx
 +
* Gunicorn
  
 +
Create a non-root user with sudo access ''' netboxuser '''
 
== 1. Install and configure PostgreSQL ==
 
== 1. Install and configure PostgreSQL ==
  
 
Install PostgreSQL:
 
Install PostgreSQL:
 +
 
<code>
 
<code>
 
sudo apt install postgresql libpq-dev -y
 
sudo apt install postgresql libpq-dev -y
Line 13: Line 19:
  
 
Start the database server:
 
Start the database server:
 +
 
<code>
 
<code>
 
sudo systemctl start postgresql
 
sudo systemctl start postgresql
Line 18: Line 25:
  
 
Enable the database server to start automatically on reboot:
 
Enable the database server to start automatically on reboot:
 +
 
<code>
 
<code>
 
sudo systemctl enable postgresql
 
sudo systemctl enable postgresql
Line 23: Line 31:
  
 
Change the default PostgreSQL password:
 
Change the default PostgreSQL password:
 +
 
<code>
 
<code>
 
sudo passwd postgres
 
sudo passwd postgres
Line 28: Line 37:
  
 
Switch to the postgres user:
 
Switch to the postgres user:
 +
 
<code>
 
<code>
 
su - postgres
 
su - postgres
Line 33: Line 43:
  
 
Log in to PostgreSQL:
 
Log in to PostgreSQL:
 +
 
<code>
 
<code>
 
psql
 
psql
 
</code>
 
</code>
  
Create database netbox:
+
Create the NetBox database:
 +
 
 
<code>
 
<code>
 
CREATE DATABASE netbox;
 
CREATE DATABASE netbox;
 
</code>
 
</code>
  
Create user netbox with password my_strong_password. Use a strong password in place of my_strong_password:
+
Create the netbox user with a strong password (replace my_strong_password with a secure one):
 +
 
 
<code>
 
<code>
CREATE USER netbox WITH ENCRYPTED password 'my_strong_password';
+
CREATE USER netbox WITH ENCRYPTED PASSWORD 'my_strong_password';
 
</code>
 
</code>
  
Grant all the privileges on the netbox database to the netbox user:
+
Grant privileges to the netbox user on the netbox database:
 +
 
 
<code>
 
<code>
GRANT ALL PRIVILEGES ON DATABASE netbox to netbox;
+
GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
 
</code>
 
</code>
  
 
Exit PostgreSQL:
 
Exit PostgreSQL:
 +
 
<code>
 
<code>
 
\q
 
\q
Line 58: Line 73:
  
 
Return to your non-root sudo user account:
 
Return to your non-root sudo user account:
 +
 
<code>
 
<code>
 
exit
 
exit
Line 63: Line 79:
  
 
== 2. Install Redis® ==
 
== 2. Install Redis® ==
Redis® is an in-memory key-value store. NetBox uses it for caching and queuing.
+
 
 +
Redis® is an in-memory key-value store used by NetBox for caching and queuing.
  
 
Install Redis®:
 
Install Redis®:
 +
 
<code>
 
<code>
 
sudo apt install -y redis-server
 
sudo apt install -y redis-server
Line 71: Line 89:
  
 
== 3. Install and configure NetBox ==
 
== 3. Install and configure NetBox ==
It's recommended to install NetBox from the official git repository to allows for seamless upgrades by re-pulling the master branch.
 
  
Install all the required packages:
+
Install all required packages:
 +
 
 
<code>
 
<code>
 
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
 
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
Line 79: Line 97:
  
 
Update pip to the latest version:
 
Update pip to the latest version:
 +
 
<code>
 
<code>
 
sudo pip3 install --upgrade pip
 
sudo pip3 install --upgrade pip
 
</code>
 
</code>
  
We'll use /opt/netbox/ as the installation directory. Create directory /opt/netbox/ and change to /opt/netbox/ directory:
+
Create the installation directory and change to it:
 +
 
 
<code>
 
<code>
 
sudo mkdir -p /opt/netbox/ && cd /opt/netbox/
 
sudo mkdir -p /opt/netbox/ && cd /opt/netbox/
 
</code>
 
</code>
  
Clone NetBox from official git repository to the current directory:
+
Clone NetBox from the official Git repository:
 +
 
 
<code>
 
<code>
 
sudo git clone -b master https://github.com/netbox-community/netbox.git .
 
sudo git clone -b master https://github.com/netbox-community/netbox.git .
Line 94: Line 115:
  
 
Create a system user named netbox:
 
Create a system user named netbox:
 +
 
<code>
 
<code>
 
sudo adduser --system --group netbox
 
sudo adduser --system --group netbox
 
</code>
 
</code>
  
Grant user netbox ownership of /opt/netbox/netbox/media/:
+
Grant the netbox user ownership of the media directory:
 +
 
 
<code>
 
<code>
 
sudo chown --recursive netbox /opt/netbox/netbox/media/
 
sudo chown --recursive netbox /opt/netbox/netbox/media/
 
</code>
 
</code>
  
Browse to the /opt/netbox/netbox/netbox/ directory:
+
Navigate to the configuration directory:
 +
 
 
<code>
 
<code>
 
cd /opt/netbox/netbox/netbox/
 
cd /opt/netbox/netbox/netbox/
 
</code>
 
</code>
  
Copy example configuration file configuration.example.py to a configuration file configuration.py that we will use to configure the project:
+
Copy the example configuration file:
 +
 
 
<code>
 
<code>
sudo cp configuration.example.py configuration.py
+
sudo cp configuration_example.py configuration.py
 
</code>
 
</code>
  
Create a symbolic link of Python binary:
+
Create a symbolic link for the Python binary:
 +
 
 
<code>
 
<code>
 
sudo ln -s /usr/bin/python3 /usr/bin/python
 
sudo ln -s /usr/bin/python3 /usr/bin/python
 
</code>
 
</code>
  
Generate a random SECRET_KEY of at least 50 alphanumeric characters:
+
Generate a random SECRET_KEY for the configuration:
 +
 
 
<code>
 
<code>
 
sudo /opt/netbox/netbox/generate_secret_key.py
 
sudo /opt/netbox/netbox/generate_secret_key.py
 
</code>
 
</code>
  
You will get a random secret similar to the below example. Copy it and save it somewhere. You will need it in the configuration file:
+
Copy the generated secret key and use it in the configuration file.
<code>
+
 
-^%YEl*Q2etCR6$kNG70H=&sM(45XvJaBWdf3O)inZ@L9j8_w1
+
Edit the configuration file:
</code>
 
  
Open and edit the configuration file configuration.py:
 
 
<code>
 
<code>
 
sudo nano /opt/netbox/netbox/netbox/configuration.py
 
sudo nano /opt/netbox/netbox/netbox/configuration.py
 
</code>
 
</code>
  
The final file should have the following configurations:
+
Update the file with the following settings:
 +
 
 
<pre>
 
<pre>
 
ALLOWED_HOSTS = ['*']
 
ALLOWED_HOSTS = ['*']
  
 
DATABASE = {
 
DATABASE = {
     'NAME': 'netbox',                           # Database name you created
+
     'NAME': 'netbox',
     'USER': 'netbox',                           # PostgreSQL username you created
+
     'USER': 'netbox',
     'PASSWORD': 'my_strong_password',           # PostgreSQL password you set
+
     'PASSWORD': 'my_strong_password',
     'HOST': 'localhost',                       # Database server
+
     'HOST': 'localhost',
     'PORT': '',                                 # Database port (leave blank for default)
+
     'PORT': '',
 
}
 
}
  
SECRET_KEY = '-^%YEl*Q2etCR6$kNG70H=&sM(45XvJaBWdf3O)inZ@L9j8_w1'
+
SECRET_KEY = '<generated_secret_key>'
 
</pre>
 
</pre>
 +
Run the upgrade script:
  
Run the upgrade script:
 
 
<code>
 
<code>
 
sudo /opt/netbox/upgrade.sh
 
sudo /opt/netbox/upgrade.sh
 
</code>
 
</code>
 +
Enter the Python virtual environment:
  
Enter the Python virtual environment:
 
 
<code>
 
<code>
 
source /opt/netbox/venv/bin/activate
 
source /opt/netbox/venv/bin/activate
 
</code>
 
</code>
 +
Navigate to the NetBox directory:
  
Go to /opt/netbox/netbox directory:
 
 
<code>
 
<code>
 
cd /opt/netbox/netbox
 
cd /opt/netbox/netbox
 
</code>
 
</code>
 +
Create a superuser account:
  
Create a superuser account:
 
 
<code>
 
<code>
 
python3 manage.py createsuperuser
 
python3 manage.py createsuperuser
 
</code>
 
</code>
 +
Reboot the system:
  
Reboot the system to apply the changes:
 
 
<code>
 
<code>
 
sudo reboot
 
sudo reboot
Line 174: Line 200:
  
 
== 4. Configure Gunicorn ==
 
== 4. Configure Gunicorn ==
Copy /opt/netbox/contrib/gunicorn.py to /opt/netbox/gunicorn.py:
+
 
 +
Copy the Gunicorn configuration file:
 +
 
 
<code>
 
<code>
 
sudo cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
 
sudo cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
 
</code>
 
</code>
 +
== 5. Configure Systemd ==
 +
 +
Copy the systemd service files:
  
== 5. Configure Systemd ==
 
Copy contrib/netbox.service and contrib/netbox-rq.service to the /etc/systemd/system/ directory:
 
 
<code>
 
<code>
 
sudo cp /opt/netbox/contrib/*.service /etc/systemd/system/
 
sudo cp /opt/netbox/contrib/*.service /etc/systemd/system/
 
</code>
 
</code>
 +
Reload the systemd daemon:
  
Reload daemon to enable the Systemd changes:
 
 
<code>
 
<code>
 
sudo systemctl daemon-reload
 
sudo systemctl daemon-reload
 
</code>
 
</code>
 +
Start the NetBox services:
  
Start the netbox and netbox-rq services:
 
 
<code>
 
<code>
 
sudo systemctl start netbox netbox-rq
 
sudo systemctl start netbox netbox-rq
 
</code>
 
</code>
 +
Enable the services to start at boot:
  
Enable the services to initiate at boot time:
 
 
<code>
 
<code>
 
sudo systemctl enable netbox netbox-rq
 
sudo systemctl enable netbox netbox-rq
 
</code>
 
</code>
 +
== 6. Configure Nginx Web Server ==
 +
 +
Install the Nginx web server:
  
== 6. Configure Nginx Web Server ==
 
Install Nginx web server:
 
 
<code>
 
<code>
 
sudo apt install -y nginx
 
sudo apt install -y nginx
 
</code>
 
</code>
 +
Copy the Nginx configuration file:
  
Copy NetBox Nginx configuration file nginx.conf to /etc/nginx/sites-available/netbox:
 
 
<code>
 
<code>
 
sudo cp /opt/netbox/contrib/nginx.conf /etc/nginx/sites-available/netbox
 
sudo cp /opt/netbox/contrib/nginx.conf /etc/nginx/sites-available/netbox
 
</code>
 
</code>
  
Edit file netbox:
+
Edit the configuration file:
 +
 
 
<code>
 
<code>
 
sudo nano /etc/nginx/sites-available/netbox
 
sudo nano /etc/nginx/sites-available/netbox
 
</code>
 
</code>
  
Replace all the files content with the below code. Modify the server_name value with your server IP address:
+
Replace the server name with your server's IP address:
 +
 
 
<pre>
 
<pre>
 
server {
 
server {
 
     listen 80;
 
     listen 80;
 
+
     server_name 192.0.2.10; # Update this with your server's IP
    # CHANGE THIS TO YOUR SERVER'S NAME
 
     server_name 192.0.2.10;
 
  
 
     client_max_body_size 25m;
 
     client_max_body_size 25m;
Line 239: Line 269:
 
</pre>
 
</pre>
  
Delete /etc/nginx/sites-enabled/default:
+
Delete the default Nginx configuration:
 +
 
 
<code>
 
<code>
 
sudo rm /etc/nginx/sites-enabled/default
 
sudo rm /etc/nginx/sites-enabled/default
 
</code>
 
</code>
  
Create a symlink in the sites-enabled directory to the netbox configuration file:
+
Create a symbolic link for the NetBox configuration:
 +
 
 
<code>
 
<code>
 
sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/netbox
 
sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/netbox
 
</code>
 
</code>
  
Restart nginx service to enable the new configurations:
+
Restart the Nginx service:
 +
 
 
<code>
 
<code>
 
sudo systemctl restart nginx
 
sudo systemctl restart nginx
 
</code>
 
</code>
  
Would you like me to explain or break down any part of this wiki syntax?
+
access your url via the browser
 +
 
 +
== Author ==
 +
* '''Author''': [[User:Manhal.Mohamed|Manhal Mohamed]]'' , SdNOG Team

Latest revision as of 11:19, 16 August 2024

Prerequisites

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

1. Install and configure PostgreSQL

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

2. Install Redis®

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

Install Redis®:

sudo apt install -y redis-server

3. Install and configure NetBox

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 https://github.com/netbox-community/netbox.git .

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

4. Configure Gunicorn

Copy the Gunicorn configuration file:

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

5. Configure Systemd

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

6. Configure Nginx Web Server

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

Author