India English
Kenya English
United Kingdom English
South Africa English
Nigeria English
United States English
United States Español
Indonesia English
Bangladesh English
Egypt العربية
Tanzania English
Ethiopia English
Uganda English
Congo - Kinshasa English
Ghana English
Côte d’Ivoire English
Zambia English
Cameroon English
Rwanda English
Germany Deutsch
France Français
Spain Català
Spain Español
Italy Italiano
Russia Русский
Japan English
Brazil Português
Brazil Português
Mexico Español
Philippines English
Pakistan English
Türkiye Türkçe
Vietnam English
Thailand English
South Korea English
Australia English
China 中文
Canada English
Canada Français
Somalia English
Netherlands Nederlands

How to Install Apache on Ubuntu (22.04 and 24.04)

Buy domains, business emails, hosting, VPS and more: Get Started

You have just logged in over SSH. The prompt is blank, the server is new, and nothing answers on port 80. Meanwhile, a browser tab remains open on the server IP, waiting for a page that does not yet exist.

In five minutes, that tab will show the Apache default page. The core install needs two commands and one firewall rule. Later steps add a domain, modules, and HTTPS.

This guide covers Ubuntu 22.04 LTS and 24.04 LTS. The commands are identical on both. If you want the short version, run two lines of code.

Those lines install Apache and start it. However, visitors still cannot reach the server until the firewall allows web traffic. The steps below cover that rule, a browser test, a virtual host, and fixes for common errors.

What You Need Before You Install Apache on Ubuntu

Gather these items before you start:

  • An Ubuntu 22.04 or 24.04 server. A VPS works well.
  • A user account with sudo rights.
  • SSH access to the server.
  • The public IP address of the server.
  • A domain name, if you plan to follow the virtual host and HTTPS steps.

Apache runs fine on a small VPS. Size the server based on the traffic you expect, not on the install itself.

Create a Sudo User (Skip This If You Already Have One)

Many new servers give you only the root account. Running everything as root is risky, so create a regular user first. Log in as root and run these commands:

adduser deploy

usermod -aG sudo deploy

The first command asks for a password and a few profile details. The second command gives the new user sudo rights. Replace deploy with any name you like. From here on, log in as that user.

Log In and Check Your Ubuntu Version

Log in to the server:

ssh your_user@your_server_ip

Next, confirm your Ubuntu version:

cat /etc/os-release

The output should show VERSION_ID="22.04" or VERSION_ID="24.04". Other releases often work too, but this guide covers only those two.

Step 1: Update the Package Index

apt upgrade -y apt update

Run the update command before you install anything.

sudo apt update

This command refreshes the list of available packages. As a result, apt installs the latest version of Apache from the Ubuntu repositories.

Healthy output ends with a line that reports how many packages can be upgraded. You can skip that upgrade for now. The confirmation prompt appears at the install step, not here.

Step 2: Install the apache2 Package

sudo apt install apache2

Next, install Apache with a single command.

sudo apt install apache2

Apt lists the packages it will add and asks for confirmation. Type y and press Enter.

The package name is apache2. Ubuntu has no package called apache or httpd. The name httpd is used on Red Hat-family systems such as Rocky Linux.

After the install finishes, check the version:

apache2 -v

The output looks similar to this: Server version: Apache/2.4.x (Ubuntu)

Ubuntu starts Apache automatically after the install. It also enables Apache at boot, so you need no extra steps.

Step 3: Check That Apache Is Running

sudo systemctl status apache2

Now confirm that the service is up.

sudo systemctl status apache2

Look at the Active line in the output. The text active (running) means Apache is up. The Loaded line should say enabled, which means Apache starts at boot.

The status view stays open until you press q. Press q to return to the prompt.

If Apache is stopped or disabled, fix both in one command:

sudo systemctl enable --now apache2

If the status shows failed, jump to the troubleshooting section below. Those fixes cover most start-up errors.

Step 4: Allow Apache Through the Firewall

sudo ufw allow 'Apache' sudo ufw status sudo ufw app list

Ubuntu ships with UFW, the Uncomplicated Firewall. Apache registers its own UFW profiles during the install. List them with this command:

sudo ufw app list

You should see Apache, Apache Full, and Apache Secure in the output. Each profile opens a different set of ports.

ProfilePorts openedUse it when
Apache80You serve plain HTTP only
Apache Full80 and 443You plan to add HTTPS
Apache Secure443You serve HTTPS only

Check whether UFW is active before you add a rule:

sudo ufw status

If the status says active, add the rule directly:

sudo ufw allow 'Apache'

If the status says inactive, rules do nothing yet. In that case, allow SSH before you enable UFW. Otherwise, you can lock yourself out of the server.

sudo ufw allow OpenSSH

sudo ufw allow 'Apache'

sudo ufw enable

Type y when UFW warns about disrupting SSH connections. Since you allowed OpenSSH first, your session stays alive. Then verify the rules:

sudo ufw status

Check Your Hosting Provider’s Firewall

Many hosting providers run a separate firewall in front of your server. That firewall can block port 80 even when UFW allows it.

Open the firewall or security group settings in your VPS control panel. Then allow inbound TCP traffic on port 80. If you plan to add HTTPS, allow port 443 as well.

Skip this check, and you may chase a firewall problem on the server that does not exist.

Step 5: Test Apache in Your Browser

hostname -I
curl -I http://localhost

Now open the server in a browser. First, find the server IP:

hostname -I

This command lists the addresses on the server. On some VPS plans, it shows a private address. In that case, copy the public IP from your provider dashboard.

Type the address into your browser like this:

http://your_server_ip

Start the address with http://, not https://. A fresh Apache install does not listen on port 443. Without an SSL certificate, an https:// address fails to load.

A working install shows the Apache2 Ubuntu Default Page. Its banner includes the words “It works!”

No browser on hand? Run this check on the server itself:

curl -I http://localhost

A line reading HTTP/1.1 200 OK means Apache answers locally. If curl is missing, install it with sudo apt install curl.

Step 6: Set a Global ServerName

echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/servername.conf

sudo a2enconf servername

sudo systemctl reload apache2

Apache wants a name for itself. Without one, it prints the AH00558 warning on every start and reload. Set the name once, and the warning disappears.

echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/servername.conf

sudo a2enconf servername

sudo systemctl reload apache2

Use your domain in place of localhost if you have one. Ubuntu keeps small configuration files in the conf-available folder. The a2enconf command links the file into conf-enabled, which turns it on.

Confirm the result with this command:

sudo apachectl configtest

The output should end with Syntax OK.

Step 7: Set Up a Virtual Host for Your Domain

The default site serves files from /var/www/html. A virtual host lets Apache serve a separate folder for each domain. This step also keeps your own site apart from the default page.

Point Your Domain at the Server

Open your DNS settings and create an A record. Set the name to your domain name, and set the value to your server’s public IP address. Add a second record for www if you want it to work.

Check the record from the server:

getent hosts example.com

The command should print your server’s IP address. DNS changes can take a while to spread, so wait and retry if it prints nothing.

Create the Site Folder and a Test Page

Make a folder for the site and give your user ownership:

sudo mkdir -p /var/www/example.com

sudo chown -R $USER:$USER /var/www/example.com

sudo chmod -R 755 /var/www/example.com

Next, create a test page:

nano /var/www/example.com/index.html

Paste this content, then save and close the file:

<html>

  <head>

    <title>Welcome to example.com</title>

  </head>

  <body>

    <h1>The example.com virtual host works.</h1>

  </body>

</html>

Write the Virtual Host File

Create the configuration file:

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

Paste this block and change the domain to yours:

<VirtualHost *:80>

    ServerAdmin [email protected]

    ServerName example.com

    ServerAlias www.example.com

    DocumentRoot /var/www/example.com

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log

    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

</VirtualHost>

Ubuntu already grants access to everything under /var/www, so this block needs no extra permission rules. A folder outside /var/www needs its own Directory block.

Enable the Site

sudo a2ensite example.com.conf

sudo a2dissite 000-default.conf

sudo apachectl configtest

sudo systemctl reload apache2

Turn on your site and turn off the default page:

sudo a2ensite example.com.conf

sudo a2dissite 000-default.conf

sudo apachectl configtest

sudo systemctl reload apache2

Now visit http://example.com. Your test page should replace the default page. If it does not, use the 404 and wrong-page fixes in the troubleshooting section.

Step 8: Enable the Apache Modules You Need

Apache loads features through modules. Ubuntu stores them in mods-available and links the active ones into mods-enabled. List the loaded modules with this command:

sudo apachectl -M

To turn on a module, use a2enmod. The rewrite module is a common first choice. It enables pretty URLs and many CMS plugins.

sudo a2enmod rewrite

sudo systemctl restart apache2

To disable a module, use a2dismod with the module name. Restart Apache again afterward.

Step 9: Secure Apache With a Free SSL Certificate

sudo snap install --classic certbot

sudo ln -s /snap/bin/certbot /usr/bin/certbot

HTTPS needs a domain that points to your server. Port 80 must also be reachable from the internet. Both checks matter, since the certificate authority tests them.

Install Certbot with Snap, which Ubuntu includes:

sudo snap install --classic certbot

sudo ln -s /snap/bin/certbot /usr/bin/certbot

Then request a certificate and let Certbot edit your virtual host:

sudo certbot --apache

Certbot asks for your email address and for permission to proceed. It then lists your domains and lets you pick which ones to secure. It also offers to redirect HTTP traffic to HTTPS. Accept that redirect.

Now open port 443 and close the plain HTTP rule:

sudo ufw allow 'Apache Full'

sudo ufw delete allow 'Apache'

Also open port 443 in your provider’s firewall if it runs one. Finally, test automatic renewal:

sudo certbot renew --dry-run

A passing dry run means renewals will work on their own. Visit https://example.com to see the padlock.

Start, Stop, Restart, and Enable Apache on Boot

Apache runs under systemd, so you control it with systemctl. Keep this table close.

TaskCommand
Start Apachesudo systemctl start apache2
Stop Apachesudo systemctl stop apache2
Restart Apachesudo systemctl restart apache2
Reload the configurationsudo systemctl reload apache2
Start Apache at bootsudo systemctl enable apache2
Stop Apache starting at bootsudo systemctl disable apache2

Restart stops and then starts Apache again. That action drops active connections. Reload applies configuration changes without dropping them.

Use reload for routine configuration edits. Use restart when a reload does not apply your change.

To confirm the boot setting, run this command:

systemctl is-enabled apache2

The output should read enabled.

Where Apache Keeps Its Files

You will touch these locations most often.

  • Document root: /var/www/html holds the files for the default site. A new install contains one file there, index.html, which is the default page.
  • Main configuration: /etc/apache2/apache2.conf holds the global settings. The ports.conf file in the same folder specifies which TCP ports Apache listens on.
  • Sites: sites-available holds virtual host files. The sites-enabled folder holds links to the active ones.
  • Modules and snippets: mods-available and conf-available hold files you can switch on. Their -enabled twins hold the links.
  • Logs: /var/log/apache2 holds access.log for requests and error.log for problems.

Do not edit the links in the -enabled folders by hand. Use a2ensite, a2enmod, and a2enconf instead.

Before you reload Apache after any edit, test the files:

sudo apachectl configtest

A Syntax OK line means Apache can read the configuration.

Fix Common Apache Install Errors on Ubuntu

Start every fix with the error log. It names the real cause far more often than the browser does. Watch it live in a second terminal:

sudo tail -f /var/log/apache2/error.log

Then match your symptom in this table.

What you seeLikely causeJump to
“Could not get lock” during installAnother package process is runningPackage lock error
“Unable to locate package apache2”Stale package list or old releaseUnable to locate package
Service shows “failed”Port conflict or bad configurationFailed service sections
“Connection refused” in the browserApache is not listeningRefused versus timed out
Browser times outA firewall drops the trafficRefused versus timed out
403 ForbiddenFile permissions or missing index file403 Forbidden
Wrong page or 404Virtual host or DNS problemWrong page or 404

Install-Time Errors

1) “Could Not Get Lock /var/lib/dpkg/lock-frontend”

This error means another package process is running. On a new server, automatic updates often cause it. The safest fix is to wait a few minutes and run the command again.

To see which process holds the lock, use this command:

sudo lsof /var/lib/dpkg/lock-frontend

Do not delete the lock files as a first response. Deleting them while a process runs can damage the package database. If nothing holds the lock and the error stays, repair the database:

sudo dpkg --configure -a

Then retry the install.

2) “Unable to Locate Package apache2”

This error means apt cannot find the package in its list. Work through these fixes in order:

  1. Check the spelling. The name is apache2, in lowercase.
  2. Refresh the list with sudo apt update. A new server often has an empty cache.
  3. Enable the extra repositories with sudo add-apt-repository universe, then run sudo apt update again.
  4. Test your network with curl -I http://archive.ubuntu.com/ubuntu/. A failure indicates a DNS or firewall issue on the server.
  5. Check your release in /etc/os-release. An Ubuntu release that has reached end of life loses its normal repositories. Move to 22.04 or 24.04 in that case.

3) Unmet Dependencies or an Interrupted Install

A dropped SSH session can halt an install halfway through. Apt then reports unmet dependencies or broken packages. Repair the state with these commands:

sudo apt --fix-broken install

sudo dpkg --configure -a

After both finish, run sudo apt install apache2 again.

Start-Up Errors

1) The Service Shows “Failed” After Install

Read the real error before you try fixes. Run these two commands:

sudo journalctl -xeu apache2

sudo apachectl configtest

The journal shows why the service stopped. The configuration test points to the file and line that break the syntax.

2) “Address Already in Use” on Port 80

This error indicates that another program is already listening on port 80. Apache cannot start until that port is free. Find the program with this command:

sudo ss -tlnp | grep ':80'

The last column names the process. Nginx is a frequent culprit. If you do not need it, stop it, then start Apache:

sudo systemctl stop nginx

sudo systemctl start apache2

To keep both servers, change the Apache port instead. Edit the Listen 80 line in /etc/apache2/ports.conf. Then update any virtual host that uses port 80.

3) The AH00558 ServerName Warning

You may see a message that Apache could not determine the server’s fully qualified domain name. The warning is harmless, and Apache still runs. Step 6 above removes it for good.

4) Configuration Syntax Errors

Run sudo apachectl configtest after every edit. A failing test names the file and the line. For example, you may see “Syntax error on line 12 of /etc/apache2/sites-available/example.com.conf“. Open that file and fix the line.

Another common message reads: “Invalid command 'RewriteEngine'.” It means the needed module is off. Enable it, then restart:

sudo a2enmod rewrite

sudo systemctl restart apache2

5) Apache Stops on Its Own

A small VPS can run out of memory under load, and the system then ends processes to recover. Check memory, disk space, and the kernel log:

free -h

df -h

sudo journalctl -k | grep -i "out of memory"

A match in the kernel log means memory ran out. Lower the Apache worker limits, add swap space, or move to a larger plan. A full disk can cause similar trouble, so clear old logs in /var/log/apache2 if df -h shows 100%.

Access Errors

I) Connection Refused Versus Timed Out

The browser message tells you where to look. “Connection refused” means the server responded, but no one is listening on that port. A timeout usually means a firewall drops the traffic.

For a refusal, check that Apache runs and listens on port 80:

sudo systemctl status apache2

sudo ss -tlnp | grep apache2

For a timeout, check UFW first, then your provider’s firewall. Both must allow port 80.

II) The Page Loads Locally but Not From Your Browser

First, run curl -I http://localhost on the server. If it returns 200 OK, Apache works. The block sits between the server and your browser.

Check these items in order:

  1. UFW allows the Apache profile.
  2. Your provider’s firewall allows port 80.
  3. You typed the correct public IP address.

III) Locked Out After Enabling UFW

You may enable UFW before you allow SSH. The firewall then blocks your own session. Open the web console in your hosting dashboard and log in there. Then allow SSH again:

sudo ufw allow OpenSSH

If your SSH daemon uses a custom port, allow that port number instead.

IV) 403 Forbidden

A 403 error means Apache answered but refused to serve the file. Apache runs as the www-data user, so that user must be able to read the files. Check the error log first. Then look for these causes:

  • The folder has no index.html file, and directory listing is off.
  • The file or a parent folder blocks the www-data user.
  • The site files are located in a user’s home folder.
  • The site folder sits outside /var/www and has no Require all granted rule.
  • A bad rule in an .htaccess file blocks the request.

Inspect the permissions along the full path with this command:

namei -l /var/www/example.com/index.html

Then set safe permissions on folders and files:

sudo find /var/www/example.com -type d -exec chmod 755 {} \;

sudo find /var/www/example.com -type f -exec chmod 644 {} \;

Never use chmod 777 on your web folder. It gives every user on the server write access.

V) The Wrong Page or a 404 Error

If the default page still appears after Step 7, Apache is not matching your virtual host. Print the host map:

sudo apachectl -S

The output lists each virtual host and the name it answers to. Check that your ServerName matches the domain you typed. Then confirm the site link exists:

ls /etc/apache2/sites-enabled

If your file is missing, run sudo a2ensite example.com.conf and reload Apache. A 404 on a single path usually means the file is missing or misspelled.

Check names carefully, since Linux treats Index.html and index.html as different files.

VI) Your Domain Does Not Load, but the IP Does

This pattern points to DNS. Run getent hosts example.com and compare the address with your server IP. If it differs, fix the A record and wait for DNS to catch up.

You can test the virtual host without DNS by sending the hostname yourself:

curl -I -H "Host: example.com" http://your_server_ip

A 200 OK reply proves the virtual host works. The remaining problem is DNS.

VII) Certbot Fails to Issue a Certificate

Certbot checks your domain from the internet. A failure usually has one of three causes:

  • The domain does not point to your server yet.
  • Port 80 is blocked in UFW or in your provider’s firewall.
  • The ServerName in your virtual host does not match the domain.

Fix the cause, then run sudo certbot --apache again. For details, read the log at /var/log/letsencrypt/letsencrypt.log.

VIII) The Browser Shows an Old Page

Browsers cache pages aggressively. Press Ctrl+F5 for a hard refresh. You can also check the real response with curl -I http://example.com, which ignores the browser cache.

FAQs

Is the Ubuntu package called apache2 or httpd?

How do I uninstall Apache from Ubuntu?

How do I add PHP to Apache on Ubuntu?

Your Apache Server Is Live: Host Your Site With Confidence

Apache now runs on your Ubuntu server, answers on port 80, and starts at boot. Your domain has its own virtual host, and HTTPS protects the traffic. You also know where to look when something breaks.

Your next task is the site itself. Upload your files to the site folder, then add PHP and a database if your project needs them.

You also need a reliable server to run all this on. A Truehost Ubuntu VPS gives you root access, a public IP, and a clean Ubuntu image. Deploy one, then repeat the steps above in minutes. 

Elias N
Author

Elias N

SEO Expert Nairobi, KEN

SEO nerd by trade. Obsessing over keywords, content, and why Google does what it does.

View All Posts