文章目录
环境介绍
# 非docker方式部署,便于按需修改代码
RockyLinux 9.4
Python 3.12
PostgreSQL 16
Redis 7
NetBox 4.0.7 (该版本集成的中文语言,基本比较完善准确了,无需手动汉化了)
参考文章:https://songxwn.com/netbox4-CN/
安装和配置 PostgreSQL 数据库
# 关闭防火墙
systemctl disable --now firewalld
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config && setenforce 0
# 安装基本工具软件
dnf install tree vim bash-completion tar -y
# 安装数据库,并初始化数据库
dnf module install postgresql:16 -y
postgresql-setup --initdb
# 将主机连接的加密方式将ident改为scram-sha-256。
vim /var/lib/pgsql/data/pg_hba.conf
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# 启动并设置开机启动
systemctl enable --now postgresql
# 登录到PostgreSQL shell
sudo -u postgres psql
# 运行查询,为默认的PostgreSQL用户“postgres”设置新密码。
ALTER USER postgres WITH PASSWORD 'ABcDP-l~sOnq';
# 创建数据库,并退出
CREATE DATABASE netboxdb;
quit
# Tips:PG优化配置生成器:https://pgtune.leopard.in.ua/ (若不配置,会很卡)
安装和配置 Redis 数据库
# 使用DNF安装
dnf module install redis:7 -y
# 配置访问密码(打开配置文件,找到被注释的requirepass行,修改密码为XXXqq.xx.xx。保存并退出)
vim /etc/redis/redis.conf
requirepass XXXqq.xx.xx
# 配置启动,开机启动,并验证
systemctl enable --now redis
# 验证启动
systemctl status redis ; ss -an | grep 6379
# 输入密码登录验证是否正常
redis-cli
127.0.0.1:6379> AUTH XXXqq.xx.xx
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
安装和验证 Python 3.12
# 安装python3.12,并验证版本
dnf install python3.12 python3.12-pip python3.12-devel python3-pip -y
python3.12 -V
准备基础环境,下载并解压Netbox 4.0.7代码,并安装
# 安装环境
dnf install gcc libxml2-devel libxslt-devel libffi-devel libpq-devel openssl-devel redhat-rpm-config git wget -y
# 创建netbox用户
useradd -r -d /opt/netbox -s /usr/sbin/nologin netbox
# 下载代码包,解压,创建软链接(重命名),重新赋值权限
cd /opt
wget https://github.com/netbox-community/netbox/archive/refs/tags/v4.0.7.tar.gz
tar -zxvf v4.0.7.tar.gz
ln -s netbox-4.0.7 netbox
rm -rf v4.0.7.tar.gz
chown -R netbox:netbox /opt/netbox*
# 生成配置加密密钥
cd /opt/netbox/netbox/netbox
# 依据配置模版文件生成配置文件
sudo -u netbox cp configuration_example.py configuration.py
# 生成密钥
sudo -u netbox python3 ../generate_secret_key.py
#生成的密钥示例:PYi8TgNWe&+6vI8io5c_#AQ)x4m*d_KI6o8u^f!V8(t+JP8bL2
编辑netbox配置文件,设置配置加密key、数据库连接信息、本地化语言、时区
# 打开配置文件,将生成的密钥写入进去。
cd /opt/netbox/netbox/netbox
sudo -u netbox vim configuration.py
# 请完善如下字段
ALLOWED_HOSTS = ["*"] # 允许任意域名访问Netbox
DATABASE = {
'ENGINE': 'django.db.backends.postgresql', # Database engine
'NAME': 'netboxdb', # 数据库名
'USER': 'postgres', # 数据库用户
'PASSWORD': 'ABcDP-l~sOnq', # 数据库用户的密码
'HOST': 'localhost', # Database server
'PORT': '', # Database port (leave blank for default)
'CONN_MAX_AGE': 300, # Max database connection age
}
REDIS = {
'tasks': {
'HOST': 'localhost',
'PORT': 6379,
'USERNAME': '',
'PASSWORD': 'XXXqq.xx.xx', # 配置数据库密码
'DATABASE': 0,
'SSL': False,
},
'caching': {
'HOST': 'localhost',
'PORT': 6379,
'USERNAME': '',
'PASSWORD': 'XXXqq.xx.xx', # 配置数据库密码
'DATABASE': 1,
'SSL': False,
}
}
# https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-SECRET_KEY
# 将上面生成的密钥写入进去
SECRET_KEY = 'PYi8TgNWe&+6vI8io5c_#AQ)x4m*d_KI6o8u^f!V8(t+JP8bL2'
# 开启本地化,让其可以更换语言。(默认没有,需要自己写这一段)
ENABLE_LOCALIZATION = True
# 配置默认语言、时区
DEFAULT_LANGUAGE = 'zh-cn'
TIME_ZONE = 'Asia/Shanghai'
# 因为默认netbox只配置http,若是你需要配置https,
# 前端用nginx或者其他的来实现https,防止出现CSRF 403错误,需要配置好请求来源地址(注意有端口的场景)
CSRF_TRUSTED_ORIGINS = (
'http://a.xxx.cc',
'https://a.xxx.cc:50443',
)
初始化Python虚拟环境,初始化数据库,生成静态Web
# 配置安装环境的时候,使用清华源的pypi。(可不配置)
sed -i '1i pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple' /opt/netbox/upgrade.sh
# 指定用户执行python3版本,执行安装并导入数据库,需要较久时间。
sudo -u netbox PYTHON=/usr/bin/python3.12 /opt/netbox/upgrade.sh
# 出现以下字符代表成功。
Completed.
Removing expired user sessions (python3 netbox/manage.py clearsessions)...
Clearing the cache (python3 netbox/manage.py clearcache)...
Cache has been cleared.
Upgrade complete! Don't forget to restart the NetBox services:
> sudo systemctl restart netbox netbox-rq
创建netbox管理员账号、配置定时任务
# 进入虚拟环境
source /opt/netbox/venv/bin/activate
cd /opt/netbox/netbox
python3 manage.py createsuperuser # 输入该命令后,就按提示输入账号(注意账号不能输入root),邮箱和密码
# 安装数据源同步插件
pip install dulwich
# 配置定时任务
sudo ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping
配置Gunicorn WSGI
# Gunicorn 是一个 Python 的 WSGI HTTP 服务器。
# 复制创建配置文件
sudo -u netbox cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
# 可修改配置文件,更改监听端口,默认8001。(非必须)
sudo -u netbox vim /opt/netbox/gunicorn.py
# 复制到系统服务、重载系统服务、配置启动并开机启动、查看状态
cp -v /opt/netbox/contrib/*.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable --now netbox netbox-rq
systemctl status netbox
systemctl status netbox-rq
# 检查端口监听情况、和启动日志
ss -an | grep 8001
journalctl -eu netbox
# 浏览器访问 http://ip:8001 输入刚才设置的管理员账户密码即可访问
附、安装QR二维码插件,并进行汉化;安装ip子网计算插件(未汉化)
# 进入netbox虚拟python环境
source /opt/netbox/venv/bin/activate
pip install netbox-qrcode
pip install netbox_ipcalculator
# 中文字体支持和汉化、下载功能增加
cd /opt/netbox/venv/lib/python3.12/site-packages/netbox_qrcode/fonts/
#下载开源字体到字体目录
wget -O SourceHanSansSC.ttf https://mirrors.bfsu.edu.cn/adobe-fonts/source-han-sans/OTF/SimplifiedChinese/SourceHanSansSC-Normal.otf
#进入库的Web模板目录,移动原文件到临时目录,下载汉化和增加下载功能的QR替换模板(可选,建议操作)
cd /opt/netbox/venv/lib/python3.12/site-packages/netbox_qrcode/templates/netbox_qrcode/
mv qrcode3.html /tmp
wget -O qrcode3.html https://qiniu.wsfnk.com/bokefiles/qrcode3.html
# 开启和配置
# 进入目录,以netbox 用户打开文件
cd /opt/netbox/netbox/netbox
sudo -u netbox vim configuration.py # 如下是要添加到配置文件中的内容
# 插件开启列表
PLUGINS = ['netbox_qrcode', 'netbox_ipcalculator']
PLUGINS_CONFIG = {
'netbox_qrcode': {
'with_text': True, # 是否开启文字显示
'text_fields': ['name', 'serial'],
'font': 'SourceHanSansSC', # 指定上面下载的中文开源字体
'custom_text': '所属: boke.wsfnk.com',
'text_location': 'right', # 文字显示方向为右边
'qr_version': 1, # QR版本
'qr_error_correction': 0,
'qr_box_size': 6,
'qr_border': 4,
# per object options
'cable': None, # disable QR code for Cable object
'rack': {
'text_template': '{{ obj.name }}\n租户: {{ obj.tenant }}\n站点: {{ obj.site }}',
'qr_box_size': 6,
'custom_text': None,
}, # 机柜二维码生成开启并定义显示文字的字段
'device': {
'text_template': '{{ obj.name }}\n带外管理IP: {{ obj.oob_ip }}\n主IPv4地址: {{ obj.primary_ip }}\n序列号: {{ obj.serial }}\n位置: {{ obj.location }}\n机柜: {{ obj.rack }} - U位: {{ obj.position }}',
'qr_box_size': 6,
'custom_text': None,
} # 设备二维码生成开启并定义显示文字的字段
}
}
# 重启Netbox(在web的设备内页就能看到二维码)
systemctl restart netbox netbox-rq.service
附、如何配置nginx反向代理
# 安装nginx
dnf install nginx
# 创建配置文件,注意修改a.atstm.cc 为自己的域名。反向代理到8001端口
cat /etc/nginx/conf.d/netbox.conf
# 不用https就只需这段
server {
listen 80;
server_name a.atstm.cc;
client_max_body_size 25m;
fastcgi_connect_timeout 1200s;
fastcgi_send_timeout 1200s;
fastcgi_read_timeout 1200s;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
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;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
}
# 只用https就只需这段(注意ssl证书路径和文件请自行准备好)
server {
listen [::]:30443 ssl ipv6only=off;
# CHANGE THIS TO YOUR SERVER'S NAME
server_name a.atstm.cc;
ssl_certificate /etc/nginx/ssl/atstm.cc_public.crt;
ssl_certificate_key /etc/nginx/ssl/atstm.cc.key;
client_max_body_size 25m;
location /static/ {
alias /opt/netbox/netbox/static/;
}
location / {
# Remove these lines if using uWSGI instead of Gunicorn
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;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
# Uncomment these lines if using uWSGI instead of Gunicorn
# include uwsgi_params;
# uwsgi_pass 127.0.0.1:8001;
# uwsgi_param Host $host;
# uwsgi_param X-Real-IP $remote_addr;
# uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
# uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
}
}
# 启动并设置nginx开机自启动
systemctl enable --now nginx
# 浏览器访问 http://ip 输入刚才设置的管理员账户密码即可访问
附、如何迁移netbox(如何备份和还原)
# 思路:迁移迁,先在新环境,用一样版本的netbox搭一套,然后删除数据库,把老系统的数据库备份并还原到新系统中(这个过程新老系统的 SECRET_KEY 可以不一样)
# (备份库)使用postgres用户,将数据库netboxdb,备份成文件 /root/netboxdb.backup (注意:回车后需要输入数据库密码)
pg_dump -U postgres -h localhost -F c -b -v -f /root/netboxdb.backup netboxdb
# 登录postgres数据库软件,删除老的数据库(注意自己先备份好),再创建一个空的新数据库。并退出
sudo -u postgres psql
DROP DATABASE netboxdb;
CREATE DATABASE netboxdb;
quit
# (恢复库)使用postgres用户,将数据库备份文件 /root/netboxdb.backup,还原到 netboxdb 数据库。
pg_restore -U postgres -h localhost -d netboxdb -v /root/netboxdb.backup
附、netbox常规的文件路径
# logo文件路径
/opt/netbox-4.0.7/netbox/static/netbox_logo.svg
如果文章对你有帮助,欢迎点击上方按钮打赏作者
暂无评论