拉拉维尔 |共享主机路由无法正常工作 [英] Laravel | Shared hosting routes not working properly

查看:32
本文介绍了拉拉维尔 |共享主机路由无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在共享主机(godaddy)上部署我的 Laravel 项目,但到目前为止我只成功了一部分.

I am trying to deploy my Laravel project on a shared host(godaddy) and so far I have only partially succeeded.

我遵循的步骤:

  • 创建子域 abc.xyz.com , root -> public_html/finance/.
  • 将我的 Laravel 项目上传到与 public_html 相同级别的文件夹
  • softlink 我项目的public目录到public_html/finance/
  • 我发现去 abc.xyz.com 没有用,但 abc.xyz.com/public 做到了,所以我设置了一个重定向 abc.xyz.comabc.xyz.com
  • 使 Storage 可在网络上访问.
  • dumpautoload,缓存configroutes.
  • 迁移数据库.
  • Create a subdomain abc.xyz.com , root -> public_html/finance/.
  • Upload my laravel project to a folder which is at same level as that of public_html
  • softlink the public directory of my project to public_html/finance/
  • I found that going to abc.xyz.com didn't work but abc.xyz.com/public did so I set up a redirect which redirects abc.xyz.com to abc.xyz.com
  • making Storage accessible for web.
  • dumpautoload, caching config and routes.
  • migarting databases.

现在我可以成功登录,这会将我带到 abc.xyz.com/public 但我所有其他没有 /public 的路由都不起作用.例如:

Now I can successfully login which takes me to abc.xyz.com/public but my all other routes which does not have /public do not work. For example:

  • abc.xyz.com/public - 作品
  • abc.xyz.com/home - 不起作用

我注意到,如果我手动将 /public 添加到所有路由,它们都可以工作!.所以 abc.xyz.com/public/home 有效.

I noticed that if I manually add /public to all routes they work!. so abc.xyz.com/public/home works.

我的 .htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

那么如何将 /publicabc.xyz.com/public 一样附加到所有路由?我不太擅长修改.htaccess,但我尝试了一些在线解决方案,但没有奏效.

So how can append /public like abc.xyz.com/public to all the routes? I am not very good at modifying .htaccess but I tried some solutions from online and they did not work.

推荐答案

摘自 在 Linux 服务器上的共享主机上部署 Laravel 5 应用.原作者是DonkarnashPassionInfinitePete HoustonKyslik.可以在 贡献者页面上找到归因详细信息.来源根据 CC BY-SA 3.0 获得许可,可以在 文档存档.参考主题 ID:2410.

Excerpted from Deploy Laravel 5 App on Shared Hosting on Linux Server. The original authors were Donkarnash, PassionInfinite, Pete Houston and Kyslik. Attribution details can be found on the contributor page. The source is licenced under CC BY-SA 3.0 and may be found in the Documentation archive. Reference topic ID: 2410.

默认情况下,Laravel 项目的 public 文件夹公开了任何人都可以从任何地方请求的应用程序的内容,其余的应用程序代码对于没有适当权限的任何人都是不可见的或无法访问的.

By default Laravel project's public folder exposes the content of the app which can be requested from anywhere by anyone, the rest of the app code is invisible or inaccessible to anyone without proper permissions.

在您的开发机器上开发应用程序后,需要将其推送到生产服务器,以便可以从任何地方通过 Internet 访问它 - 对吗?

After developing the application on your development machine, it needs to be pushed to a production server so that it can be accessed through the internet from anywhere - right?

对于大多数应用/网站,首选是使用来自 GoDaddy、HostGator 等托管服务提供商的共享托管包,主要是因为成本低.

For most apps/websites the first choice is to use shared hosting package from hosting service providers like GoDaddy, HostGator etc. mainly due to low cost.

注意:您可以要求您的提供商手动更改document_root,因此您所要做的就是将您的 Laravel 应用程序上传到服务器(通过 FTP),请求更改root 到 {app}/public,你应该没问题.

note: you may ask your provider to manually change document_root, so all you have to do is upload your Laravel application to server (via FTP), request change of root to {app}/public and you should be good.

此类共享主机包在终端访问和文件权限方面确实存在限制.默认情况下,必须将他们的应用程序/代码上传到其共享主机帐户上的 public_html 文件夹.

Such shared hosting packages, however do have limitations in terms of terminal access and file permissions. By default one has to upload their app/code to the public_html folder on their shared hosting account.

那么,如果您想将 Laravel 项目上传到共享主机帐户,您会怎么做?您是否应该将整个应用程序(文件夹)上传到共享主机帐户上的 public_html 文件夹?- 当然没有

So if you want to upload a Laravel project to a shared hosting account how would you go about it? Should you upload the entire app (folder) to the public_html folder on your shared hosting account? - Certainly NO

因为 public_html 文件夹中的所有内容都可以公开,即任何人"访问,这将是一个很大的安全风险.

Because everything in the public_html folder is accessible "publically i.e. by anyone" which would be a big security risk.

将项目上传到共享主机帐户的步骤 - Laravel 方式

Steps to upload a project to shared hosting account - the Laravel way

步骤 1
在与 public_html 文件夹相同的级别上创建一个名为 laravel(或您喜欢的任何内容)的文件夹.

Step 1
Create a folder called laravel (or anything you like) on the same level as the public_html folder.

Eg:  
/
|--var  
    |---www
        |----laravel       //create this folder in your shared hosting account
        |----public_html  
        |----log  

第 2 步
从您的 Laravel 项目(在开发机器上)复制除 public 文件夹之外的所有内容,并将其复制到 laravel 文件夹(在服务器主机上 - 共享主机帐户)中.
您可以使用:
- C面板:这将是最慢的选择
- FTP 客户端:像 FileZilla 连接到您的共享主机帐户并通过 FTP 上传传输您的文件和文件夹
- 映射网络驱动器:您还可以在开发机器上创建映射网络驱动器,以使用 "ftp 连接到共享主机帐户的根文件夹://your-domain-name" 作为网络地址.

Step 2
Copy every thing except the public folder from your laravel project (on development machine) in the laravel folder (on server host - shared hosting account).
You can use:
- C-panel : which would be the slowest option
- FTP Client: like FileZilla to connect to you shared hosting account and transfer your files and folders through FTP upload
- Map Network Drive: you can also create a mapped network drive on your development machine to connect to your shared hosting account's root folder using "ftp://your-domain-name" as the network address.

第 3 步
打开 Laravel 项目的 public 文件夹(在开发机器上),复制所有内容并粘贴到 public_html 文件夹(在服务器主机 - 共享主机帐户上).
第 4 步
现在打开共享主机帐户上 public_html 文件夹中的 index.php 文件(在 cpanel 编辑器或任何其他连接的编辑器中)和:

Step 3
Open the public folder of your laravel project (on development machine), copy everything and paste in the public_html folder (on server host - shared hosting account).
Step 4
Now open the index.php file in the public_html folder on the shared hosting account (in cpanel editor or any other connected editor) and:

改变:

require __DIR__.'/../bootstrap/autoload.php';   

致:

require __DIR__.'/../laravel/bootstrap/autoload.php';  

和改变:

$app = require_once __DIR__.'/../bootstrap/app.php';

致:

$app = require_once __DIR__.'/../laravel/bootstrap/app.php';

保存并关闭.

步骤 5
现在转到 laravel 文件夹(在共享主机帐户 -server 上)并打开 server.php 文件
改变

Step 5
Now go to the laravel folder (on shared hosting account -server) and open server.php file
Change

require_once __DIR__.'/public/index.php';

致:

require_once __DIR__.'../public_html/index.php';  

保存并关闭.

步骤 6
laravel/storage 文件夹的文件权限(递归)以及共享主机帐户 - 服务器上的所有文件、子文件夹和文件设置为 777.
注意:注意linux中的文件权限,它们就像双刃剑,如果使用不当,可能会使您的应用程序容易受到攻击.要了解 Linux 文件权限,您可以阅读 https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions

Step 6
Set file permissions for the laravel/storage folder (recursively) and all files, sub-folders and file within them on shared hosting account - server to 777.
Note: Be careful with the file permissions in linux, they are like double edged sword, if not used correctly, they may make your app vulnerable to attacks. For understanding Linux file permissions you can read https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions

步骤 7

As .env 本地/开发服务器的文件被 git 忽略,它应该被忽略,因为它具有包括 APP_KEY 在内的所有环境变量,并且不应通过将其推送到公共存储库".您还可以看到 .gitignore 文件提到了 .env 因此它不会将其上传到存储库.

As .env file of local/development server is Ignored by git and it should be ignored as it has all the environment variables including the APP_KEY and it should not be exposed to public by pushing it into the repositories'. You can also see that .gitignore file has .env mentioned thus it will not upload it to repositories.

完成上述所有步骤后,在 laravel 文件夹中创建一个 .env 文件,并添加您在本地/开发服务器的 .env 中使用的所有环境变量文件到生产服务器的 .env 文件.

After following all the above steps make a .env file in the laravel folder and add all the environment variable which you have used from the local/development server's .env file to the .env file of production server.

即使在 Laravel 应用程序的 config 文件夹中也有像 app.phpdatabase.php 这样的配置文件,默认情况下在 的第二个参数中定义了这个变量env() 但不要硬编码这些文件中的值,因为它会影响拉取存储库的用户的配置文件.所以建议手动创建 .env 文件!

Even there are configuration files like app.php, database.php in config folder of laravel application which defines this variables as by default in second parameter of env() but don't hard-code the values in these files as it will affect the configuration files of the users who pulls your repository. So it is recommended to create .env file manually!

此外,laravel 还提供了 .env-example 文件,您可以将其用作参考.

Also laravel gives .env-example file that you can use as a reference.

就是这样.

现在,当您访问配置为服务器域的 url 时,您的 Laravel 应用程序应该可以像在本地主机上一样工作 - 开发机器,而应用程序代码仍然是安全的,没有正确文件的任何人都无法访问权限.

Now when you visit the url which you configured as the domain with your server, your laravel app should work just as it worked on your localhost - development machine, while still the application code is safe and not accessible by anyone without proper file permissions.

这篇关于拉拉维尔 |共享主机路由无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆