News
Happy System Administrator Appreciation Day - to the true heroes of IT!
Serverspace Black Friday
BK
July 25 2025
Updated July 21 2025

Installing Apache Web Server on Ubuntu

Web server

A web server is software that accepts HTTP requests from clients, such as web browsers, and sends back web pages, images, style files, or other resources in response. The primary task of a web server is to provide access to web content, whether static HTML pages or dynamic sites created using server-side programming languages like PHP.

Several popular web servers exist, each with its strengths:

  • Apache: One of the most widespread web servers, known for its flexibility, modular architecture, and extensive community support. Apache suits various scenarios, from small personal sites to large enterprise applications.
  • Nginx: A high-performance web server frequently used for handling large numbers of concurrent connections. It is popular as a reverse proxy and load balancer.
  • IIS (Internet Information Services): Microsoft's web server integrated with the Windows operating system. It is often used in environments leveraging Microsoft technologies like ASP.NET.

In this article, we will focus on installing and configuring Apache on the Ubuntu operating system, as it is one of the most popular combinations for web hosting.

Why Apache?

Apache HTTP Server, developed in 1995, remains one of the world's most popular web servers. According to W3Techs data for July 2025, Apache holds approximately 25.7% of the web server market among tracked sites (W3Techs). While other sources like Netcraft indicate a smaller share (17% in January 2025), Apache maintains its leadership due to the following advantages:

  • Popularity and community support: Apache has a vast community of users and developers, providing extensive documentation, active forums, and regular updates. The official Apache documentation (httpd.apache.org/docs/) is one of the best resources for learning and troubleshooting.
  • Flexibility: Apache supports numerous modules that allow customizing the server for specific tasks. For example, you can enable modules for SSL support, URL rewriting, or integration with programming languages.
  • Modularity: Apache's architecture is based on modules that can be enabled or disabled as needed. This allows optimizing server performance and functionality.

These qualities make Apache an ideal choice for both novice and experienced administrators seeking to build a reliable web server.

Prerequisites

Before installing Apache on Ubuntu, ensure you have the following:

  • Linux operating system (Ubuntu): This article uses Ubuntu as it is one of the most popular Linux distributions, supported by extensive documentation and community. Instructions apply to Ubuntu versions like 20.04 or 22.04 but can be adapted for other distributions.
  • Root or sudo access: Installing and configuring Apache requires administrative privileges for installing packages and modifying system files.
  • Basic command-line knowledge: You should be familiar with basic Linux commands like cd, sudo, and nano to execute commands in the terminal.

Remote server access via SSH is also recommended. Ensure your system is updated to avoid installation issues.

Installing Apache on Ubuntu

Installing Apache on Ubuntu is straightforward using the apt package manager. Follow these steps:

  1. Update package lists
    Before installing software, update package lists to ensure you install the latest versions:
    sudo apt update

    This command synchronizes the local package index with Ubuntu repositories.

  2. Install Apache
    Install the apache2 package, which includes all necessary dependencies:
    sudo apt install apache2

    During installation, Ubuntu automatically starts Apache and configures it to launch at system boot.

  3. Check Apache status
    Verify Apache is running:
    sudo systemctl status apache2

    The output should show the apache2 service as active (active (running)). Example output:

    ● apache2.service - The Apache HTTP Server
    Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
    Active: active (running) since Thu 2025-07-17 22:49:00 UTC; 5min ago
  4. Verify autostart
    Ensure Apache is configured to start on system boot:
    sudo systemctl is-enabled apache2

    If the command returns enabled, Apache will start automatically. If it returns disabled, enable autostart:

    sudo systemctl enable apache2

Basic Configuration

After installing Apache, proceed to configure it for hosting websites.

  1. Understanding configuration files
    Apache's main configuration file is located at /etc/apache2/apache2.conf. This file contains global server settings like performance parameters and paths to other configuration files. In modern Ubuntu versions, the httpd.conf file used in other distributions is not used; it is replaced by apache2.conf.
    Virtual hosts (enabling multiple sites on one server) are configured in /etc/apache2/sites-available/. By default, this directory contains 000-default.conf, which defines the configuration for the primary site.
  2. Configuring virtual hosts
    To create a new virtual host:
    • Create a configuration file for the new site (e.g., mysite.conf):
      sudo nano /etc/apache2/sites-available/mysite.conf
    • Add the following configuration, replacing mysite.com with your domain:

      ServerAdmin webmaster@mysite.com
      ServerName mysite.com
      ServerAlias www.mysite.com
      DocumentRoot /var/www/mysite
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
    • Create the site's directory:
      sudo mkdir /var/www/mysite
    • Set correct permissions so Apache can read files:
      sudo chown -R www-data:www-data /var/www/mysite
      sudo chmod -R 755 /var/www/mysite
    • Enable the virtual host:
      sudo a2ensite mysite.conf
    • Verify configuration syntax:
      sudo apache2ctl configtest

      If the command returns Syntax OK, the configuration is valid.

    • Reload Apache to apply changes:
      sudo systemctl reload apache2
  3. Virtual host configuration example
    The above configuration creates a virtual host for the domain mysite.com. The DocumentRoot parameter specifies where site files are stored. Error and access logs are recorded to files defined in ErrorLog and CustomLog. You may add additional directives (e.g., .htaccess settings) by including AllowOverride All in a <Directory> section.

Testing the Installation

After installing and configuring Apache, verify it works correctly:

  1. Access the default Apache page
    Open a web browser and enter http://localhost or your server's IP (e.g., http://192.168.1.100). You should see the default Apache page titled "Apache2 Ubuntu Default Page" with text confirming successful installation.
  2. Create a test HTML file
    To test if Apache serves custom files correctly:
    echo "Hello World" | sudo tee /var/www/html/test.html

    Then open http://localhost/test.html or http://<your_IP>/test.html in your browser. You should see a page displaying "Hello World".

Troubleshooting Common Issues

If Apache doesn't work as expected, use these diagnostic methods:

  1. Check Apache status
    Verify if the service is running:
    sudo systemctl status apache2

    If inactive, restart it:

    sudo systemctl restart apache2
  2. Check error logs
    Apache error logs are in /var/log/apache2/error.log. Inspect them for diagnostics:
    sudo cat /var/log/apache2/error.log

    Access logs in /var/log/apache2/access.log help track processed requests.

  3. Common errors and solutions
    Typical issues and fixes:
    • Port 80 occupied: If Apache fails to start due to port conflict:
      sudo netstat -tulpn | grep :80

      If another process (e.g., Nginx) uses the port, stop it:

      sudo systemctl stop <service_name>

      Then restart Apache.

    • Configuration errors: Verify configuration syntax:
      sudo apache2ctl configtest

      Fix any errors in the relevant configuration file.

    • Permission issues: Ensure Apache has access to /var/www/ files. The www-data user should own directories:
      sudo chown -R www-data:www-data /var/www/html
      sudo chmod -R 755 /var/www/html
    • Firewall blocking port: If server access is blocked, allow port 80:
      sudo ufw allow 80/tcp
    • Package dependencies: If installation fails due to missing dependencies:
      sudo apt-get install -f

      Or update the system:

      sudo apt-get update && sudo apt-get upgrade

Installing and configuring the Apache web server on Ubuntu is a straightforward process that enables quick deployment of a web server for hosting sites. By following these steps, you can install Apache, configure virtual hosts, and resolve common issues. Apache remains a reliable choice due to its flexibility, modularity, and community support.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.