157 TP5报错Fatal error: require(): Failed opening required '/home/www/xx/public/../thinkphp/start.php

博客提供了两个网页链接,分别是百度经验和博客园的文章,提示遇到TP5中require()函数打开文件失败的问题,可搜索百度经验获取相关解决办法。
[root@yfw ~]# cd /www/wwwroot/yfw.szrengjing.com [root@yfw yfw.szrengjing.com]# ./deploy.sh 🔧 开始自动化部署... fatal: not a git repository (or any of the parent directories): .git fatal: not a git repository (or any of the parent directories): .git No composer.json in current directory, do you want to use the one at /www/wwwroot? [Y,n]? y Always want to use the parent dir? Use "composer config --global use-parent-dir true" to change the default. Installing dependencies from lock file Verifying lock file contents can be installed on current platform. Nothing to install, update or remove Generating optimized autoload files 1 package you are using is looking for funding. Use the `composer fund` command to find out more! Could not open input file: think Could not open input file: think chown: changing ownership of 'public/.user.ini': Operation not permitted chmod: changing permissions of 'public/.user.ini': Operation not permitted PHP Warning: require(/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php): failed to open stream: No such file or directory in Standard input code on line 2 Warning: require(/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php): failed to open stream: No such file or directory in Standard input code on line 2 PHP Fatal error: require(): Failed opening required '/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php' (include_path='.:') in Standard input code on line 2 Fatal error: require(): Failed opening required '/www/wwwroot/yfw.szrengjing.com/vendor/autoload.php' (include_path='.:') in Standard input code on line 2 Failed to reload php7.4-fpm.service: Unit php7.4-fpm.service not found. 🎉 部署完成 [root@yfw yfw.szrengjing.com]# lsattr /www/wwwroot/yfw.szrengjing.com/public/.user.ini ----i---------e----- /www/wwwroot/yfw.szrengjing.com/public/.user.ini [root@yfw yfw.szrengjing.com]# chattr -i /www/wwwroot/yfw.szrengjing.com/public/.user.ini [root@yfw yfw.szrengjing.com]# cd /www/wwwroot [root@yfw wwwroot]# [root@yfw wwwroot]# # 备份旧站点 [root@yfw wwwroot]# mv yfw.szrengjing.com yfw.szrengjing.com.bak [root@yfw wwwroot]# [root@yfw wwwroot]# # 使用 Composer 创建新 TP6 项目 [root@yfw wwwroot]# composer create-project topthink/think yfw.szrengjing.com Creating a "topthink/think" project at "./yfw.szrengjing.com" Cannot use topthink/think's latest version v8.1.3 as it requires php >=8.0.0 which is not satisfied by your platform. Installing topthink/think (v6.1.0) - Downloading topthink/think (v6.1.0) - Installing topthink/think (v6.1.0): Extracting archive Created project in /www/wwwroot/yfw.szrengjing.com Loading composer repositories with package information Updating dependencies Lock file operations: 17 installs, 0 updates, 0 removals - Locking league/flysystem (1.1.10) - Locking league/flysystem-cached-adapter (1.1.0) - Locking league/mime-type-detection (1.16.0) - Locking psr/cache (1.0.1) - Locking psr/container (1.1.2) - Locking psr/http-message (1.1) - Locking psr/log (1.1.4) - Locking psr/simple-cache (1.0.1) - Locking symfony/polyfill-mbstring (v1.33.0) - Locking symfony/polyfill-php72 (v1.31.0) - Locking symfony/polyfill-php80 (v1.33.0) - Locking symfony/var-dumper (v4.4.47) - Locking topthink/framework (v6.1.5) - Locking topthink/think-filesystem (v1.0.3) - Locking topthink/think-helper (v3.1.11) - Locking topthink/think-orm (v2.0.62) - Locking topthink/think-trace (v1.6) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 17 installs, 0 updates, 0 removals - Downloading psr/cache (1.0.1) - Downloading psr/container (1.1.2) - Downloading psr/http-message (1.1) - Downloading symfony/var-dumper (v4.4.47) - Downloading topthink/think-helper (v3.1.11) - Downloading psr/simple-cache (1.0.1) - Downloading psr/log (1.1.4) - Downloading topthink/think-orm (v2.0.62) - Downloading topthink/framework (v6.1.5) - Downloading league/flysystem (1.1.10) - Downloading league/flysystem-cached-adapter (1.1.0) - Downloading topthink/think-filesystem (v1.0.3) - Downloading topthink/think-trace (v1.6) - Installing league/mime-type-detection (1.16.0): Extracting archive - Installing psr/cache (1.0.1): Extracting archive - Installing psr/container (1.1.2): Extracting archive - Installing psr/http-message (1.1): Extracting archive - Installing symfony/polyfill-php80 (v1.33.0): Extracting archive - Installing symfony/polyfill-php72 (v1.31.0) - Installing symfony/polyfill-mbstring (v1.33.0): Extracting archive - Installing symfony/var-dumper (v4.4.47): Extracting archive - Installing topthink/think-helper (v3.1.11): Extracting archive - Installing psr/simple-cache (1.0.1): Extracting archive - Installing psr/log (1.1.4): Extracting archive - Installing topthink/think-orm (v2.0.62): Extracting archive - Installing topthink/framework (v6.1.5): Extracting archive - Installing league/flysystem (1.1.10): Extracting archive - Installing league/flysystem-cached-adapter (1.1.0): Extracting archive - Installing topthink/think-filesystem (v1.0.3): Extracting archive - Installing topthink/think-trace (v1.6): Extracting archive 12 package suggestions were added by new dependencies, use `composer suggest` to see details. Generating autoload files > @php think service:discover Succeed! > @php think vendor:publish Succeed! 6 packages you are using are looking for funding. Use the `composer fund` command to find out more! Found 1 security vulnerability advisory affecting 1 package. Run "composer audit" for a full list of advisories. [root@yfw wwwroot]# ls -la /www/wwwroot/yfw.szrengjing.com total 108 drwxr-xr-x 10 root root 4096 Nov 14 17:05 . drwxr-xr-x 13 www www 4096 Nov 14 17:05 .. drwxr-xr-x 3 root root 4096 Oct 24 2022 app -rw-r--r-- 1 root root 1138 Oct 24 2022 composer.json -rw-r--r-- 1 root root 37667 Nov 14 17:05 composer.lock drwxr-xr-x 2 root root 4096 Nov 14 17:05 config -rw-r--r-- 1 root root 231 Oct 24 2022 .example.env drwxr-xr-x 2 root root 4096 Oct 24 2022 extend -rw-r--r-- 1 root root 34 Oct 24 2022 .gitignore -rw-r--r-- 1 root root 1822 Oct 24 2022 LICENSE.txt drwxr-xr-x 3 root root 4096 Oct 24 2022 public -rw-r--r-- 1 root root 1459 Oct 24 2022 README.md drwxr-xr-x 2 root root 4096 Oct 24 2022 route drwxr-xr-x 2 root root 4096 Oct 24 2022 runtime -rw-r--r-- 1 root root 180 Oct 24 2022 think -rw-r--r-- 1 root root 2038 Oct 24 2022 .travis.yml drwxr-xr-x 8 root root 4096 Nov 14 17:05 vendor drwxr-xr-x 2 root root 4096 Oct 24 2022 view [root@yfw wwwroot]#
最新发布
11-15
你已经完成了非常关键的一步:成功使用 `composer create-project topthink/think yfw.szrengjing.com` 创建了一个 **完整的 ThinkPHP 6 项目结构**! 但现在仍然存在几个 **权限问题、所有权问题 和 部署衔接问题**。下面我们一步步来解决,确保你的网站能正常运行,并让 `deploy.sh` 脚本可以正确执行。 --- ## ✅ 一、当前状态分析 ### 🟢 成功点: - 已通过 Composer 正确安装 ThinkPHP 6(v6.1.0) - 生成了 `vendor/`, `app/`, `config/`, `public/`, `think` 等核心文件 - 自动加载已生成,`composer install` 成功 - 命令行工具 `php think` 可用 ### 🔴 存在问题: | 问题 | 描述 | |------|------| | ❌ 所有者是 `root:root` | Web 服务器(www 用户)无法读写 `runtime` 和 `public` | | ❌ 缺少 `.env` 文件 | 数据库等配置未生效 | | ❌ `deploy.sh` 还在旧目录中 | 新项目里没有部署脚本 | | ❌ PHP-FPM 服务名不匹配 | `systemctl reload php7.4-fpm.service` 找不到服务 | --- ## ✅ 二、修复步骤:设置权限 + 拷贝脚本 + 初始化环境 ### 🔧 步骤 1:修改所有者为 `www:www` ```bash chown -R www:www /www/wwwroot/yfw.szrengjing.com ``` > ⚠️ 必须递归设置,否则 `runtime` 缓存、日志写入失败。 ### 🔧 步骤 2:解锁并重新锁定 `.user.ini`(宝塔专用) ```bash # 解锁(允许修改) chattr -i /www/wwwroot/yfw.szrengjing.com/public/.user.ini # 修改权限和所有者 chown www:www /www/wwwroot/yfw.szrengjing.com/public/.user.ini chmod 644 /www/wwwroot/yfw.szrengjing.com/public/.user.ini # 完成后重新锁定(安全防护) chattr +i /www/wwwroot/yfw.szrengjing.com/public/.user.ini ``` --- ### 🔧 步骤 3:创建 `.env` 配置文件 ```bash cat > /www/wwwroot/yfw.szrengjing.com/.env << 'EOF' APP_DEBUG = true # 数据库配置 DB_TYPE = mysql DB_HOST = 127.0.0.1 DB_PORT = 3306 DB_NAME = yfw_szrengjing_c DB_USER = yfw_szrengjing_c DB_PWD = 7FF8m3KArh8xGcCn DB_PREFIX = tp_ # 缓存与会话 CACHE_TYPE = redis REDIS_HOST = 127.0.0.1 REDIS_PORT = 6379 SESSION_DRIVER = redis EOF # 设置权限 chown www:www /www/wwwroot/yfw.szrengjing.com/.env chmod 644 /www/wwwroot/yfw.szrengjing.com/.env ``` --- ### 🔧 步骤 4:恢复 `deploy.sh` 到新项目中 ```bash cp /www/wwwroot/yfw.szrengjing.com.bak/deploy.sh /www/wwwroot/yfw.szrengjing.com/deploy.sh chown www:www /www/wwwroot/yfw.szrengjing.com/deploy.sh chmod +x /www/wwwroot/yfw.szrengjing.com/deploy.sh ``` --- ### 🔧 步骤 5:测试 `php think` 是否可用 ```bash cd /www/wwwroot/yfw.szrengjing.com php think version ``` ✅ 正常输出应为: ``` ThinkPHP v6.1.5 ``` 如果没有报错,说明框架完全就绪。 --- ### 🔧 步骤 6:清空缓存(首次部署必须) ```bash php think clear ``` 这会清除: - `runtime/cache/` - `runtime/temp/` - `runtime/log/` --- ### 🔧 步骤 7:验证伪静态 & 入口文件 访问:`http://yfw.szrengjing.com` 你应该看到 ThinkPHP 的欢迎页: > “Welcome To ThinkPHP 6.0” 如果显示的是“404”或空白页,请检查 Nginx 配置是否指向 `public/` 目录。 #### ✅ 推荐 Nginx 配置片段: ```nginx server { listen 80; server_name yfw.szrengjing.com; root /www/wwwroot/yfw.szrengjing.com/public; index index.php index.html; location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; } } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(ht|user|git) { deny all; } } ``` 然后重启 Nginx: ```bash systemctl reload nginx ``` --- ### 🔧 步骤 8:修正 `deploy.sh` 中的服务名(避免报错) 原脚本报错: ``` Failed to reload php7.4-fpm.service: Unit php7.4-fpm.service not found. ``` 说明系统中没有这个服务名。查看真实名称: ```bash systemctl list-units | grep fpm ``` 常见输出可能是: ``` php-fpm-74.service loaded active running PHP FastCGI Process Manager (7.4) ``` 所以你要修改 `deploy.sh` 中的最后一行: ```bash # 替换这一行: # systemctl reload php7.4-fpm # 改成实际存在的服务名(根据上面查询结果): systemctl reload php-fpm-74 ``` 或者更通用的方式: ```bash pkill -f "php-fpm" && service php-fpm start >/dev/null 2>&1 || systemctl start php-fpm-74 ``` --- ## ✅ 三、最终验证:运行 `deploy.sh` ```bash cd /www/wwwroot/yfw.szrengjing.com ./deploy.sh ``` 预期输出: ``` 🔧 开始自动化部署... Already up to date. Nothing to install, update or remove Generating optimized autoload files Could not open input file: think Could not open input file: think ✅ 管理员账号 admin 创建成功 🎉 部署完成 ``` > ⚠️ 注意:前两个 `Could not open input file: think` 是因为你脚本里写了 `php think clear` 和 `php think storage:link`,但我们现在还没加这些逻辑。 --- ## ✅ 四、增强版 `deploy.sh`(推荐替换) ```bash #!/bin/bash PROJECT_ROOT="/www/wwwroot/yfw.szrengjing.com" ADMIN_USER="admin" ADMIN_PASS="ZXcv506448@" MYSQL_USER="yfw_szrengjing_c" MYSQL_PASS="7FF8m3KArh8xGcCn" MYSQL_DB="yfw_szrengjing_c" cd "$PROJECT_ROOT" || exit 1 echo "🔧 开始自动化部署..." # 1. 拉取代码(跳过 git 错误不影响) git pull origin main || git pull origin master || echo "ℹ️ Git 仓库不存在,跳过拉取" # 2. 安装依赖 composer install --optimize-autoloader --no-dev # 3. 清理缓存 if [ -f "think" ]; then php think clear php think storage:link 2>/dev/null || echo "💡 storage link 已存在" else echo "❌ think 文件不存在!请检查 ThinkPHP 是否安装完整" fi # 4. 设置 runtime 和 public 可写 chown -R www:www runtime public find public -type d -exec chmod 755 {} \; find public -type f -exec chmod 644 {} \; # 5. 解锁 .user.ini(仅临时) chattr -i public/.user.ini 2>/dev/null || true # 6. 重新设置 .user.ini 权限 chown www:www public/.user.ini chmod 644 public/.user.ini # 7. 重新锁定 .user.ini chattr +i public/.user.ini 2>/dev/null || true # 8. 初始化管理员账户 php <<'EOF' <?php require __DIR__ . '/vendor/autoload.php'; use think\facade\Db; // 检查是否存在 user 表 \$tables = Db::query("SHOW TABLES LIKE 'tp_user'"); if (empty(\$tables)) { echo "❌ tp_user 表不存在,请先导入数据库。\n"; exit; } \$exists = Db::name('user')->where('username', 'admin')->value('id'); if (!\$exists) { \$data = [ 'username' => 'admin', 'password' => password_hash('ZXcv506448@', PASSWORD_DEFAULT), 'nickname' => '超级管理员', 'role' => 'admin', 'status' => 1, 'create_time' => date('Y-m-d H:i:s'), ]; Db::name('user')->insert(\$data); echo "✅ 管理员账号 admin 创建成功\n"; } else { echo "ℹ️ 管理员账号已存在\n"; } EOF # 9. 重载 PHP-FPM(适配不同命名) systemctl reload php-fpm-74 || systemctl reload php74-php-fpm || pkill -f php-fpm && service php-fpm start echo "🎉 部署完成!访问后台: http://yfw.szrengjing.com" ``` --- ## ✅ 五、下一步建议 | 功能 | 建议操作 | |------|----------| | 导入数据库 | 准备 `init.sql` 并用 `mysql -u... < init.sql` 导入 | | 多商户客服 | 在 `app/controller/Admin/ChatController.php` 中实现 | | WebSocket | 使用 Swoole 或 Workerman 实现实时通信 | | CI/CD | 结合 GitHub/Gitee Webhook 实现自动部署 | --- ###
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值