NETBOX - Install Script On Ubuntu 24.4: Difference between revisions

From IT-Arts.net
Created page with "Category:Wiki == install-netbox.sh == <nowiki> #!/bin/bash # Script to install and configure NetBox on Ubuntu 24.04 # Define variables NETBOX_VERSION="v3.4.5" # Specify the desired NetBox version here NETBOX_DIR="/opt/netbox" NETBOX_USER="netbox" DB_NAME="netbox" DB_USER="netbox" DB_PASS="your-db-password" SECRET_KEY="your-secret-key" ALLOWED_HOSTS="127.0.0.1,localhost" # Update this to your domain or IP addresses # Update and upgrade system packages echo "Up..."
 
m Text replacement - "Category:Wiki" to "Category:Wiki '''''[https://it-arts.net/index.php/Category:Wiki Return to Wiki Index]''''' "
 
Line 1: Line 1:
[[Category:Wiki]]
[[Category:Wiki]]
'''''[https://it-arts.net/index.php/Category:Wiki Return to Wiki Index]'''''


== install-netbox.sh ==
== install-netbox.sh ==

Latest revision as of 07:04, 17 January 2026


Return to Wiki Index


install-netbox.sh

#!/bin/bash

# Script to install and configure NetBox on Ubuntu 24.04

# Define variables
NETBOX_VERSION="v3.4.5"  # Specify the desired NetBox version here
NETBOX_DIR="/opt/netbox"
NETBOX_USER="netbox"
DB_NAME="netbox"
DB_USER="netbox"
DB_PASS="your-db-password"
SECRET_KEY="your-secret-key"
ALLOWED_HOSTS="127.0.0.1,localhost"  # Update this to your domain or IP addresses

# Update and upgrade system packages
echo "Updating and upgrading system packages..."
sudo apt update -y && sudo apt upgrade -y

# Install dependencies
echo "Installing required packages..."
sudo apt install -y \
    python3-pip \
    python3-dev \
    python3-venv \
    git \
    libpq-dev \
    postgresql \
    postgresql-contrib \
    redis-server \
    nginx \
    supervisor \
    virtualenv \
    curl \
    gcc \
    libxml2-dev \
    libxslt1-dev \
    zlib1g-dev \
    libjpeg-dev \
    libffi-dev \
    libssl-dev

# Add NetBox system user
echo "Adding NetBox user..."
sudo useradd -r -m -d $NETBOX_DIR -s /bin/bash $NETBOX_USER

# Switch to NetBox user
echo "Switching to the NetBox user..."
sudo su - $NETBOX_USER

# Clone NetBox repository
echo "Cloning NetBox repository..."
git clone https://github.com/netbox-community/netbox.git $NETBOX_DIR
cd $NETBOX_DIR
git checkout $NETBOX_VERSION

# Create virtual environment and activate it
echo "Creating and activating virtual environment..."
python3 -m venv venv
source venv/bin/activate

# Install Python dependencies
echo "Installing Python dependencies..."
pip install -r requirements.txt

# Set up the database
echo "Setting up PostgreSQL database..."
sudo -u postgres psql <<EOF
CREATE DATABASE $DB_NAME;
CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';
ALTER ROLE $DB_USER SET client_encoding TO 'utf8';
ALTER ROLE $DB_USER SET default_transaction_isolation TO 'read committed';
ALTER ROLE $DB_USER SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
EOF

# Configure NetBox settings
echo "Configuring NetBox settings..."
cp netbox/netbox/configuration.example.py netbox/netbox/configuration.py
sed -i "s/SECRET_KEY = 'your-secret-key'/SECRET_KEY = '$SECRET_KEY'/g" netbox/netbox/configuration.py
sed -i "s/ALLOWED_HOSTS = \['localhost'\]/ALLOWED_HOSTS = ['$ALLOWED_HOSTS']/g" netbox/netbox/configuration.py
sed -i "s/DATABASE = {'NAME': 'netbox', 'USER': 'netbox', 'PASSWORD': '', 'HOST': 'localhost', 'PORT': '5432'}/DATABASE = {'NAME': '$DB_NAME', 'USER': '$DB_USER', 'PASSWORD': '$DB_PASS', 'HOST': 'localhost', 'PORT': '5432'}/g" netbox/netbox/configuration.py

# Run database migrations
echo "Running database migrations..."
./manage.py migrate

# Create a superuser (optional)
echo "Creating NetBox superuser..."
./manage.py createsuperuser

# Collect static files
echo "Collecting static files..."
./manage.py collectstatic --no-input

# Configure Gunicorn (as a systemd service)
echo "Configuring Gunicorn..."
cat <<EOF | sudo tee /etc/systemd/system/netbox.service
[Unit]
Description=NetBox
After=network.target

[Service]
User=$NETBOX_USER
Group=www-data
WorkingDirectory=$NETBOX_DIR
ExecStart=$NETBOX_DIR/venv/bin/gunicorn --workers 3 --bind unix:$NETBOX_DIR/netbox.sock netbox.wsgi:application
Environment=PATH=$NETBOX_DIR/venv/bin
Environment=PYTHONUNBUFFERED=1

[Install]
WantedBy=multi-user.target
EOF

# Enable and start Gunicorn service
echo "Starting Gunicorn service..."
sudo systemctl enable netbox
sudo systemctl start netbox

# Configure Nginx
echo "Configuring Nginx..."
cat <<EOF | sudo tee /etc/nginx/sites-available/netbox
server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://unix:$NETBOX_DIR/netbox.sock;
        proxy_set_header Host \$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

# Enable Nginx site and restart Nginx
sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

# Enable and start Redis
echo "Starting Redis server..."
sudo systemctl enable redis-server
sudo systemctl start redis-server

# Enable and start PostgreSQL
echo "Starting PostgreSQL service..."
sudo systemctl enable postgresql
sudo systemctl start postgresql

# Enable and start Supervisor (to keep Gunicorn running)
echo "Starting Supervisor..."
sudo systemctl enable supervisor
sudo systemctl start supervisor

# Finish
echo "NetBox installation completed. You can access it at http://localhost"
echo "Don't forget to configure your domain in Nginx and adjust firewall settings if needed."

# Exit from the NetBox user
exit