如何在普通的Apache服务器上运行Laravel项目?

4
我有一个 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项目呢?

3个回答

4
请按照以下方式设置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项目。

编辑hosts文件:

请按照以下步骤进行操作:

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

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

  • 对文件进行以下更改。

在文件末尾添加以下行:

127.0.0.1        exampledomain.com www.exampledomain.com

点击“文件”>“保存”以保存您的更改。

我们做的是告诉Windows,当我们尝试访问exampledomain.com或www.exampledomain.com时,不要尝试在互联网上查找服务器,而是将请求发送到本地机器(127.0.0.1),本地机器将返回您的Laravel项目。

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


4
是的,使用Apache可以完全为Laravel应用提供服务。要调试链接为什么产生404错误,您需要提供更多有关这些链接指向的URL的信息。最好始终在打印URL时使用Laravel的url()secure_url()route()辅助函数。还要确认启用了Apache模块mod_rewriteLaravel安装:Web服务器配置)。

mod_rewrite 已禁用。 - Dims

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接