Ubuntu 18 Apache2 404 Not Found

I have a site up and running on Ubuntu 14. I have recently set up a new server with the same provider to migrate to Ununtu 16, and that is up and running. At the same time, I am trying out Digital Ocean with Ubuntu 18. This requires a lot of manual installation of the webserver, php, msqyl, etc., which is new to me as I’m used to doing everything through Plesk.

I’ve been able install and configure everything with no obvious problems. I can access the frontend/web/index.php and everything looks fine. I can even get Yii formatted errors.

However, if I click on any link, I get what appears to be an Apache error, NOT FOUND. This goes for static or dynamic pages. Also, I don’t have access to gii or the debug toolbar, even though I’ve enabled those in the configs.

When I run requirements.php, it appears that everything I need is installed.

What could possibly be going wrong here? I’m suspecting my Apache setup, but what could it be?

I’ve been able install and configure everything with no obvious problems. I can access the frontend/web/index.php and everything looks fine. I can even get Yii formatted errors.

What do the URL look like here?

However, if I click on any link, I get what appears to be an Apache error, NOT FOUND

And what’s the URL here?

Comparing both working and non working cases will help you find the solution.

Could you tell us more about your apache setup ? What does your vhost look like ?

The homepage is example.com.
Any other url I try, such as example.com/terms or example.com/site/settings gives the error. The url rules work fine on two other servers. I don’t think the url rules are even being applied in this case.

example.com.conf:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/html/frontend/web
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<Directory /var/www/example.com/html/frontend/web>
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

example.com-le-ssl.conf:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/html/frontend/web
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.example.com
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>

That’s a great idea. Actually I’m having a hard time finding my virtual host configuration at the command line on the other servers as those were created through plesk. The etc/apache2/sites-available doesn’t list my other domains, so I’m not sure where they’ve stashed that.

Indeed, you seem to have misconfigured the rewrite rules.
Try this:

RewriteEngine on
    # If a directory or a file exists, use the request directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Otherwise forward the request to index.php
    RewriteRule . index.php

Instead of:

RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI...
1 Like

hhhm…I made those updates and restarted apache, but still get the same 404 error.

Actually, the whole file structure looks odd:

  • ServerAlias is a duplication of ServerName, so not needed
  • Directory should be inside the VirtualHost section

Here’s how I usually lay out my vhosts:

<VirtualHost *:80>
    ServerName example.com
    ServerAdmin admin@example.com

    DocumentRoot /var/www/example.com/html/frontend/web
    <Directory /var/www/example.com/html/frontend/web>
        Options FollowSymLinks
        AllowOverride None
        Require all granted

        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
    </Directory>

    ErrorLog  ${APACHE_LOG_DIR}/example.com.frontend.error.log
    CustomLog ${APACHE_LOG_DIR}/example.com.frontend.access.log combined
</VirtualHost>
1 Like

PS: You are accessing the site through http, not https, right? Or else you will need to adapt your SSL vhost as well.

I’m accessing through https. I thought example.com-le-ssl.conf took care of that.

Yes, example.com-le-ssl.conf is indeed your https vhost, but it’s missing the apache Rewrite rules.

So when you access example.com/, apache looks for index.html,index.php,… inside your document root. It finds Yii index.php and is able to show the home page.

But for example.com/foobar, it looks for the /var/www/example.com/html/frontend/web/foobar/ directory and doesn’t find it. That’s what the 4 lines starting at RewriteEngine on are all about, they tell Apache: If you don’t find the requested file, or folder, then uses frontend/web/index.php

Here the new example.com-le-ssl.conf file:

<VirtualHost *:443>
    ServerName example.com
    ServerAdmin admin@example.com

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

    DocumentRoot /var/www/example.com/html/frontend/web
    <Directory /var/www/example.com/html/frontend/web>
        Options FollowSymLinks
        AllowOverride None
        Require all granted

        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
    </Directory>

    ErrorLog  ${APACHE_LOG_DIR}/example.com.frontend.error.log
    CustomLog ${APACHE_LOG_DIR}/example.com.frontend.access.log combined
</VirtualHost>
2 Likes

Great. That was it. I was following the digital ocean docs and missed some important details. Thank you much.

1 Like

This works for me too, at the same scenario, thanks