NetBox 是一款單純管理網路環境裝置以及IPAM的開源軟體,可以為開發者這或是公司同時開發不同客戶的管理工具。
這次只有單純安裝 NetBox 網站服務,資料庫服務僅部屬 Table Schema。
版本: v3.3.7
安裝之前先準備資料庫環境
sudo -u postgres psql postgres -c "CREATE DATABASE netbox"
sudo -u postgres psql postgres -c "CREATE USER netbox WITH PASSWORD 'J5brHrAXFLQSif0K'"
sudo -u postgres psql postgres -c "GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox"
psql -U postgres -d netboxdb
GRANT ALL ON schema public TO netbox;
GRANT USAGE ON SCHEMA public TO your_user;
pollochang@postgresql:~$ sudo -u postgres psql postgres -c "CREATE DATABASE netboxdb"
CREATE DATABASE
pollochang@postgresql:~$ sudo -u postgres psql postgres -c "CREATE USER netbox WITH PASSWORD 'J5brHrAXFLQSif0K'"
CREATE ROLE
pollochang@postgresql:~$ sudo -u postgres psql postgres -c "GRANT ALL PRIVILEGES ON DATABASE netboxdb TO netbox"
GRANT
CREATE TABLE newtable (
column1 int
);
select * from newtable;
insert into newtable (column1) values(1);
drop table newtable;
sudo apt install -y redis-server curl python3 python3-pip python3-venv python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev libpq-dev libssl-dev zlib1g-dev liberror-perl
sudo systemctl start redis-server
sudo systemctl enable redis-server
python3 -V
pollochang@manage-server:~$ python3 -V
Python 3.9.2
redis-server -v # 檢查版本
pollochang@manage-server:~$ redis-server -v
Redis server v=6.0.16 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=6d95e1af3a2c082a
這部份如果 Redis 服務與 NetBox 是安裝在不同主機,必特別檢查。
redis-cli ping
pollochang@manage-server:~$ redis-cli ping
PONG
sudo groupadd -g 600 netbox
sudo useradd -u 600 -g netbox -d /opt/netbox -s /bin/bash -c "netbox-service" netbox
這部份我是直接下載
cd /tmp
wget -O /tmp/netbox-3.3.7.tar.gz https://github.com/netbox-community/netbox/archive/refs/tags/v3.3.7.tar.gz
sudo tar -xzf /tmp/netbox-3.3.7.tar.gz -C /opt
sudo ln -s /opt/netbox-3.3.7 /opt/netbox
sudo chown -R netbox:netbox /opt/netbox-3.3.7
sudo su netbox -c "python3 /opt/netbox/netbox/generate_secret_key.py"
pollochang@manage-server:/tmp$ sudo su netbox -c "python3 /opt/netbox/netbox/generate_secret_key.py"
qFV&jHlvb1Oc0o4bsdj&DyuE^knwXGQjJ@nRFwoX#auK2jXIFv
※ 首次安裝需要從範例中複製
sudo su netbox -c "cp /opt/netbox/netbox/netbox/configuration_example.py /opt/netbox/netbox/netbox/configuration.py"
變更文件內容段落
sudo su netbox -c "vim /opt/netbox/netbox/netbox/configuration.py"
ALLOWED_HOSTS
這部份主要是設定網站 URL 的 domain 限制。如果規劃的登入網址是 https://netbox.example.com/
則設定 netbox.example.com
,單純網址依此類推。
ALLOWED_HOSTS = ['netbox.example.com', '192.0.2.123']
如果不想限制可以用星號處理
ALLOWED_HOSTS = ['*']
DATABASE
DATABASE = {
'NAME': 'netbox', # Database name
'USER': 'netbox', # PostgreSQL username
'PASSWORD': 'J5brHrAXFLQSif0K', # PostgreSQL password
'HOST': '192.168.56.x', # Database server
'PORT': '5432', # Database port (leave blank for default)
'CONN_MAX_AGE': 300, # Max database connection age (seconds)
}
REDIS
這部份如果是本機直接安裝可以直接複製即可。
REDIS = {
'tasks': {
'HOST': 'localhost', # Redis server
'PORT': 6379, # Redis port
'PASSWORD': '', # Redis password (optional)
'DATABASE': 0, # Database ID
'SSL': False, # Use SSL (optional)
},
'caching': {
'HOST': 'localhost',
'PORT': 6379,
'PASSWORD': '',
'DATABASE': 1, # Unique ID for second database
'SSL': False,
}
}
SECRET_KEY
SECRET_KEY= 'qFV&jHlvb1Oc0o4bsdj&DyuE^knwXGQjJ@nRFwoX#auK2jXIFv'
BASE_PATH
TIME_ZONE
時區設定
參數可以參考wiki 文獻。例如台灣時區可以這樣設定:
TIME_ZONE = 'Asia/Taipei'
sudo su netbox -c "/opt/netbox/upgrade.sh"
source /opt/netbox/venv/bin/activate
python3 /opt/netbox/netbox/manage.py createsuperuser
pollochang@manage-server:~$ source /opt/netbox/venv/bin/activate
(venv) pollochang@manage-server:~$ python3 /opt/netbox/netbox/manage.py createsuperuser
Username (leave blank to use 'pollochang'): root
Email address: test@mail.com
Password:
Password (again):
Superuser created successfully.
sudo ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping
source /opt/netbox/venv/bin/activate
python3 /opt/netbox/netbox/manage.py runserver 0.0.0.0:8000 --insecure
我這邊的作法僅供參考,我是拿來測試研究方便用的。
實際部屬還是參可官網站 Gunicorn
#!/bin/bash
source /opt/netbox/venv/bin/activate
cd /opt/netbox/netbox
python3 /opt/netbox/netbox/manage.py runserver 0.0.0.0:8000 --insecure
sudo chmod +x /opt/netbox/startnetbox.sh
[Unit]
Description=NetBox WSGI Service
Documentation=https://docs.netbox.dev/
After=network-online.target
Wants=network-online.target
[Service]
#Type=simple
Type=idle
User=netbox
Group=netbox
PIDFile=/var/tmp/netbox.pid
WorkingDirectory=/opt/netbox
ExecStart=/opt/netbox/startnetbox.sh
#ExecStart=/opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
ExecStop=/bin/kill -15 $MAINPID
Restart=on-failure
RestartSec=30
PrivateTmp=true
[Install]
WantedBy=multi-user.target