Search code examples
laravelapachelamp

Laravel - set /public as root on LAMP stack


I'm trying to deploy a Laravel App on a LAMP stack although I am having trouble setting the document root as /public. I've looked at several answers on here already for similar problems but am yet to find my problem or a solution. I ideally I want to do it through the Apache web server and not modify any Laravel files.

I copied the default .conf file a change it to the following using sudo nano /etc/apache2/sites-available/subdomain.website.com.conf

<VirtualHost *:80>
        ServerAdmin email@gmail.com
        ServerName subdomain.website.com
        ServerAlias www.subdomain.website.com
        DocumentRoot /var/www/subdomain.website.com/public_html/public

        <Directory /var/www/subdomain.website.com/public_html/public/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Require all granted
        </Directory>

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

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
        </IfModule>

RewriteEngine on
RewriteCond %{SERVER_NAME} =www.subdomain.website.com [OR]
RewriteCond %{SERVER_NAME} =subdomain.website.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

After the I ran the following commands in this order.

sudo a2ensite subdomain.website.com.conf

sudo a2enmod rewrite

sudo systemctl restart apache2

sudo service apache2 restart

I cleared cookies, did a hard refresh and it hasn't worked. I still have to go to https://subdomain.website.com/public to view the website. Is there anything I have missed or anything I can do find out what the problem may be?

The .htaccess file in the /public folder contains the following

<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]

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

Solution

  • This virtualhost is for http:// (Port 80). You have a rewrite rule that redirects to https:// as soon as it hits this virtualhost.

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.subdomain.website.com [OR]
    RewriteCond %{SERVER_NAME} =subdomain.website.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    

    Therefore, you need to apply your changes to the other virtualhost that is serving this domain from https:// (Port 443).