#!/usr/bin/php
<?php
function isPackageInstalled($package) {
$status = shell_exec("dpkg -s $package 2>&1");
return strpos($status, 'install ok installed') !== false;
}
function installPackage($package) {
shell_exec("sudo apt-get update");
shell_exec("sudo apt-get install -y $package");
}
function isCommandAvailable($command) {
$status = shell_exec("which $command 2>&1");
return !empty($status);
}
function ensure_php_setting($setting, $value, $phpini) {
$setting_exists = shell_exec("grep -q '^$setting' $phpini");
if ($setting_exists) {
echo "$setting is already enabled in $phpini.\n";
} else {
shell_exec("echo '$setting = $value' >> $phpini");
echo "$setting has been added with value: $value in $phpini\n";
}
}
$PHPINI_FPM = "/etc/php/8.2/fpm/php.ini";
$PHPINI_APACHE = "/etc/php/8.2/apache2/php.ini";
$PHPINI_CLI = "/etc/php/8.2/cli/php.ini";
$requiredPackages = ['apache2', 'php'];
foreach ($requiredPackages as $package) {
if (!isPackageInstalled($package)) {
installPackage($package);
}
}
if (!isCommandAvailable('a2ensite')) {
die("The 'a2ensite' command is not available.\n");
}
$INSTALLED_PHP_VERSION = shell_exec("php -r 'echo PHP_VERSION;'");
$TARGET_PHP_VERSION = "8.2";
if (version_compare($INSTALLED_PHP_VERSION, $TARGET_PHP_VERSION, '>')) {
echo "You currently have PHP version $INSTALLED_PHP_VERSION installed, which is greater than $TARGET_PHP_VERSION.\n";
echo "Do you want to downgrade to PHP 8.2? (y/n): ";
$choice = trim(fgets(STDIN));
if ($choice === 'y') {
shell_exec("sudo apt purge 'php${INSTALLED_PHP_VERSION%%.*}*' -y");
shell_exec("sudo apt update");
shell_exec("sudo apt install -y software-properties-common");
shell_exec("sudo add-apt-repository ppa:ondrej/php -y");
shell_exec("sudo apt update");
shell_exec("sudo apt install php8.2 php8.2-cli php8.2-fpm php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl php8.2-zip -y");
}
}
echo "Enter the path for open_basedir (e.g., /var/www/html/): ";
$open_basedir_path = trim(fgets(STDIN));
ensure_php_setting("extension=json", "", $PHPINI_FPM);
ensure_php_setting("extension=json", "", $PHPINI_APACHE);
ensure_php_setting("extension=json", "", $PHPINI_CLI);
ensure_php_setting("extension=openssl", "", $PHPINI_FPM);
ensure_php_setting("extension=openssl", "", $PHPINI_APACHE);
ensure_php_setting("extension=openssl", "", $PHPINI_CLI);
ensure_php_setting("extension=mbstring", "", $PHPINI_FPM);
ensure_php_setting("extension=mbstring", "", $PHPINI_APACHE);
ensure_php_setting("extension=mbstring", "", $PHPINI_CLI);
ensure_php_setting("extension=curl", "", $PHPINI_FPM);
ensure_php_setting("extension=curl", "", $PHPINI_APACHE);
ensure_php_setting("extension=curl", "", $PHPINI_CLI);
ensure_php_setting("extension=xml", "", $PHPINI_FPM);
ensure_php_setting("extension=xml", "", $PHPINI_APACHE);
ensure_php_setting("extension=xml", "", $PHPINI_CLI);
ensure_php_setting("extension=fileinfo", "", $PHPINI_FPM);
ensure_php_setting("extension=fileinfo", "", $PHPINI_APACHE);
ensure_php_setting("extension=fileinfo", "", $PHPINI_CLI);
ensure_php_setting("session.save_path", "/var/lib/php/sessions", $PHPINI_FPM);
ensure_php_setting("file_uploads", "On", $PHPINI_FPM);
ensure_php_setting("upload_max_filesize", "20M", $PHPINI_FPM);
ensure_php_setting("max_execution_time", "300", $PHPINI_FPM);
ensure_php_setting("memory_limit", "256M", $PHPINI_FPM);
ensure_php_setting("display_errors", "On", $PHPINI_FPM);
ensure_php_setting("error_reporting", "E_ALL", $PHPINI_FPM);
ensure_php_setting("session.save_path", "/var/lib/php/sessions", $PHPINI_APACHE);
ensure_php_setting("file_uploads", "On", $PHPINI_APACHE);
ensure_php_setting("upload_max_filesize", "20M", $PHPINI_APACHE);
ensure_php_setting("max_execution_time", "300", $PHPINI_APACHE);
ensure_php_setting("memory_limit", "256M", $PHPINI_APACHE);
ensure_php_setting("display_errors", "On", $PHPINI_APACHE);
ensure_php_setting("error_reporting", "E_ALL", $PHPINI_APACHE);
ensure_php_setting("open_basedir", $open_basedir_path, $PHPINI_FPM);
ensure_php_setting("open_basedir", $open_basedir_path, $PHPINI_APACHE);
ensure_php_setting("open_basedir", $open_basedir_path, $PHPINI_CLI);
shell_exec("sudo chown -R www-data:www-data .");
shell_exec("sudo chmod -R 755 .");
shell_exec("sudo systemctl start php8.2-fpm");
shell_exec("sudo a2enmod php8.2");
shell_exec("sudo systemctl restart apache2");
echo "PHP configuration completed.\n";
echo "Enter the folder name: ";
$folderName = trim(fgets(STDIN));
echo "Enter the path for the web files: ";
$documentRootBase = trim(fgets(STDIN));
echo "Enter the port number for the virtual host: ";
$port = trim(fgets(STDIN));
if (!$folderName || !$documentRootBase || !$port) {
die("All inputs are required. Exiting...\n");
}
$documentRoot = rtrim($documentRootBase, '/') . '/' . $folderName;
if (!is_dir($documentRoot)) {
mkdir($documentRoot, 0755, true);
mkdir("$documentRoot/public_html", 0755, true);
file_put_contents("$documentRoot/public_html/index.php", "<?php echo 'Hello, $folderName!'; ?>");
shell_exec("sudo chown -R www-data:www-data $documentRoot");
shell_exec("sudo chmod -R 775 $documentRoot");
shell_exec("sudo chmod g+s $documentRoot");
}
$vhostConfig = "
<VirtualHost *:$port>
ServerAdmin webmaster@$folderName
DocumentRoot $documentRoot/public_html
ErrorLog \${APACHE_LOG_DIR}/$folderName-error.log
CustomLog \${APACHE_LOG_DIR}/$folderName-access.log combined
<Directory $documentRoot/public_html>
Options +ExecCGI Indexes FollowSymLinks
AddHandler php-script .php
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
";
$configFile = "/etc/apache2/sites-available/$folderName.conf";
file_put_contents($configFile, $vhostConfig);
$portsConf = '/etc/apache2/ports.conf';
$portsConfContents = file_get_contents($portsConf);
if (strpos($portsConfContents, "Listen $port") === false) {
file_put_contents($portsConf, "\nListen $port\n", FILE_APPEND);
}
shell_exec("sudo a2ensite $folderName.conf");
shell_exec("sudo systemctl reload apache2");
echo "Virtual host for $folderName created, port $port added to ports.conf, and Apache reloaded.\n";
?>