Files
Cpanel/cpanel_install.sh
2026-02-04 19:50:49 -08:00

302 lines
9.4 KiB
Bash

#!/bin/bash
################################################################################
# cPanel & WHM Installation Script with Auto-Distro Detection
# Auto-detects: CentOS, RHEL, AlmaLinux, Rocky Linux, CloudLinux
# Auto-selects: Package Manager (DNF or YUM)
# Requirements: Fresh OS installation, Root access, Valid cPanel license/trial
################################################################################
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging function
log() {
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
################################################################################
# Pre-flight checks
################################################################################
log "Starting cPanel & WHM installation script..."
# Check if running as root
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run as root"
exit 1
fi
log "Checking system requirements..."
# Detect OS and set package manager
detect_os_and_package_manager() {
if [ ! -f /etc/os-release ]; then
log_error "Cannot determine OS type"
exit 1
fi
. /etc/os-release
OS_ID=$ID
OS_VERSION=$VERSION_ID
OS_PRETTY=$PRETTY_NAME
log "Detected OS: $OS_PRETTY"
# Determine package manager and validate supported distro
case "$OS_ID" in
centos|rhel|rocky|almalinux)
if command -v dnf &> /dev/null; then
PKG_MANAGER="dnf"
log "DNF package manager detected (will use dnf)"
else
PKG_MANAGER="yum"
log "YUM package manager detected (will use yum)"
fi
;;
cloudlinux)
PKG_MANAGER="yum"
log "CloudLinux detected (will use yum)"
;;
debian|ubuntu)
log_error "Unsupported OS: Debian/Ubuntu. cPanel requires RHEL-based distributions"
log_error "Supported distributions: CentOS 7+, AlmaLinux, Rocky Linux, CloudLinux"
exit 1
;;
*)
log_error "Unknown or unsupported OS: $OS_ID"
log_error "cPanel requires one of: CentOS, RHEL, AlmaLinux, Rocky Linux, or CloudLinux"
exit 1
;;
esac
# Check version compatibility
MAJOR_VERSION=$(echo "$OS_VERSION" | cut -d. -f1)
if [ "$MAJOR_VERSION" -lt 7 ]; then
log_error "Unsupported version: $OS_PRETTY"
log_error "Minimum version required: CentOS/RHEL/AlmaLinux 7.x or CloudLinux 7.x"
exit 1
fi
log_success "OS detection complete: $OS_ID (v$MAJOR_VERSION) - Using $PKG_MANAGER"
}
# Run OS detection
detect_os_and_package_manager
# Check RAM (minimum 1GB, 2GB+ recommended)
RAM_MB=$(free -m | awk 'NR==2{print $2}')
log "System RAM: ${RAM_MB}MB"
if [ "$RAM_MB" -lt 1024 ]; then
log_warning "RAM is less than 1GB. cPanel may not install properly. Minimum 2GB recommended."
fi
# Check disk space (minimum 20GB recommended)
DISK_GB=$(df / | awk 'NR==2{printf "%.0f", $4/1024/1024}')
log "Available disk space: ${DISK_GB}GB"
if [ "$DISK_GB" -lt 20 ]; then
log_warning "Less than 20GB available disk space. At least 40GB+ is recommended."
fi
################################################################################
# Disable firewall and network manager
################################################################################
log "Disabling firewall and Network Manager..."
# Disable and stop firewalld (if running)
if systemctl is-active --quiet firewalld; then
log "Stopping firewalld..."
systemctl stop firewalld
systemctl disable firewalld
log_success "firewalld disabled"
fi
# Disable and stop NetworkManager (if running)
if systemctl is-active --quiet NetworkManager; then
log "Stopping NetworkManager..."
systemctl stop NetworkManager
systemctl disable NetworkManager
log_success "NetworkManager disabled"
fi
################################################################################
# Disable SELinux
################################################################################
log "Disabling SELinux..."
if [ -f /etc/selinux/config ]; then
# Check current SELinux status
SELINUX_STATUS=$(getenforce 2>/dev/null || echo "Disabled")
if [ "$SELINUX_STATUS" != "Disabled" ]; then
# Disable SELinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
# Set to permissive mode immediately without reboot
setenforce 0 2>/dev/null || true
log_success "SELinux disabled (reboot required for full effect)"
else
log_success "SELinux already disabled"
fi
fi
################################################################################
# Update system packages
################################################################################
log "Updating system packages..."
$PKG_MANAGER update -y
log_success "System packages updated"
################################################################################
# Install required dependencies
################################################################################
log "Installing required dependencies..."
# Install Perl (required for cPanel)
if ! command -v perl &> /dev/null; then
log "Installing Perl..."
$PKG_MANAGER install -y perl
log_success "Perl installed"
else
log_success "Perl already installed"
fi
# Install curl (required for downloading installer)
if ! command -v curl &> /dev/null; then
log "Installing curl..."
$PKG_MANAGER install -y curl
log_success "curl installed"
else
log_success "curl already installed"
fi
# Install wget
if ! command -v wget &> /dev/null; then
log "Installing wget..."
$PKG_MANAGER install -y wget
log_success "wget installed"
else
log_success "wget already installed"
fi
# Install screen (recommended for long-running installations)
if ! command -v screen &> /dev/null; then
log "Installing screen..."
$PKG_MANAGER install -y screen
log_success "screen installed"
fi
################################################################################
# Download and install cPanel
################################################################################
log "Downloading latest cPanel installer..."
# Change to /home directory
cd /home || exit 1
# Download the latest cPanel installer
if curl -o latest -L https://securedownloads.cpanel.net/latest; then
log_success "cPanel installer downloaded successfully"
else
log_error "Failed to download cPanel installer"
exit 1
fi
# Verify the download
if [ ! -f /home/latest ] || [ ! -s /home/latest ]; then
log_error "Downloaded file is empty or missing"
exit 1
fi
log_success "Starting cPanel installation (this may take 10-70 minutes)..."
log "Installation logs can be monitored in: /var/log/cpanel/install.log"
# Run the installer
if sh latest; then
log_success "cPanel installation completed successfully"
else
log_error "cPanel installation failed"
log "Check installation logs for details: /var/log/cpanel/install.log"
exit 1
fi
################################################################################
# Update license
################################################################################
log "Updating cPanel license information..."
if [ -x /usr/local/cpanel/cpkeyclt ]; then
/usr/local/cpanel/cpkeyclt
log_success "License check completed"
else
log_warning "Could not run license update utility"
fi
################################################################################
# Post-installation configuration
################################################################################
log "Performing post-installation configuration..."
# Ensure cPanel services are running
if command -v systemctl &> /dev/null; then
systemctl start cpanel
systemctl enable cpanel
log_success "cPanel service configured"
fi
################################################################################
# Final information
################################################################################
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}cPanel Installation Completed!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${BLUE}Next Steps:${NC}"
echo "1. Note that a reboot may be required for SELinux changes to take effect"
echo "2. Access WHM using: https://<server-ip>:2087"
echo "3. Log in with root username and root password"
echo "4. Complete the initial setup wizard"
echo "5. Enter your cPanel license or start free 15-day trial"
echo ""
echo -e "${BLUE}Useful Commands:${NC}"
echo "Check cPanel version: /usr/local/cpanel/cpanel -V"
echo "Check WHM access: https://\$(hostname -I | awk '{print $1}'):2087"
echo "View installation log: tail -f /var/log/cpanel/install.log"
echo ""
echo -e "${YELLOW}Important Notes:${NC}"
echo "• This script disables firewall, NetworkManager, and SELinux"
echo "• A reboot may be required after installation"
echo "• Ensure you have a valid cPanel license or use the 15-day trial"
echo "• For production servers, enable firewall rules after installation"
echo ""
log_success "Installation script completed at $(date)"