如何为生产或子域系统创建 laravel 存储符号 link?

How to create laravel storage symbolic link for production or sub domain system?

当我在 laravel 本地开发服务器上工作时 php artisan storage:link 对我来说工作正常。但是当我将我的网站转移到生产服务器时,我看到我的 public 存储 link 是一个文件夹。然后我删除了试图创建 link 的那个。我得到一个错误,因为我的应用程序在根文件夹中并试图解决这个问题。

我通过 terminal/cmd/shh 创建符号 link 的另一个命令解决了这个问题:

ln -s /path/to/laravel/storage/app/public /path/to/public/storage

我也用 laravel 网络路由 routes/web.php

解决了这个问题
Route::get('/foo', function () {
    Artisan::call('storage:link');
});

另一个简单的方法是运行以编程方式执行php artisan storage:link命令

在 routes/web.php

Route::get('/foo', function () {
Artisan::call('storage:link');
});

假设你的laravel项目"public"目录和"public_html"目录不同。或者您的子域指针目录可能不同,然后使用您的子域指针目录,如下面给定过程的 "public_html"。

应用命令:$ php artisan storage:link

此命令将为您的 laravel 项目创建 symlink 到 "public" 目录。不在 "public_html" 内。所以我们需要将 "storage" link 移动到 "public_html" 目录。为此,您可以直接移动。如果它不起作用,则使用 command 移动它。进入 "public" 目录并根据 "public_html" 目录位置 运行 下面的命令。

$ mv storage /home/yourHostinInfo/domains/domainName/public_html

根据您的托管信息,路径可能会有所不同。

使用 SSH 在 Web 服务器中创建符号 link

在 Laravel 文档中,应创建从 public/storage 到 storage/app/public 的符号 link(symlink 或软 link)使文件可从 Web 访问。

(此过程将在 LARAVEL 项目目录中创建符号 LINK)

以下是有关如何使用 SSH 客户端在 Linux 网络服务器中创建符号 link 的步骤:

1. 使用 SSH 客户端(例如 PUTTY)连接并登录到您的 Web 服务器。

2. Link storage/app/public 到 public/storage 使用语法

ln -s target_path link_path

示例(在 CPanel 文件目录中)

ln -s /home/cpanel_username/project_name/storage/app/public /home/cpanel_sername/project_name/public/storage

我在创建指向位于 subdomain 下的文件夹的符号链接时遇到问题,尝试了将近 3 个小时的所有操作,但没有成功。我的情况如下:

  1. 上传文件夹位于子域下:images.example.com
  2. 我使用根文件上传图片:$_SERVER['DOCUMENT_ROOT'].'/uploads/
  3. 我想在该根文档中创建一个符号链接。

最后,我尝试了这个解决方案,它按预期工作:

ln -s /home/customer/www/images.example.com/public_html/uploads /home/customer/www/example.com/public_html/uploads

注意:我使用 PuTTY 创建了那个符号链接

如果有人正在使用 php 寻找另一种灵活的解决方案:

Route::get('/link', function () {        
   $target = '/home/public_html/storage/app/public';
   $shortcut = '/home/public_html/public/storage';
   symlink($target, $shortcut);
});

并对 $target 变量和文件结构上的 $shortcut 进行必要的更改。

您可以在该 Web 路由或任何其他函数中使用上述代码。

如果您使用的是 CPANEL 网络托管,其常见的域根目录是 public_html 目录,这可能是一个问题,因为默认情况下 Laravel 应用程序将尝试查找名为 public 在您的项目根目录中。 要解决此问题,请打开您的 config/filesystems.php 文件并更改:

/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/

'links' => [
    public_path('storage') => storage_path('app/public'),
],

对此:

/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/

'links' => [
    base_path('public_html/storage') => storage_path('app/public'),
],

请注意,我们已将 links 数组值从默认 public_path('storage') 更改为自定义 base_path('public_html/storage') 目录

您所要做的就是通过 SSH 访问服务器,导航到您的 laravel 项目所在的根路径,例如:

cd /path/to/your/domain/public_html

然后导航至 public 目录(laravel 项目)并删除 storage 目录(如果您已上传)...

然后返回到 public_html 和 运行 命令:

php artisan storage:link

这将完成工作...

我在 Hostinger 上有一个帐户。

我在使用 Laravel 文件管理器时遇到了一个大问题,但现在我知道问题出在哪里了:)

link 存储存在问题。 我的文件夹 public_html 没有 link 但存储的副本!

  1. 如果要备份,请删除此存储或重命名它:)

  2. 登录ssh并使用命令pwd获取当前文件夹,这将帮助您构建完整路径。

  3. 使用这个命令:

    ln -s

在我的例子中是这样的:

ln -s /home/u16345434/domains/website.name/storage/app/public /home/u16345434/domains/website.name/public_html/storage

祝你好运:)

最简单的选择是运行终端中的以下内容

php artisan storage:link