在Apache虚拟主机上为Laravel项目设置文档根目录

4
我继承了一个运行在Apache服务器上的php/Laravel应用程序,但我没有访问权限。我的任务是让它在另一个Apache服务器上运行。我对php很熟悉,但对Laravel相对较新,对Apache配置更是一窍不通。
我已经找到了如何在运行在Ubuntu虚拟机(VirtualBox)上的Apache上运行Laravel应用程序。我可以通过http://localhost在Ubuntu虚拟机的浏览器中访问Laravel应用程序。我也可以通过http://appname.com/public从Internet浏览器中访问Laravel应用程序。但是,如果我只使用http://appname.com,那么我只能得到/var/www/appname的文件夹列表。
我尝试了对/etc/apache2/available-sites/appname.conf文件进行多次修改,但似乎还没有完全正确。我还阅读了许多关于修改各种其他配置文件(包括php配置文件和Apache配置文件)的帖子。看起来这些其他修改(虽然可能可行)不应该是必要的。
以下是我的当前/etc/apache2/available-sites/appname.conf文件:
    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName appname.com
        ServiceAlias www.appname.com
        DocumentRoot /var/www/appname/public

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

欢迎提出任何建议。

  • Bob

你在hosts文件中设置了虚拟主机吗? - Tai Le
谢谢。我在哪里可以找到“hosts”文件? - bcrimmins
你可以尝试使用 sudo gedit /etc/hosts 命令,并在该文件中编写您的虚拟主机,例如: 127.0.0.1 appname.com - Tai Le
这是我的当前hosts文件。我需要为新应用程序添加一行吗? 127.0.0.1 localhost 127.0.1.1 linuxstudio #以下行对于IPv6兼容的主机是必需的 ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters - bcrimmins
是的,您需要添加一行新的代码:127.0.0.1 appname.com,然后重新启动XAMPP,它就会运行。 - Tai Le
2个回答

10

您需要在Apache服务器中启用mod_rewrite和allowSymLinks。

来源
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName appname.com
    ServiceAlias www.appname.com
    DocumentRoot /var/www/appname/public

    <Directory "/var/www/appname/public">
            Options FollowSymLinks
            ReWriteEngine On
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
在DocumentRoot目录中,我还会允许MultiViews。
<Directory "/var/www/appname/public">
        Options FollowSymLinks MultiViews
        ReWriteEngine On
</Directory>

你可能还需要执行

sudo a2enmod rewrite

以启用rewrite模块。

编辑1:

在我的.conf文件中,它们被带引号并且可以工作。您启用了rewrite模块吗?

除了一些选项外,我还有“/”文件夹的下一个配置。

<Directory "/">
    Options FollowSymLinks
    AllowOverride All
    ReWriteEngine On
</Directory>

这里我将写出我的公共目录的完整代码。

<Directory "/var/www/appname/public">
        Options FollowSymLinks MultiViews
        Order Allow,Deny
        Allow from all
        ReWriteEngine On
</Directory>

尝试一下,看看是否有效,在删除您不想使用的选项后。


1
在启用rewrite模块后,也要重新启动apache sudo service apache2 restart - Angad Dubey
谢谢。我添加了这些选项,禁用然后启用了主机,重新启动了Apache,但是没有变化。我注意到您在“目录”部分周围使用了引号。这是必需的吗?我在我遇到的其他示例中都没有看到过。我要尝试吗? - bcrimmins
@Ellesar,谢谢,但那没用。看起来我可能缺少一些非常基础的东西。我想做的就是将http调用重定向到appname.com到appname.com/public/。 - bcrimmins
你能展示一下项目文件夹中的 .htaccess 文件吗?也许问题就出在那里。 - Ellesar

4

按照以下步骤操作即可轻松完成:

1).terminal中输入以下命令:

cd /etc/apache2/sites-available

2). 创建一个新的配置文件

sudo cp 000-default.conf appname.dev.conf

3. 打开新的配置文件并粘贴以下代码

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin yourmail@example.com
        ServerAlias appname.dev
        DocumentRoot /var/www/html/appname/public

        <Directory /var/www/html/appname/public>
           Options -Indexes +FollowSymLinks +MultiViews
            AllowOverride All
            Require all granted
            <FilesMatch \.php$>
               #Change this "proxy:unix:/path/to/fpm.socket"
               #if using a Unix socket
               #SetHandler "proxy:fcgi://127.0.0.1:9000"
            </FilesMatch>
        </Directory>

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

4). 按下CTRL+x,然后按y键,再按Enter键,在终端中运行以下命令:

sudo a2ensite appname.dev.conf

5). 输入以下命令并编辑/etc/hosts文件:

sudo nano /etc/hosts

127.0.0.1 appname.dev

按下CTRL+x,然后按Enter键,并输入以下命令:

sudo service apache2 restart

6). 现在您的应用程序将成功地在appname.dev上执行。


2
注意:在运行本地应用程序时,请不要使用.dev TLD,因为.dev TLD与其他TLD一样。请使用developmentlocal等。 - М.Б.

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