Apache vhost loads incorrect doc root - apache

My issue is that Apache randomly loads a non-default vhost instead of the domain I ask it to. I've tried just about every solution I found on this site and through searching but nothing seems to fix this problem.
Background:
This is a virtual server hosted on a popular cloud platform
The global apache conf file contains no VirtualHost definitions and the config file below is included properly (Include sites-enabled/*.conf)
Apache 2.4 on Ubuntu 16.04
I had each domain in their own conf file but that did not work either.
Apache has chosen domain3.com as the default for some reason.
When I execute curl localhost on the server, it returns the contents of domain3.com.
The only thing excluded from the vhost below are some SSL definitions that don't seem to be causing any problems.
ServerName is defined as localhost in apache.conf
The vhosts below are saved at /etc/apache2/sites-enabled/000-default.conf
Server weirdness?
I can't seem to actually stop apache. Executing service apache2 stop and then service apache2 status will identify the process as inactive, but I can still load pages under domain1/2/3.com. It's like nothing I do has any effect.
There's like 3 ways to access apache now. Some seem to work, some don't. /etc/init.d/apache2, service apache2, apache2ctl.
It only loads the wrong site about 50% of the time.
I'm completely out of ideas...
NameVirtualHost *:80
# domain1.com
<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.domain1.com
ServerAlias domain1.com
UseCanonicalName Off
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain1.com/
<Directory /var/www/domain1.com>
Options Indexes FollowSymLinks
AllowOverride All
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>
# domain3.com
<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.domain3.com
ServerAlias domain3.com
ServerAlias domain3.net
ServerAlias www.domain3.net
UseCanonicalName Off
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain3.com/
<Directory /var/www/domain3.com>
Options Indexes FollowSymLinks
AllowOverride All
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}/cq2_error.log
CustomLog ${APACHE_LOG_DIR}/cq2_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>
# domain2.com
<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.domain2.com
ServerAlias domain2.com
UseCanonicalName Off
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain2.com/
<Directory /var/www/domain2.com>
Options Indexes FollowSymLinks
AllowOverride All
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>
And my apache2ctl -S output:
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:1
VirtualHost configuration:
*:80 is a NameVirtualHost
default server www.domain1.com (/etc/apache2/sites-enabled/000-default.conf:4)
port 80 namevhost www.domain1.com (/etc/apache2/sites-enabled/000-default.conf:4)
alias domain1.com
port 80 namevhost www.domain3.com (/etc/apache2/sites-enabled/000-default.conf:54)
alias domain3.com
alias domain3.net
alias www.domain3.net
port 80 namevhost www.domain2.com (/etc/apache2/sites-enabled/000-default.conf:95)
alias domain2.com
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/lock/apache2" mechanism=fcntl
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 not_used
Group: name="www-data" id=33 not_used

As far as Apache choosing a random site:
When you do curl localhost, it is likely that it sending out an HTTP header that has no Host: defined, as it behaves similarly to when you type the IP of the server directly in the browser. The Host: header in the HTTP request is the field that Apache matches against the ServerName and ServerAlias trying to find a match to load up the corresponding DocumentRoot.
So, in that case, if you don't have a default host setup, and an empty Host: comes in, then Apache will randomly choose a site (I have recreated this behavior on my dev server, just to verify).
It is better to set up 000-default.conf as your catch-all and decide what to do when that happens, like redirecting to one of your sites or to a simple php file that just echos something bland. Then set up separate conf files for each site just to stay organized.
If you have DNS records pointing to your server correctly, then the default will only trigger when bots come around testing IPs.

On Ubuntu 16.04 you have systemd .. so the right way to interact with apache is using systemctl <command> apache2
I suggest to kill every apache process and then try to start it again using systemctl start apache2 ..
Moreover, to be sure my default domain is loaded at first, I usually rename its configuration file as 000-domain.conf .. then add other domains, one per file, with no numerical prefix at all

Your configuration looks alright, the weirdness you seem to describe seem to be related to a external issue, being it distro scripts, or whatever..., so try to use only apachectl only to stop/start, if the distro hasn't changed it much is the most "official" method you will find to manage your httpd server start/stop, etc, plus it is the only thing you have now giving you correct information
Also, In your configuration domain3.com would not ever be your default virtualhost, not even if you had defined your servernames incorrectly. So chances are you are seeing what other "service/server" is sending you.
Answer:
Use apachectl only until you fix your discro script issues

Related

IP Address showing instead of Domain apache server

I just purchased a domain from GoDaddy, let's call it example.com. I set up my A record and CNAME to properly point to my home IP address (see picture below) where I run an Apache2 server on my Raspberry Pi.
My 000-default.conf file is as follows:
<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
ServerAlias example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorDocument 404 /404.html
<Directory "/var/www/html">
AllowOverride All
Options All
Require all granted
# Any other directives
</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
I have searched the internet for hours and even asked another question on StackOverflow to where I thought the problem was resolved, however after about an hour of restarting the Apache2 service on the Raspberry Pi, it goes back to showing my home IP Address in the address bar instead of my newly purchased domain. I have tried restarting the service multiple times but I always end up with the same result. Flushing DNS on my devices does nothing, too. How can I make it so that it always shows my domain in the address bar?
Make sure that in your Domain provider (GoDaddy for example) that you aren't forwarding to your IP address. Instead, forward to your domain.

Apache does not pick up default index file when I enter domain name

I installed an ubuntu server 16.04 and LAMP, it works very well and I can load default web by using ip address.
Also I created a new virtual host by domain synappse.ir. If I enter synappse.ir/index.html, it loads the page and there is not any issue but when I enter just the name of the domain without index.php, it can not find or load the index.html file!
Here is my config file for virtual host :
<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 ServerNamei
# 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 nasser.man#gmail.com
ServerName synappse.ir
ServerAlias synappse.ir
DocumentRoot /var/www/synappse.ir
DirectoryIndex index.html
# 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
#<Directory /var/www/html>
# Options Indexes FollowSymLinks MultiViews
# AllowOverride All
# Require all granted
#</Directory>
# 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>
Is there any other configuration for enabling this feature?
Sorry, pages were cached, every thing is ok.

Openproject, apache and /var/www/html

I have an issue with Openproject, Apache and other apps that are installed in /var/www/html.
When I try to connect to any app in /var/www/html or even index.html it doesn't work because it looks like Openproject conf file is 'redirecting' traffic to another folder (openproject folder I guess).
Here is my Openproject.conf
Include /etc/openproject/addons/apache2/includes/server/*.conf
<VirtualHost *:80>
ServerName tools.mydomain.com
DocumentRoot /opt/openproject/public
ProxyRequests off
Include /etc/openproject/addons/apache2/includes/vhost/*.conf
# Can't use Location block since it would overshadow all the other proxypass directives on CentOS
ProxyPass /openproject/ http://127.0.0.1:6000/openproject/ retry=0
ProxyPassReverse /openproject/ http://127.0.0.1:6000/openproject/
</VirtualHost>
Here is the content of my 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
# 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
When I change the ServerName in the openproject.conf to anything else than
tools.mydomain.com I can access the apps in /var/www/html but I can't figure out how this works... I don't understand the way apache works like this..
Thanks for your help.
This is old thread but I had the same problem, as of the time I have installed OpenProject all my other VHOSTs stopped working. If I would disable OP, then all the rest worked properly.
I fixed it by changing OP ServerName to op-dev and port from *:80 to *:81, and one important place where this should also be done is within file:
/etc/openproject/addons/apache2/includes/server/20_repoman_svn_vhost.conf
Here you should also change :
"VirtualHost 127.0.0.1:80" to "VirtualHost *:80" and
ServerName can stay localhost.
Now, all VHOSTs are working properly.
Be careful with the port 80, because is very common for the most applications. First, try to reload apache.
sudo service apache2 reload
Next, see the output if this give error.
sudo journalctl -xe
If everything is ok, register the site
sudo a2ensite openproject
Reload apache again and add to /etc/hosts file the following line
127.0.1.1 tools.mydomain.com
Then, try to enter on http://tools.mydomain.com:[port]
All you need to do is fix that string:
DocumentRoot /opt/openproject/public
to point to youre root /var/www/html/
I had the same issue.
If no ServerName of a virtual host configuration matches the client request header, the first virtual host in the apache2 configuration will be served.
This is the default virtual host, because filename starts with 000.
If ServerName in openproject's virtual host configuration matches the client request header, default virtual host won't be served any more.

Set different data/folders for different domains on apache

So I have Ubuntu 16 installed with LAMP and couple of other things on it (like FTP server...).
IP of my VPS is 1.2.3.4.
I have 2 domains, example.com and mydomain.com - both of these domains have their A record pointed at 1.2.3.4
If I access 1.2.3.4 and example.com and mydomain.com in my browser, all those three are showing the SAME data, specifically the contents of /var/www/html - if I change content of this directory it affects example.com and mydomain.com and the 1.2.3.4.
Now how can I set additional folders for domains to read from? I want different data on example.com and on mydomain.com - I want to make them read from different folder on server. How can i setup this please?
What you want to achieve is technically defined as setting virtual hosts which could be set using the following steps:
Under your Apache configurations directory, usually at /etc/apache2/, you will find a directory named sites-enabled.
For each of your domains, you will need to configure a special configuration file in order to point them to the right direction. The name of the file is typically your-domain.conf.
Here's an example of the file:
<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 your_domain
ServerAlias www.your_domain
ServerAdmin webmaster#your_domain
DocumentRoot website_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
<Directory website_directory>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
After saving the file you will need to restart the Apache server
/etc/init.d/apache2 restart
Repeat the above steps for each of your domains.
If everything goes right, your site will show when you access your domain.

Apache works on localhost but not on server name

As my user id suggests, I am a novice programmer.
I am trying a simple Apache server configuration in ubuntu,
I have created below .conf file in /etc/apache2/sites-available/awesome.conf with below code. Also I have created a symbolic link in /etc/apache2/sites-enable and restarted the Apache.
<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.myawesome.org
ServerAdmin webmaster#localhost
DocumentRoot /var/www/awesome
DirectoryIndex hello.php
# 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>
when I access localhost/awesome/hello.php I get the desired text "Hello World".
But when I type www.myawesome.org it dispays below message:
"Server not found"
Any help/guidance is appreciated.
Regards....
You need to edit the hosts file and add the ServerName here pointing to localhost
Open hosts file with:
sudo nano /etc/hosts
...and at the end of this file add:
127.0.0.1 www.myawesome.org