Apache redirects to wrong virtual host - apache

I am trying to set up two virtual hosts, one for my website and another for my nextcloud instance. This is what my config files look like:
/etc/apache2/site-available/000-default.conf
<IfModule mod_ssl.c>
<VirtualHost _default_:80>
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot "/var/www/html"
CustomLog /var/log/apache2/nc-access.log combined
ErrorLog /var/log/apache2/nc-error.log
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>
<Directory /var/www/html/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dav.c>
Dav off
</IfModule>
LimitRequestBody 0
SSLRenegBufferSize 10486000
</Directory>
</IfModule>
/etc/apache2/site-available/nextcloud.conf
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerName nextcloud.domain.com
DocumentRoot "/var/www/nextcloud"
CustomLog /var/log/apache2/nc-access.log combined
ErrorLog /var/log/apache2/nc-error.log
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>
<Directory /var/www/nextcloud/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dav.c>
Dav off
</IfModule>
LimitRequestBody 0
SSLRenegBufferSize 10486000
</Directory>
</IfModule>
Output of apache2ctl -S, showing the activated hosts:
VirtualHost configuration:
*:80 domain.com (/etc/apache2/sites-enabled/000-default.conf:2)
*:443 nextcloud.domain.com (/etc/apache2/sites-enabled/nextcloud.conf:2)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
When I enter domain.com in my browser, I am expecting it to load /var/www/html, my website. However, it loads my nextcloud (/var/www/nextcloud) instance. What do I do to make domain.com redirect to my website instead of my nextcloud instance?

You should modify your configurations like this (explanation below):
/etc/apache2/site-available/000-default.conf
<VirtualHost _default_:80>
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot "/var/www/html"
CustomLog /var/log/apache2/80_nc-access.log combined
ErrorLog /var/log/apache2/80_nc-error.log
<Directory /var/www/html/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dav.c>
Dav off
</IfModule>
LimitRequestBody 0
</Directory>
</VirtualHost>
/etc/apache2/site-available/nextcloud.conf
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerName nextcloud.domain.com
DocumentRoot "/var/www/nextcloud"
CustomLog /var/log/apache2/443_nc-access.log combined
ErrorLog /var/log/apache2/443_nc-error.log
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
<Directory /var/www/nextcloud/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dav.c>
Dav off
</IfModule>
LimitRequestBody 0
SSLRenegBufferSize 10486000
</Directory>
</VirtualHost>
</IfModule>
Changes
In your :80 VirtualHost, you should not have any SSL directives. Port 80 is for http:// connections. So I remove everything that had to do with SSL.
In your :443 VirtualHost, there you want to have your SSL directives. So keep those.
<Directory> directives should be included in the <VirtualHost> sections. These only make sense in the context of your <VirtualHost> anyway. There should be a generic <Directory> in httpd.conf that blocks direct access to all directories anyway.
I suggest you use separate log files for each <VirtualHost>. It is a good habit to have, trust me I have had Apache server with 20 hosts, it is impossible to figure out which log entry is for which. Also you might want to put your SSL in Debug log to troubleshoot something...
To reach the first <VirtualHost>, enter http://example.com
To reach the second <VirtualHost>, enter https://example.com
There is no redirection here. Just <Virtualhost> names and ports assignment. Apache looks at what you asked for (the site in the address bar of your browser) and uses the <VirtualHost> that best matches your request. If it cannot find a specific one, it will use the first one by default.
That explains why if you try https://example.com, Apache will select the <VirtualHost> based on the port. Here it is port 443 (https://). So Apache will respond with your cloud <VirtualHost>. The port is the deciding factor here, not the domain name.

Related

VirtualHost with two domains doesn't work

I can't configure two domains with VirtualHost, because, always show me only one domain. I mean, if I visit domainB.com that redirect me to domainA.com and domainA.com works fine. I made this procedure in anothers hosts like: DigitalOcean or my own localhost, but with this instance I can't find the issue. If I disable one site the other works without problem.
I have a google instance with Ubuntu 17.10 and Apache2 with two domains (virtualhost). I disabled the default virtualhost. My folder structure is /var/www/domainA/ and /var/www/domainB/
Cloud DNS is configured with two zones domainA.com and domainB.com with A and CNAME records point to my server IP. I have too NS records pointed to googledomains.com and in my registar I added those DNS.
If I do a ping to both domains, them resolve correctly to my IP address without problem.
I have the two domains configured in sites-available:
#in /etc/apache/sites-available/domainA.com.conf
<VirtualHost *:443>
ServerAdmin webmaster#domainA.com
ServerName domainA.com
ServerAlias www.domainA.com
DocumentRoot /var/www/domainA
ErrorLog ${APACHE_LOG_DIR}/error-domainA.log
CustomLog ${APACHE_LOG_DIR}/access-domainA.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/sslcert/certified-number.crt
SSLCertificateKeyFile /etc/apache2/sslcert/domainA.com.key
SSLCertificateChainFile /etc/apache2/sslcert/gd_bundle-g2-g1.crt
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /var/www/domainA/>
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
#in /etc/apache/sites-available/domainB.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#domainB.com
ServerName domainB.com
ServerAlias www.domainB.com
DocumentRoot /var/www/domainB
ErrorLog ${APACHE_LOG_DIR}/error-domainB.log
CustomLog ${APACHE_LOG_DIR}/access-domainB.log combined
<Directory /var/www/domainB/>
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
Result of apachectl -S command:
sudo apache2ctl -S
VirtualHost configuration:
*:80 domainB.com (/etc/apache2/sites-enabled/domainB.conf:1)
*:443 domainA.com (/etc/apache2/sites-enabled/domainA.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
What other setting I must to check?
UPDATE INFORMATION: If I try with www for the domainA, works fine.
www.domainA.com -> Works
domainA.com -> Doesn't work, redirect to domainB.com
www.domainB.com -> Works
domainB.com -> Works
I had only a VirtualHost configuration in the port 443 for domainA.com.
I added a configuration *:80 with a redirection to ssl and works. Like this:
<VirtualHost *:80>
ServerAdmin webmaster#domainA.com
ServerName domainA.com
ServerAlias www.domainA.com
Redirect / https://www.domainA.com/
</VirtualHost>
Thanks Matt Raines for his comment.

httpd.conf file vhost settings

I try to fix httpd.conf file vhost settings, however there is not much information on web to find on my case. When apaceh updated on my server, httpd.conf file settings are changing. Now my website gives /cgi-sys/defaultwebpage.cgi error. You can check with biletme.com. Setting are like this on this file: (Root of website is home/wwwbiletme/public_html)
##################################################
#
# Define default vhosts for shared IPs
#
##################################################
##################################################
<VirtualHost 95.179.142.199:80>
ServerName 95.179.142.199
ServerAlias www.biletme.com
DocumentRoot /var/www/html
ServerAdmin ************
<Directory "/var/www/html">
AllowOverride All
</Directory>
<IfModule suphp_module>
suPHP_UserGroup nobody nobody
</IfModule>
</VirtualHost>
##################################################
##################################################
#
# Define default vhosts for unbound IPs
#
##################################################
##################################################
<VirtualHost *>
ServerName www.biletme.com
DocumentRoot /var/www/html
ServerAdmin ***********
<Directory "/var/www/html">
AllowOverride All
</Directory>
<IfModule suphp_module>
suPHP_UserGroup nobody nobody
</IfModule>
</VirtualHost>
##################################################

Multiple SSL wildcards on 1 IP

Currently my server has 1 website running on https/ssl. The thing is when i enable a second vhost, also with https/ssl, the first website I have running is now using the ssl cert of the new website.
I have tried putting the two websites in a single vhost file, didn't work so I made 2 seperate files instead.
Here are my vhost config files:
(Naming them websiteZ and website Y because of alfabetical order they are in)
vhost current running website .conf
<VirtualHost *:80>
ServerAlias *.websiteZ.nl
Redirect 301 / https://websiteZ.nl
</VirtualHost>
NameVirtualHost *:443
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.websiteZ.nl
DocumentRoot "/var/www/html/websites/websiteZ.nl/public"
<Directory "/var/www/html/websites/websiteZ.nl/public">
Require all granted
Options Includes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/websiteZ.nl/certificate.crt
SSLCertificateKeyFile /etc/apache2/ssl/websiteZ.nl/certificate.key
SSLCertificateChainFile /etc/apache2/ssl/websiteZ.nl/cabundle.crt
</VirtualHost>
</IfModule>
new website with ssl .conf
<VirtualHost *:80>
ServerName websiteY.nl
ServerAlias www.websiteY.nl
RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]
DocumentRoot "/var/www/html/websites/websiteY.nl/public/"
<Directory "/var/www/html/websites/websiteY.nl/public/">
Require all granted
Options Includes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.websiteY.nl
DocumentRoot "/var/www/html/websites/websiteY.nl/public"
<Directory "/var/www/html/websites/websiteY.nl/public">
Require all granted
Options Includes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
SSLStrictSNIVHostCheck on
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/websiteY.nl/certificate.crt
SSLCertificateKeyFile /etc/apache2/ssl/websiteY.nl/certificate.key
SSLCertificateChainFile /etc/apache2/ssl/websiteY.nl/cabundle.crt
</VirtualHost>
</IfModule>
ports.conf
NameVirtualHost *:80
NameVirtualHost *:443
Listen 80
<IfModule mod_ssl.c>
Listen 443
</IfModule>
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
I looked up the SNI thing, but I think i'm missing something. The way I understand it is that I have to use NameVirtualHost to make it work.
The server is running on AWS ece2 with Ubuntu 16.04.2
The problem occors when i type in terminal:
a2ensite websiteY.conf
When I do that websiteZ will lose it's https cert and will show a big red cross wich says: NOT SECURE! When you click to proceed it links to websiteY
I am a little bit out of options, can someone help me out? Thanks!
When you enter www.websiteZ.nl without https, the request will first be caught by
<VirtualHost *:80>
ServerAlias *.websiteZ.nl
Redirect 301 / https://websiteZ.nl
</VirtualHost>
and therefore redirected to https://websiteZ.nl
Since none of your :443 Virtual Hosts has neither ServerName or ServerAlias configured with websiteZ.nl, then the one from alphabetically first .conf file will be used, which is in this case the one with websiteY cert.

Laravel - the requested url cannot be found on this server

I have deployed my project on linode and configured everything correctly but I find the homepage the only thing to be working. Anything else is giving me The requested url can not be found on this server. I have check all my configurations and everything seems to be correct. a2enmod rewrite is enabled and this 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
<Directory /var/www>
Options Indexes FollowSymLinks
AllowOverride All
RewriteEngine On
RewriteBase /var/www/application
Require all granted
</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
What else is missing?
Edit:
VirtualHost configuration:
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: ENABLE_USR_LIB_CGI_BIN
User: name="www-data" id=33
Group: name="www-data" id=33
You need to set document root to the public folder of your application like so:
"var/www/your-site.com/public"
Try follow my config:
<VirtualHost *:80>
ServerName yourproject.com
ServerAlias www.yourproject.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/yourproject/public
<Directory "/var/www/yourproject/public">
Require all granted
Allowoverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Turns out the .htaccess was missing so that was the reason. I have added it and it is working normally.

Name and IP Virtual Host on Apache

I am trying to direct web traffic for our domain to a Vhost site and our internal web site to separate directory that is only accessible within our private network,i.e 192.168.x.x.
I have modified Vhost configuration on Apache to include a name-based Vhost for the external website and a IP Vhost for the Intranet. No far I have had no luck, Apache does not like it.
Here is my modified Vhost config file.
NameVirtualHost *:80
<Directory "/home/webs">
Options +FollowSymLinks +Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot /home/webs/foo
ServerName www.foo.com
ServerAlias foo.com
LogLevel warn
ErrorLog /home/webs/foo/error.log
CustomLog /home/webs/foo/logs/access.log combined
</VirtualHost>
NameVirtualHost 192.168.0.*:80
<Directory "/home/webs/OffCat">
Options +FollowSymLinks +Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost 192.168.0.*:80>
DocumentRoot /home/webs/OffCat
ServerName 192.168.0.15/OffCat
LogLevel warn
ErrorLog /home/webs/OffCat/logs/error.log
CustomLog /home/webs/OffCat/logs/access.log combined
</VirtualHost>
I would appreciate any help.
Thanks,
Tony Cripps