本文向你介绍如何采用Apache和Supervisord在CentOS 7系统上安装Netbox的方法,按照以下步骤操作即可。
一、安装Netbox前的准备 添加EPEL存储库: 我们需要一个epel存储库来安装一些依赖包,使用以下命令将其添加到CentOS 7服务器: sudo yum install epel-release 禁用SELinux,由于我们将使用自定义TCP端口,因此建议禁用或将SELinux置于许可模式,除非你知道如何使用该工具: 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=> 参考:在CentOS 7系统上安装PostgreSQL 11的步骤。
三、安装并配置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 = '30mhqd09h2i5hrof2zh_sey' 安装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: admin@example.com 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' 创建supervisor配置: $ 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配置syntax并重新启动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; } } 检查syntax并启动nginx: sudo nginx -t sudo systemctl start nginx sudo systemctl enable nginx 配置firewalld: 如果有正在运行的firewalld服务,请启用netbox端口: sudo firewall-cmd --permanent --add-port=8085/tcp sudo firewall-cmd --reload
六、访问Netbox Web UI 做完以上步骤,再打开系统中的浏览器并打开Apache上配置的Netbox服务器IP和端口就可以访问Netbox Web UI了:
至此,在CentOS 7上安装配置NetBox全部完成了。
相关主题 |