如何在 Ubuntu 18.04 LTS 上安装 NetBox 网络文档和管理工具
本教程适用于这些操作系统版本
- Ubuntu 20.04(Focal Fossa)
- Ubuntu 18.04(仿生海狸)
在此页
- 要求
- 开始
- 安装和配置 PostgreSQL
- 安装和配置 NetBox
- 为 NetBox 安装和配置 Gunicorn
- 为 Netbox 配置主管
- 为 NetBox 配置 Nginx
- 访问 NetBox 网络界面
NetBox 是一种免费的开源 Web 应用程序软件,可用于管理和记录计算机网络。它专门设计用于通过网络浏览器管理 IP 地址和数据中心基础设施。 NetBox 是用 Django Python 框架编写的,数据库使用 PostgreSQL。使用 NetBox,您可以从中央位置轻松管理和记录虚拟机和集群、数据电路、网络、控制台和电源连接。
在本教程中,我们将解释如何在 Ubuntu 18.04 服务器上安装 NetBox。
要求
- 一台运行 Ubuntu 18.04 的服务器。
- 在您的服务器上设置了根密码。
入门
首先,使用以下命令将系统包更新到最新版本:
apt-get update -y
apt-get upgrade -y
更新所有包后,重新启动系统以应用所有配置更改。
接下来,您需要安装运行 NetBox 所需的依赖项。您可以使用以下命令安装所有这些:
apt-get install wget ca-certificates nginx supervisor git gcc python3 python3-dev python3-pip python3-setuptools build-essential libxml2-dev libxslt1-dev libffi-dev graphviz libpq-dev libssl-dev zlib1g-dev unzip -y
安装所有依赖项后,您可以继续下一步。
安装和配置 PostgreSQL
接下来,您需要在您的服务器上安装 PostgreSQL。默认情况下,Ubuntu 18.04 默认存储库中不提供最新版本的 PostgreSQL。因此,您需要将 PostgreSQL 存储库添加到您的系统。
首先,使用以下命令下载并添加 GPG 密钥:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
接下来,使用以下命令添加存储库:
nano /etc/apt/sources.list.d/postgres.list
添加以下行:
deb http://apt.postgresql.org/pub/repos/apt/ xanial-pgdg main
保存并关闭文件。然后,更新存储库并使用以下命令安装 PostgreSQL:
apt-get update -y
apt-get install postgresql postgresql-contrib -y
安装后,使用以下命令检查 PostgreSQL 的状态:
systemctl status postgresql
您应该看到以下输出:
? postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Wed 2019-08-07 07:00:51 UTC; 23s ago
Main PID: 13552 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 1098)
CGroup: /system.slice/postgresql.service
Aug 07 07:00:50 hitesh systemd[1]: Starting PostgreSQL RDBMS...
Aug 07 07:00:51 hitesh systemd[1]: Started PostgreSQL RDBMS.
接下来,您需要为 NetBox 创建数据库和用户。首先,使用以下命令登录到 PostgreSQL shell:
su - postgres
:~$ psql
输出:
psql (11.4 (Ubuntu 11.4-1.pgdg18.04+1))
Type "help" for help.
接下来,使用以下命令为 NetBox 创建数据库和用户:
postgres=# CREATE DATABASE netbox;
postgres=# CREATE USER netbox WITH PASSWORD 'password';
接下来,使用以下命令授予 NetBox 所有权限:
postgres=# GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
最后,使用以下命令退出 PostgreSQL shell:
postgres=#exit
完成后,您可以继续下一步。
安装和配置 NetBox
您可以使用以下命令将最新版本的 NetBox 下载到 /opt 目录:
cd /opt
git clone -b master https://github.com/digitalocean/netbox.git
接下来,将目录更改为 netbox 并生成 Django SECRET Key:
cd /opt/netbox/netbox/netbox/
./generate_secret_key.py
您应该在以下输出中看到生成的密钥:
+XHR3o&7K6isFk^DLc2%(jwN#tfGbV=O1hgMU$
接下来,使用以下命令重命名默认配置文件:
mv configuration.example.py configuration.py
接下来,打开配置文件并定义您的数据库详细信息和密钥:
nano configuration.py
进行以下更改:
#Replace your-server-ip with your server IP address:
# Example: ALLOWED_HOSTS = ['netbox.example.com', 'netbox.internal.local']
ALLOWED_HOSTS = ['your-server-ip']
# PostgreSQL database configuration.
DATABASE = {
'NAME': 'netbox', # Database name
'USER': 'netbox', # PostgreSQL username
'PASSWORD': 'password', # PostgreSQL password
'HOST': 'localhost', # Database server
'PORT': '', # Database port (leave blank for default)
}
SECRET_KEY = '+XHR3o&7K6isFk^DLc2%(jwN#tfGbV=O1hgMU$'
完成后保存并关闭文件。
接下来,使用以下命令安装 NetBox 所需的所有依赖项:
pip3 install -r /opt/netbox/requirements.txt
安装后,使用以下命令迁移数据库:
cd /opt/netbox/netbox/
python3 manage.py migrate
迁移成功后,您应该会看到以下输出:
Applying secrets.0004_tags... OK
Applying secrets.0005_change_logging... OK
Applying secrets.0006_custom_tag_models... OK
Applying ipam.0021_vrf_ordering... OK
Applying ipam.0022_tags... OK
Applying ipam.0023_change_logging... OK
Applying ipam.0024_vrf_allow_null_rd... OK
Applying ipam.0025_custom_tag_models... OK
Applying dcim.0067_device_type_remove_qualifiers... OK
Applying dcim.0068_rack_new_fields... OK
Applying dcim.0069_deprecate_nullablecharfield... OK
Applying dcim.0070_custom_tag_models... OK
Applying extras.0020_tag_data... OK
Applying extras.0021_add_color_comments_changelog_to_tag... OK
Applying dcim.0071_device_components_add_description... OK
Applying dcim.0072_powerfeeds... OK
Applying dcim.0073_interface_form_factor_to_type... OK
Applying extras.0022_custom_links... OK
Applying extras.0023_fix_tag_sequences... OK
Applying ipam.0026_prefix_ordering_vrf_nulls_first... OK
Applying ipam.0027_ipaddress_add_dns_name... OK
Applying sessions.0001_initial... OK
Applying taggit.0003_taggeditem_add_unique_index... OK
Applying users.0001_api_tokens_squashed_0002_unicode_literals... OK
Applying users.0003_token_permissions... OK
接下来,您需要为 NetBox 创建一个管理员帐户。您可以使用以下命令执行此操作:
python3 manage.py createsuperuser
提供您的管理员用户名和密码并按 Enter,您应该会看到以下输出:
Username (leave blank to use 'root'): netboxadmin
Email address:
Password:
Password (again):
Superuser created successfully.
接下来使用以下命令移动静态文件并加载初始数据:
python3 manage.py collectstatic
python3 manage.py loaddata initial_data
为 NetBox 安装和配置 Gunicorn
接下来,您需要为 NetBox 安装 Gunicorn。您可以使用 pip 命令安装它,如下所示:
pip3 install gunicorn
接下来,使用以下命令为 NetBox 创建 Gunicorn 配置文件:
nano /opt/netbox/gunicorn_config.py
添加以下行:
command = '/usr/local/bin/gunicorn'
pythonpath = '/opt/netbox/netbox'
bind = 'your-server-ip:8001'
workers = 3
user = 'www-data'
完成后保存并关闭文件。
为 Netbox 配置 Supervisor
接下来,您需要配置 Supervisor 来管理 NetBox 服务。您可以使用以下命令执行此操作:
nano /etc/supervisor/conf.d/netbox.conf
添加以下行:
[program:netbox]
command = gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
directory = /opt/netbox/netbox/
user = www-data
保存并关闭文件,然后重新启动 supervisor 服务,并使用以下命令使其在系统重启后启动:
systemctl restart supervisor
systemctl enable supervisor
您还可以使用以下命令验证 Supervisor 服务:
systemctl status supervisor
输出:
? supervisor.service - Supervisor process control system for UNIX
Loaded: loaded (/lib/systemd/system/supervisor.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2019-08-07 07:13:26 UTC; 8s ago
Docs: http://supervisord.org
Process: 15013 ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown (code=exited, status=0/SUCCESS)
Main PID: 15015 (supervisord)
Tasks: 5 (limit: 1098)
CGroup: /system.slice/supervisor.service
??15015 /usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
??15037 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
??15042 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
??15043 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
??15044 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
Aug 07 07:13:26 hitesh systemd[1]: Started Supervisor process control system for UNIX.
Aug 07 07:13:27 hitesh supervisord[15015]: 2019-08-07 07:13:27,087 CRIT Supervisor running as root (no user in config file)
Aug 07 07:13:27 hitesh supervisord[15015]: 2019-08-07 07:13:27,089 INFO Included extra file "/etc/supervisor/conf.d/netbox.conf" during parsing
Aug 07 07:13:27 hitesh supervisord[15015]: 2019-08-07 07:13:27,117 INFO RPC interface 'supervisor' initialized
Aug 07 07:13:27 hitesh supervisord[15015]: 2019-08-07 07:13:27,117 CRIT Server 'unix_http_server' running without any HTTP authentication check
Aug 07 07:13:27 hitesh supervisord[15015]: 2019-08-07 07:13:27,118 INFO supervisord started with pid 15015
Aug 07 07:13:28 hitesh supervisord[15015]: 2019-08-07 07:13:28,123 INFO spawned: 'netbox' with pid 15037
Aug 07 07:13:29 hitesh supervisord[15015]: 2019-08-07 07:13:29,610 INFO success: netbox entered RUNNING state, process has st
为 NetBox 配置 Nginx
接下来,您需要配置 Nginx 以使用端口 80 访问 NetBox。为此,请使用以下命令创建一个虚拟主机配置文件:
nano /etc/nginx/sites-available/netbox
添加以下行:
server {
listen 80;
server_name your-domain-name;
client_max_body_size 25m;
location /static/ {
alias /opt/netbox/netbox/static/;
}
location / {
proxy_pass http://your-server-ip:8001;
}
}
保存并关闭文件。然后,使用以下命令启用虚拟主机:
ln -s /etc/nginx/sites-available/netbox.conf /etc/nginx/sites-enabled/
最后,使用以下命令重新启动 Nginx 服务以应用所有配置更改:
systemctl restart nginx
访问 NetBox Web 界面
NetBox 现已安装和配置,是时候访问 NetBox Web 界面了。
打开您的 Web 浏览器并输入 URL http://your-domain.com。您将被重定向到以下页面:

现在,单击右上角的“登录”按钮。您应该会看到以下页面:

现在,提供您的管理员用户名和密码,然后单击“登录”按钮。您应该在以下页面中看到 NetBox 默认仪表板:

恭喜!您已在 Ubuntu 18.04 服务器上成功安装和配置 NetBox。您现在可以从中央位置管理和记录您的网络和 IP 地址。如果您有任何问题,请随时问我。