Skip to content

Instantly share code, notes, and snippets.

@md-riaz
Last active January 27, 2025 17:30
Show Gist options
  • Save md-riaz/d61d5b8396ef50e839a08de83b59f80a to your computer and use it in GitHub Desktop.
Save md-riaz/d61d5b8396ef50e839a08de83b59f80a to your computer and use it in GitHub Desktop.
#!/bin/bash
# Ensure script is run as root
local INST_USER=$(id -u)
if [ $INST_USER != 0 ]; then
echoR "Sorry, only the root user can install."
echo
exit 1
fi
# Detect installed web server
detect_webserver() {
if systemctl is-active --quiet nginx; then
echo "nginx"
elif systemctl is-active --quiet apache2; then
echo "apache"
elif systemctl is-active --quiet lsws; then
echo "openlitespeed"
else
echo "none"
fi
}
# Install Nginx
install_nginx() {
apt update
apt install -y nginx
systemctl start nginx
systemctl enable nginx
}
# Install PHP and dependencies
install_php() {
local webserver=$1
case "$webserver" in
"nginx" | "apache")
echo "Installing PHP-FPM for $webserver..."
apt install -y php-fpm php-cli php-mysql php-mbstring php-curl php-xml
;;
"openlitespeed")
echo "Detected OpenLiteSpeed - checking LSphp installation..."
# Check if lsphp is already installed
if ! command -v /usr/local/lsws/lsphp*/bin/php &>/dev/null; then
echo "Installing LSphp..."
apt install -y lsphp$(lsb_release -sc) lsphp$(lsb_release -sc)-common
lsphp$(lsb_release -sc)-mysqlnd lsphp$(lsb_release -sc)-curl
else
echo "LSphp already installed - skipping PHP installation"
fi
;;
esac
}
# Configure Nginx
setup_nginx() {
local php_version=$(php -v | grep -oP 'PHP K[0-9] .[0-9] ')
local config_path="/etc/nginx/sites-available/control_panel"
cat >"$config_path" <<EOF
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/control_panel;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php${php_version}-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
ln -sf "$config_path" /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
}
# Configure Apache
setup_apache() {
local config_path="/etc/apache2/sites-available/control_panel.conf"
cat >"$config_path" <<EOF
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/control_panel
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/control_panel>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
a2dissite 000-default
a2ensite control_panel
a2enmod rewrite
systemctl reload apache2
}
# Configure OpenLiteSpeed
setup_openlitespeed() {
# Find the actual PHP binary path
local php_bin=$(find /usr/local/lsws/lsphp*/bin/php -type f 2>/dev/null | sort -V | tail -n1)
if [[ -z "$php_bin" ]]; then
echo "ERROR: No LSphp installation found!"
exit 1
fi
# Extract numeric version (e.g., 8.3 instead of 8.3.1)
local php_version=$("$php_bin" -v | grep -oP '^PHP \K\d+\.\d+')
# Create version string without dots for directory names
local php_dir_version=$(echo "$php_version" | tr -d '.')
echo "Detected PHP binary: $php_bin"
echo "Using PHP version: $php_version (directory: lsphp$php_dir_version)"
# Create virtual host directory structure
local vh_root="/var/www/control_panel"
local vh_conf="/usr/local/lsws/conf/vhosts/control_panel/vhconf.conf"
# Configure listeners directly in httpd_config.conf
local MAIN_CFG="/usr/local/lsws/conf/httpd_config.conf"
echo "Configuring OpenLiteSpeed listeners..."
if ! grep -q "listener control_panel {" $MAIN_CFG; then
cat >>$MAIN_CFG <<EOF
listener control_panel {
address *:80
secure 0
map control_panel *
}
# listener control_panel_ssl {
# address *:443
# secure 1
# map control_panel *
# keyFile $SERVER_ROOT/conf/example.key
# certFile $SERVER_ROOT/conf/example.crt
# }
EOF
else
echo "Listener 'control_panel' already exists in $MAIN_CFG. Skipping..."
fi
# Create virtual host entry
echo "Creating virtual host configuration..."
mkdir -p /usr/local/lsws/conf/vhosts/control_panel/
local VHOST_CFG="/usr/local/lsws/conf/vhosts/control_panel/vhconf.conf"
if [[ ! -f "$VHOST_CFG" ]]; then
cat >$VHOST_CFG <<EOF
docRoot $vh_root/
index {
useServer 0
indexFiles index.php
}
context / {
type NULL
location $vh_root/
allowBrowse 1
indexFiles index.php
rewrite {
enable 1
autoLoadHtaccess 1
}
}
context ~ .php$ {
type lsapi
handler lsphp${php_dir_version}
phpIniOverride {
php_admin_value open_basedir $vh_root/
}
}
EOF
else
echo "Virtual host configuration already exists at $VHOST_CFG. Skipping..."
fi
# Add virtual host to main config
if ! grep -q "virtualhost control_panel {" $MAIN_CFG; then
cat >>$MAIN_CFG <<EOF
virtualhost control_panel {
vhRoot $vh_root
configFile $VHOST_CFG
allowSymbolLink 1
enableScript 1
restrained 0
setUIDMode 2
}
EOF
else
echo "Virtual host 'control_panel' already exists in $MAIN_CFG. Skipping..."
fi
# Set permissions
echo "Setting permissions..."
mkdir -p "$vh_root"
chown -R nobody:nogroup "$vh_root"
chmod -R 755 "$vh_root"
chown -R lsadm:lsadm /usr/local/lsws/conf/
# Restart service
echo "Restarting OpenLiteSpeed..."
systemctl restart lsws
}
# Create control panel files
create_control_panel() {
local webserver=$1 # Add webserver parameter
local web_root="/var/www/control_panel"
# Different directory structure for OpenLiteSpeed
if [ "$webserver" = "openlitespeed" ]; then
web_root="/var/www/control_panel"
else
web_root="/var/www/control_panel/html"
fi
mkdir -p "$web_root"
cat >"${web_root}/index.php" <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VPS Control Panel</title>
<style>
body { font-family: Arial, sans-serif; margin: 2rem; }
.container { max-width: 800px; margin: 0 auto; }
.status-box { padding: 1rem; background: #f5f5f5; border-radius: 5px; margin-bottom: 2rem; }
</style>
</head>
<body>
<div class="container">
<h1>VPS Control Panel</h1>
<div class="status-box">
<h2>System Information</h2>
<?php
echo "<p>Hostname: " . gethostname() . "</p>";
echo "<p>PHP Version: " . phpversion() . "</p>";
echo "<p>Server Software: " . $_SERVER['SERVER_SOFTWARE'] . "</p>";
?>
</div>
<h2>Server Management</h2>
<form method="post">
<button type="submit" name="reboot">Reboot Server</button>
<button type="submit" name="restart_web">Restart Web Service</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['reboot'])) {
shell_exec('sudo reboot');
echo "<p>System reboot initiated!</p>";
} elseif (isset($_POST['restart_web'])) {
$service = 'lsws'; // Default to OpenLiteSpeed
if (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) {
$service = 'apache2';
} elseif (strpos($_SERVER['SERVER_SOFTWARE'], 'nginx') !== false) {
$service = 'nginx';
}
shell_exec("sudo systemctl restart $service");
echo "<p>Web service restarted!</p>";
}
}
?>
</div>
</body>
</html>
EOF
# Modify permissions based on webserver
if [ "$webserver" = "openlitespeed" ]; then
chown -R nobody:nogroup "/var/www/control_panel"
else
chown -R www-data:www-data "/var/www/control_panel"
fi
chmod -R 755 "/var/www/control_panel"
}
# Main installation process
main() {
# Detect OS
if ! command -v apt &>/dev/null; then
echo "This script only supports Debian-based systems"
exit 1
fi
# Detect web server
local webserver=$(detect_webserver)
# Install web server if needed
if [ "$webserver" = "none" ]; then
install_nginx
webserver="nginx"
fi
# Install PHP only if needed for the detected web server
install_php "$webserver"
# Create control panel files with webserver type
create_control_panel "$webserver"
# Configure web server
case "$webserver" in
"nginx") setup_nginx ;;
"apache") setup_apache ;;
"openlitespeed") setup_openlitespeed ;;
esac
# Get server IP
local ip_address=$(hostname -I | awk '{print $1}')
echo ""
echo "================================================"
echo " Installation Complete!"
echo " Access your control panel at:"
echo " http://${ip_address}"
echo "================================================"
}
# Execute main function
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment