Install Netbox on Ubuntu 18

by Daniel Pham
Published: Updated:

Install Netbox on Ubuntu 18. In this article, I will guide you step by step to successfully install Netbox.

Netbox can be installed on both Ubuntu 18 and CentOS 7 operating systems. In this article, I guide you to install on Ubuntu 18. If you want to use CentOS 7, you can read Netbox’s documentation and follow step by step.

Prepare the server

Now, you need to prepare an Ubuntu server 18.04 LTS to install Netbox.

The minimum configuration that I think you should set up to use for the system is as follows:

  • CPU: 2-4 vCPU.
  • RAM: 2 GB.
  • HDD: 20 GB

By default, you have already set up a basic Ubuntu server such as: ssh into the server, update package, install timezone.

Install Netbox on Ubuntu 18

Now we will proceed step by step to install Netbox. Commands are executed with sudo or root privileges.

Install Netbox on Ubuntu 18
Netbox components.

Update the operating system.

sudo apt-get update
sudo apt-get install -y postgresql libpq-dev

Create database

Netbox uses PostgreSQL version 9.6 or later. You use the commands below to create a database for Netbox.

root@netbox:/home/ubuntu# sudo -u postgres psql
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
Type "help" for help.

postgres=# CREATE DATABASE netbox;
CREATE DATABASE
postgres=# CREATE USER netbox WITH PASSWORD 'your_db_password';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
GRANT
postgres=# \q

Note you should change your_db_password with your own password.

After you have created the database, type the command below to check whether the connection to the database is correct or not.

root@netbox:/home/ubuntu# psql -U netbox -W -h localhost netbox
Password for user netbox: 
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

netbox=> \q

If your results appear as above, it means the database has been created successfully.

Install Redis

Redis is used as caching for Netbox. Installing Redis on Ubuntu 18 is quite simple, you almost don’t need to edit much configuration.

sudo apt-get install -y redis-server

Install Netbox

Below are the steps for you to install Netbox.

Install dependent packages

You run the command below to install the auxiliary packages used for Netbox.

sudo apt-get install -y git python3.6 python3-pip python3-venv python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev libpq-dev libssl-dev zlib1g-dev
Download Netbox source code

Create a directory containing source code on the server.

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

Clone the source code to the server. Please note the period (.) at the end of the command.

git clone -b master https://github.com/netbox-community/netbox.git .

The result looks like this.

root@netbox:/opt/netbox# git clone -b master https://github.com/netbox-community/netbox.git .
Cloning into '.'...
remote: Enumerating objects: 787, done.
remote: Counting objects: 100% (787/787), done.
remote: Compressing objects: 100% (461/461), done.
remote: Total 47411 (delta 541), reused 488 (delta 325), pack-reused 46624
Receiving objects: 100% (47411/47411), 20.42 MiB | 4.86 MiB/s, done.
Resolving deltas: 100% (37576/37576), done.
Create Netbox user

We will create a system user named netbox. This user will be used for Netbox’s web service.

You run the commands below to create a group user.

sudo groupadd --system netbox

Then, run the command below to find the Group ID. You will receive a number, for example 999.

sudo grep "netbox" /etc/group|cut -d: -f3

Next, add the netbox user to the above group.

sudo adduser --system --gid 999 netbox

Assign ownership rights to netbox users.

sudo chown --recursive netbox /opt/netbox/netbox/media/
Set up the Python environment

Netbox uses the Django framework so Python will be needed. We will use virtual environment in Python to avoid Netbox conflicts with other system programs.

Create environment folder.

python3 -m venv /opt/netbox/venv

Active python environment.

source venv/bin/activate

Install the wheel package.

(venv) root@netbox:/opt/netbox# pip3 install wheel

If you do not install this package, you may encounter errors.

  ----------------------------------------
  Failed building wheel for funcy
  Running setup.py clean for funcy
  Running setup.py bdist_wheel for coreschema ... error
  Complete output from command /opt/netbox/venv/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-mum5_2q3/coreschema/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmppkxjybygpip-wheel- --python-tag cp36:
  usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
     or: -c --help [cmd1 cmd2 ...]
     or: -c --help-commands
     or: -c cmd --help
  
  error: invalid command 'bdist_wheel'
  
  ----------------------------------------

Next, we install the necessary libraries for Netbox.

(venv) root@netbox:/opt/netbox# pip3 install -r requirements.txt
Set up configuration for Netbox

Now, we need to set up some parameters for Netbox.

Note that you are still in python virtual environment mode.

Create SECRET_KEY. The command to execute the script below will generate a random string of 50 characters. Please save it, we will use it in the Netbox configuration, without it Netbox will not work.

(venv) # cd /opt/netbox/netbox/
(venv) # ./generate_secret_key.py

Copy the sample configuration file.

(venv) # cd /opt/netbox/netbox/netbox
(venv) # cp configuration.example.py configuration.py

Now, open the file /opt/netbox/netbox/netbox/configuration.py and modify the information in the following sections.

  • ALLOWED_HOSTS: in this section you enter the IP of the server or domain you want to use to access Netbox.
  • DATABASE: you fill in the database information that we created before.
  • REDIS: you can leave this as default.
  • SECRET_KEY: enter the secret key you created above in this section.

The content of the edit will look like below.

# Example: ALLOWED_HOSTS = ['netbox.example.com', 'netbox.internal.local']
ALLOWED_HOSTS = ['netbox.devopslite.com']

# PostgreSQL database configuration. See the Django documentation for a complete list of available parameters:
#   https://docs.djangoproject.com/en/stable/ref/settings/#databases
DATABASE = {
    'NAME': 'netbox',         # Database name
    'USER': 'netbox',               # PostgreSQL username
    'PASSWORD': 'your_db_password',           # PostgreSQL password
    'HOST': 'localhost',      # Database server
    'PORT': '',               # Database port (leave blank for default)
    'CONN_MAX_AGE': 300,      # Max database connection age
}

# Redis database settings. Redis is used for caching and for queuing background tasks such as webhook events. A separate
# configuration exists for each. Full connection details are required in both sections, and it is strongly recommended
# to use two separate database IDs.
REDIS = {
    'tasks': {
        'HOST': 'localhost',
        'PORT': 6379,
        # Comment out `HOST` and `PORT` lines and uncomment the following if using Redis Sentinel
        # 'SENTINELS': [('mysentinel.redis.example.com', 6379)],
        # 'SENTINEL_SERVICE': 'netbox',
        'PASSWORD': '',
        'DATABASE': 0,
        'DEFAULT_TIMEOUT': 300,
        'SSL': False,
    },
    'caching': {
        'HOST': 'localhost',
        'PORT': 6379,
        # Comment out `HOST` and `PORT` lines and uncomment the following if using Redis Sentinel
        # 'SENTINELS': [('mysentinel.redis.example.com', 6379)],
        # 'SENTINEL_SERVICE': 'netbox',
        'PASSWORD': '',
        'DATABASE': 1,
        'DEFAULT_TIMEOUT': 300,
        'SSL': False,
    }
}

# This key is used for secure generation of random numbers and strings. It must never be exposed outside of this file.
# For optimal security, SECRET_KEY should be at least 50 characters in length and contain a mix of letters, numbers, and
# symbols. NetBox will not run without this defined. For more information, see
# https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-SECRET_KEY
SECRET_KEY = 'R8n5f`(B%xwp&zPhkcHT<JL.a=@Kg4XucCU4)fFc]`}HfMg?hp'
Run database migrations

After setting up the above configuration, run the command below to perform database migration. The results of this step are a bit long, so I’ll just put the … mark below.

(venv) # cd /opt/netbox/netbox/
(venv) # python3 manage.py migrate
Operations to perform:
  Apply all migrations: dcim, sessions, admin, ipam, utilities, auth, circuits, contenttypes, extras, secrets, users
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  ...
Create an admin account

Now we will create an administrator account, the system needs to be authenticated before it can be used. Of course you don’t want everyone to be able to see your system information.

(venv) root@netbox:/opt/netbox/netbox# python3 manage.py createsuperuser
Username (leave blank to use 'root'): admin
Email address: [email protected]
Password: 
Password (again): 
Superuser created successfully.
Collect static files
(venv) root@prd-netbox:/opt/netbox/netbox# python3 manage.py collectstatic --no-input

976 static files copied to '/opt/netbox/netbox/static' .

At this step, you have almost finished installing Netbox.

Set up HTTP Daemon

Netbox uses gunicorn to run the front-end. It can be used in conjunction with both Nginx and Apache.

In this article, I will show you how to use Nginx – because it is faster than Apache and I use Nginx quite a lot.

Create SSL certificate

In this part, we will create a local SSL certificate, you can use a commercial SSL certificate or Let’s Encrypt.

openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/ssl/private/netbox.key -out /etc/ssl/certs/netbox.crt
Install HTTP Daemon

Essentially, this is to install a web server on your netbox server.

sudo apt-get install -y nginx

Copy the netbox sample configuration file to the nginx domain directory.

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

Open the file /etc/nginx/sites-available/netbox and edit the domain information to match the ALLOWED_HOSTS section set above. An example here is: netbox.devopslite.com.

Next, you delete nginx’s default virtualhost configuration file.

cd /etc/nginx/sites-enabled/
sudo rm default
ln -s /etc/nginx/sites-available/netbox

Then you restart nginx.

sudo service nginx restart
Set up Gunicorn

This part is quite simple. You just need to copy the sample configuration file into the source folder to run the netbox application and you’re done.

cd /opt/netbox
cp contrib/gunicorn.py /opt/netbox/gunicorn.py
Set up systemd

Ubuntu 18 and CentOS 7 and later use systemd to manage programs.

We will create the netbox service configuration file in systemd.

Copy the sample configuration files to the systemd directory.

cp contrib/*.service /etc/systemd/system/

Next, reload the daemon and start the netbox.

sudo systemctl daemon-reload
sudo systemctl start netbox netbox-rq
sudo systemctl enable netbox netbox-rq

Now you can test the netbox service with the following command:

sudo systemctl status netbox

Access Netbox

Now you can open your browser and access netbox (in the article it is https://netbox.devopslite.com). By default you will see a window like this.

Install Netbox on Ubuntu 18
Netbox main window.

You can log in with the admin user information we created during the installation process.

Conclusion

This article has guided you step by step to successfully install Netbox on Ubuntu 18. I hope the article is useful to you. In the next articles, we will learn more about setting up and using Netbox.

0 0 votes
Article Rating

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.