1

It's the first time that I try to configure apache so I hope you can help me.

This is what I want to achieve:

  • All undefined subdomains should lead to not found if they aren't defined
  • Root folder should never be accessible

Now this is what I have so far:

my.conf

ServerName server1 (is hostname)

ServerSignature off
ServerTokens prod

Options -Includes
Options -ExecCGI

sites-available/default

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /htdocs

    <Directory />
            Options None
            Order deny,allow
            Deny from all
    </Directory>

    <Directory /htdocs/>
            Options -Indexes -FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

sites-available/example

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /htdocs/example

    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Well, my questions are:

  • Does the "Directory /" from the default-config also apply on the example.com site? Or do I need to put it in there, too. Or should it rather be in the my.conf?
  • Same question for "Directory /htdocs/" - should I put this in the example.com-config, too? Of course like "Directory /htdocs/example/" then.
  • At the moment every subdomain of the example.com is leading to the default directory. How can I prevent that? Only the Server IP should lead there - if at all.
0

For undefined subdomains you could add a new virtualhost AFTER all known subdomain.example.com virtualhosts.

<VirtualHost *:80>
    ServerName *.example.com
    Redirect 404 /
</VirtualHost>

Redirect 404 / replies with "404 Not found" on every request.


The scope of configuration directives

Directives can be server wide or restricted to particular directories, files, virtualhosts etc. Directory, DirectoryMatch, Files, FilesMatch, If, IfDefine, IfModule, IfVersion, Location, LocationMatch, Proxy, ProxyMatch and VirtualHost are called Configuration Section Containers: directives within them only affects inside the container.

In the following example "Require all denied" affects on /htdocs/private.html and "Options -Indexes" affects to "/htdocs" but only within VirtualHost with ServerName example.com.

<VirtualHost *:80>
    ServerName example.com
    <Directory /htdocs/>
        Options -Indexes
        <Files private.html>
           Require all denied
        </Files>
    </Directory>
<VirtualHost *:80>
  • Thank you. That surely will work. Do you also have an answer to my other questions? – sleepless Mar 11 '15 at 22:21
  • I just added section about scope of configuration directives which should answer to rest of your questions. – Esa Jokinen Mar 11 '15 at 22:38
  • Okay, so if I got you right I have to put the "Directory /" to my.conf to apply it on everything. Then only put it in a virtual host if I want do override it, right? – sleepless Mar 12 '15 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.