首页 > 解决方案 > EC2 实例上的 Laravel 项目显示目录结构或错误 404

问题描述

我已经使用 Ubuntu v. 20.04.1 LTS 设置了一个 EC2 实例。我已经从 Gitlab 克隆了一个现有的 Laravel 项目,该项目在本地 WAMP 上运行得非常好。

我需要一些有关 Apache 虚拟主机和 .htaccess 配置的帮助,因为根据我的尝试,我要么得到目录结构,要么得到一个很好的旧 404 错误。

/etc/apache2/sites-available/000-default.conf

<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 webmaster@localhost
    DocumentRoot /var/www/html/demo/public
    <Directory "/var/www/html/demo/public">
        AllowOverride All
        Order allow,deny
        allow from all
    </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

/var/www/html/demo/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

/var/www/html/demo/public/.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

我已经尝试了这些的所有常见变体。使用上面给出的特定配置,我得到一个错误 404。

我已经启用了重写模块,所以这不是问题。

请帮忙!!

标签: laravelapache.htaccessamazon-ec2deployment

解决方案


默认情况下,config in/etc/apache2/sites-available未启用或将被忽略。您需要设置 enable 以创建符号链接到/etc/apache2/sites-enabled. 尝试运行此命令:

sudo a2dissite 000-default
sudo a2ensite 000-default
sudo systemctl restart apache2

但我认为最好定义域/虚拟主机而不是使用 000-default


推荐阅读