2. Configuration
July 30th, 2010 by jde
Basic configuration
In the early days there was only one configuration file for Apache (httpd.conf). Over time it has been split into several files, each containing a specific category of directives (httpd-vhosts.conf, httpd-ssl.conf etc, etc). The original httpd.conf is still available at the root of the Apache configuration directory /usr/local/etc/apache22. The others are located in the /extra folder.
Open /usr/local/etc/apache22/httpd.conf and change the following values:
ServerAdmin your_email@address.tld ServerName www.address.tld:80 ServerRoot "/usr/local" DocumentRoot "/usr/local/www" Directory "/usr/local/www" AllowOverride None DirectoryIndex index.php index.html
ServerAdmin: Apache will display this address in the browser if something goes wrong.
ServerName: This is the site that will be displayed if none of the virtual hosts matches the request.
AllowOverride: If set to “None” any .htaccess files will be ignored. If you want maximum performance, you should avoid htaccess files and use the *.conf files instead.
DirectoryIndex: Add filenames which must be recognized as site index files. This is the file Apache will display if the request does not contain a specific page.
Virtual host configuration
The place to add virtual hosts is /usr/local/etc/apache22/extra/httpd-vhosts.conf:
The directives in httpd.conf are global… That is, they are implied for virtual hosts unless another value is specified.
Directives inside a virtual host overwrites any global directive and applies only to the virtual host where it is specified.
The following example is a namebased virtual host. Note the * in the first line. This is where you would put an IP address if you wanted to create an IP-based virtual host.
The former is most widely used since most people only have one IP address for the server.
<VirtualHost *:80> ServerAdmin webmaster@domain.tld DocumentRoot /usr/local/www/domain.tld ServerName www.domain.tld ErrorLog /usr/local/www/logs/www.domain.tld-error_log CustomLog /usr/local/www/logs/www.domain.tld-access_log common </VirtualHost>
This will tell Apache to look in /usr/local/www/domain.tld/ if a request is made for www.domain.tld. Also note ErrorLog and CustomLog – this is where error and access logs are written.
You can add as many virtual hosts as you like. Just enclose each host between a <VirtualHost *:80> and </VirtualHost>
It’s just as easy to create subdomains:
<VirtualHost *:80> ServerAdmin webmaster@example.tld DocumentRoot /usr/local/www/mysub.example.tld ServerName mysub.example.tld ErrorLog /usr/local/www/logs/mysub.example.tld-error_log CustomLog /usr/local/www/logs/mysub.example.tld-access_log common </VirtualHost>
Note: If you plan to use a subdomain for mail accounts, you must create an A record in DNS, unless the main domain is specified in DNS with a wildcard. You must also create an MX record pointing to the subdomain.
Don’t forget to create all the directories referenced in the virtual host (just the directories – you don’t need to create the log files in advance, Apache will create them the first time they are needed).
If you want to prevent directory listings in folders not containing an index file, add this to the virtual host:
<Directory /usr/local/www/domain.tld> Options -Indexes </Directory>
You must restart Apache to make any changes take effect.
# apachectl stop # apachectl start
… or
# apachectl restart
- No Comments »
- Posted in Apache

