如何在不公开 /app/ 文件夹的情况下将 Laravel 4 安装到网络主机子文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16683046/
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
How to install Laravel 4 to a web host subfolder without publicly exposing /app/ folder?
提问by Emmanuel Figuerola
I was wondering if any of you know of a way to install Laravel 4 in a web host SUBDIRECTORY / subfolder while not exposing the /app/ folder and other sensible files to the publicly accesible part of the host.
我想知道你们中是否有人知道在网络主机 SUBDIRECTORY / 子文件夹中安装 Laravel 4 的方法,同时又不将 /app/ 文件夹和其他敏感文件暴露给主机的可公开访问的部分。
The idea is, I'd be able to access http://mydomain.com/mylaravel/to be able to use Laravel, but at the same time I want to avoid anyone doing something like going to http://mydomain.com/app/or http://mydomain.com/mylaravel/app/and basically being able to see my config files and other code.
这个想法是,我可以访问http://mydomain.com/mylaravel/以使用 Laravel,但同时我想避免任何人做类似访问http://mydomain.com 的事情/app/或http://mydomain.com/mylaravel/app/并且基本上能够看到我的配置文件和其他代码。
回答by Emmanuel Figuerola
So I figured out how to do this. I'll explain with an example.
所以我想出了如何做到这一点。我会用一个例子来解释。
Suppose you a domain, http://domain.com. Here's an example of the structure you might be using:
假设您有一个域http://domain.com。以下是您可能正在使用的结构示例:
domain.com/ (the root of your web hosting)
|-- yourlaravel4_base/
|-- [some other folders...]
|-- public_html/ (where your html files and such go)
| |-- [some other folders...]
| |-- yourlaravel4/
/public_html/
is the root of the publicly accessible part of your web hosting files.
You want to create a subfolder in /public_html/
(in this case /public_html/yourlaravel4/
). In this subfolder you will store all the contentsof the Laravel 4 public/
folder.
/public_html/
是您的网络托管文件的可公开访问部分的根。您想在/public_html/
(在本例中/public_html/yourlaravel4/
)创建一个子文件夹。在这个子文件夹中,您将存储Laravel 4文件夹的所有内容public/
。
Now, for the rest of the files. You have to go to the root of your web hosting files, that is, you wanna be at domain.com/
level, therefore being able to see public_html/
and some other folders. Then, we need to create a folder here, where Laravel 4's base files will be stored. In this case, it's domain.com/yourlaravel4_base/
. Inside yourlaravel4_base/
we need to store every file and folder that exists in the base Laravel 4 directory. That would be app/
, bootstrap/
, vendor/
, server.php
, etc. Everything EXCEPT the /public/
folder, whose contents you already stored in public_html/yourlaravel4/
.
现在,对于其余的文件。您必须转到 Web 托管文件的根目录,也就是说,您希望处于同一domain.com/
级别,因此能够查看public_html/
其他一些文件夹。然后,我们需要在这里创建一个文件夹,用于存储 Laravel 4 的基础文件。在这种情况下,它是domain.com/yourlaravel4_base/
. 在里面,yourlaravel4_base/
我们需要存储 Laravel 4 基础目录中存在的每个文件和文件夹。那将是app/
, bootstrap/
, vendor/
,server.php
等。除了/public/
文件夹之外的所有内容,您已经将其内容存储在public_html/yourlaravel4/
.
Finally, we need to edit 2 files: Laravel's /bootstrap/paths.php
and /public/index.php
.
最后,我们需要编辑 2 个文件:Laravel/bootstrap/paths.php
和/public/index.php
.
In the paths.php
file, replace:
在paths.php
文件中,替换:
'app' => __DIR__.'/../app',
with:
和:
'app' => __DIR__.'/../../yourlaravel4_base/app',
In the paths.php
file, replace:
在paths.php
文件中,替换:
'public' => __DIR__.'/../public',
with:
和:
'public' => __DIR__,
In the paths.php
file, replace:
在paths.php
文件中,替换:
'base' => __DIR__.'/..',
with:
和:
'base' => __DIR__.'/../../yourlaravel4_base',
In paths.php
, replace:
在paths.php
,替换:
'storage' => __DIR__.'/../app/storage',
with:
和:
'storage' => __DIR__.'/../../yourlaravel4_base/app/storage',
In index.php
, replace:
在index.php
,替换:
require __DIR__.'/../bootstrap/autoload.php';
with:
和:
require __DIR__.'/../../yourlaravel4_base/bootstrap/autoload.php';
In index.php
, replace:
在index.php
,替换:
$app = require_once __DIR__.'/../bootstrap/start.php';
with:
和:
$app = require_once __DIR__.'/../../yourlaravel4_base/bootstrap/start.php';
Upload changes. Now you should be able to have Laravel 4 installed in a subfolder in your website without actually exposing the app/
folder and other sensitive files. :)
上传更改。现在您应该能够将 Laravel 4 安装在您网站的子文件夹中,而无需实际暴露app/
文件夹和其他敏感文件。:)
回答by Tom
You can use an alias: no hankey-pankey with widespread directories on your system.
您可以使用别名:no hankey-pankey 在您的系统上具有广泛的目录。
Specify in your httpd.conf or admin-panel:
在您的 httpd.conf 或管理面板中指定:
/mylaravel/ /path/to/laravel/public
Furthermore, specify the URL in your Laravel's app.php
此外,在 Laravel 的 app.php 中指定 URL
Line 29
第 29 行
'url' => 'http://yourdomain.com/mylaravel/',
Eventually set the RewriteBase in your /public/.htaccess
最终在您的 /public/.htaccess 中设置 RewriteBase
RewriteBase /mylaravel/
Works like a charm and keeps your directory structure clean and to the point!
像魅力一样工作,让您的目录结构保持干净,切中要害!
回答by sisou
People, people! No need to over-complicate things!
人们,人们!没必要把事情复杂化!
(For other public folders, such as htdocs
or www
just replace public_html
in the following with your one.)
(对于其他公用文件夹,例如htdocs
或www
只是将public_html
以下内容替换为您的文件夹。)
- Put all your Laravel stuff in the top-level folder (so that the
app
folder is next to thepublic_html
folder) - Move all files from
public
topublic_html
(be sure to also move hidden files, such as.htaccess
!) - Delete the now empty
public
folder In
bootstrap/path.php
change'public' => __DIR__.'/../public',
to'public' => __DIR__.'/../public_html',
- Done.
- 把你所有的 Laravel 东西放在顶级文件夹中(这样
app
文件夹就在文件夹旁边public_html
) - 将所有文件从 移动
public
到public_html
(确保也移动隐藏文件,例如.htaccess
!) - 删除现在为空的
public
文件夹 在
bootstrap/path.php
变化'public' => __DIR__.'/../public',
到'public' => __DIR__.'/../public_html',
- 完毕。
回答by Hassan Gilak
emmanuel answer doesnt work for me but i did some changes to make it work . although i used my web root to be the source of a project . there it is ; i have htdocs folder as a root folder but you may have other, such as public_html , www or so on . you can easily replace my htdocs with the one you have .
伊曼纽尔的回答对我不起作用,但我做了一些改变以使其起作用。虽然我用我的 web root 作为一个项目的源。就在那里;我有 htdocs 文件夹作为根文件夹,但您可能还有其他文件夹,例如 public_html 、 www 等。您可以轻松地用您拥有的 htdoc 替换我的 htdoc。
fine .
美好的 。
my dircetory
我的目录
root/ htdocs .htaccess .override
first i solved all of laravel dependencies on my localhost on windows with composer . after that i did pack the whole package into laravel4.zip . in my htdocs i create a laravel folder and i did upload and unzip the zip file in it.
首先,我使用 composer 在 Windows 上解决了我的本地主机上的所有 Laravel 依赖项。之后我确实将整个包打包到 laravel4.zip 中。在我的 htdocs 中,我创建了一个 Laravel 文件夹,并上传并解压缩了其中的 zip 文件。
htdocs/ laravel/ app bootstrap public vendors ...
so after i did unzip the file i move everything in public folder into htdocs folder
所以在我解压缩文件后,我将公共文件夹中的所有内容移动到 htdocs 文件夹中
htdocs/ laravel packages .htaccess favicon.ico index.php robots.txt
now we have to rewrite some pathes in htdocs/index.php and htdocs/laravel/bootstrap/paths.php
现在我们必须在 htdocs/index.php 和 htdocs/laravel/bootstrap/paths.php 中重写一些路径
but lets open htdocs/laravel/bootstrap/paths.php . you would see
但是让我们打开 htdocs/laravel/bootstrap/paths.php 。你会看到
'app' => __DIR__.'/../app',
what does it mean . it means that we are standing at htdocs/laravel/bootstrap/ path . and there for we need exit this folder to laravel folder then going for app folder . ok . this is the structure of the laravel for routing . if you get this right you can make your laravel run .
这是什么意思 。这意味着我们站在 htdocs/laravel/bootstrap/ path 。因为我们需要退出这个文件夹到 laravel 文件夹然后去 app 文件夹。好的 。这是用于路由的 Laravel 的结构。如果你做对了,你可以让你的 laravel 运行。
find
找
'public' => __DIR__.'/../public',
and change it to
并将其更改为
'public' => __DIR__.'/../../htdocs',
(simple ha ) with one (../) of these we can go up one folder . so now we are at laravel folder . but we need to be in htdocs . so we have to write another (../) to get there .
(简单哈)用其中一个 (../) 我们可以上一个文件夹。所以现在我们在 laravel 文件夹。但我们需要在 htdocs 中。所以我们必须写另一个 (../) 才能到达那里。
open htdocs/index.php and find
打开 htdocs/index.php 并找到
require __DIR__.'/../bootstrap/autoload.php';
change it into
把它改成
require 'laravel/bootstrap/autoload.php';
require is a built-in php function so we are going to treat it as a simple php . we dont realy need laravel style for fetching autoload.php file . so simply remove it and go for classic style
require 是一个内置的 php 函数,所以我们将把它当作一个简单的 php 。我们真的不需要 Laravel 样式来获取 autoload.php 文件。所以只需删除它并选择经典风格
find
找
$app = require_once __DIR__.'/../bootstrap/start.php';
change it into
把它改成
$app = require_once 'laravel/bootstrap/start.php';
回答by Mohamed Salem Lamiri
If you are trying to run Laravel 5.1 into a shared hosting space and all of the above answers seems to be a bit different from the actual Laravel files/folders or you are looking how to put your laravel 5/5.1 into a sub directory on your shared hosting space so you can access it like this:
如果您尝试将 Laravel 5.1 运行到共享主机空间并且上述所有答案似乎与实际的 Laravel 文件/文件夹略有不同,或者您正在寻找如何将 Laravel 5/5.1 放入您的子目录中共享主机空间,因此您可以像这样访问它:
http://mywebsite.com/mylaravel/
So this answer is for you, first of all make sure you meet
所以这个答案是给你的,首先确保你满足
Laravel 5.1 requirements :
Laravel 5.1 要求:
- PHP 5.5
- PHP extension Mcrypt
- PHP extension Mbstring
- PHP extension OpenSSL
here an easy two tutorials for you :
这里有两个简单的教程供您使用:
回答by Hassan Gilak
if you are using addondomain or subdomaind wizard in your panel ,wizard should ask you for document root . for example i want to addon droidman.com , wizard should suggest me with making droidman.com folder . in this step tell wizard to make droidman.com/public folder . then put your application in droidman.com folder . your host will look at public folder ;) . problem solved
如果您在面板中使用 addondomain 或 subdomaind 向导,向导应该要求您提供文档根目录。例如我想添加 droidman.com ,向导应该建议我制作 droidman.com 文件夹。在这一步告诉向导制作 droidman.com/public 文件夹。然后将您的应用程序放在 droidman.com 文件夹中。您的主机将查看公共文件夹 ;) 。问题解决了
回答by Sumit Patil
If you want to simply setup Laravel Project on shared hosting and you have cPanel access then you can do following steps which I have followed and it worked well.
如果您想简单地在共享主机上设置 Laravel 项目并且您拥有 cPanel 访问权限,那么您可以按照我遵循的步骤进行操作,并且效果很好。
Step 1 : Make zip or tar file of your entire Laravel Application on your Local Machine.
第 1 步:在本地机器上制作整个 Laravel 应用程序的 zip 或 tar 文件。
Step 2 : Go to Cpanel. Upload zip/tar file to public_html directory and then extract it. (You can also create separate folder for your laravel application. As I want to upload more than one Laravel applications I have created separate directory for each one as shown in below image.)
第 2 步:转到 Cpanel。将 zip/tar 文件上传到 public_html 目录,然后解压。(你也可以为你的 Laravel 应用程序创建单独的文件夹。因为我想上传多个 Laravel 应用程序,我为每个应用程序创建了单独的目录,如下图所示。)
Step 3 : Now go to 'Addon Domain' and change document root of your specified domain. Point the domain to public folder.
第 3 步:现在转到“附加域”并更改指定域的文档根目录。将域指向公用文件夹。
Now check your domain it will redirect to public/index.php file. There is no need to change any paths.
现在检查您的域,它将重定向到 public/index.php 文件。无需更改任何路径。
回答by Troy
If you're using an apache server, the simplest way to do this is to host store your Laravel application outside of the www root (or in a protected folder in www root) then symlink the desired subfolder to point it the public folder in laravel
如果您使用的是 apache 服务器,最简单的方法是将您的 Laravel 应用程序托管在 www 根目录之外(或 www 根目录中的受保护文件夹中),然后符号链接所需的子文件夹以将其指向 Laravel 中的公共文件夹
For example, I place my laravel installations in:
例如,我将我的 Laravel 安装放在:
/usr/local/share/myLaravel
Then, in your public www folder, create a symlink as so:
然后,在您的公共 www 文件夹中,创建一个符号链接,如下所示:
ln -s /usr/local/share/myLaravel/public mylaravel
You'll need to make some adjustments to your installation, like putting your Rewrite Base to /mylaravel/ and make sure that your config files allow overrides in the base location, but this is simpler than modifying everything.
您需要对安装进行一些调整,例如将 Rewrite Base 放在 /mylaravel/ 并确保您的配置文件允许在 base 位置进行覆盖,但这比修改所有内容更简单。
回答by James Edward
Make sure to check your permissions. I deployed through the following although I don't recommend it.
请务必检查您的权限。虽然我不推荐它,但我通过以下方式进行了部署。
I used SSH and did something like:
我使用 SSH 并做了类似的事情:
rm -rf public_html/ // WARNING: READ NOTE AT THE BOTTOM BEFORE RUNNING
ln -s project_folder/public/ public_html // where the format is ln -s target name
Afterwards, I would receive a message like "Whoops, looks like something went wrong" (or an error depending on your debug settings). To fix this, I just had to change my permissions to 755 for my folders. reference: http://kb.webhostface.com/laravel/fix-permission-denied-error-laravel
之后,我会收到一条消息,如“哎呀,好像出了点问题”(或错误,取决于您的调试设置)。为了解决这个问题,我只需要将我的文件夹的权限更改为 755。参考:http: //kb.webhostface.com/laravel/fix-permission-denied-error-laravel
Overall, I'm going to go back and try the original way with changed permissions as I find this route to be less secure, and more reliant on my hosting customer service.
总的来说,我将返回并尝试更改权限的原始方式,因为我发现这条路线不太安全,并且更依赖于我的托管客户服务。
*****Note***** This command obviously removes your public_html (also sometimes called public or www) folder. You need to have a backup of all the files in it because you are deleting it. Also, you will need to be prepared to go through customer service to restore it. Many hosting services don't allow you to simply recreate the folder on your own.
*****注意***** 此命令显然会删除您的 public_html(有时也称为 public 或 www)文件夹。您需要备份其中的所有文件,因为您要删除它。此外,您需要准备好通过客户服务来恢复它。许多托管服务不允许您简单地自行重新创建文件夹。