sodcalculator

10 Tech How-To

Install and Configure Apache on Ubuntu

Guide 10-44304 · Updated · Step by step

To install and configure Apache on Ubuntu, use the command sudo apt install apache2 and then edit configuration files in /etc/apache2/. This guide walks through the full process, from installation to virtual hosts, with troubleshooting tips for common issues.

Prerequisites and Background

Apache HTTP Server, commonly called Apache, is one of the most widely used open-source web servers. It runs on Linux, Windows, and macOS, but is especially popular on Ubuntu due to its stability and ease of management through the apt package manager. Before you begin, you need:

  • An Ubuntu server or desktop (18.04 or newer is recommended)
  • Access to a user account with sudo privileges
  • An internet connection to download packages
  • A firewall (like UFW) if you plan to serve content publicly

Apache is often chosen because it is modular, well-documented, and has a large community. It can serve static files, host dynamic applications (via PHP, Python, etc.), and act as a reverse proxy.

Step 1: Update System Packages

Always update the package index before installing new software. This ensures you get the latest available versions and avoids dependency conflicts.

  1. Open a terminal (Ctrl+Alt+T).
  2. Run sudo apt update to refresh the package list.
  3. Optionally run sudo apt upgrade to upgrade existing packages. This can take a while but is recommended.

Updating is a good habit because Ubuntu’s repositories change frequently, and a stale index can lead to installation errors.

Step 2: Install Apache

Install the apache2 package using the following command:

sudo apt install apache2 -y

The -y flag automatically answers “yes” to prompts. Once installed, Apache is usually started automatically. To verify it is running, use:

sudo systemctl status apache2

You should see active (running) in the output. If it’s not running, start it with sudo systemctl start apache2.

To ensure Apache starts on boot, run sudo systemctl enable apache2. This is especially important for servers.

Step 3: Adjust Firewall Settings

If you have UFW (Uncomplicated Firewall) enabled, you need to allow web traffic. Apache registers a few profiles with UFW:

  • Apache – opens port 80 (HTTP)
  • Apache Full – opens both 80 and 443 (HTTPS)
  • Apache Secure – opens only 443 (HTTPS)

For a basic setup, allow HTTP and HTTPS:

sudo ufw allow 'Apache Full'

Check the status with sudo ufw status. If you don’t use UFW, you can skip this step, but remember to configure your cloud provider’s security groups if applicable.

Step 4: Test the Default Installation

Open a web browser and visit http://your_server_ip (or http://localhost on your local machine). You should see the default Ubuntu Apache welcome page. This page confirms that the server is working and shows you the location of key files.

If you don’t see the page, check that Apache is running and that the firewall allows port 80. You can also try curl http://localhost from the terminal to see the HTML output.

Step 5: Configure Virtual Hosts

Virtual hosts allow you to run multiple websites on one server. The default site is defined in /etc/apache2/sites-available/000-default.conf. To create your own site, follow these steps:

  1. Create a directory for your site: sudo mkdir /var/www/example.com (replace with your domain).
  2. Set ownership: sudo chown -R $USER:$USER /var/www/example.com
  3. Create a sample HTML file: nano /var/www/example.com/index.html and add some content.
  4. Create a new virtual host file: sudo nano /etc/apache2/sites-available/example.com.conf
  5. Paste the following configuration (adjust paths and domain):
<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  1. Enable the site: sudo a2ensite example.com.conf
  2. Disable the default site (optional): sudo a2dissite 000-default.conf
  3. Test the configuration: sudo apache2ctl configtest
  4. Reload Apache: sudo systemctl reload apache2

Now your domain should serve the content from its own directory.

Step 6: Manage Apache Modules and Settings

Apache is modular. You can enable or disable modules to add functionality. For example, to enable the rewrite module (useful for SEO-friendly URLs), run:

sudo a2enmod rewrite

Other common modules include ssl for HTTPS, proxy for reverse proxy, and headers for security headers. After enabling a module, reload Apache.

Main configuration files:

  • /etc/apache2/apache2.conf – global settings
  • /etc/apache2/ports.conf – listening ports
  • /etc/apache2/sites-available/ – virtual host definitions
  • /etc/apache2/mods-available/ – available modules

Always run sudo apache2ctl configtest after editing configuration files to catch syntax errors before reloading.

Common Problems and Solutions

Port 80 Already in Use

If you get an error like Address already in use, another process (often Nginx) is using port 80. You can either stop that service or change Apache’s port in ports.conf.

403 Forbidden Error

This usually happens when the DocumentRoot directory has incorrect permissions. Ensure the directory is readable by Apache: sudo chmod -R 755 /var/www/example.com and that the owner is correct.

Virtual Host Not Working

Check that the site is enabled (a2ensite) and that your domain’s DNS points to your server. For local testing, you can edit /etc/hosts to map a domain to 127.0.0.1.

Apache Won’t Start After Configuration Change

Run sudo apache2ctl configtest to see the exact error. Often it’s a missing closing tag or a typo in a file path.

Frequently Asked Questions

How do I start, stop, or restart Apache?

Use sudo systemctl start apache2, sudo systemctl stop apache2, and sudo systemctl restart apache2. To reload configuration without dropping connections, use sudo systemctl reload apache2.

Where are Apache log files?

By default, logs are in /var/log/apache2/. Access logs are in access.log and errors in error.log. You can view them with tail -f /var/log/apache2/error.log.

How do I enable HTTPS?

Install Certbot and the Apache plugin: sudo apt install certbot python3-certbot-apache, then run sudo certbot --apache. This will automatically configure SSL certificates from Let’s Encrypt.

Can I run Apache and Nginx together?

Yes, but they cannot both listen on the same port (80 or 443). You can run Apache on port 8080 and Nginx on 80, then use Nginx as a reverse proxy, or vice versa. This is common for complex setups.

How do I change the default web root?

Edit the DocumentRoot directive in your virtual host file. For example, change /var/www/html to /var/www/example.com. Then reload Apache.

Why is my server slow?

Possible reasons include insufficient RAM, too many concurrent connections, or unoptimized modules. Check top for resource usage and apache2ctl -M to list loaded modules. Disable any you don’t need.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →