This guide walks you through how to install Apache 2.4 on an EC2 Ubuntu 22.04 instance, configure a virtual host, and manage the service. Apache is the most common web server for hosting websites, running PHP apps, and serving as a reverse proxy on Linux.
Prerequisites
- An EC2 instance running Ubuntu 22.04 LTS — follow How to deploy EC2 Ubuntu 22.04 LTS on AWS if you need one
- SSH access to the instance
- Your EC2 Security Group must allow inbound traffic on port 80 (HTTP) and 443 (HTTPS) — this is the most common reason Apache appears unreachable after installation
Connect to Your EC2 Instance
ssh -i /path/to/your-key.pem ubuntu@203.0.113.10
Replace the key path and IP with your own. If you have passwordless SSH configured, you can connect without specifying the key file.
Install Apache 2.4
Ubuntu 22.04 includes Apache 2.4 in its default repositories. Update the package list and install it:
sudo apt update
sudo apt install -y apache2
Optional: Use the ondrej/apache2 PPA for a newer version
The default Ubuntu 22.04 repos ship Apache 2.4.52. If you need a newer point release (for specific module support or bug fixes), add the ondrej PPA before installing:
sudo add-apt-repository ppa:ondrej/apache2 -y
sudo apt update
sudo apt install -y apache2
For most setups, the default version is fine.
Verify the Installation
Check that Apache is running and see which version was installed:
sudo systemctl status apache2
apache2ctl -v
You should see active (running) in the status output. Now open your browser and go to http://your-ec2-public-ip. You should see the default Apache2 Ubuntu welcome page.
If the page doesn’t load, check your EC2 Security Group — port 80 must be open for inbound traffic.
Configure the Firewall (UFW)
On EC2, Security Groups act as the primary firewall at the network level. UFW (Uncomplicated Firewall) is an optional additional layer running on the instance itself. If you decide to enable UFW, make sure to allow SSH first — otherwise you’ll lock yourself out:
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
Verify the rules:
sudo ufw status
The order matters — always allow SSH (port 22) before running ufw enable.
Manage the Apache Service
| Action | Command |
|---|---|
| Start Apache | sudo systemctl start apache2 |
| Stop Apache | sudo systemctl stop apache2 |
| Restart Apache | sudo systemctl restart apache2 |
| Reload config (no downtime) | sudo systemctl reload apache2 |
| Enable on boot | sudo systemctl enable apache2 |
| Check status | sudo systemctl status apache2 |
Use reload instead of restart when you change configuration files — it applies the new config without dropping active connections.
Set Up a Virtual Host
Virtual hosts let you serve multiple websites from a single Apache instance. Here’s how to create one for example.com.
1. Create the document root
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_html
2. Create the virtual host config
sudo nano /etc/apache2/sites-available/example.com.conf
Add the following:
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.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>
ServerName— your primary domainServerAlias— additional domain (e.g., the www version)DocumentRoot— where Apache looks for your site files- Separate log files per site make troubleshooting easier
3. Enable the site and disable the default
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
4. Test and reload
sudo apache2ctl configtest
sudo systemctl reload apache2
Always run configtest before reloading — it catches syntax errors in your config files before they take down the server.
Conclusion
You now have Apache 2.4 running on your EC2 Ubuntu 22.04 instance with a virtual host configured and ready to serve traffic.
Next steps: install PHP 8.3 to run dynamic applications, or secure Apache with Let’s Encrypt to enable HTTPS. If you’re building a full WordPress stack, check out the complete WordPress installation guide.


