< Previous | Contents | Next >
1.2.1. Basic Settings
This section explains Apache2 server essential configuration parameters. Refer to the Apache2 Documentation3 for more details.
• Apache2 ships with a virtual-host-friendly default configuration. That is, it is configured with a single default virtual host (using the VirtualHost directive) which can be modified or used as-is if you have a single site, or used as a template for additional virtual hosts if you have multiple sites. If left alone, the default virtual host will serve as your default site, or the site users will see if the URL they enter does not match the ServerName directive of any of your custom sites. To modify the default virtual host, edit the file
/etc/apache2/sites-available/000-default.conf.
The directives set for a virtual host only apply to that particular virtual host. If a directive is set server-wide and not defined within the virtual host settings, the default setting is used. For
example, you can define a Webmaster email address and not define individual email addresses for each virtual host.
If you wish to configure a new virtual host or site, copy that file into the same directory with a name you choose. For example:
3 http://httpd.apache.org/docs/2.4/
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ mynewsite.conf
Edit the new file to configure the new site using some of the directives described below.
• The ServerAdmin directive specifies the email address to be advertised for the server's administrator. The default value is webmaster@localhost. This should be changed to an email address that is delivered to you (if you are the server's administrator). If your website has a problem, Apache2 will display an error message containing this email address to report the problem to. Find this directive in your site's configuration file in / etc/apache2/sites-available.
• The Listen directive specifies the port, and optionally the IP address, Apache2 should listen on. If the IP address is not specified, Apache2 will listen on all IP addresses assigned to the machine it runs on. The default value for the Listen directive is 80. Change this to 127.0.0.1:80 to cause Apache2 to listen only on your loopback interface so that it will not be available to the Internet, to (for example) 81 to change the port that it listens on, or leave it as is for normal operation. This directive can be found and changed in its own file, /etc/apache2/ports.conf
• The ServerName directive is optional and specifies what FQDN your site should answer to. The default virtual host has no ServerName directive specified, so it will respond to all requests that do not match a ServerName directive in another virtual host. If you have just acquired the domain name ubunturocks.com and wish to host it on your Ubuntu server, the value of the ServerName directive in your virtual host configuration file should be ubunturocks.com. Add this directive to the new virtual host file you created earlier (/etc/apache2/sites-available/mynewsite.conf).
You may also want your site to respond to www.ubunturocks.com, since many users will assume the www prefix is appropriate. Use the ServerAlias directive for this. You may also use wildcards in the ServerAlias directive.
For example, the following configuration will cause your site to respond to any domain request ending in
.ubunturocks.com.
ServerAlias *.ubunturocks.com
• The DocumentRoot directive specifies where Apache2 should look for the files that make up the site. The default value is /var/www/html, as specified in /etc/apache2/sites-available/000-default.conf. If desired, change this value in your site's virtual host file, and remember to create that directory if necessary!
Enable the new VirtualHost using the a2ensite utility and restart Apache2:
sudo a2ensite mynewsite
sudo systemctl restart apache2.service
Be sure to replace mynewsite with a more descriptive name for the VirtualHost. One method is to name the file after the ServerName directive of the VirtualHost.
Similarly, use the a2dissite utility to disable sites. This is can be useful when troubleshooting configuration problems with multiple VirtualHosts:
sudo a2dissite mynewsite
sudo systemctl restart apache2.service