+(91)70149-37521Subscribe Now

How to Install Apache on Ubuntu 24.04 LTS

Apache is a dependable choice when you want to host a website, a small application, or several domains on one Ubuntu server. This guide shows you how to install Apache on Ubuntu 24.04 LTS, open the firewall safely, create a virtual host, and check the setup before it goes live. The commands also work on […]

How to Install Apache on Ubuntu

Apache is a dependable choice when you want to host a website, a small application, or several domains on one Ubuntu server. This guide shows you how to install Apache on Ubuntu 24.04 LTS, open the firewall safely, create a virtual host, and check the setup before it goes live.

The commands also work on a normal Ubuntu 22.04 server. You need a user with sudo access and, for the domain section, a domain name whose DNS points to your server.

Before You Install Apache

  • Use a supported Ubuntu Server release.
  • Make sure SSH access works before changing the firewall.
  • Point the domain’s A record to the public server IP.
  • Take a server backup if this machine already hosts a website.

Log in through SSH and update the package index:

sudo apt update
sudo apt upgrade -y

Install Apache on Ubuntu 24.04

Ubuntu provides Apache in its official repositories. Install the apache2 package:

sudo apt install apache2 -y

The service normally starts automatically. Check it:

sudo systemctl status apache2 --no-pager

You should see active (running). If it is stopped, enable and start it with:

sudo systemctl enable --now apache2

Allow Apache Through the UFW Firewall

First confirm that your SSH rule is allowed. Closing the current SSH connection does not prove that a new connection can get through, so open a second terminal if you are working on a remote server.

sudo ufw status
sudo ufw app list

The Apache Full profile allows HTTP on port 80 and HTTPS on port 443:

sudo ufw allow 'Apache Full'
sudo ufw status

If UFW is not enabled and you want to use it, allow OpenSSH before running sudo ufw enable. Our UFW firewall guide explains the process in simple steps.

Check the Default Apache Page

Open http://SERVER_IP in a browser. The Ubuntu Apache default page should appear. You can also test from the command line:

curl -I http://127.0.0.1

A response such as HTTP/1.1 200 OK confirms that Apache answers locally. The default document root is /var/www/html, while Ubuntu stores site configuration files in /etc/apache2/sites-available/.

Create an Apache Virtual Host

A virtual host lets Apache serve the correct website for a domain. Replace example.com in every command with your real domain.

Create the Website Directory

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com
sudo chmod -R 755 /var/www/example.com

Create a small test page:

nano /var/www/example.com/public_html/index.html
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Example Site</title></head>
<body><h1>Apache is working</h1></body>
</html>

Add the Site Configuration

sudo nano /etc/apache2/sites-available/example.com.conf

Add this configuration:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Options -Indexes stops visitors from seeing a directory listing when no index file exists. Keep AllowOverride None unless your application needs .htaccess. If it does, enable only the permissions the application requires.

Enable and Test the Virtual Host

sudo a2ensite example.com.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

Do not reload Apache if the configuration test reports an error. Fix the named file and line first. To see how Apache maps domains to virtual hosts, run:

sudo apache2ctl -S

Add HTTPS After HTTP Works

Do not leave a public login, store, or form on plain HTTP. Once the DNS record resolves correctly and the port 80 test works, install a trusted TLS certificate. Certbot is a common option for Let’s Encrypt certificates on Ubuntu. Back up the virtual-host files before allowing any tool to edit them, then confirm that HTTP redirects to HTTPS and automatic renewal works.

sudo apache2ctl configtest
sudo systemctl reload apache2
curl -I https://example.com

Useful Apache Commands

sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2
sudo journalctl -u apache2 --since today
sudo tail -f /var/log/apache2/error.log

Use reload for a valid configuration change because it is less disruptive than a full restart. Use the error log when a page returns a 500 error or Apache refuses to reload.

Basic Security Checklist

  • Install Ubuntu security updates regularly.
  • Open only the ports the server needs.
  • Use HTTPS and test certificate renewal.
  • Keep directory listing disabled.
  • Give website files the minimum necessary permissions; avoid chmod 777.
  • Check logs and keep tested backups outside the server.
  • Use SSH keys and restrict privileged access. See our SSH key authentication guide.

Troubleshooting Apache on Ubuntu

The Browser Cannot Connect

Check the service, UFW rules, cloud-provider firewall, and DNS record. A local curl test may work even when an external firewall blocks visitors.

The Wrong Website Opens

Confirm ServerName, DNS, and the enabled configuration. Run sudo apache2ctl -S to see which virtual host Apache selects.

Apache Will Not Reload

Run sudo apache2ctl configtest. The result normally names the broken file and line. Correct it before restarting the service.

Frequently Asked Questions

What package installs Apache on Ubuntu?

The package and service are both called apache2. Install it with sudo apt install apache2.

Where are Apache configuration files stored?

The main configuration is under /etc/apache2/. Site files go in sites-available and enabled links appear in sites-enabled.

Should I restart or reload Apache?

After a successful configuration test, use reload for most configuration changes. Restart is useful when the service or a module truly needs a fresh process.

Conclusion

You have now installed Apache on Ubuntu 24.04, allowed web traffic, created a virtual host, and tested the configuration. Before putting a real site online, add HTTPS, apply updates, and make a tested backup plan.


Reviewed and updated: August 2026. Commands were checked against the official Ubuntu Server Apache documentation and Apache HTTP Server virtual-host documentation.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to Our Newsletter

Get free how-to tutorials and over 700+ courses. Seo tips, create a wordpress, or learn a new skill.