云网牛站
所在位置:首页 > Linux云服务器 > 在Ubuntu 18.04 LTS上安装NetBox的方法

在Ubuntu 18.04 LTS上安装NetBox的方法

2019-01-14 09:28:15作者:彭即元稿源:云网牛站

本文介绍在Ubuntu 18.04 LTS系统上安装NetBox的方法,如果已经安装了以下所说的软件,则可以跳过。

 

简介

NetBox是一个开源的IPAM/DCIM Web应用程序,用于管理和记录计算机网络以及管理IP地址。

Netbox包含以下网络管理方面:

IP地址管理(IPAM)-IP网络和地址,VRF和VLAN。

设备架-由团体和场地组织。

设备-设备类型及其安装位置。

连接-设备之间的网络,控制台和电源连接。

虚拟化-虚拟机和集群。

数据电路-长途通信电路和提供商。

 

一、安装所需的依赖项

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

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

 

二、安装和配置PostgreSQL数据库

NetBox使用PostgreSQL数据库来存储其数据,参考在Ubuntu 18.04/Ubuntu 16.04上安装PostgreSQL 11的说明

为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=>

 

三、安装并配置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 = ['192.168.121.31']

# 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)

}

将192.168.121.31替换为你的Netbox服务器IP地址。

生成Django SECRET密钥:

cd /opt/netbox/netbox

sudo ./generate_secret_key.py

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

例:

SECRET_KEY = '30mhqd09h2i5hro00'

安装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: admin@example.com

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'.

加载初始数据:

$ sudo python3 manage.py loaddata initial_data

Installed 53 object(s) from 5 fixture(s)

 

四、安装和配置gunicorn

使用pip3安装gunicorn,pip3的安装方法参考在Ubuntu 18.04系统中安装pip并使用pip命令

$ 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 = '192.168.121.31:8085'

workers = 3

user = 'www-data'

EOF

 

五、创建一个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

状态如下:

$ systemctl status  supervisor

在Ubuntu 18.04 LTS上安装NetBox的方法

 

六、配置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://192.168.121.31:8001;

}

}

检查Nginx配置syntax并重新启动其服务:

$ 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

 

七、访问Netbox Web UI

打开默认Web浏览器并打开Netbox服务器主机名,要进行更改,请使用之前创建的admin用户登录。

在Ubuntu 18.04 LTS上安装NetBox的方法

至此,可以随时使用Netbox了。

 

相关主题

Ubuntu 16.04配置Nginx为WWW站点启用HTTPS

精选文章
热门文章