How to Install and Set Up Apache on Ubuntu (24.04 and 22.04)

Key Takeaways
- ✓Install with sudo apt install apache2; it starts automatically.
- ✓Allow 'Apache Full' in UFW to open ports 80 and 443.
- ✓Each site gets a virtual host in /etc/apache2/sites-available.
- ✓Certbot's Apache plugin adds free HTTPS and renews it automatically.
Table of Contents
- •How to Install and Set Up Apache on Ubuntu (24.04 and 22.04)
- •Step 1: Install Apache
- •Step 2: Open the Firewall
- •Step 3: Create a Virtual Host for Your Domain
- •Step 4: Add HTTPS With Certbot
- •Step 5: Add PHP (Optional)
- •Everyday Apache Commands
- •Where Apache Keeps Things on Ubuntu
- •Troubleshooting
- •Apache or Nginx?
- •Frequently Asked Questions
- •Your Own Ubuntu Server
How to Install and Set Up Apache on Ubuntu (24.04 and 22.04)
Apache is still one of the most widely used web servers, and it’s the default choice for PHP applications that rely on .htaccess files, including WordPress. On Ubuntu it installs from the standard repositories in one command. This guide covers installing Apache, opening the firewall, setting up a virtual host for your domain, adding free HTTPS with Certbot, and the commands you’ll use every day.
Quick answer: sudo apt update && sudo apt install apache2, then sudo ufw allow 'Apache Full', create a virtual host in /etc/apache2/sites-available/, enable it with a2ensite, and run sudo certbot --apache for HTTPS.
Step 1: Install Apache
sudo apt update
sudo apt install apache2The service starts automatically. Check it:
sudo systemctl status apache2
apache2 -vOpen http://YOUR_SERVER_IP in a browser and you should see the “Apache2 Default Page”.
Step 2: Open the Firewall
If UFW is enabled, Apache registers application profiles you can allow by name:
sudo ufw app list
sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw enable
sudo ufw statusApache Full opens ports 80 and 443. Allow OpenSSH first so you don’t lock yourself out. On a cloud server, also check the provider’s firewall or security group; our guide to opening ports on a VPS covers both.
Step 3: Create a Virtual Host for Your Domain
Each site gets its own folder and its own configuration file. Create the web root and a test page:
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_html
echo '<h1>It works</h1>' > /var/www/example.com/public_html/index.htmlCreate /etc/apache2/sites-available/example.com.conf:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>AllowOverride All lets .htaccess files work, which WordPress and many PHP apps need. Enable the site, enable mod_rewrite, disable the default site, test and reload:
sudo a2ensite example.com.conf
sudo a2enmod rewrite
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl reload apache2configtest should print Syntax OK. Point your domain’s DNS A record at the server, then visit http://example.com.
Step 4: Add HTTPS With Certbot
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.comCertbot gets a free Let’s Encrypt certificate, creates the HTTPS virtual host and can add the HTTP to HTTPS redirect for you. Renewal is automatic; test it with sudo certbot renew --dry-run.
Step 5: Add PHP (Optional)
To run PHP applications with Apache’s PHP module:
sudo apt install php libapache2-mod-php php-mysql
sudo systemctl restart apache2For better performance under load, many setups use PHP-FPM with Apache’s event MPM instead (php-fpm plus a2enconf php*-fpm and a2enmod proxy_fcgi).
Everyday Apache Commands
| Task | Command |
|---|---|
| Test the configuration | sudo apache2ctl configtest |
| Reload without dropping connections | sudo systemctl reload apache2 |
| Restart | sudo systemctl restart apache2 |
| Enable / disable a site | sudo a2ensite name / sudo a2dissite name |
| Enable / disable a module | sudo a2enmod name / sudo a2dismod name |
| List loaded modules | apache2ctl -M |
| Follow the error log | sudo tail -f /var/log/apache2/error.log |
Where Apache Keeps Things on Ubuntu
/etc/apache2/apache2.conf: main configuration./etc/apache2/sites-available/andsites-enabled/: virtual hosts (enabled ones are symlinks)./etc/apache2/mods-available/andmods-enabled/: modules./var/www/: web roots./var/log/apache2/: access and error logs.
Troubleshooting
Port 80 already in use: another web server (often Nginx) is running. Check with
sudo ss -tlnp | grep :80.403 Forbidden: the
<Directory>block is missingRequire all granted, or file permissions stop Apache reading the folder..htaccess rules ignored:
AllowOverrideisNoneormod_rewriteisn’t enabled.Still seeing the default page: the default site is still enabled or
ServerNamedoesn’t match the domain.
Apache or Nginx?
Apache shines with .htaccess and per-directory configuration; Nginx is lighter for static files and as a reverse proxy. Many servers run Nginx in front of Apache or PHP-FPM. If you’re deciding, see our comparison of Caddy and Nginx and the guide to common Nginx errors.
Frequently Asked Questions
What is the Apache package called on Ubuntu?
apache2. Install it with sudo apt install apache2.
How do I check if Apache is running?
Run sudo systemctl status apache2, or open the server’s IP address in a browser.
Where is the Apache web root on Ubuntu?
The default is /var/www/html. For your own sites, create a folder under /var/www and point a virtual host at it.
How do I enable .htaccess in Apache?
Set AllowOverride All in the site’s Directory block and enable mod_rewrite with sudo a2enmod rewrite, then reload Apache.
How do I get HTTPS on Apache for free?
Install certbot and python3-certbot-apache, then run sudo certbot --apache -d yourdomain.com.
Your Own Ubuntu Server
RackUp’s Developer VPS gives you root access to install Apache, PHP and anything else: 8 vCPU, 12 GB RAM, 150 GB NVMe and 32 TB of monthly bandwidth for $35 a month. Just want WordPress on Apache without the setup? Cloud WordPress runs it for you from $2.49 a month. See plans.
Unsure Which Cloud Hosting Plan Fits Your Site?
Answer 4 quick diagnostic questions to estimate the server CPU, RAM and storage you need with transparent pricing.
Cloud Infrastructure Engineer.
Related Posts
Server StackNginx: Redirect HTTP to HTTPS (and www to non-www) the Right Way
The correct Nginx server blocks for HTTP to HTTPS and www to non-www redirects, Certbot's --redirect, HSTS, testing with curl, and fixing redirect loops.
Server StackTraefik vs Caddy vs Nginx: Which Reverse Proxy Should You Use?
Traefik routes Docker containers automatically, Caddy does HTTPS with almost no config, and Nginx does everything. Which reverse proxy fits your setup.
Server StackNginx Monitoring: stub_status, Logs, Prometheus and Alerts
Set up Nginx monitoring with the built-in stub_status page and timed access logs, then add Prometheus, Grafana and the alerts that catch problems early.