【问题标题】:How to run Laravel project on normal Apache?如何在普通 Apache 上运行 Laravel 项目?
【发布时间】:2017-08-18 15:35:24
【问题描述】:

我有 Laravel 项目,可以运行它

php artisan serve

我在D:\Users\Dims\Design\MyApplication 目录中执行这个命令。之后我可以在http://localhost:8000 上看到网站并可以浏览它,虽然速度很慢。

现在我正在配置由 apache 服务的同一个地方:

<VirtualHost *:80>
    ServerAdmin webmaster@myapplication.app
    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>

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

如果我发球

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

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

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

【问题讨论】:

    标签: php apache laravel laravel-artisan


    【解决方案1】:

    如果在配置完虚拟主机后你仍然无法访问你的 laravel 应用,但它在 artisan serve 上运行良好,请检查你的 apache php 版本:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 它是 mod_rewrite 禁用
      【解决方案3】:

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

      <VirtualHost *:80>
          ServerAdmin user@email.com
          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>
      

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

      编辑主机文件:

      请按照以下步骤操作:

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

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

      • 对文件进行以下更改。

      在文件末尾添加以下行:

      127.0.0.1        exampledomain.com www.exampledomain.com
      

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

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

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

      【讨论】:

        猜你喜欢
        • 2011-06-27
        • 2018-09-02
        • 2018-10-30
        • 2019-05-12
        • 1970-01-01
        • 1970-01-01
        • 2011-10-01
        • 2017-05-15
        • 1970-01-01
        相关资源
        最近更新 更多