如何在Ubuntu 18.04 LTS上安装NetBox

时间:2020-02-23 14:32:47  来源:igfitidea点击:

如何在Ubuntu 18.04 LTS上安装NetBox是我们今天的文章的标题。
NetBox是一个用于管理和记录计算机网络和管理IP地址的开源IPAM/DCIM Web应用程序。
它最初被数字沉积的网络工程团队构思。

NetBox包含网络管理的以下各个方面:IP地址管理(IPAM) - IP网络和地址,VRF和VLANECUCHIC件机架 - 由Group和Sitedevices组织 - 设备类型以及它们是安装的连接 - 网络,控制台和电源连接DevicesVirtualization - 虚拟机和ClustersData电路 - 长途通信电路和ProvidersSecrets - 加密敏感凭证的存储

在Ubuntu 18.04 LTS上安装NetBox

本节将讨论在Ubuntu 18.04 LTS服务器上安装NetBox的实际步骤。
按照外观的顺序跟随它们,但如果我们已安装软件,则可以跳过零件。

第1步:安装所需的依赖项

首先安装在Ubuntu 18.04 LTS上运行NetBox所需的所有依赖性应用程序。

sudo apt-get install -y git gcc nginx redis supervisor python3 python3-dev python3-pip python3-setuptools build-essential libxml2-dev libxslt1-dev libffi-dev graphviz libpq-dev libssl-dev zlib1g-dev

第2步:安装和配置PostgreSQL数据库服务器

NetBox使用PostgreSQL数据库服务器来存储其数据。
因此,请使用下面的前教程在Ubuntu 18.04上安装和配置它:

在Ubuntu 18.04/Ubuntu 16.04上安装PostgreSQL 12

然后为netbox创建一个数据库和用户。

$sudo -u postgres psql
CREATE DATABASE netbox;
CREATE USER netbox WITH PASSWORD 'StrongPassword';
GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
\q

确认我们可以登录数据库 netbox用户。

# psql -U netbox -h localhost -W
Password: 
psql (11.1 (Ubuntu 11.1-1.pgdg18.04+1))
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
netbox=>

第3步:安装和配置netbox

改成 /opt/目录

cd /opt/
git clone -b master https://github.com/digitalocean/netbox.git

创建配置文件

cd netbox/netbox/netbox/
sudo cp configuration.example.py configuration.py

编辑配置文件并设置允许的主机和数据库登录详细信息

# Example: ALLOWED_HOSTS = ['netbox.example.com', 'netbox.internal.local']
ALLOWED_HOSTS = ['localhost']
# PostgreSQL database configuration.
DATABASE = {
    'NAME': 'netbox',                           # Database name
    'USER': 'netbox',                           # PostgreSQL username
    'PASSWORD': 'StrongPassword',               # PostgreSQL password
    'HOST': 'localhost',                        # Database server
    'PORT': '',                                 # Database port (leave blank for default)
}

生成django秘密密钥:

cd /opt/netbox/netbox
sudo ./generate_secret_key.py

然后在文件上设置密钥 /opt/netbox/netbox/netbox/configuration.py例子:

$sudo vim /opt/netbox/netbox/netbox/configuration.py
SECRET_KEY = '30m&Hyman@theitroad=^l8wqtjw2$!3j%=f2!zh_sey+13jg%3$'

安装netbox依赖项

sudo pip3 install -r /opt/netbox/requirements.txt

迁移数据库数据:

cd /opt/netbox/netbox/
sudo python3 manage.py migrate

数据库迁移的示例输出。

Applying circuits.0014_circuittermination_description... OK
  Applying dcim.0067_device_type_remove_qualifiers... OK
  Applying dcim.0068_rack_new_fields... OK
  Applying tenancy.0004_tags... OK
  Applying tenancy.0005_change_logging... OK
  Applying extras.0001_initial_squashed_0010_customfield_filter_logic... OK
  Applying extras.0011_django2... OK
  Applying extras.0012_webhooks... OK
  Applying extras.0013_objectchange... OK
  Applying extras.0014_configcontexts... OK
  Applying extras.0015_remove_useraction... OK
  Applying extras.0016_exporttemplate_add_cable... OK
  Applying ipam.0021_vrf_ordering... OK
  Applying ipam.0022_tags... OK
  Applying ipam.0023_change_logging... OK
  Applying secrets.0001_initial_squashed_0003_unicode_literals... OK
  Applying secrets.0004_tags... OK
  Applying secrets.0005_change_logging... OK
  Applying sessions.0001_initial... OK
  Applying users.0001_api_tokens_squashed_0002_unicode_literals... OK
  Applying users.0003_token_permissions... OK
  Applying virtualization.0002_virtualmachine_add_status_squashed_0004_virtualmachine_add_role... OK
  Applying virtualization.0005_django2... OK
  Applying virtualization.0006_tags... OK
  Applying virtualization.0007_change_logging... OK
  Applying virtualization.0008_virtualmachine_local_context_data... OK

创建管理员用户:

$sudo python3 manage.py createsuperuser
Username (leave blank to use 'root'): admin
Email address: Hyman@theitroad
Password: <Enter Password>
Password (again): <Re-enter Password>
Superuser created successfully.

移动静态文件

$cd /opt/netbox/netbox
$sudo python3 manage.py collectstatic
280 static files copied to '/opt/netbox/netbox/static'.

第4步:安装和配置鼠尾草

使用pip3安装枪手:

$sudo pip3 install gunicorn
Collecting gunicorn
Downloading https://files.pythonhosted.org/packages/8c/da/b8dd8deb741bff556db53902d4706774c8e1e67265f69528c14c003644e6/gunicorn-19.9.0-py2.py3-none-any.whl (112kB)     100% |████████████████████████████████| 122kB 737kB/s
Installing collected packages: gunicorn
Successfully installed gunicorn-19.9.0

为netbox配置Gunicorn:

cat <<EOF | sudo tee /opt/netbox/gunicorn_config.py
command = '/usr/local/bin/gunicorn'
pythonpath = '/opt/netbox/netbox'
bind = 'localhost:8085'
workers = 3
user = 'www-data'
EOF

第5步:配置Supervisord

创建Supervisord配置文件:

cat <<EOF | sudo tee /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
EOF

重新启动并启用Supervisord服务以启动启动。

sudo systemctl restart supervisor.service
sudo systemctl enable supervisor.service

状态应显示使用netbox输出运行。

$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 Sun 2019-01-13 06:28:55 PST; 4s ago
  Docs: http://supervisord.org
 Process: 28799 ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown (code=exited, status=0/SUCCESS)
 Main PID: 28830 (supervisord)
 Tasks: 5 (limit: 1110)
 CGroup: /system.slice/supervisor.service
        |-28830 /usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
        |-28850 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
        |-28855 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
        |-28857 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
        `-28859 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi 
 Jan 13 06:28:55 ubuntu-01 systemd[1]: Started Supervisor process control system for UNIX.
 Jan 13 06:28:55 ubuntu-01 supervisord[28830]: 2019-01-13 06:28:55,572 CRIT Supervisor running as root (no user in config file)
 Jan 13 06:28:55 ubuntu-01 supervisord[28830]: 2019-01-13 06:28:55,572 INFO Included extra file "/etc/supervisor/conf.d/netbox.conf" during parsing
 Jan 13 06:28:55 ubuntu-01 supervisord[28830]: 2019-01-13 06:28:55,580 INFO RPC interface 'supervisor' initialized
 Jan 13 06:28:55 ubuntu-01 supervisord[28830]: 2019-01-13 06:28:55,580 CRIT Server 'unix_http_server' running without any HTTP authentication checking
 Jan 13 06:28:55 ubuntu-01 supervisord[28830]: 2019-01-13 06:28:55,580 INFO supervisord started with pid 28830
 Jan 13 06:28:56 ubuntu-01 supervisord[28830]: 2019-01-13 06:28:56,583 INFO spawned: 'netbox' with pid 28850
 Jan 13 06:28:57 ubuntu-01 supervisord[28830]: 2019-01-13 06:28:57,871 INFO success: netbox entered RUNNING state, process has stayed up for > than 1 se

步骤6:配置Nginx Web服务器

让我们配置Nginx Web服务器,通过域名来帮助我们访问NetBox,而不是指定IP地址和端口。

为netbox创建新的nginx配置文件。

sudo vim /etc/nginx/conf.d/netbox.conf

具有以下数据。

server {
    listen 80;
    server_name netbox.example.com;
    client_max_body_size 25m;
    location /static/{
        alias /opt/netbox/netbox/static/;
    }
    location/{
        proxy_pass http://localhost:8085;
    }
}

检查nginx配置语法并重新启动其服务

$sudo  nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果确定,请重新启动nginx服务

sudo systemctl restart nginx

第7步:访问netbox web ui

打开默认的Web浏览器并打开NetBox Server主机名。
要进行更改,请使用前面创建的管理员用户登录。