How to Install Apache HTTP Server on Ubuntu
Install Apache HTTP Server on Ubuntu with APT, verify the service, test localhost, and troubleshoot common installation problems.
Apache HTTP Server is software that accepts HTTP requests and returns web resources such as HTML pages, CSS files, images, and other content. Ubuntu provides Apache through the apache2 package.
A browser and a web server have different roles: the browser requests a page, while Apache receives the request and delivers the page. For background, see what a web server does and what Apache HTTP Server is.
Prerequisites
- A working Ubuntu installation with network access to configured Ubuntu package repositories.
- A terminal session.
- An account permitted to run administrative commands with
sudo.sudoruns a command with elevated privileges. - No other service using the standard HTTP port, port 80, if Apache is expected to listen there.
Ubuntu is a Linux distribution that uses APT, its package management tool and package ecosystem. A package repository is a configured source from which APT obtains package metadata and software packages.
Refresh and upgrade Ubuntu packages
Before installing software, refresh APT's package metadata. This downloads current information about available package versions; it does not itself install upgrades.
sudo apt update
You can then apply available upgrades to already-installed packages:
sudo apt upgrade
APT may ask you to confirm the operation. Review the proposed changes and enter Y when you want to continue. To perform both operations in sequence, use:
sudo apt update && sudo apt upgrade
Install Apache with APT
Install the Ubuntu Apache package named apache2:
sudo apt install apache2
APT downloads Apache and any required dependencies from the configured package repositories. If APT asks for confirmation, enter Y and press Enter to proceed.
The package installation normally enables Apache to start during system boot and starts the Apache service immediately. A service is a background process managed by the operating system; Apache runs as a service named apache2.
Check that the Apache service is running
Modern Ubuntu releases use systemd to manage services. The primary status command is:
sudo systemctl status apache2
Look for a result such as Active: active (running). This indicates that the service started successfully. The status view may open in a pager; press q to exit.
You can also check whether Apache is enabled to start automatically at boot:
sudo systemctl is-enabled apache2
The traditional service interface is an alternative status check:
sudo service apache2 status
Test the local Apache website
Open a browser on the same Ubuntu computer and visit http://localhost/. localhost is a hostname that refers to the computer currently being used, so this test checks the local Apache listener rather than a remote server.
A successful installation normally displays the Ubuntu Apache default page. Seeing that page confirms that Apache is installed, running, listening for HTTP requests, and serving its default site.
You can test the HTTP response without a browser by using curl:
curl -I http://localhost
The -I option requests response headers. A response containing an HTTP status such as 200 OK is evidence that Apache answered the local request.
Installation commands and expected results
| Step | Command | Purpose | Expected result |
|---|---|---|---|
| Refresh package metadata | sudo apt update | Obtain current information about packages in configured repositories. | Repository metadata is downloaded without an unresolved error. |
| Upgrade installed packages | sudo apt upgrade | Apply available updates to installed packages. | APT lists upgrades and may request confirmation. |
| Install Apache | sudo apt install apache2 | Download and install Apache and its dependencies. | The apache2 package is installed; the service normally starts and becomes enabled. |
| Check service status | sudo systemctl status apache2 | Inspect the Apache service. | The status includes active (running) when startup succeeded. |
| Test local HTTP response | curl -I http://localhost | Send a local HTTP request without a browser. | Apache returns HTTP response headers, commonly including 200 OK. |
Common problems and troubleshooting
| Symptom | Likely cause | How to check | Resolution |
|---|---|---|---|
| APT cannot download packages | No network connection, incorrect repository configuration, or a temporary repository problem. | Confirm network connectivity, then run sudo apt update again and read the repository error. | Restore connectivity or correct the configured Ubuntu package sources before retrying. |
| Apache is not active | A configuration error, port conflict, failed dependency, or another startup condition. | Run sudo systemctl status apache2. For service logs, run sudo journalctl -u apache2. | Resolve the reported error, then start or restart Apache with sudo systemctl start apache2 or sudo systemctl restart apache2. |
localhost does not load | Apache is stopped, the URL or browser proxy is unexpected, or Apache is not listening on the expected port or interface. | Check service status, run curl -I http://localhost, and inspect port 80 listeners. | Start Apache and address the listener, URL, or browser configuration issue identified by the checks. |
| Apache fails because port 80 is already in use | Another web server, development server, or old Apache process occupies port 80. | Run sudo ss -ltnp | grep ':80' to identify the listening process. | Stop or reconfigure the conflicting service, or intentionally change Apache's listening port. |
| Apache works at localhost but not from another computer | An Ubuntu firewall, cloud or network firewall, or interface binding prevents remote access. | Confirm local access first; then check host and upstream firewall rules and inspect Apache's listener configuration. | Allow HTTP traffic where appropriate and ensure Apache listens on the intended network interface. |
Port 80 conflicts
Port 80 is the standard TCP port for unencrypted HTTP traffic. If another process already owns it, Apache may install successfully but fail to start. Identify the process before stopping anything:
sudo ss -ltnp | grep ':80'
Do not stop an unknown service blindly. Determine whether it is another web server, a development server, or an older Apache process, then stop or reconfigure it when appropriate.
Important Apache paths and next steps
The default document root is /var/www/html. A document root is the directory containing the files served by a website. After installation, Apache serves its default site from this location.
- Main Apache configuration:
/etc/apache2/apache2.conf - Default site configuration:
/etc/apache2/sites-available/000-default.conf - Default document root:
/var/www/html
To host a custom site, place suitable content in a document root and, when more than one site is needed, configure a virtual host. Continue with Apache configuration files, the default virtual host, or creating a new virtual host.
Production or network-facing servers also require deliberate firewall rules, HTTPS, updates, permissions, and security hardening. Useful follow-up subjects include configuring SSL, Apache access and error logs, and the ports configuration file.
Exam-relevant summary
- Ubuntu's Apache package and service name is
apache2. - Use
sudo apt updateto refresh package metadata andsudo apt upgradeto apply available package upgrades. - Install Apache with
sudo apt install apache2. - Use
sudo systemctl status apache2and look foractive (running). - Test the local server at
http://localhost/or withcurl -I http://localhost. - The default document root is
/var/www/html, and port 80 is the normal HTTP port.