Difference between revisions of "Install and Configure NetBox IPAM on Ubuntu"
(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...") |
|||
Line 1: | Line 1: | ||
− | + | = Prerequisites = | |
− | |||
− | |||
− | |||
+ | Create a non-root user with sudo access. | ||
== 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 12: | ||
Start the database server: | Start the database server: | ||
+ | |||
<code> | <code> | ||
sudo systemctl start postgresql | sudo systemctl start postgresql | ||
Line 18: | Line 18: | ||
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 24: | ||
Change the default PostgreSQL password: | Change the default PostgreSQL password: | ||
+ | |||
<code> | <code> | ||
sudo passwd postgres | sudo passwd postgres | ||
Line 28: | Line 30: | ||
Switch to the postgres user: | Switch to the postgres user: | ||
+ | |||
<code> | <code> | ||
su - postgres | su - postgres | ||
Line 33: | Line 36: | ||
Log in to PostgreSQL: | Log in to PostgreSQL: | ||
+ | |||
<code> | <code> | ||
psql | psql | ||
</code> | </code> | ||
− | Create database | + | Create the NetBox database: |
+ | |||
<code> | <code> | ||
CREATE DATABASE netbox; | CREATE DATABASE netbox; | ||
</code> | </code> | ||
− | Create user | + | Create the netbox user with a strong password (replace my_strong_password with a secure one): |
+ | |||
<code> | <code> | ||
− | CREATE USER netbox WITH ENCRYPTED | + | CREATE USER netbox WITH ENCRYPTED PASSWORD 'my_strong_password'; |
</code> | </code> | ||
− | Grant | + | Grant privileges to the netbox user on the netbox database: |
+ | |||
<code> | <code> | ||
− | GRANT ALL PRIVILEGES ON DATABASE netbox | + | GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox; |
</code> | </code> | ||
Exit PostgreSQL: | Exit PostgreSQL: | ||
+ | |||
<code> | <code> | ||
\q | \q | ||
Line 58: | Line 66: | ||
Return to your non-root sudo user account: | Return to your non-root sudo user account: | ||
+ | |||
<code> | <code> | ||
exit | exit | ||
Line 63: | Line 72: | ||
== 2. Install Redis® == | == 2. Install Redis® == | ||
− | Redis® is an in-memory key-value store | + | |
+ | 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 82: | ||
== 3. Install and configure NetBox == | == 3. Install and configure NetBox == | ||
− | |||
− | Install all | + | 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 90: | ||
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> | ||
− | + | 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 | + | 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 108: | ||
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 | + | 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> | ||
− | + | Navigate to the configuration directory: | |
+ | |||
<code> | <code> | ||
cd /opt/netbox/netbox/netbox/ | cd /opt/netbox/netbox/netbox/ | ||
</code> | </code> | ||
− | Copy example configuration file | + | Copy the example configuration file: |
+ | |||
<code> | <code> | ||
− | sudo cp | + | sudo cp configuration_example.py configuration.py |
</code> | </code> | ||
+ | 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 | + | 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> | ||
− | + | Copy the generated secret key and use it in the configuration file. | |
− | + | ||
− | + | Edit the configuration file: | |
− | |||
− | |||
<code> | <code> | ||
sudo nano /opt/netbox/netbox/netbox/configuration.py | sudo nano /opt/netbox/netbox/netbox/configuration.py | ||
</code> | </code> | ||
− | + | Update the file with the following settings: | |
+ | |||
<pre> | <pre> | ||
ALLOWED_HOSTS = ['*'] | ALLOWED_HOSTS = ['*'] | ||
DATABASE = { | DATABASE = { | ||
− | 'NAME': 'netbox', | + | 'NAME': 'netbox', |
− | 'USER': 'netbox', | + | 'USER': 'netbox', |
− | 'PASSWORD': 'my_strong_password', | + | 'PASSWORD': 'my_strong_password', |
− | 'HOST': 'localhost', | + | 'HOST': 'localhost', |
− | 'PORT': '', | + | 'PORT': '', |
} | } | ||
− | SECRET_KEY = ' | + | SECRET_KEY = '<generated_secret_key>' |
</pre> | </pre> | ||
+ | Run the upgrade script: | ||
− | |||
<code> | <code> | ||
sudo /opt/netbox/upgrade.sh | sudo /opt/netbox/upgrade.sh | ||
</code> | </code> | ||
+ | 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: | ||
− | |||
<code> | <code> | ||
cd /opt/netbox/netbox | cd /opt/netbox/netbox | ||
</code> | </code> | ||
+ | Create a superuser account: | ||
− | |||
<code> | <code> | ||
python3 manage.py createsuperuser | python3 manage.py createsuperuser | ||
</code> | </code> | ||
+ | Reboot the system: | ||
− | |||
<code> | <code> | ||
sudo reboot | sudo reboot | ||
</code> | </code> | ||
+ | == 4. Configure Gunicorn == | ||
+ | |||
+ | 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: | ||
− | |||
− | |||
<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: | ||
− | |||
<code> | <code> | ||
sudo systemctl daemon-reload | sudo systemctl daemon-reload | ||
</code> | </code> | ||
+ | Start the NetBox services: | ||
− | |||
<code> | <code> | ||
sudo systemctl start netbox netbox-rq | sudo systemctl start netbox netbox-rq | ||
</code> | </code> | ||
+ | Enable the services to start at boot: | ||
− | |||
<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: | ||
− | |||
− | |||
<code> | <code> | ||
sudo apt install -y nginx | sudo apt install -y nginx | ||
</code> | </code> | ||
+ | Copy the Nginx configuration file: | ||
− | |||
<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 the configuration file: | ||
− | |||
<code> | <code> | ||
sudo nano /etc/nginx/sites-available/netbox | sudo nano /etc/nginx/sites-available/netbox | ||
</code> | </code> | ||
+ | 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 | |
− | |||
− | server_name 192.0.2.10; | ||
client_max_body_size 25m; | client_max_body_size 25m; | ||
Line 238: | Line 257: | ||
} | } | ||
</pre> | </pre> | ||
+ | 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 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 | + | Restart the Nginx service: |
+ | |||
<code> | <code> | ||
sudo systemctl restart nginx | sudo systemctl restart nginx | ||
</code> | </code> | ||
− | |||
− |
Revision as of 10:04, 15 August 2024
Contents
Prerequisites
Create a non-root user with sudo access.
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