When I set up my first Linux server years ago, Apache was the very first piece of software I installed. It’s still, to this day, one of the most reliable and widely used web servers on the internet, and if you’re running Ubuntu, getting it up and running takes just a few minutes. In this guide, I’ll walk you through everything I do when installing Apache on Ubuntu, from the very first command to verifying that it’s actually serving pages.
What Is Apache Web Server?
Apache HTTP Server, often just called Apache, is an open-source web server that has been around since 1995. It’s maintained by the Apache Software Foundation and powers a huge portion of websites across the globe. I like Apache because it’s flexible, well-documented, and has a massive community behind it, which means almost any problem I run into has already been solved by someone else.
At its core, Apache listens for HTTP (and HTTPS) requests from browsers and responds by serving files, running scripts, or forwarding requests to other applications. It supports a modular architecture, which I’ll touch on later, and it works beautifully with Ubuntu’s package management system.
Why I Choose Apache for My Projects
Before diving into installation, I want to explain why Apache is still my go-to choice for many projects:
- Stability: It’s been battle-tested for decades.
- Flexibility: Modules let me add or remove functionality as needed.
- .htaccess support: I can override configuration on a per-directory basis without touching the main config.
- Wide compatibility: PHP, Python, Perl, and other languages integrate smoothly.
- Community support: Documentation and tutorials are everywhere.
Prerequisites
Before I start the installation, I make sure I have the following in place:
- A server or virtual machine running Ubuntu (this guide works for Ubuntu 20.04, 22.04, and 24.04).
- A user account with
sudoprivileges. - An active internet connection so the package manager can download Apache.
- Basic familiarity with the terminal.
If you’re working on a fresh VPS, I’d also recommend updating your system packages first so you’re not installing Apache on top of outdated libraries.
Step 1: Update Your Package Index
The first thing I always do on a new server is refresh the local package index. This ensures I’m installing the latest available version of Apache from Ubuntu’s repositories.
sudo apt update
I like to follow this up with an upgrade, just to make sure everything else on the system is current:
sudo apt upgrade -y
Step 2: Install Apache
With the package index updated, installing Apache is a single command:
sudo apt install apache2 -y
This command pulls in the apache2 package along with its dependencies, including common utility packages like apache2-utils. Depending on your connection speed, this should only take a minute or two.
Step 3: Verify the Installation
Once the installation finishes, I like to confirm that Apache is actually running before moving on.
sudo systemctl status apache2
You should see output indicating the service is active (running). If it isn’t running for some reason, I start it manually:
sudo systemctl start apache2
I also make sure Apache starts automatically on boot, which is usually enabled by default on Ubuntu, but it’s worth double-checking:
sudo systemctl enable apache2
Step 4: Adjust the Firewall
If you’re using UFW (Uncomplicated Firewall), which is common on Ubuntu, you’ll need to allow HTTP and HTTPS traffic through. I check the available Apache profiles first:
sudo ufw app list
You should see entries like Apache, Apache Full, and Apache Secure. I usually go with Apache Full, which opens both port 80 (HTTP) and port 443 (HTTPS):
sudo ufw allow 'Apache Full'
Then I check the firewall status to confirm the rule was applied:
sudo ufw status
Step 5: Test Apache in Your Browser
This is the moment of truth. I open a browser and navigate to my server’s IP address:
http://your_server_ip
If you don’t know your server’s public IP address, you can find it with:
curl -4 icanhazip.com
If everything worked, you’ll see the default Apache Ubuntu welcome page, which confirms the web server is installed and reachable.
If you’re testing on a local machine rather than a remote server, you can simply visit:
http://localhost
Understanding Apache’s Directory Structure
Once Apache is installed, it helps to know where things live on the filesystem:
/var/www/html— the default web root, where I place my website files./etc/apache2/— the main configuration directory./etc/apache2/apache2.conf— the primary configuration file./etc/apache2/sites-available/— where virtual host configuration files live./etc/apache2/sites-enabled/— symlinks to active virtual hosts./etc/apache2/mods-available/and/etc/apache2/mods-enabled/— module configuration./var/log/apache2/— access and error logs.
Getting comfortable with this layout early on saves a lot of confusion later when troubleshooting.
Common Mistakes I See Beginners Make
- Forgetting to open the firewall port. Apache can be running perfectly, but if UFW blocks port 80, nothing will load in the browser.
- Confusing the web root with the home directory. Files need to go in
/var/www/html, not a personal home folder, unless you’ve set up a custom virtual host. - Not restarting Apache after configuration changes. Any time I edit a config file, I make sure to reload or restart the service.
- Ignoring error logs. The
/var/log/apache2/error.logfile is the first place I look when something isn’t working.
Security Best Practices After Installation
Once Apache is installed, I take a few extra steps to harden it:
- Hide the Apache version and OS details by editing
/etc/apache2/conf-available/security.confand setting:ServerTokens ProdServerSignature Off - Keep the system updated regularly with
sudo apt update && sudo apt upgrade. - Install an SSL certificate using Let’s Encrypt and Certbot for HTTPS support.
- Disable unnecessary modules to reduce the attack surface (I cover this in my separate guide on enabling and disabling Apache modules).
- Set proper file permissions on
/var/www/htmlso that only necessary users can write to it.
Performance Optimization Tips
Apache runs fine out of the box, but I usually tweak a few things for better performance on production servers:
- Switch to the event MPM (Multi-Processing Module) instead of the default
preforkfor better concurrency, especially if you’re using PHP-FPM. - Enable compression with
mod_deflateto reduce page load times. - Enable caching with
mod_expiresandmod_headersfor static assets. - Monitor resource usage with tools like
htoporapachetopto catch bottlenecks early.
Troubleshooting Common Issues
Apache won’t start: Check the error log for clues:
sudo journalctl -xe
sudo tail -f /var/log/apache2/error.log
Port 80 already in use: Another service (like Nginx) might be occupying the port. Check with:
sudo ss -tulpn | grep :80
“Forbidden” error in browser: This usually points to incorrect file permissions in /var/www/html. I fix this with:
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
Frequently Asked Questions
Does installing Apache automatically start it? Yes, on Ubuntu, the apt install apache2 command starts the service immediately and enables it to run on boot.
Can I run Apache alongside Nginx? Not on the same port. You’d need to configure them to listen on different ports or use one as a reverse proxy in front of the other.
How do I uninstall Apache if I no longer need it? Run sudo apt purge apache2 apache2-utils -y followed by sudo apt autoremove -y.
Is Apache free to use? Yes, it’s completely open-source and free under the Apache License 2.0.
Summary and Key Takeaways
Installing Apache on Ubuntu is a straightforward process that takes just a handful of commands: updating your package index, installing the apache2 package, verifying the service is running, opening the firewall, and testing it in a browser. From there, understanding the directory structure and applying a few security and performance tweaks sets you up for a solid, production-ready web server.
I’ve installed Apache more times than I can count, and this same basic workflow has never let me down, whether I’m setting up a personal blog or a production application server.