简体   繁体   English

如何在已部署的网站中创建符号链接

[英]How to make a symlink in a deployed website

I am new in Laravel, as you know in order to access stored files you should have a symlink from storage/app/public to public/storage. 我是Laravel的新手,你知道为了访问存储文件,你应该有一个从存储/ app / public到public / storage的符号链接。 In computer you can just type in command line: php artisan storage:link. 在计算机中,您只需输入命令行:php artisan storage:link。 But now I have deployed my website on hosting and want to create a symlink. 但现在我已将我的网站部署在托管上,并希望创建一个符号链接。 How can I do it? 我该怎么做?

I'm afraid this is near impossible on shared hosting since you would need a shell access to create the same symlink like on your computer. 我担心这在共享主机上几乎是不可能的,因为您需要shell访问权来创建与计算机上相同的符号链接。

Depending on your options maybe Laravel Forge is helpful to you. 根据您的选择, Laravel Forge可能对您有所帮助。

Kind regards 亲切的问候

Maybe if you can add in the " scripts " tag of your composer a call to create the link after the deploy of your project: 也许如果你可以在你的作曲家的“ scripts ”标签中添加一个调用来在项目部署后创建链接:

composer.json file: composer.json文件:

"scripts": {
    "post-root-package-install": [
        "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ],
    "post-install-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postInstall",
        "php artisan optimize"
        "php artisan storage:link"
    ],
    "post-update-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postUpdate",
        "php artisan optimize"
        "php artisan storage:link"
    ]
}

This will cause the link to be created every time a **composer update** or **composer install** call occurs. 这将导致每次**composer update****composer install**调用发生时创建链接。

If the storage folder is not created, you will receive: 如果未创建存储文件夹,您将收到:

The [public / storage] directory has been linked.

If it is already created: 如果已经创建:

The "public / storage" directory already exists.

See also this discussion that may help you: https://github.com/laravel/internals/issues/34 另请参阅此讨论可能对您有所帮助: https//github.com/laravel/internals/issues/34

Cron job does the trick for me on shared hosting. Cron的工作对我来说是共享主机的诀窍。 Simply configure cron job to run the command the next minute (or a specific close time), and delete it after. 只需配置cron作业以在下一分钟(或特定关闭时间)运行命令,然后将其删除。

For example, this will run the command at 9:30am and log any output to myjob.log 例如,这将在上午9:30运行命令并将任何输出记录到myjob.log

30 9 * * * /path/to/php /path/to/artisan storage:link >> /path/to/myjob.log 2>&1

Just create a symlink in a php file and upload it to your public folder then navigate to it through your browser. 只需在php文件中创建一个符号链接并将其上传到您的公共文件夹,然后通过浏览器导航到它。 The links must be absolute path.... 链接必须是绝对路径....

$targetFolder = '/home/account_name/laravel/storage/app/public';

$linkFolder = '/home/account_name/public_html/storage';

symlink($targetFolder, $linkFolder);

echo 'Done!'

Lets save the file as link.php and upload it then navigate to it via http://your_site.com/link.php 让我们将文件保存为link.php并上传,然后通过http://your_site.com/link.php导航到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM