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

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

Setting document root for Laravel project on Apache virtual host

phplaravelapache

提问by bcrimmins

I inherited a php/Laravel app that was running on an Apache server that I don't have access to. My task is to get it running on another Apache server. I'm pretty good with php but relatively new to Laravel and very new to Apache configuration.

我继承了一个在我无权访问的 Apache 服务器上运行的 php/Laravel 应用程序。我的任务是让它在另一台 Apache 服务器上运行。我对 php 非常熟悉,但对 Laravel 来说相对较新,对 Apache 配置也很陌生。

I have figured out how to get the Laravel app running on Apache that is running on an Ubuntu VM (VirtualBox.) I can access the Laravel app in a browser on the Ubuntu VM via http://localhost. I can also access the Laravel app in a browser from the Internet via http://appname.com/public. However, if I just use http://appname.com, then I just get a folder listing of /var/www/appname.

我已经弄清楚如何让 Laravel 应用程序在运行在 Ubuntu VM (VirtualBox) 上的 Apache 上运行。我可以通过http://localhost在 Ubuntu VM 上的浏览​​器中访问 Laravel 应用程序。我还可以通过http://appname.com/public从 Internet 在浏览器中访问 Laravel 应用程序。但是,如果我只使用http://appname.com,那么我只会得到 /var/www/appname 的文件夹列表。

I have tried several modifications to the /etc/apache2/available-sites/appname.conf file but haven't quite got it right yet, apparently. I have also read a number of posts around the nets about making modifications to various other config files including php config files and Apache config files. It seems like these other mods (while they may be workable) shouldn't be necessary.

我已经尝试了对 /etc/apache2/av​​ailable-sites/appname.conf 文件的一些修改,但显然还没有完全正确。我还阅读了许多关于网络修改各种其他配置文件的帖子,包括 php 配置文件和 Apache 配置文件。似乎不需要这些其他模组(虽然它们可能可行)。

Here is my current /etc/apache2/available-sites/appname.conf

这是我当前的 /etc/apache2/av​​ailable-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>

Any advise is appreciated.

任何建议表示赞赏。

  • Bob
  • 鲍勃

回答by Ellesar

You need to allow the mod_rewrite in the apache server and allowSymLinks. Source

您需要允许 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>

in the DocumentRoot Directory i would also allow MultiViews

在 DocumentRoot 目录中,我还允许多视图

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

You may need to also do

你可能还需要做

sudo a2enmod rewrite

须藤 a2enmod 重写

to enable module rewrite.

启用模块重写。

Edit 1:

编辑1:

In my .conf files i got them with the quotes and they are working. Did you enable the modudle rewrite?

在我的 .conf 文件中,我用引号得到了它们,它们正在工作。您是否启用了模块重写?

Besides some options i also have the "/" folder with the next config.

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

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

and here i'll write my full code of public directory

在这里我将编写我的公共目录的完整代码

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

Try it and see if it works, after delete the options that you don't like to use.

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

回答by Shahrukh Anwar

Follow the steps and all will be good and easy,

按照步骤操作,一切都会变得美好而轻松,

1).Type following command in terminal

1)。输入以下命令terminal

cd /etc/apache2/sites-available

cd /etc/apache2/sites-available

2).Make a new config file

2)。新建一个配置文件

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

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

3.Open the new config file and paste the following code

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 [email protected]
        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, then press ythen press enterand run following command in terminal

4)。CTRL+x,然后press ypress enter和下列运行在终端命令

sudo a2ensite appname.dev.conf

sudo a2ensite appname.dev.conf

5).Type following command and edit the /etc/hosts file

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

sudo nano /etc/hosts

sudo nano /etc/hosts

127.0.0.1 appname.dev

127.0.0.1 appname.dev

press CTRL xthen press Enterand type following command

press CTRL x然后press Enter输入以下命令

sudo service apache2 restart

sudo service apache2 restart

6).Now your app will execute on appname.devsuccessfully.

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