When you install Apache2, it automatically sets up a default website or default virtual host, which serves content from a default directory. This default site is used to serve a webpage if the server receives a request without a specific domain or if no other virtual host matches the requested domain.
Here’s how the Apache2 default website works and how you can modify it.
Default Web Directory:
By default, Apache2 serves web pages from the /var/www/html/ directory. Any file you place in this directory (such as index.html) will be served by Apache2 when the server’s IP address or domain name is accessed.
The default webpage can be found at:
/var/www/html/index.html
You can replace the index.html file with your own HTML, PHP, or other web content to customize what is served.
Default Virtual Host Configuration:
The configuration file for the default site is typically stored at:
/etc/apache2/sites-available/000-default.conf
This file defines how the default website behaves and what content is served. By default, it looks something like this:
<VirtualHost *:80>
# The ServerAdmin directive sets the contact email address for the server.
ServerAdmin webmaster@localhost
# DocumentRoot defines the directory from which files are served.
DocumentRoot /var/www/html
# Log files to track errors and access.
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
*).Once Apache2 is installed, you can access the default site using the server’s IP address or the domain name (if DNS is set up).
By IP:
If your server’s IP address is 192.168.1.10, accessing http://192.168.1.10/ in a browser will load the default website from /var/www/html.
By Domain:
If the server’s domain is properly configured in DNS, accessing http://example.com/ will load the default website from the same directory.
To customize the default Apache2 website, you can edit the following:
Change the Web Content:
Modify the content in /var/www/html/. For example, replace index.html with your own HTML file, PHP script, or other content types.
sudo nano /var/www/html/index.html
Modify the Default Virtual Host Configuration:
Edit the 000-default.conf file to make changes such as pointing the DocumentRoot to another directory or setting up more advanced configurations like redirects or rewrites.
Example: Changing the document root to a different directory:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/mywebsite
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
After making any changes, reload Apache to apply the new configuration:
sudo systemctl reload apache2
If SSL (HTTPS) is enabled on the server, there is also a default SSL virtual host that listens on port 443. The configuration for this is found at:
/etc/apache2/sites-available/default-ssl.conf
This file defines the default behavior for HTTPS requests:
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</IfModule>
You can enable this SSL virtual host using:
sudo a2ensite default-ssl.conf
sudo systemctl reload apache2
To enable HTTPS, you will also need to configure valid SSL certificates (e.g., using Let’s Encrypt or self-signed certificates).
If you want to disable the default website (perhaps because you’re setting up multiple virtual hosts), you can disable the default configuration:
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
This will stop Apache from serving the default site. You can then set up other virtual hosts to serve different content.
If no index.html or similar file is found in the default directory, Apache will display a directory listing of the files in /var/www/html/. You can control this behavior with the Options directive in the virtual host configuration.
Allow Directory Listings:
<Directory /var/www/html>
Options Indexes FollowSymLinks
</Directory>
Disable Directory Listings:
To prevent Apache from showing directory listings, remove the Indexes option:
<Directory /var/www/html>
Options FollowSymLinks
</Directory>
The default Apache2 website is a simple, out-of-the-box configuration that serves content from /var/www/html/. You can customize the content and behavior of the default site by modifying files in this directory and editing the default virtual host configuration at /etc/apache2/sites-available/000-default.conf.
If you want to host multiple sites, you can disable the default site and create new virtual hosts for each domain or subdomain.