如何使用Apache和Supervisord在CentOS 7上安装NetBox

时间:2020-02-23 14:30:48  来源:igfitidea点击:

欢迎来到我们的指南,该指南介绍了如何使用Apache和Supervisord在CentOS 7上安装Netbox。 NetBox是旨在帮助管理和记录计算机网络的开源Web应用程序。最初是由DigitalOcean的网络工程团队构想的。

Netbox包含以下网络管理方面:IP地址管理(IPAM)IP网络和地址,VRF和VLAN设备机架按组和站点组织设备设备类型及其安装位置连接网络设备之间的网络,控制台和电源连接虚拟化虚拟机和集群数据电路远程通信电路和提供商秘密敏感凭证的加密存储

如何在CentOS 7上安装Netbox

请按照以下步骤在CentOS 7服务器上安装Netbox。

添加EPEL存储库

我们需要一个epel资料库来安装一些依赖包。使用以下命令将其添加到CentOS 7服务器:

sudo yum -y install epel-release

禁用SELinux

由于我们将使用自定义TCP端口,因此除非我们知道如何使用该工具,否则建议将SELinux禁用或者置于许可模式。

sudo setenforce 0
sudo sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

安装依赖包

sudo yum install -y gcc httpd git libxml2-devel libxslt libffi-devel graphviz libxslt-devel supervisor

安装Python 3.6

sudo yum -y install yum-utils
sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
sudo yum -y install python36u python36u-devel
sudo yum -y install python36u-pip

安装和配置PostgreSQL数据库服务器

安装PostgreSQL 9.6

sudo rpm -ivh https://yum.postgresql.org/9.6/redhat/rhel-7.3-x86_64/pgdg-centos96-9.6-3.noarch.rpm
sudo yum install postgresql96 postgresql96-server postgresql96-libs postgresql96-contrib postgresql96-devel

初始化数据库

# /usr/pgsql-9.6/bin/postgresql96-setup initdb
Initializing database ... OK

启动并启用PostgreSQL服务以在启动时启动

sudo systemctl enable postgresql-9.6
sudo systemctl start postgresql-9.6
sudo systemctl status postgresql-9.6

为NetBox创建数据库和用户

$sudo -u postgres psql

CREATE DATABASE netbox;
CREATE USER netbox WITH PASSWORD 'StrongPassword';
GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
\q

启用密码登录PostgreSQL数据库

sudo sed -i -e 's/ident/md5/' /var/lib/pgsql/9.6/data/pg_hba.conf

重新启动数据库服务以使更改生效

sudo systemctl restart postgresql-9.6

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

$psql -U netbox -h localhost -W
Password for user netbox:
psql (9.2.23)
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 = ['10.1.1.20']

# 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 SECRET密钥:

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

然后在configuration.py文件上设置密钥

SECRET_KEY = '30m&theitroad@localhost=^l8wqtjw2$!3j%=f2!zh_sey+13jg%3$'

安装Nextbox依赖项

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

迁移数据库数据

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

创建管理员用户

$sudo python3.6 manage.py createsuperuser

Username (leave blank to use 'root'): admin
Email address: theitroad@localhost
Password:
Password (again):
Superuser created successfully.

移动静态文件

cd /opt/netbox/netbox
sudo python3.6 manage.py collectstatic

加载初始数据

$sudo python3.6 manage.py loaddata initial_data
Installed 53 object(s) from 5 fixture(s)python3.6 manage.py loaddata initial_data

使用pip3.6安装gunicorn

$sudo pip3.6 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

sudo vim /opt/netbox/gunicorn_config.py

加:

command = '/usr/bin/gunicorn'
pythonpath = '/opt/netbox/netbox'
bind = '127.0.0.1:8001'
workers = 3
user = 'apache'

创建主管配置

$sudo vim /etc/supervisord.d/netbox.ini

[program:netbox]
command = gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
directory = /opt/netbox/netbox/
user = apache

启动并启用supervisord

sudo systemctl enable supervisord
sudo systemctl start supervisord

配置httpd或者Nginx

对于Apache,在/etc/httpd/conf.d/netbox.conf上创建VirtualHost。

Listen 8085
    ProxyPreserveHost On
    ServerName netbox.example.com
    Alias /static /opt/netbox/netbox/static
       <Directory /opt/netbox/netbox/static>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Require all granted
       </Directory>
       <Location  /static>
            ProxyPass !
      </Location>

    ProxyPass/http://127.0.0.1:8001/
    ProxyPassReverse/http://127.0.0.1:8001/

检查apache配置语法并重新启动httpd服务

$sudo httpd -t
Syntax OK
$sudo systemctl restart httpd

确认服务正在监听

# ss -tunelp | grep 8085 
tcp    LISTEN     0      128      :::8085                 :::*                   users:(("httpd",pid=2471,fd=6),("httpd",pid=2470,fd=6),("httpd",pid=2468,fd=6),("httpd",pid=2466,fd=6),("httpd",pid=2465,fd=6),("httpd",pid=2464,fd=6),("httpd",pid=2463,fd=6)) ino:54671265 sk:ffff9890baf47700 v6only:0 <->

对于Nginx

安装nginx软件包

sudo yum -y install nginx

创建一个VirtualHost文件> /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://127.0.0.1:8001;
    }
}

检查语法并启动nginx

sudo nginx -t
sudo systemctl start nginx
sudo systemctl enable nginx

配置防火墙

如果我们有正在运行的firewalld服务,请启用netbox端口

sudo firewall-cmd --permanent --add-port=8085/tcp
sudo firewall-cmd --reload

访问Netbox Web UI

打开默认的Web浏览器,然后打开在Apache上配置的Netbox服务器IP和端口。