How to debug an apache virtual host configuration? - apache

Once again, I have a problem with my apache virtual host configuration. (The default configuration is used instead of my specific one).
The problem is not really the misconfiguration but how to solve it.
Does anyone has good advices to do resolve this kind of problem quickly?
Some more informations.
The default conf file is this one:
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
And the virtual host config that doesn't apply is this one:
<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://ip.ip.ip.ip:8088/
ProxyPassReverse / http://ip.ip.ip.ip:8088/
ServerName wiki.mydomain.com
</VirtualHost>

Syntax check
To check configuration files for syntax errors:
# Red Hat-based (Fedora, CentOS), Arch-based and OSX
httpd -t
# Debian-based (Ubuntu)
apache2ctl -t
# MacOS
apachectl -t
List virtual hosts
To list all virtual hosts, and their locations:
# Red Hat-based (Fedora, CentOS), Arch-based and OSX
httpd -S
# Debian-based (Ubuntu)
apache2ctl -S
# MacOS
apachectl -S

Here's a command I think could be of some help :
apachectl -t -D DUMP_VHOSTS
You'll get a list of all the vhosts, you'll know which one is the default one and you'll make sure that your syntax is correct (same as apachectl configtest suggested by yojimbo87).
You'll also know where each vhost is declared. It can be handy if your config files are a mess. ;)

If you are trying to debug your virtual host configuration, you may find the Apache -S command line switch useful. That is, type the following command:
httpd -S
This command will dump out a description of how Apache parsed the configuration file. Careful examination of the IP addresses and server names may help uncover configuration mistakes. (See the docs for the httpd program for other command line options).

First check out config files for syntax errors with apachectl configtest and then look into apache error logs.

I had a new VirtualHost configuration file that was not showing when using the apachectl -S command. After much head scratching I realised that my file did not have suffix ".conf". Once I renamed the file with that suffix my Vhost started showing and working!

I recently had some issues with a VirtualHost. I used a2ensite to enable a host but before running a restart (which would kill the server on fail) I ran
apache2ctl -S
Which gives you some info about what's going on with your virtual hosts. It's not perfect, but it helps.

I found my own mistake, I did not add log file name:
ErrorLog /var/log/apache2
And this path:
Directory "/usr/share/doc/"
Did not contain website sources.
After I changed these two, all worked.
Interestingly, apache did not issue any errors, just did not open my website silently on my Mac OS Sierra.

a very important tool is
apachectl -t -D DUMP_INCLUDES
it showed me that the file that i was fixing and mending
was in fact included by a file that was itself not included.
thank everybody

Related

Apache 2 Forbidden 403 Error despite "Require all granted"

I am getting the apparently infamous apache 2 forbidden error #403, and I tried following the guides on the subject, but none seem to be working.
I am using Ubuntu Server and Apache 2.4.41
My website structure looks like /var/www/html/index.html
My apache2.conf [/etc/apache2/apache2.conf]:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/html>
Order allow,deny
Allow from all
Require all granted
</Directory>
My vhosts.conf [/etc/apache2/sites-available/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
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from 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>
From an Apache standpoint, your configuration seems ok.
Please verify the permissions on your /var/www/html directory. The user that runs Apache should have at least read and traversal (x) permission.
I gave the user running Apache2 permissions to the /var/www/html folder, and was then able to access the website.
Here is a URL with several permissions, I don't know specifically which one was responsible, but I believe it was sudo chmod g+w /var/www/html
https://askubuntu.com/questions/767504/permissions-problems-with-var-www-html-and-my-own-home-directory-for-a-website

Apache : how to make multiple subdomains and reverse proxy work together? [duplicate]

Once again, I have a problem with my apache virtual host configuration. (The default configuration is used instead of my specific one).
The problem is not really the misconfiguration but how to solve it.
Does anyone has good advices to do resolve this kind of problem quickly?
Some more informations.
The default conf file is this one:
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
And the virtual host config that doesn't apply is this one:
<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://ip.ip.ip.ip:8088/
ProxyPassReverse / http://ip.ip.ip.ip:8088/
ServerName wiki.mydomain.com
</VirtualHost>
Syntax check
To check configuration files for syntax errors:
# Red Hat-based (Fedora, CentOS), Arch-based and OSX
httpd -t
# Debian-based (Ubuntu)
apache2ctl -t
# MacOS
apachectl -t
List virtual hosts
To list all virtual hosts, and their locations:
# Red Hat-based (Fedora, CentOS), Arch-based and OSX
httpd -S
# Debian-based (Ubuntu)
apache2ctl -S
# MacOS
apachectl -S
Here's a command I think could be of some help :
apachectl -t -D DUMP_VHOSTS
You'll get a list of all the vhosts, you'll know which one is the default one and you'll make sure that your syntax is correct (same as apachectl configtest suggested by yojimbo87).
You'll also know where each vhost is declared. It can be handy if your config files are a mess. ;)
If you are trying to debug your virtual host configuration, you may find the Apache -S command line switch useful. That is, type the following command:
httpd -S
This command will dump out a description of how Apache parsed the configuration file. Careful examination of the IP addresses and server names may help uncover configuration mistakes. (See the docs for the httpd program for other command line options).
First check out config files for syntax errors with apachectl configtest and then look into apache error logs.
I had a new VirtualHost configuration file that was not showing when using the apachectl -S command. After much head scratching I realised that my file did not have suffix ".conf". Once I renamed the file with that suffix my Vhost started showing and working!
I recently had some issues with a VirtualHost. I used a2ensite to enable a host but before running a restart (which would kill the server on fail) I ran
apache2ctl -S
Which gives you some info about what's going on with your virtual hosts. It's not perfect, but it helps.
I found my own mistake, I did not add log file name:
ErrorLog /var/log/apache2
And this path:
Directory "/usr/share/doc/"
Did not contain website sources.
After I changed these two, all worked.
Interestingly, apache did not issue any errors, just did not open my website silently on my Mac OS Sierra.
a very important tool is
apachectl -t -D DUMP_INCLUDES
it showed me that the file that i was fixing and mending
was in fact included by a file that was itself not included.
thank everybody

Stop Apache from serving content from apache root directory

I noticed this by chance earlier and after a quick Google, really couldn't find anything to help. I'm genuinely having trouble even explaining the problem!
When I access http://mydomain.com/error/README I appear to be getting the error readme found in /var/www/error/README. My virtual host for mydomain.com is document root is pointing to /var/www/html/mydomain, and /var/www/html/mydomain/error/ does not exist.
Naturally I feel this shouldn't be happening and although I haven't been able to replicate it, my main concern is that a user could in fact access anything off of /var/www/ not just /var/www/error/
Linux version: CentOS release 6.5 (Final)
Apache version: Apache/2.2.15
My virtual host file is:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /var/www/html/mydomain
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/html/mydomain/>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
LogLevel error
CustomLog /var/log/httpd/mydomain_access.log combined
ErrorLog /var/log/httpd/mydomain_error.log
</VirtualHost>
Naturally I feel this shouldn't be happening and although I haven't been able to replicate it, my main concern is that a user could in fact access anything off of /var/www/ not just /var/www/error/
You probably have the following directive in your httpd.conf:
Alias /error/ "/var/www/error/"
This just redirects all your error queries to /var/www/error (e.g. if you need www.example.com/error/README it would redirect me to one of the README in that director). Removing the alias should fix your problem.
If you are concerned about access to your other directories then I, as a complete newbie to apache, would recommend.
Auditing your httpd.conf and removing any aliases that you don't need.
Modifying your containers to have "Deny from all" and "AllowOverride None" in all of them.
If you have selinux enabled, then remove the httpd security contexts from those directories.

What is simplest way to add domain name to a Debian server?

I have the latest brand new Debian server. The website should host only 1 single domain name.
DNS are resolved by the registrar and A points to my dedicated server. Site is present in /var/www/.
What is the simplest way (step by step) to configure Apache to add this domain name?
It should be very straighforward:
Install Apache (sudo aptitude install apache2)
The default configuration points to /var/www so...
Start Apache (sudo service apache2 restart)
Enjoy your website
You can add your domain name into /etc/hosts
Take some steps to secure your dedicated server (iptables, fail2ban, ...)
You could also use the automatic wizard with sudo dpkg-reconfigure apache2.
I think this should be enough if you DNS is already pointing at your IP.
If you really need to change the configuration file, it's in etc/apache2/sites-available/default and contains the following (by default):
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster#localhost # <= Email of webadmin (shown on error pages)
DocumentRoot /var/www/ # <= Root of your web server with public access
<Directory />
Options FollowSymLinks
AllowOverride None # <= Disable usage of .htaccess files
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
#RedirectMatch ^/$ /apache2-default/
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
As for the hosts file, you can add the following:
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
#Virtual Hosts
12.34.56.789 example.com #<= change here :)
Then, don't forget to secure your web server with appropriate tools (iptables, fail2ban, ...)

Localhost Permissions denied after upgrade to Mavericks

Upgraded to Mavericks and edited httpd.conf to enable php and virtual hosts. That's the only change made. Now when I try to access localhost I get the error: Forbidden - you don't have permission to access xxxxx
Any ideas how to fix this?
The problem results from php not being executed any more, try to call e.g. the index.php directly and you will very likely see the sourcecode of your file.
go to /etc/apache2/httpd.conf and uncomment:
LoadModule php5_module libexec/apache2/libphp5.so
you have to restart your apache afterwards, so execute on command line:
sudo apachectl -k restart
Following steps made it work for me:
Add an error_log path to your misbehaving vhost entries, so you can
track the problem more closely
<VirtualHost *>
...
ErrorLog "/accessible/path/to/error_log"
</VirtualHost>
Restore all your previous apache settings, to be found at /etc/apache2/httpd.conf~previous
Make sure your chmod and chown rights are up to date
Alter documentRoot settings if necessery (adding + did the trick for me)
<Directory "/path/to/webroot">
Options +Indexes +FollowSymLinks +SymLinksIfOwnerMatch +MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Optionally, add following entry to your .htaccess file
Options +Indexes +FollowSymLinks +SymLinksIfOwnerMatch +MultiViews
Extra hint
Incase you are using php, restore your ini file by copying php.ini-5.2-previous to php.ini
$ cd /private/etc/
$ sudo cp php.ini-5.2-previous php.ini
Sources
http://brianflove.com/2013/10/23/os-x-mavericks-and-apache/
http://jason.pureconcepts.net/2012/10/install-apache-php-mysql-mac-os-x/
Options FollowSymLinks or SymLinksIfOwnerMatch is off