Created
May 25, 2025 12:51
-
-
Save sphinxid/6a9315677b5f34260c786da8bb925fee to your computer and use it in GitHub Desktop.
script to install neo4j
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Exit immediately if a command exits with a non-zero status. | |
set -euo pipefail | |
# --- Configuration --- | |
NEO4J_JAVA_VERSION="17" # Recommended Java version (11 or 17) | |
NEO4J_VERSION_SERIES="5" # Neo4j 5.x stable series | |
# --- Functions --- | |
log_info() { | |
echo -e "\n\033[1;34m[INFO]\033[0m $1" | |
} | |
log_success() { | |
echo -e "\n\033[1;32m[SUCCESS]\033[0m $1" | |
} | |
log_error() { | |
echo -e "\n\033[1;31m[ERROR]\033[0m $1" >&2 | |
exit 1 | |
} | |
check_sudo() { | |
if [[ "$EUID" -ne 0 ]]; then | |
log_error "This script requires root privileges. Please run with 'sudo bash $0'." | |
fi | |
} | |
install_java_debian() { | |
log_info "Installing OpenJDK $NEO4J_JAVA_VERSION..." | |
sudo apt update || log_error "Failed to update apt packages." | |
sudo apt install -y "openjdk-${NEO4J_JAVA_VERSION}-jdk" || log_error "Failed to install OpenJDK $NEO4J_JAVA_VERSION." | |
log_success "OpenJDK $NEO4J_JAVA_VERSION installed." | |
} | |
install_java_rhel() { | |
log_info "Installing OpenJDK $NEO4J_JAVA_VERSION..." | |
if command -v dnf &> /dev/null; then | |
sudo dnf install -y "java-${NEO4J_JAVA_VERSION}-openjdk-devel" || log_error "Failed to install OpenJDK $NEO4J_JAVA_VERSION via dnf." | |
elif command -v yum &> /dev/null; then | |
sudo yum install -y "java-${NEO4J_JAVA_VERSION}-openjdk-devel" || log_error "Failed to install OpenJDK $NEO4J_JAVA_VERSION via yum." | |
else | |
log_error "Neither dnf nor yum found. Cannot install Java." | |
fi | |
log_success "OpenJDK $NEO4J_JAVA_VERSION installed." | |
} | |
install_neo4j_debian() { | |
log_info "Configuring Neo4j repository for Debian/Ubuntu..." | |
sudo apt update || log_error "Failed to update apt." | |
sudo apt install -y curl gnupg || log_error "Failed to install curl or gnupg." | |
curl -fsSL https://debian.neo4j.com/neotechnology.gpg.key | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/neo4j.gpg || log_error "Failed to import Neo4j GPG key." | |
echo "deb [signed-by=/etc/apt/trusted.gpg.d/neo4j.gpg] https://debian.neo4j.com stable ${NEO4J_VERSION_SERIES}" | sudo tee /etc/apt/sources.list.d/neo4j.list > /dev/null || log_error "Failed to add Neo4j repository." | |
sudo apt update || log_error "Failed to update apt after adding Neo4j repo." | |
log_success "Neo4j repository added." | |
log_info "Installing Neo4j Community Edition..." | |
sudo apt install -y neo4j || log_error "Failed to install Neo4j." | |
log_success "Neo4j installed successfully." | |
} | |
install_neo4j_rhel() { | |
log_info "Configuring Neo4j repository for RedHat/CentOS/Fedora..." | |
if command -v dnf &> /dev/null; then | |
PKG_MGR="dnf" | |
elif command -v yum &> /dev/null; then | |
PKG_MGR="yum" | |
else | |
log_error "Neither dnf nor yum found. Cannot install Neo4j." | |
fi | |
sudo tee "/etc/yum.repos.d/neo4j.repo" > /dev/null <<EOF | |
[neo4j] | |
name=Neo4j Yum Repository | |
baseurl=https://yum.neo4j.com/stable/${NEO4J_VERSION_SERIES} | |
enabled=1 | |
gpgcheck=1 | |
gpgkey=https://yum.neo4j.com/neotechnology.gpg.key | |
EOF | |
log_success "Neo4j repository added." | |
log_info "Installing Neo4j Community Edition..." | |
sudo "$PKG_MGR" install -y neo4j || log_error "Failed to install Neo4j." | |
log_success "Neo4j installed successfully." | |
} | |
configure_firewall() { | |
log_info "Attempting to configure firewall..." | |
if command -v ufw &> /dev/null; then | |
if sudo ufw status | grep -q "Status: active"; then | |
log_info "UFW detected and active. Opening ports 7474 (HTTP) and 7687 (Bolt)." | |
sudo ufw allow 7474/tcp comment 'Neo4j HTTP' || log_error "Failed to open port 7474 with UFW." | |
sudo ufw allow 7687/tcp comment 'Neo4j Bolt' || log_error "Failed to open port 7687 with UFW." | |
sudo ufw reload || log_error "Failed to reload UFW." | |
log_success "UFW rules updated." | |
else | |
log_info "UFW detected but not active. Skipping firewall configuration." | |
fi | |
elif command -v firewall-cmd &> /dev/null; then | |
if sudo systemctl is-active firewalld &> /dev/null; then | |
log_info "FirewallD detected and active. Opening ports 7474 (HTTP) and 7687 (Bolt)." | |
sudo firewall-cmd --add-port=7474/tcp --permanent --zone=public || log_error "Failed to open port 7474 with FirewallD." | |
sudo firewall-cmd --add-port=7687/tcp --permanent --zone=public || log_error "Failed to open port 7687 with FirewallD." | |
sudo firewall-cmd --reload || log_error "Failed to reload FirewallD." | |
log_success "FirewallD rules updated." | |
else | |
log_info "FirewallD detected but not active. Skipping firewall configuration." | |
fi | |
else | |
log_info "No common firewall (UFW/FirewallD) detected. Please configure your firewall manually if needed." | |
fi | |
} | |
configure_neo4j_listen_address() { | |
log_info "Configuring Neo4j to listen on 0.0.0.0..." | |
local NEO4J_CONF="/etc/neo4j/neo4j.conf" | |
if [ ! -f "$NEO4J_CONF" ]; then | |
log_error "Neo4j configuration file not found at $NEO4J_CONF." | |
fi | |
# Check if the listen address line exists, uncomment/replace it. | |
# If it doesn't exist, append it. | |
echo "server.default_listen_address=0.0.0.0" | sudo tee -a "$NEO4J_CONF" > /dev/null || log_error "Failed to add listen address to $NEO4J_CONF." | |
log_success "Neo4j listen address set to 0.0.0.0." | |
} | |
# --- Main Script Logic --- | |
check_sudo | |
# Determine OS type | |
OS_TYPE="" | |
if [ -f /etc/os-release ]; then | |
. /etc/os-release | |
case "$ID" in | |
debian|ubuntu|pop|linuxmint) | |
OS_TYPE="debian" | |
;; | |
rhel|centos|fedora|rocky|almalinux) | |
OS_TYPE="rhel" | |
;; | |
*) | |
log_error "Unsupported OS: $ID. This script supports Debian/Ubuntu and RHEL/CentOS/Fedora families." | |
;; | |
esac | |
else | |
log_error "Cannot determine OS type. /etc/os-release not found." | |
fi | |
log_info "Detected OS: $OS_TYPE" | |
# Install Java | |
if [[ "$OS_TYPE" == "debian" ]]; then | |
install_java_debian | |
elif [[ "$OS_TYPE" == "rhel" ]]; then | |
install_java_rhel | |
fi | |
# Install Neo4j | |
if [[ "$OS_TYPE" == "debian" ]]; then | |
install_neo4j_debian | |
elif [[ "$OS_TYPE" == "rhel" ]]; then | |
install_neo4j_rhel | |
fi | |
# Configure listen address BEFORE starting the service for the first time | |
configure_neo4j_listen_address | |
# Start and enable Neo4j service | |
log_info "Starting and enabling Neo4j service..." | |
sudo systemctl daemon-reload || log_error "Failed to reload systemd daemon." | |
sudo systemctl start neo4j || log_error "Failed to start Neo4j service." | |
sudo systemctl enable neo4j || log_error "Failed to enable Neo4j service." | |
sleep 5 # Give Neo4j a moment to start | |
if sudo systemctl is-active --quiet neo4j; then | |
log_success "Neo4j service is running and enabled for autostart." | |
else | |
log_error "Neo4j service failed to start. Check 'sudo systemctl status neo4j' for details." | |
fi | |
sudo neo4j-admin dbms set-initial-password "$NEO4J_ROOT_PASSWORD" || log_error "Failed to set initial password." | |
log_success "Initial Neo4j password set." | |
# Configure firewall | |
configure_firewall | |
# --- Final Verification --- | |
log_info "Verifying Neo4j browser access..." | |
sleep 5 | |
if curl -s http://localhost:7474 | grep -q "neo4j_version"; then | |
log_success "Neo4j Browser is accessible locally at http://localhost:7474." | |
else | |
log_error "Neo4j Browser not accessible locally at http://localhost:7474. Check firewall, service status, or configuration." | |
fi | |
log_info "Neo4j installation complete!" | |
log_info "You can now access Neo4j Browser at: http://<Your_Server_IP_or_Hostname>:7474" | |
log_info "Login with username 'neo4j' and the password from the '${NEO4J_ROOT_PASSWORD}' environment variable." | |
log_info "For more information, refer to the official Neo4j documentation." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment