When working with Apache on Ubuntu, many developers encounter this confusing situation:
# Check the files
ls -lh /etc/apache2/httpd.conf
ls -lh /etc/apache2/apache2.conf
You'll typically find httpd.conf
empty while apache2.conf
contains actual configuration. This isn't an error - it's by design in Debian/Ubuntu's Apache packaging.
Ubuntu (derived from Debian) organizes Apache configuration differently than other Linux distributions:
- apache2.conf: Main configuration file that includes all others
- httpd.conf: Historically main config, now maintained empty for backwards compatibility
- conf-available/: Stores available configuration fragments
- conf-enabled/: Contains symlinks to active configurations
For most modifications, you should:
- Create a new file in
/etc/apache2/conf-available/
:# Example: Create a custom config sudo nano /etc/apache2/conf-available/my-custom.conf
- Enable it using:
sudo a2enconf my-custom sudo systemctl reload apache2
Here's how to set up a virtual host properly in Ubuntu's structure:
# Create virtual host file
sudo nano /etc/apache2/sites-available/example.com.conf
# File content:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# Enable the site
sudo a2ensite example.com.conf
sudo systemctl reload apache2
File | Purpose | When to Modify |
---|---|---|
apache2.conf | Main configuration | Global server settings |
httpd.conf | Legacy empty file | Don't use |
conf-available/ | Available configurations | Most custom configs |
sites-available/ | Virtual host definitions | Website-specific settings |
- Never modify
httpd.conf
- it might be overwritten during updates - Use
a2enconf
anda2disconf
to manage configurations - Test configurations with
apache2ctl configtest
before reloading - For major changes, consider creating backup:
sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak
When facing problems, check the loading order:
# See how configurations are loaded
apache2ctl -S
# Check for syntax errors
apache2ctl configtest
Remember that Ubuntu's Apache reads files in this order:
1. apache2.conf
2. conf-enabled/*.conf (alphabetically)
3. sites-enabled/*.conf (alphabetically)
When working with Apache on Ubuntu/Debian systems, you'll notice a different configuration structure compared to other Linux distributions. Here's what you need to know:
# Typical Ubuntu Apache configuration files: /etc/apache2/ ├── apache2.conf # Main configuration file ├── httpd.conf # Traditionally empty (legacy) ├── ports.conf # Port settings ├── conf-available/ # Available configuration snippets ├── conf-enabled/ # Enabled configuration snippets ├── mods-available/ # Available module configurations ├── mods-enabled/ # Enabled modules ├── sites-available/ # Available virtual hosts └── sites-enabled/ # Enabled virtual hosts
The empty httpd.conf
file exists primarily for backward compatibility. In Ubuntu's Apache package, the main configuration was moved to apache2.conf
to implement a more modular approach.
This design allows for:
- Better organization through configuration snippets
- Easier package management
- Clear separation between base configuration and site-specific settings
For most configurations, you should use these files:
# Global settings (e.g., Timeout, KeepAlive, etc.) /etc/apache2/apache2.conf # Virtual host configurations (recommended approach) /etc/apache2/sites-available/your-site.conf # To enable a site: sudo a2ensite your-site.conf sudo systemctl reload apache2
Here are some common configuration scenarios:
Changing the ServerName:
# In /etc/apache2/apache2.conf or a virtual host file ServerName example.com ServerAdmin webmaster@example.com
Setting up a Virtual Host:
<VirtualHost *:80> ServerName example.com DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com_error.log CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined </VirtualHost>
- Always create new configurations in
sites-available
rather than modifying the main files - Use
a2ensite
anda2dissite
to manage configurations - Test configurations before applying:
sudo apache2ctl configtest
- Consider using
.conf
include files for complex setups
Remember that after any configuration changes, you'll need to reload Apache:
sudo systemctl reload apache2 # Or for older systems: sudo service apache2 reload
If you're having trouble with your Apache configuration:
# Check for syntax errors sudo apache2ctl configtest # View full configuration (helpful for debugging) apache2ctl -S # Check which files are being loaded apache2ctl -t -D DUMP_INCLUDES