sodcalculator

10 Tech How-To

Install Apache on Ubuntu: Step-by-Step Guide

Guide 10-42402 · Updated · Step by step

This guide walks you through installing and configuring the Apache HTTP Server on Ubuntu, one of the most widely used web servers for hosting websites and applications. You’ll start with a clean installation, then move on to essential configuration like virtual hosts, firewall rules, and SSL, so you can serve your own content securely.

What You’ll Need

  • A machine running Ubuntu (20.04, 22.04, or 24.04) with sudo access.
  • An internet connection to download packages.
  • A domain name (optional, but recommended for production use).
  • Basic familiarity with the command line.

Step-by-Step

  1. Update your package list. Open a terminal and run:
    sudo apt update
    This refreshes the package index so you get the latest available versions.
  2. Install Apache. Run:
    sudo apt install apache2 -y
    The -y flag automatically confirms the installation.
  3. Verify the installation. After installation, Apache should start automatically. Check its status with:
    sudo systemctl status apache2
    You should see active (running). If not, start it with sudo systemctl start apache2.
  4. Adjust the firewall. If you use UFW (Uncomplicated Firewall), allow Apache traffic:
    sudo ufw allow 'Apache Full'
    This opens ports 80 (HTTP) and 443 (HTTPS). If you only need HTTP, use sudo ufw allow 'Apache' instead.
  5. Test the default page. Open a browser and go to http://your_server_ip. You should see the default Ubuntu Apache welcome page. If you don’t, check your IP with curl ifconfig.me or hostname -I.
  6. Create your own document root. By default, Apache serves files from /var/www/html. For a new site, create a directory:
    sudo mkdir -p /var/www/example.com
    Replace example.com with your domain or project name.
  7. Set permissions. Give your user ownership of the directory so you can edit files without sudo:
    sudo chown -R $USER:$USER /var/www/example.com
  8. Create a sample index file. Use a text editor to create /var/www/example.com/index.html with some basic HTML content, like <h1>Hello Apache!</h1>.
  9. Configure a virtual host. Apache uses virtual hosts to serve multiple sites. Copy the default config file:
    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
  10. Edit the virtual host file. Open the new file with sudo nano /etc/apache2/sites-available/example.com.conf and update the ServerAdmin, ServerName, and DocumentRoot directives. A minimal example:
    <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>
    
  11. Enable the new site. Use the a2ensite tool to enable it:
    sudo a2ensite example.com.conf
  12. Disable the default site (optional but recommended). Run sudo a2dissite 000-default.conf to avoid conflicts.
  13. Test the configuration. Before reloading, check for syntax errors:
    sudo apache2ctl configtest
    You should see Syntax OK.
  14. Reload Apache. Apply the changes:
    sudo systemctl reload apache2
  15. Verify your site. Visit http://example.com (or your server IP if you didn’t set a domain) and you should see your custom page.
  16. Enable SSL (optional but recommended). Install Certbot to get a free Let’s Encrypt certificate:
    sudo apt install certbot python3-certbot-apache -y
    Then run sudo certbot --apache and follow the prompts. Certbot will automatically configure HTTPS and set up auto-renewal.

Common Mistakes

  • Forgetting to enable the site. Editing the config file but not running a2ensite means your changes won’t take effect.
  • Not checking permissions. If Apache can’t read your files, you’ll get a 403 Forbidden error. Ensure the document root and files are readable by the www-data user.
  • Overlooking the firewall. If you can’t access the page, the firewall may be blocking port 80 or 443. Double-check with sudo ufw status.
  • Using the wrong config file syntax. A missing semicolon or bracket can break Apache. Always run apache2ctl configtest after editing.
  • Not restarting after changes. Some changes require a full restart (sudo systemctl restart apache2) rather than a reload, especially when installing new modules.

Tips

  • Use sudo systemctl enable apache2 to ensure Apache starts automatically on boot.
  • Keep your server updated regularly with sudo apt update && sudo apt upgrade to patch security vulnerabilities.
  • For better performance, consider enabling modules like mod_deflate (compression) and mod_expires (caching) with sudo a2enmod.
  • Use a dedicated user for each virtual host if you host multiple sites, to isolate permissions.
  • Always back up your config files before making significant changes: sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/example.com.conf.bak.

Troubleshooting

Apache won’t start. Check the error log at /var/log/apache2/error.log. Common causes include port conflicts (e.g., another web server using port 80) or syntax errors in config files. Resolve the issue, then run sudo systemctl start apache2.

403 Forbidden. This usually means Apache can’t access your document root. Check directory permissions: ls -ld /var/www/example.com. The path should be accessible by the www-data user. You can also check the Directory directive in your virtual host file to ensure it allows access.

Default page still shows. If you see the default page instead of your site, the default virtual host is still enabled. Disable it with sudo a2dissite 000-default.conf and reload Apache.

SSL certificate fails to install. Ensure your domain resolves to your server’s IP. If you’re using a local IP, Certbot won’t work. Also, make sure port 80 is open for the HTTP-01 challenge.

Slow response times. This could be due to insufficient resources or misconfigured modules. Check system resources with htop and review Apache’s mpm configuration in /etc/apache2/mods-enabled/mpm_prefork.conf (or mpm_event.conf).

Frequently Asked Questions

How do I uninstall Apache?

Run sudo apt purge apache2 to remove Apache and its configuration files. You may also want to remove unused dependencies with sudo apt autoremove.

Can I host multiple websites on one Apache instance?

Yes, using virtual hosts. Create a separate config file for each site in /etc/apache2/sites-available, enable them all, and ensure each has a unique ServerName.

How do I change the default port from 80 to something else?

Edit /etc/apache2/ports.conf and change Listen 80 to Listen 8080 (or your desired port). Then update your virtual host’s <VirtualHost *:8080> and reload Apache.

What is the difference between reload and restart?

reload gracefully restarts Apache, keeping existing connections alive, while restart stops and starts the service, dropping all connections. Use reload for config changes and restart for module changes.

How do I check Apache logs?

Access logs are in /var/log/apache2/access.log and error logs in /var/log/apache2/error.log. You can tail them with sudo tail -f /var/log/apache2/error.log to monitor in real time.

Is Apache still a good choice in 2024?

Yes. Apache remains reliable, highly configurable, and well-documented. It’s an excellent choice for shared hosting, dynamic sites, and compatibility with .htaccess. For high-concurrency static content, Nginx might perform better, but Apache is far from obsolete.

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 →