NetBox 最新版 4.4.1 完整安装指南

NetBox 最新版 4.4.1 完整安装指南(修正版)by 大牛蛙

1. 系统准备

# 关闭 SELinux 和防火墙(仅测试环境)

systemctl disable –now firewalld

sed -i ‘s/^SELINUX=enforcing$/SELINUX=disabled/’ /etc/selinux/config && setenforce 0

# 安装基础工具

dnf install tree vim bash-completion tar -y

2. 安装和配置 PostgreSQL

# 安装 PostgreSQL 16

dnf module install postgresql:16 -y

# 初始化数据库

postgresql-setup –initdb

# 修改认证方式

sed -i ‘s/ident$/scram-sha-256/g’ /var/lib/pgsql/data/pg_hba.conf

# 启动 PostgreSQL

systemctl enable –now postgresql

# 创建数据库和用户

sudo -u postgres psql -c “ALTER USER postgres WITH PASSWORD ‘adminpassword123’;”

sudo -u postgres psql -c “CREATE DATABASE netboxdb;”

3. 安装和配置 Redis

# 安装 Redis

dnf module install redis:7 -y

# 设置密码

echo “requirepass adminpassword123” >> /etc/redis/redis.conf

# 启动 Redis

systemctl enable –now redis

# 验证连接

redis-cli -a adminpassword123 ping

4. 安装 Python 3.12

dnf install python3.12 python3.12-pip python3.12-devel python3-pip -y

5. 安装 NetBox 依赖

dnf install gcc libxml2-devel libxslt-devel libffi-devel libpq-devel openssl-devel redhat-rpm-config git wget -y

6. 创建 NetBox 用户和目录

useradd -r -d /opt/netbox -s /usr/sbin/nologin netbox

# 下载和解压 NetBox(如果还没做)

cd /opt

wget https://github.com/netbox-community/netbox/archive/refs/tags/v4.4.1.tar.gz

tar -zxvf v4.4.1.tar.gz

ln -s netbox-4.4.1 netbox

# 设置权限

chown -R netbox:netbox /opt/netbox*

chown -R netbox:netbox /opt/netbox-4.4.1

7. 配置 NetBox

# 切换到正确目录

cd /opt/netbox/netbox/netbox

# 复制配置文件

sudo -u netbox cp configuration_example.py configuration.py

# 生成 SECRET_KEY 并配置

cd /opt/netbox/netbox

SECRET_KEY=$(sudo -u netbox python3 generate_secret_key.py)

cd /opt/netbox/netbox/netbox

sudo -u netbox sed -i “s/^SECRET_KEY = .*/SECRET_KEY = ‘$SECRET_KEY’/” configuration.py

# 编辑配置文件

sudo -u netbox tee -a configuration.py > /dev/null << EOF

ALLOWED_HOSTS = [‘*’]

DATABASES = {

‘default’: {

‘ENGINE’: ‘django.db.backends.postgresql’,

‘NAME’: ‘netboxdb’,

‘USER’: ‘postgres’,

‘PASSWORD’: ‘adminpassword123’,

‘HOST’: ‘localhost’,

‘PORT’: ”,

‘CONN_MAX_AGE’: 300,

}

}

REDIS = {

‘tasks’: {

‘HOST’: ‘localhost’,

‘PORT’: 6379,

‘USERNAME’: ”,

‘PASSWORD’: ‘adminpassword123’,

‘DATABASE’: 0,

‘SSL’: False,

},

‘caching’: {

‘HOST’: ‘localhost’,

‘PORT’: 6379,

‘USERNAME’: ”,

‘PASSWORD’: ‘adminpassword123’,

‘DATABASE’: 1,

‘SSL’: False,

}

}

# 设置默认语言和时区

DEFAULT_LANGUAGE = ‘zh-hans’

TIME_ZONE = ‘Asia/Shanghai’

# 如果需要本地化支持

ENABLE_LOCALIZATION = True

EOF

8. 安装 NetBox 应用

# 切换到 NetBox 根目录

cd /opt/netbox

# 配置 pip 使用国内源

mkdir -p /opt/netbox/.config/pip

chown -R netbox:netbox /opt/netbox/.config

sudo -u netbox tee /opt/netbox/.config/pip/pip.conf > /dev/null << EOF

[global]

index-url = https://pypi.tuna.tsinghua.edu.cn/simple

trusted-host = pypi.tuna.tsinghua.edu.cn

EOF

# 运行安装脚本

sudo -u netbox PYTHON=/usr/bin/python3.12 ./upgrade.sh

9. 创建管理员账户

# 切换到 manage.py 所在目录

cd /opt/netbox/netbox

# 创建超级用户(交互式)

sudo -u netbox /opt/netbox/venv/bin/python manage.py createsuperuser

按照提示输入:

用户名:admin

邮箱:admin@example.com

密码:adminpassword123

10. 安装额外插件

sudo -u netbox /opt/netbox/venv/bin/pip install dulwich

11. 配置系统服务

# 配置 Gunicorn

sudo -u netbox cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py

# 安装系统服务

cp -v /opt/netbox/contrib/*.service /etc/systemd/system/

systemctl daemon-reload

# 启动 NetBox 服务

systemctl enable –now netbox netbox-rq

# 配置定时任务

ln -sf /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping

12. 配置 Nginx 反向代理

# 安装 Nginx

dnf install nginx -y

# 创建配置文件

tee /etc/nginx/conf.d/netbox.conf > /dev/null << EOF

server {

listen 80;

server_name _;

client_max_body_size 25m;

location /static/ {

alias /opt/netbox/netbox/static/;

}

location / {

proxy_pass http://127.0.0.1:8001;

proxy_set_header Host $http_host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Proto $scheme;

}

}

EOF

# 启动 Nginx

systemctl enable –now nginx

13. 验证安装

# 检查服务状态

systemctl status netbox

systemctl status nginx

# 检查端口监听

ss -tlnp | grep -E ‘(8001|80)’

# 测试访问

curl -I http://localhost

14. 访问 NetBox

访问地址:http://你的服务器IP

用户名:admin

密码:adminpassword123

关键目录说明

NetBox 根目录:/opt/netbox/(软链接指向 /opt/netbox-4.4.1/)

配置文件:
/opt/netbox/netbox/netbox/configuration.py

管理命令:
/opt/netbox/netbox/manage.py

虚拟环境:/opt/netbox/venv/

PS: PG优化配置生成器:
https://pgtune.leopard.in.ua/ (若不配置,会很卡)

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容