Created
January 24, 2025 14:41
-
-
Save md-riaz/1de940dfebbd1fb62a45601c97a407e8 to your computer and use it in GitHub Desktop.
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
<?php | |
require 'vendor/autoload.php'; // Include phpseclib | |
use phpseclib3\Net\SSH2; | |
class VPSSetup { | |
private $ssh; | |
private $domain; | |
private $email; | |
private $dbPassword; | |
private $webServer; | |
private $phpVersion; | |
private $nodeVersion; | |
private $pythonVersion; | |
public function __construct($host, $username, $password, $domain = null, $email = null, $dbPassword = null, $webServer = null, $phpVersion = null, $nodeVersion = null, $pythonVersion = null) { | |
$this->ssh = new SSH2($host); | |
if (!$this->ssh->login($username, $password)) { | |
throw new Exception('Login Failed'); | |
} | |
$this->domain = $domain; | |
$this->email = $email; | |
$this->dbPassword = $dbPassword; | |
$this->webServer = $webServer; | |
$this->phpVersion = $phpVersion; | |
$this->nodeVersion = $nodeVersion; | |
$this->pythonVersion = $pythonVersion; | |
} | |
private function executeCommand($command) { | |
return $this->ssh->exec($command); | |
} | |
public function updateSystem() { | |
echo "Updating system...\n"; | |
$this->executeCommand('sudo apt-get update -y && sudo apt-get upgrade -y'); | |
} | |
public function installWebServer() { | |
if (!$this->webServer) { | |
echo "No web server specified. Skipping web server installation.\n"; | |
return; | |
} | |
echo "Installing {$this->webServer}...\n"; | |
if ($this->webServer === 'nginx') { | |
$this->executeCommand('sudo apt-get install -y nginx'); | |
} elseif ($this->webServer === 'apache') { | |
$this->executeCommand('sudo apt-get install -y apache2'); | |
} elseif ($this->webServer === 'openlitespeed') { | |
$this->executeCommand('wget -O - https://repo.litespeed.sh | sudo bash'); | |
$this->executeCommand('sudo apt-get install -y openlitespeed'); | |
} | |
$this->executeCommand("sudo systemctl enable {$this->webServer}"); | |
$this->executeCommand("sudo systemctl start {$this->webServer}"); | |
} | |
public function installPHP() { | |
if (!$this->phpVersion) { | |
echo "No PHP version specified. Skipping PHP installation.\n"; | |
return; | |
} | |
echo "Installing PHP {$this->phpVersion}...\n"; | |
$this->executeCommand("sudo apt-get install -y php{$this->phpVersion} php{$this->phpVersion}-fpm php{$this->phpVersion}-mysql php{$this->phpVersion}-curl php{$this->phpVersion}-gd php{$this->phpVersion}-mbstring php{$this->phpVersion}-xml php{$this->phpVersion}-zip"); | |
$this->executeCommand("sudo systemctl enable php{$this->phpVersion}-fpm"); | |
$this->executeCommand("sudo systemctl start php{$this->phpVersion}-fpm"); | |
} | |
public function installNodeJS() { | |
if (!$this->nodeVersion) { | |
echo "No Node.js version specified. Skipping Node.js installation.\n"; | |
return; | |
} | |
echo "Installing Node.js {$this->nodeVersion}...\n"; | |
$this->executeCommand("curl -fsSL https://deb.nodesource.com/setup_{$this->nodeVersion}.x | sudo -E bash -"); | |
$this->executeCommand('sudo apt-get install -y nodejs'); | |
$this->executeCommand('sudo npm install -g yarn'); | |
} | |
public function installPython() { | |
if (!$this->pythonVersion) { | |
echo "No Python version specified. Skipping Python installation.\n"; | |
return; | |
} | |
echo "Installing Python {$this->pythonVersion}...\n"; | |
$this->executeCommand("sudo apt-get install -y python{$this->pythonVersion} python{$this->pythonVersion}-venv python{$this->pythonVersion}-dev"); | |
} | |
public function installDatabases() { | |
if (!$this->dbPassword) { | |
echo "No database password specified. Skipping database installation.\n"; | |
return; | |
} | |
echo "Installing MySQL, PostgreSQL, and Redis...\n"; | |
$this->executeCommand('sudo apt-get install -y mysql-server postgresql postgresql-contrib redis-server'); | |
$this->executeCommand("sudo mysql_secure_installation <<EOF | |
n | |
{$this->dbPassword} | |
{$this->dbPassword} | |
y | |
y | |
y | |
y | |
EOF"); | |
$this->executeCommand("sudo -u postgres psql -c \"ALTER USER postgres WITH PASSWORD '{$this->dbPassword}';\""); | |
$this->executeCommand('sudo systemctl enable redis-server'); | |
$this->executeCommand('sudo systemctl start redis-server'); | |
} | |
public function configureWebServer() { | |
if (!$this->domain || !$this->webServer) { | |
echo "No domain or web server specified. Skipping web server configuration.\n"; | |
return; | |
} | |
echo "Configuring {$this->webServer} for domain {$this->domain}...\n"; | |
$this->executeCommand("sudo mkdir -p /var/www/{$this->domain}/html"); | |
$this->executeCommand("sudo chown -R \$USER:\$USER /var/www/{$this->domain}/html"); | |
$this->executeCommand("sudo chmod -R 755 /var/www/{$this->domain}"); | |
if ($this->webServer === 'nginx') { | |
$this->executeCommand("sudo bash -c 'cat > /etc/nginx/sites-available/{$this->domain} <<EOF | |
server { | |
listen 80; | |
server_name {$this->domain} www.{$this->domain}; | |
root /var/www/{$this->domain}/html; | |
index index.html index.php; | |
location / { | |
try_files \$uri \$uri/ =404; | |
} | |
location ~ \.php$ { | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/var/run/php/php{$this->phpVersion}-fpm.sock; | |
} | |
location ~ /\.ht { | |
deny all; | |
} | |
} | |
EOF'"); | |
$this->executeCommand("sudo ln -s /etc/nginx/sites-available/{$this->domain} /etc/nginx/sites-enabled/"); | |
$this->executeCommand('sudo nginx -t'); | |
$this->executeCommand('sudo systemctl reload nginx'); | |
} elseif ($this->webServer === 'apache') { | |
$this->executeCommand("sudo bash -c 'cat > /etc/apache2/sites-available/{$this->domain}.conf <<EOF | |
<VirtualHost *:80> | |
ServerName {$this->domain} | |
ServerAlias www.{$this->domain} | |
DocumentRoot /var/www/{$this->domain}/html | |
ErrorLog \${APACHE_LOG_DIR}/error.log | |
CustomLog \${APACHE_LOG_DIR}/access.log combined | |
<Directory /var/www/{$this->domain}/html> | |
AllowOverride All | |
</Directory> | |
</VirtualHost> | |
EOF'"); | |
$this->executeCommand("sudo a2ensite {$this->domain}.conf"); | |
$this->executeCommand('sudo a2enmod rewrite'); | |
$this->executeCommand('sudo systemctl reload apache2'); | |
} elseif ($this->webServer === 'openlitespeed') { | |
$this->executeCommand("sudo bash -c 'cat > /usr/local/lsws/conf/vhosts/{$this->domain}/vhost.conf <<EOF | |
docRoot /var/www/{$this->domain}/html | |
indexFiles index.html, index.php | |
EOF'"); | |
$this->executeCommand('sudo systemctl restart lsws'); | |
} | |
} | |
public function setupSSL() { | |
if (!$this->domain || !$this->email || !$this->webServer) { | |
echo "No domain, email, or web server specified. Skipping SSL setup.\n"; | |
return; | |
} | |
echo "Setting up SSL for {$this->domain}...\n"; | |
$this->executeCommand("sudo apt-get install -y certbot python3-certbot-{$this->webServer}"); | |
$this->executeCommand("sudo certbot --{$this->webServer} --non-interactive --agree-tos --email {$this->email} --domains {$this->domain},www.{$this->domain} --redirect"); | |
} | |
public function deployFrameworks() { | |
if (!$this->domain) { | |
echo "No domain specified. Skipping framework deployment.\n"; | |
return; | |
} | |
echo "Deploying frameworks...\n"; | |
// WordPress | |
$this->executeCommand("cd /var/www/{$this->domain}/html && wget https://wordpress.org/latest.tar.gz && tar -xvzf latest.tar.gz --strip-components=1 && rm latest.tar.gz"); | |
$this->executeCommand("sudo chown -R www-data:www-data /var/www/{$this->domain}/html"); | |
// Laravel | |
$this->executeCommand("composer create-project --prefer-dist laravel/laravel /var/www/{$this->domain}/laravel"); | |
$this->executeCommand("sudo chown -R www-data:www-data /var/www/{$this->domain}/laravel"); | |
// Python App | |
if ($this->pythonVersion) { | |
$this->executeCommand("python{$this->pythonVersion} -m venv /var/www/{$this->domain}/pythonenv"); | |
$this->executeCommand("source /var/www/{$this->domain}/pythonenv/bin/activate && pip install flask gunicorn && deactivate"); | |
$this->executeCommand("sudo bash -c 'cat > /etc/supervisor/conf.d/python.conf <<EOF | |
[program:python] | |
directory=/var/www/{$this->domain} | |
command=/var/www/{$this->domain}/pythonenv/bin/gunicorn --workers 3 --bind unix:/var/www/{$this->domain}/python.sock app:app | |
autostart=true | |
autorestart=true | |
stderr_logfile=/var/log/python.err.log | |
stdout_logfile=/var/log/python.out.log | |
EOF'"); | |
$this->executeCommand('sudo supervisorctl reread && sudo supervisorctl update && sudo supervisorctl start python'); | |
} | |
} | |
public function configureFirewall() { | |
echo "Configuring firewall...\n"; | |
$this->executeCommand('sudo ufw allow OpenSSH'); | |
if ($this->webServer) { | |
$this->executeCommand("sudo ufw allow {$this->webServer}"); | |
} | |
$this->executeCommand('sudo ufw enable'); | |
} | |
public function setupCronJobs() { | |
if (!$this->domain || !$this->email) { | |
echo "No domain or email specified. Skipping cron job setup.\n"; | |
return; | |
} | |
echo "Setting up cron jobs...\n"; | |
$this->executeCommand('(crontab -l 2>/dev/null; echo "0 0 * * * certbot renew --quiet") | crontab -'); | |
} | |
public function run() { | |
$this->updateSystem(); | |
$this->installWebServer(); | |
$this->installPHP(); | |
$this->installNodeJS(); | |
$this->installPython(); | |
$this->installDatabases(); | |
$this->configureWebServer(); | |
$this->setupSSL(); | |
$this->deployFrameworks(); | |
$this->configureFirewall(); | |
$this->setupCronJobs(); | |
echo "Setup complete! Visit https://{$this->domain} to verify.\n"; | |
} | |
} | |
// Example usage | |
$vps = new VPSSetup( | |
'your.vps.ip', // VPS IP | |
'username', // SSH username | |
'password', // SSH password | |
'example.com', // Domain (optional) | |
'[email protected]', // Email (optional) | |
'securepassword', // Database password (optional) | |
'openlitespeed', // Web server (optional: nginx, apache, openlitespeed) | |
'8.2', // PHP version (optional) | |
'18', // Node.js version (optional) | |
'3.10' // Python version (optional) | |
); | |
$vps->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment