laravel 拉拉维尔 | 共享主机路由无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44868393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:14:48  来源:igfitidea点击:

Laravel | Shared hosting routes not working properly

apachelaravel.htaccessredirectmod-rewrite

提问by Sapnesh Naik

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

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

Steps I followed:

我遵循的步骤:

  • 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
  • softlinkthe publicdirectory of my project to public_html/finance/
  • I found that going to abc.xyz.comdidn't work but abc.xyz.com/publicdid so I set up a redirect which redirects abc.xyz.comto abc.xyz.com
  • making Storageaccessible for web.
  • dumpautoload, caching configand routes.
  • migarting databases.
  • 创建一个子域abc.xyz.com,根 ->public_html/finance/.
  • 将我的 Laravel 项目上传到与 public_html
  • softlinkpublic我的项目目录public_html/finance/
  • 我发现这abc.xyz.com行不通,但abc.xyz.com/public这样做了,我设置了一个重定向abc.xyz.comabc.xyz.com
  • 使Storage可访问的网络。
  • dumpautoload,缓存configroutes
  • 迁移databases

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

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

  • abc.xyz.com/public - works
  • abc.xyz.com/home - doesn't work
  • abc.xyz.com/public - 作品
  • abc.xyz.com/home - 不起作用

I noticed that if I manually add /publicto all routes they work!. so abc.xyz.com/public/homeworks.

我注意到,如果我手动添加/public到所有路线,他们工作!。所以abc.xyz.com/public/home工作。

my .htaccess:

我的.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 ^(.*)/$ / [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>

So how can append /publiclike abc.xyz.com/publicto all the routes?I am not very good at modifying .htaccessbut I tried some solutions from online and they did not work.

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

回答by Graham

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

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

By default Laravel project's publicfolder 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.

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

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?

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

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.

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

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}/publicand you should be good.

注意:您可能会要求您的提供商手动更改document_root,因此您所要做的就是将您的 Laravel 应用程序上传到服务器(通过 FTP),请求将 root 更改为{app}/public并且您应该很好。

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_htmlfolder on their shared hosting account.

但是,此类共享托管包在终端访问和文件权限方面确实存在限制。默认情况下,必须将他们的应用程序/代码上传到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_htmlfolder on your shared hosting account? - Certainly NO

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

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

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

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

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

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

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

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

Step 2
Copy every thing except the publicfolder from your laravel project (on development machine) in the laravelfolder (on server host - shared hosting account).
You can use:
- C-panel : which would be the slowest option
- FTP Client: like FileZillato 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.

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

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

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

Change:

改变:

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

To:

到:

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

And Change:

并改变:

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

To:

到:

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

Save and close.

保存并关闭。

Step 5
Now go to the laravelfolder (on shared hosting account -server) and open server.phpfile
Change

第 5 步
现在转到laravel文件夹(在共享主机帐户 - 服务器上)并打开server.php文件
更改

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

To:

到:

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

Save and close.

保存并关闭。

Step 6
Set file permissions for the laravel/storagefolder (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

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

Step 7

第 7 步

As .envfile 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 .gitignorefile has .envmentioned thus it will not upload it to repositories.

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

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

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

Even there are configuration files like app.php, database.phpin 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 .envfile manually!

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

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

Laravel 还提供.env-example了您可以用作参考的文件。

That's it.

就是这样。

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.

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

回答by Jesus Erwin Suarez

You can probably move your public/.htaccess and public/index.php to the root files of your laravel project.

您可以将 public/.htaccess 和 public/index.php 移动到 Laravel 项目的根文件中。

回答by Rwd

You need to change the document root for you website to be the public folder of your application i.e.

您需要将网站的文档根目录更改为应用程序的公共文件夹,即

public_html/finance/.

should be:

应该:

public_html/finance/public

https://laravel.com/docs/5.4/installation#configuration

https://laravel.com/docs/5.4/installation#configuration

Hope this helps!

希望这可以帮助!

回答by mod3st

The problem is that you have placed the entire project in the public directory - public_html, haven't you? This is a bad practice (security).

问题是你已经把整个项目放在了公共目录——public_html 中,不是吗?这是一种不好的做法(安全性)。

Your domain (abc.xyz.com) points to your application root directory, should go to the app/public dir.

您的域 (abc.xyz.com) 指向您的应用程序根目录,应转到 app/public 目录。

回答by Bob Rensolance

Update Solution: require DIR.'/../bootstrap/autoload.php';
Is now: require DIR.'/../vendor/autoload.php';
So Change Torequire __DIR__.'/../laravel/vendor/autoload.php';

更新解决方案:require DIR.'/../bootstrap/autoload.php';
现在是:require DIR.'/../vendor/autoload.php';
所以改成require __DIR__.'/../laravel/vendor/autoload.php';