#!/bin/bash # Check if running as root if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" exit 1 fi # Update and upgrade the system echo "Updating and upgrading the system..." apt update && apt upgrade -y # Install dependencies echo "Installing dependencies..." apt install -y python3 python3-pip python3-dev build-essential libxml2 libxml2-dev libxslt1-dev libffi-dev graphviz git libpq-dev libssl-dev redis-server postgresql postgresql-contrib nginx # Check Python version echo "Checking Python version. Please ensure this meets the minimum requirements of Python 3.8, 3.9, 3.10 or 3.11.:" python3 --version # Prompt user to confirm if the currently installed Python version meets the minimum requirements read -p "Does the installed Python version meet the minimum requirements? (y/n): " PYTHON_CONFIRM if [[ $PYTHON_CONFIRM != "y" ]]; then echo "Please install the required version of Python and rerun this script." exit 1 fi # Add Netbox repository echo "Adding Netbox repository..." echo "deb https://packagecloud.io/netbox-community/netbox/ubuntu/ $(lsb_release -sc) main" | tee -a /etc/apt/sources.list.d/netbox-community.list curl -L https://packagecloud.io/netbox-community/netbox/gpgkey | apt-key add - # Prompt user for PostgreSQL and Netbox password read -p "Enter PostgreSQL password for 'netbox' user: " PG_PASSWORD read -p "Enter password for Netbox application: " NETBOX_PASSWORD # Create PostgreSQL user and database echo "Creating PostgreSQL user and database..." sudo -u postgres psql -c "CREATE DATABASE netbox;" sudo -u postgres psql -c "CREATE USER netbox WITH PASSWORD '$PG_PASSWORD';" sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;" # Create new user for Netbox echo "Creating new user for Netbox..." useradd -m -s /bin/bash netbox # Install Netbox echo "Installing Netbox..." apt update apt install -y netbox # Move into NetBox configuration directory and make a copy of configuration_example.py echo "Copying Netbox configuration file..." cd /opt/netbox/netbox/netbox/ cp configuration.example.py configuration.py # Populate database fields in configuration.py with PostgreSQL information echo "Populating the Netbox configuration file with prior PostgreSQL information..." sed -i "s/'USER': '',/'USER': 'netbox',/g" configuration.py sed -i "s/'NAME': '',/'NAME': 'netbox',/g" configuration.py sed -i "s/'PASSWORD': '',/'PASSWORD': '$PG_PASSWORD',/g" configuration.py sed -i "s/'HOST': '',/'HOST': 'localhost',/g" configuration.py # Prompt user to enter ALLOWED_HOSTS, REDIS, and SECRET_KEY echo "Please enter manually input the following inforation..." read -p "Enter ALLOWED_HOSTS (separated by commas): " ALLOWED_HOSTS read -p "Enter SECRET_KEY: " SECRET_KEY # Parse user input into configuration.py echo "Parsing the entered information to configuration.py..." sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \[$ALLOWED_HOSTS\]/g" configuration.py sed -i "s/SECRET_KEY = ''/SECRET_KEY = '$SECRET_KEY'/g" configuration.py # Perform initial database migration echo "Performing initial database migration..." sudo -u netbox /opt/netbox/upgrade.sh # Create directory for Netbox reports echo "Creating directory for Netbox reports..." mkdir -p /opt/netbox/netbox/media/reports # Check if directories exist echo "Checking if directories exist..." if [ ! -d "/opt/netbox/netbox/media/reports" ] || [ ! -d "/opt/netbox/netbox/media/" ] || [ ! -d "/opt/netbox/netbox/static/" ]; then read -p "Required directories are missing. Do you want to create them? (y/n): " CREATE_DIRS if [[ $CREATE_DIRS == "y" ]]; then mkdir -p /opt/netbox/netbox/media/reports mkdir -p /opt/netbox/netbox/media/ mkdir -p /opt/netbox/netbox/static/ else echo "Please create the required directories manually and rerun the script." exit 1 fi fi # Collect static files echo "Collecting static files..." sudo -u netbox /opt/netbox/manage.py collectstatic --no-input # Set file and directory permissions echo "Setting file permissions..." chown -R netbox:netbox /opt/netbox/netbox/media chown -R netbox:netbox /opt/netbox/netbox/static chown -R netbox:netbox /opt/netbox/netbox/media/reports # Enable and start services echo "Enabling and starting services..." systemctl enable netbox systemctl enable redis-server systemctl enable postgresql systemctl start netbox systemctl start redis-server systemctl start postgresql # NGINX reverse proxy config echo "Example NGINX reverse proxy configuration for Netbox:" echo " server { listen 80; server_name netbox.example.com; location / { proxy_pass http://localhost:8001; proxy_set_header X-Forwarded-Host \$server_name; 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; } location /static/ { alias /opt/netbox/netbox/static/; } location /media/ { alias /opt/netbox/netbox/media/; } } " echo "Netbox installation completed successfully!"