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

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

问题描述

我正在尝试在共享主机(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,根-> public_html/finance/.
  • 将laravel项目上传到与public_html级别相同的文件夹中.
  • softlink我项目的public目录到public_html/finance/
  • 我发现转到abc.xyz.com无效,但是abc.xyz.com/public可以,所以我设置了重定向,将abc.xyz.com重定向到abc.xyz.com
  • 使Storage可访问网络.
  • dumpautoload,缓存configroutes.
  • 迁移databases.
  • 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>

那么如何将/public之类的/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.

推荐答案

摘录自 Donkarnash PassionInfinite Pete Houston 贡献者页面上找到.来源已获得 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),请求更改根到 {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-panel:这是最慢的选择
-FTP客户端:类似于 FileZilla ,可连接到您的共享托管帐户并通过FTP上传传输文件和文件夹
-映射网络驱动器:您还可以在开发计算机上创建映射的网络驱动器,以使用ftp连接到共享主机帐户的根文件夹://您的域名"作为网络地址.

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步
现在打开共享主机帐户(在cpanel编辑器或任何其他连接的编辑器中)的public_html文件夹中的index.php文件,然后:

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:

更改:

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.

第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';

收件人:

To:

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

保存并关闭.

Save and close.

第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步

本地/开发服务器的.env文件,因此应将其忽略,因为它具有包括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.

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

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