如何在普通 Apache 上运行 Laravel 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43020807/
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 15:39:24  来源:igfitidea点击:

How to run Laravel project on normal Apache?

phpapachelaravelartisan

提问by Dims

I have Laravel project and can run it with

我有 Laravel 项目并且可以运行它

php artisan serve

I am executing this command inside D:\Users\Dims\Design\MyApplicationdirectory. After that I can see site on http://localhost:8000and can navigate it, although slow.

我正在D:\Users\Dims\Design\MyApplication目录中执行此命令。之后,我可以在http://localhost:8000上看到站点并可以导航它,尽管速度很慢。

Now I am configuring the same place to serve by apache:

现在我正在配置相同的地方以供 apache 服务:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "D:\Users\Dims\Design\MyApplication\public"
    ServerName myapplication.app
    ErrorLog "logs/myapplication-error.log"
    CustomLog "logs/myapplication-access.log" common
    <Directory />
        AllowOverride none
        Require all granted
        DirectoryIndex index.php
    </Directory>
</VirtualHost>

Not, that I pointed application not to the root of the project, but to the publicdirectory. This makes me able to open home page of the site, but clicking any links causes error 404.

不是,我不是将 application 指向项目的根public目录,而是指向目录。这使我能够打开站点的主页,但单击任何链接会导致错误 404。

If I serve

如果我服务

DocumentRoot "D:\Users\Dims\Design\MyApplication"

with Apache, I am unable to see home page at all, saying 403 forbidden. This probably because this directory has no index.php.

使用Apache,我根本看不到主页,说403禁止。这可能是因为这个目录没有index.php.

So, is it possible to serve Laravel project with Apache?

那么,是否可以使用 Apache 为 Laravel 项目提供服务?

回答by Shakti Phartiyal

Set up the apache server host file as shown below:

设置apache服务器主机文件如下图:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot D:\Users\Dims\Design\MyApplication\public
    ServerName  exampledomain.com
    ServerAlias  www.exampledomain.com
    ErrorLog "C:/some_dir/example.error.log"
    CustomLog "C:/some_dir/example.access.log" combined
    <Directory "D:\Users\Dims\Design\MyApplication\public">
        Options -Indexes
        DirectoryIndex index.php index.html
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Now that you have set the server name to exampledomain.com you also need to set up the hosts(DNS) file in windows so that you can type in exampledomain.com in your browser and access your laravel project.

现在您已经将服务器名称设置为 exampledomain.com,您还需要在 Windows 中设置主机(DNS)文件,以便您可以在浏览器中输入 exampledomain.com 并访问您的 Laravel 项目。

Editing the hosts file:

编辑主机文件:

Please follow the steps below:

请按照以下步骤操作:

  • Press the Windows key.
  • Type Notepad in the search field.
  • In the search results, right-click Notepad and select Run as administrator.
  • From Notepad, open the following file:

    c:\Windows\System32\Drivers\etc\hosts

  • Make the following changes to the file.

  • 按 Windows 键。
  • 在搜索字段中键入记事本。
  • 在搜索结果中,右键单击记事本并选择以​​管理员身份运行。
  • 从记事本中,打开以下文件:

    c:\Windows\System32\Drivers\etc\hosts

  • 对文件进行以下更改。

At the end of the file add the following line:

在文件末尾添加以下行:

127.0.0.1        exampledomain.com www.exampledomain.com

Click File > Save to save your changes.

单击文件 > 保存以保存更改。

What we did here was to tell windows that when we try to access exampledomain.com or www.exampledomain.com do not try to find the server over the internet but take the request to the local machine itself(127.0.0.1) which will in return serve your laravel project.

我们在这里所做的是告诉 windows,当我们尝试访问 exampledomain.com 或 www.exampledomain.com 时,不要尝试通过 Internet 查找服务器,而是将请求发送到本地计算机本身(127.0.0.1),它将在返回服务您的 Laravel 项目。

You can also find one another method here at wikihow -> http://www.wikihow.com/Install-Laravel-Framework-in-Windows

您还可以在 wikihow -> http://www.wikihow.com/Install-Laravel-Framework-in-Windows 上找到另一种方法

回答by bradforbes

Yes, it's absolutely possible to serve Laravel applications with Apache. To debug why your links are producing 404 errors, you'll have to provide more information about the URLs these links point to. It's best to always use Laravel's url(), secure_url()or route()helper functions when printing URLs. Also confirm that the Apache module mod_rewriteis enabled (Laravel Installation: Web Server Configuration)

是的,使用 Apache 为 Laravel 应用程序提供服务是绝对可能的。要调试链接产生 404 错误的原因,您必须提供有关这些链接指向的 URL 的更多信息。最好在打印 URL 时始终使用 Laravel 的url(),secure_url()route()辅助函数。同时确认Apache模块mod_rewrite已启用(Laravel安装:Web服务器配置

回答by AtomicNation

If after configuring the virtualhost you're still unable visit your laravel app, but it works fine with artisan serve, check your apache php version:

如果在配置虚拟主机后您仍然无法访问您的 Laravel 应用程序,但它与 artisan serve 一起工作正常,请检查您的 apache php 版本:

Check /etc/apache2/mods-enabled and if the php module is lower than the one required in laravel specifications, update it. In my case:

检查 /etc/apache2/mods-enabled ,如果 php 模块低于 laravel 规范要求的模块,请更新它。就我而言:

$ a2dismod php7.0
$ a2enmod php7.2
$ service apache2 reload

Solution found at: https://laracasts.com/discuss/channels/laravel/unexpected-illuminatesupportarrphp-on-line-388

解决方案位于:https: //laracasts.com/discuss/channels/laravel/unexpected-illuminatesupportarrphp-on-line-388