企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# ThinkPHP5实现安装程序 **install.php** - `public/install.php` > 首先在`thinkphp5`框架下的入口文件`index.php`,加入如下判断 ``` // 1.判断当前 php 版本 if (version_compare(PHP_VERSION,'5.6','<')) die('require PHP > 5.6'); // 判断是否安装 后面的地址随便你,你愿意扔那里就扔哪里 if (!is_file(APP_PATH . 'admin/command/Install/install.lock')) { // 这个地址也是你的install.php 文件在那里就指向那哪里 header("location:./install.php"); exit; } ``` > 其次在`install.php`进行详细代码 - 定义站点名称 ```php $sitename = "Your sitename" ``` - 定义报错信息 ``` $errInfo = ''; ``` - 定义目录分隔符 ``` define('DS', DIRECTORY_SEPARATOR) ``` - 定义根目录 ``` define('ROOT_PATH', __DIR__ . DS . '..' . DS); ``` - 定义应用目录 ``` define('APP_PATH', ROOT_PATH . 'application' . DS); ``` - 定义安装包目录(你的在哪里就定义在哪里) ``` define('INSTALL_PATH', APP_PATH . 'admin' . DS . 'command' . DS . 'Install' . DS); ``` - 判断文件是否有写的权限 ``` function is_really_writable($file) { if (DIRECTORY_SEPARATOR == '/' AND @ ini_get("safe_mode") == false) { return is_writable($file); } if (!is_file($file) OR ($fp = @fopen($file, "r+")) === false) { return false; } fclose($fp); return true; } ``` - 检测关键目录是否存在 ``` $checkDirs = [ 'thinkphp', 'vendor', 'public' . DS . 'assets' . DS . 'libs' ]; ``` - 缓存目录 ``` $runtimeDir = APP_PATH . 'runtime'; ``` - 数据库配置文件 - thinkphp 5.0 ``` $dbConfigFile = APP_PATH . 'database.php'; ``` - thinkphp 5.1 config目录下的 `database.php`配置文件 ``` $dbConfigFile = 'config' . 'database.php'; ``` - 锁定的文件 ``` // 锁定的文件 $lockFile = INSTALL_PATH . 'install.lock'; if (is_file($lockFile)) { $errInfo = "当前已经安装{$sitename},如果需要重新安装,请手动移除application/admin/command/Install/install.lock文件"; } else { if (version_compare(PHP_VERSION, '5.5.0', '<')) { $errInfo = "当前版本(" . PHP_VERSION . ")过低,请使用PHP5.5以上版本"; } else { if (!extension_loaded("PDO")) { $errInfo = "当前未开启PDO,无法进行安装"; } else { if (!is_really_writable($dbConfigFile)) { $open_basedir = ini_get('open_basedir'); if ($open_basedir) { $dirArr = explode(PATH_SEPARATOR, $open_basedir); if ($dirArr && in_array(__DIR__, $dirArr)) { $errInfo = '当前服务器因配置了open_basedir,导致无法读取父目录<br><a href="https://forum.fastadmin.net/thread/1145?ref=install" target="_blank">点击查看解决办法</a>'; } } if (!$errInfo) { $errInfo = '当前权限不足,无法写入配置文件application/database.php<br><a href="https://forum.fastadmin.net/thread/1145?ref=install" target="_blank">点击查看解决办法</a>'; } } else { $dirArr = []; foreach ($checkDirs as $k => $v) { if (!is_dir(ROOT_PATH . $v)) { $errInfo = '当前代码仅包含核心代码,请composer install'; break; } } } } } } ``` - 当前`post`请求 ``` if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') { if ($errInfo) { echo $errInfo; exit; } $err = ''; $mysqlHostname = isset($_POST['mysqlHost']) ? $_POST['mysqlHost'] : '127.0.0.1'; $mysqlHostport = isset($_POST['mysqlHostport']) ? $_POST['mysqlHostport'] : 3306; $hostArr = explode(':', $mysqlHostname); if (count($hostArr) > 1) { $mysqlHostname = $hostArr[0]; $mysqlHostport = $hostArr[1]; } $mysqlUsername = isset($_POST['mysqlUsername']) ? $_POST['mysqlUsername'] : 'root'; $mysqlPassword = isset($_POST['mysqlPassword']) ? $_POST['mysqlPassword'] : ''; $mysqlDatabase = isset($_POST['mysqlDatabase']) ? $_POST['mysqlDatabase'] : 'fastadmin'; $mysqlPrefix = isset($_POST['mysqlPrefix']) ? $_POST['mysqlPrefix'] : 'fa_'; $adminUsername = isset($_POST['adminUsername']) ? $_POST['adminUsername'] : 'admin'; $adminPassword = isset($_POST['adminPassword']) ? $_POST['adminPassword'] : '123456'; $adminPasswordConfirmation = isset($_POST['adminPasswordConfirmation']) ? $_POST['adminPasswordConfirmation'] : '123456'; $adminEmail = isset($_POST['adminEmail']) ? $_POST['adminEmail'] : 'admin@admin.com'; if (!preg_match("/^\w{3,12}$/", $adminUsername)) { echo "用户名只能由3-12位数字、字母、下划线组合"; exit; } if (!preg_match("/^[\S]{6,16}$/", $adminPassword)) { echo "密码长度必须在6-16位之间,不能包含空格"; exit; } if ($adminPassword !== $adminPasswordConfirmation) { echo "两次输入的密码不一致"; exit; } try { //检测能否读取安装文件 $sql = @file_get_contents(INSTALL_PATH . 'fastadmin.sql'); if (!$sql) { throw new Exception("无法读取application/admin/command/Install/fastadmin.sql文件,请检查是否有读权限"); } $sql = str_replace("`fa_", "`{$mysqlPrefix}", $sql); $pdo = new PDO("mysql:host={$mysqlHostname};port={$mysqlHostport}", $mysqlUsername, $mysqlPassword, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" )); //检测是否支持innodb存储引擎 $pdoStatement = $pdo->query("SHOW VARIABLES LIKE 'innodb_version'"); $result = $pdoStatement->fetch(); if (!$result) { throw new Exception("当前数据库不支持innodb存储引擎,请开启后再重新尝试安装"); } $pdo->query("CREATE DATABASE IF NOT EXISTS `{$mysqlDatabase}` CHARACTER SET utf8 COLLATE utf8_general_ci;"); $pdo->query("USE `{$mysqlDatabase}`"); $pdo->exec($sql); $config = @file_get_contents($dbConfigFile); $callback = function ($matches) use ($mysqlHostname, $mysqlHostport, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $mysqlPrefix) { $field = ucfirst($matches[1]); $replace = ${"mysql{$field}"}; if ($matches[1] == 'hostport' && $mysqlHostport == 3306) { $replace = ''; } return "'{$matches[1]}'{$matches[2]}=>{$matches[3]}Env::get('database.{$matches[1]}', '{$replace}'),"; }; $config = preg_replace_callback("/'(hostname|database|username|password|hostport|prefix)'(\s+)=>(\s+)Env::get\((.*)\)\,/", $callback, $config); //检测能否成功写入数据库配置 $result = @file_put_contents($dbConfigFile, $config); if (!$result) { throw new Exception("无法写入数据库信息到application/database.php文件,请检查是否有写权限"); } //检测能否成功写入lock文件 $result = @file_put_contents($lockFile, 1); if (!$result) { throw new Exception("无法写入安装锁定到application/admin/command/Install/install.lock文件,请检查是否有写权限"); } $newSalt = substr(md5(uniqid(true)), 0, 6); $newPassword = md5(md5($adminPassword) . $newSalt); $pdo->query("UPDATE {$mysqlPrefix}admin SET username = '{$adminUsername}', email = '{$adminEmail}',password = '{$newPassword}', salt = '{$newSalt}' WHERE username = 'admin'"); $adminName = ''; if (is_file($adminFile)) { $x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $adminName = substr(str_shuffle(str_repeat($x, ceil(10 / strlen($x)))), 1, 10) . '.php'; rename($adminFile, ROOT_PATH . 'public' . DS . $adminName); } echo "success|{$adminName}"; } catch (PDOException $e) { $err = $e->getMessage(); } catch (Exception $e) { $err = $e->getMessage(); } echo $err; exit; } ``` > 下面是一些显示的安装页面,页面内容,自己根据`html`和`css`能力部署页面