Virtual host returning 404

December 12, 2018 1.5k views
Ubuntu 16.04 Apache

Hi everyone!

I have vhost pointing to certain folder using the main domain and I’m setting up the second one, a subdomain, pointing to another folder.
The first one works fine since forever, and the second one never worked, always returning 404.
Funny thing is that if I call the IP address it loads the second vhost.

First vhost (working):

<VirtualHost *:80>
    ServerAdmin fechagas.dev@gmail.com
    ServerName cropflow.com.br
    ServerAlias www.cropflow.com.br
    DocumentRoot /var/www/html/cropflow

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =cropflow.com.br [OR]
RewriteCond %{SERVER_NAME} =www.cropflow.com.br
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Second vhos (return 404):

<VirtualHost *:80>
    ServerAdmin fechagas.dev@gmail.com
    ServerName api.cropflow.com.br
    ServerAlias www.api.cropflow.com.br
    DocumentRoot /var/www/html/cropflow-api/public

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =api.cropflow.com.br [OR]
RewriteCond %{SERVER_NAME} =www.api.cropflow.com.br
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Both vhosts was configured following this article:
https://www.digitalocean.com/community/tutorials/como-configurar-apache-virtual-hosts-no-ubuntu-16-04-pt

Thanks in advance!

3 Answers

what is the “public” in

DocumentRoot /var/www/html/cropflow-api/public

Are you sure it is in a public folder?

Also you could take out the redirect to https and test it on http only

  • The public folder contains the index.php.

    By asking if I’m sure that is in a public folder you mean it has the correct permissions? It has 755 permission and it is owned by the www-data user.

    Thanks, I removed the redirect rule and it worked.
    Turns out the problem is related with the HTTPS certificate, the site returns 404 when requested with HTTPS. I reinstalled the certificate following this article and the got the same result. Any advice?

    by Erika Heidi
    This tutorial will show you how to set up a free TLS/SSL certificate from Let’s Encrypt on a Ubuntu 16.04 server running Apache as web server. TLS certificates are used within web servers to encrypt the traffic between server and client, providing extra security for users accessing your application.

Your api.cropflow.com.brvhost does a redirect to https://api.cropflow.com.br. So you’d need to look at the SSL/433 vhost and see if it’s properly configured.

$ curl -I api.cropflow.com.br
HTTP/1.1 301 Moved Permanently
Date: Wed, 12 Dec 2018 19:40:01 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: https://api.cropflow.com.br/
Content-Type: text/html; charset=iso-8859-1

Yes I thought it might be the cert. And yes I do have some advice, lets encyrpt doesnt support virtual hosts in one file, so just use the working one and copy the contents too a second conf file and then run the certbot again

this might help

https://community.letsencrypt.org/t/setting-up-letsencrypt-for-a-single-wordpress-multisite-installation-with-many-domains/46511/6

It did with me

  • Thanks for the advice mate!
    I was already using different conf files without knowing lets encrypt doesn’t support all in one file.
    I actually don’t know what solved the issue, just decided to delete the second vhost file and the cert and start over. Worked.

Have another answer? Share your knowledge.