|
| 1 | +--- |
| 2 | +slug: how-to-install-apache-ubuntu-2404 |
| 3 | +title: "How to Install and Use Apache on Ubuntu 24.04" |
| 4 | +description: "Apache is an open-source web server, the most widely used on Linux systems, with a modular design that makes it highly adaptable. This guide shows you how to install Apache on Ubuntu 24.04 and how to get started using it." |
| 5 | +authors: ["Akamai"] |
| 6 | +contributors: ["Akamai"] |
| 7 | +published: 2025-08-05 |
| 8 | +keywords: ['apache','apache web server','apache http server','httpd','install apache on ubuntu'] |
| 9 | +tags: ['ubuntu'] |
| 10 | +license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)' |
| 11 | +external_resources: |
| 12 | +- '[Apache HTTP Server Documentation](https://httpd.apache.org/docs/current/)' |
| 13 | +relations: |
| 14 | + platform: |
| 15 | + key: install-apache-server |
| 16 | + keywords: |
| 17 | + - distribution: Ubuntu 24.04 |
| 18 | +--- |
| 19 | + |
| 20 | +The Apache HTTP Web Server — usually just called Apache — is one of the most widely used open-source web servers. It comes with a long history of success in a wide range of applications. In this guide, you can see how to install Apache on Ubuntu 24.04 and learn how to get started using it. |
| 21 | + |
| 22 | +## Before You Begin |
| 23 | + |
| 24 | +1. Familiarize yourself with our [Getting Started with Linode](/docs/products/platform/get-started/) guide, and complete the steps for setting your Linode's hostname and timezone. |
| 25 | + |
| 26 | +1. This guide uses `sudo` wherever possible. Complete the sections of our [How to Secure Your Server](/docs/products/compute/compute-instances/guides/set-up-and-secure/) guide to create a standard user account, harden SSH access, and remove unnecessary network services. |
| 27 | + |
| 28 | +1. Update your system: |
| 29 | + |
| 30 | + ```command |
| 31 | + sudo apt update && sudo apt upgrade |
| 32 | + ``` |
| 33 | + |
| 34 | +{{< note >}} |
| 35 | +This guide is written for a non-root user. Commands that require elevated privileges are prefixed with `sudo`. If you’re not familiar with the `sudo` command, see the [Linux Users and Groups](/docs/guides/linux-users-and-groups/) guide. |
| 36 | +{{< /note >}} |
| 37 | + |
| 38 | +## Installing Apache |
| 39 | + |
| 40 | +1. Install Apache from the package manager using the following command: |
| 41 | + |
| 42 | + ```command |
| 43 | + sudo apt install apache2 |
| 44 | + ``` |
| 45 | + |
| 46 | +1. Start the Apache service using the following command: |
| 47 | + |
| 48 | + ```command |
| 49 | + sudo systemctl start apache2 |
| 50 | + ``` |
| 51 | + |
| 52 | +1. To have Apache begin running at system startup, enable the Apache service using the following command: |
| 53 | + |
| 54 | + ```command |
| 55 | + sudo systemctl enable apache2 |
| 56 | + ``` |
| 57 | + |
| 58 | +## Managing Apache |
| 59 | + |
| 60 | +### Apache Service |
| 61 | + |
| 62 | +The Apache service runs on `systemd`, which can be used to manage the Apache service. |
| 63 | + |
| 64 | +1. View the current status of the Apache service using the following command: |
| 65 | + |
| 66 | + ```command |
| 67 | + sudo systemctl status apache2 |
| 68 | + ``` |
| 69 | + |
| 70 | +1. To stop the Apache service, use the following command: |
| 71 | + |
| 72 | + ```command |
| 73 | + sudo systemctl stop apache2 |
| 74 | + ``` |
| 75 | + |
| 76 | + You can then start the Apache service backup using the command below: |
| 77 | + |
| 78 | + ```command |
| 79 | + sudo systemctl start apache2 |
| 80 | + ``` |
| 81 | + |
| 82 | +1. To disable the Apache service, preventing it from beginning automatically at system startup, use the command below: |
| 83 | + |
| 84 | + ```command |
| 85 | + sudo systemctl disable apache2 |
| 86 | + ``` |
| 87 | + |
| 88 | + You can enable the Apache service again using the command below: |
| 89 | + |
| 90 | + ```command |
| 91 | + sudo systemctl enable apache2 |
| 92 | + ``` |
| 93 | + |
| 94 | +1. Restart the Apache service using the command below: |
| 95 | + |
| 96 | + ```command |
| 97 | + sudo systemctl restart apache2 |
| 98 | + ``` |
| 99 | + |
| 100 | +1. To reload Apache's configuration files, use the command below: |
| 101 | +
|
| 102 | + ```command |
| 103 | + sudo systemctl reload apache2 |
| 104 | + ``` |
| 105 | +
|
| 106 | +### Apache Modules |
| 107 | +
|
| 108 | +Apache can be extended and modified with modules. These range from modules that integrate interpreters like PHP and Python, enabling dynamic content, to modules that change Apache's fundamental model for handling connections. (See the next section for more on the latter type of modules, called [Multi-processing Modules](/docs/guides/how-to-install-apache-ubuntu-2404/#multi-processing-modules)). |
| 109 | + |
| 110 | +Apache modules are typically installed via the package manager. After that, you can manage modules through Apache. |
| 111 | + |
| 112 | +1. To search for available modules, use the command below: |
| 113 | + |
| 114 | + ```command |
| 115 | + sudo apt search libapache2-* |
| 116 | + ``` |
| 117 | + |
| 118 | +1. To install a module, use a command like the following. In this and the following examples, the guide uses the `php8.3` module, which is available in the package manager as `libapache2-mod-php8.3`: |
| 119 | + |
| 120 | + ```command |
| 121 | + sudo apt install libapache2-mod-php8.3 |
| 122 | + ``` |
| 123 | + |
| 124 | +1. You can enable a module as follows. When managing a module via Apache, you use the module name, rather than the package name: |
| 125 | + |
| 126 | + ```command |
| 127 | + sudo a2enmod php8.3 |
| 128 | + ``` |
| 129 | + |
| 130 | + Usually, the module name is the last part of the package name, as in `libapache-mod-{module_name}` or `libapache2-{module_name}`. |
| 131 | + |
| 132 | +1. You can subsequently disable a module using the command below: |
| 133 | + |
| 134 | + ```command |
| 135 | + sudo a2dismod php8.3 |
| 136 | + ``` |
| 137 | + |
| 138 | +1. To list the modules currently enabled, use the command below |
| 139 | + |
| 140 | + ```command |
| 141 | + sudo apache2ctl -M |
| 142 | + ``` |
| 143 | + |
| 144 | +## Multi-processing Modules |
| 145 | + |
| 146 | +Apache supports several models for handling connections through a particular kind of module: Multi-processing Modules (MPMs). On many Linux distributions, the `event` module is Apache's default MPM. However, in Ubuntu 24.04, Apache defaults to the `mpm_prefork` module. This change is primarily due to the continued use of `libapache2-mod-php`, which is not thread-safe and requires the single-threaded prefork MPM for compatibility. |
| 147 | +This section provides an overview of each of the three MPMs available and gives you the necessary commands for using them. |
| 148 | + |
| 149 | +### Prefork Module |
| 150 | + |
| 151 | +This MPM provides a single-threaded server. It has a single-parent process that spawns child processes, each of which is responsible for a single incoming request. While the `prefork` MPM is more resource-intensive, it is necessary for applications that do not support multiple threads, like PHP. |
| 152 | + |
| 153 | +1. Enable the `prefork` MPM. Be sure to first disable your current MPM — the `event` MPM in this example: |
| 154 | + |
| 155 | + ```command |
| 156 | + sudo a2dismod mpm_event |
| 157 | + sudo a2enmod mpm_prefork |
| 158 | + ``` |
| 159 | + |
| 160 | +1. Find the configuration file for the `prefork` MPM here: `/etc/apache2/mods-available/mpm_prefork.conf`. Modify the defaults as needed. |
| 161 | + |
| 162 | + The `prefork` MPM is considered highly self-regulating, so usually it is not necessary to adjust its default configuration. However, you may want to review the `MaxRequestWorkers` value. You should ensure that it is large enough to handle the expected request volume but small enough not to exceed hardware memory limits. |
| 163 | + |
| 164 | +1. Restart the Apache service: |
| 165 | + |
| 166 | + ```command |
| 167 | + sudo systemctl restart apache2 |
| 168 | + ``` |
| 169 | + |
| 170 | +### Worker Module |
| 171 | + |
| 172 | +This MPM is a multi-threaded hybrid. Like the `prefork` MPM, it consists of a parent process that spawns child processes. But, unlike the `prefork` MPM, the `worker` MPM's child processes are each multi-threaded, allowing each to handle multiple tasks in parallel. |
| 173 | +
|
| 174 | +1. Enable the `worker` MPM. Be sure to first disable your current MPM — the `event` MPM in this example: |
| 175 | +
|
| 176 | + ```command |
| 177 | + sudo a2dismod mpm_event |
| 178 | + sudo a2enmod mpm_worker |
| 179 | + ``` |
| 180 | +
|
| 181 | +1. Find the configuration file for the `worker` MPM here: `/etc/apache2/mods-available/mpm_worker.conf`. Modify the defaults as needed. |
| 182 | +
|
| 183 | +1. Restart the Apache service: |
| 184 | +
|
| 185 | + ```command |
| 186 | + sudo systemctl restart apache2 |
| 187 | + ``` |
| 188 | +
|
| 189 | +### Event Module |
| 190 | +
|
| 191 | +This MPM functions similarly to the `worker` MPM. However, it adds listener threads. These threads handle the task of waiting on incoming requests, which frees up worker threads to continue processing new requests. |
| 192 | +
|
| 193 | +1. The `event` MPM is enabled by default. Use the following command to re-enable it if needed. Remember to use `a2dismod` to disable any other enabled MPM first, as shown for the `prefork` and `worker` MPMs above: |
| 194 | +
|
| 195 | + ```command |
| 196 | + sudo a2enmod mpm_prefork |
| 197 | + ``` |
| 198 | +
|
| 199 | +1. Find the configuration file for the `event` MPM here: `/etc/apache2/mods-available/mpm_event.conf`. Modify the defaults as needed. |
| 200 | +
|
| 201 | +1. Restart the Apache service: |
| 202 | +
|
| 203 | + ```command |
| 204 | + sudo systemctl restart apache2 |
| 205 | + ``` |
| 206 | +
|
| 207 | +## Using Apache |
| 208 | +
|
| 209 | +This section walks you through setting up your own website using Apache. In doing so, it also illustrates how to use Apache's configuration files to set up *name-based virtual hosting*. This allows you to map one or more domains to your server's IP address. |
| 210 | +
|
| 211 | +### Apache Configuration |
| 212 | +
|
| 213 | +1. Apache comes with a default site configuration. Since this guide is setting up a custom configuration, you need to disable the default using the following command: |
| 214 | +
|
| 215 | + ```command |
| 216 | + sudo a2dissite 000-default.conf |
| 217 | + ``` |
| 218 | +
|
| 219 | +1. Create a configuration file for your site. Site configuration files should typically live in the `/etc/apache2/sites-available` directory, with each file being named according to the domain name you are using it for. |
| 220 | +
|
| 221 | + In this example, replace `example.com` with your site's domain, in both the filename (`example.com.conf`) and in the file's contents. Do the same whenever you see `example.com` from here on: |
| 222 | +
|
| 223 | + ```file {title="/etc/apache2/sites-available/example.com.conf" lang="aconf"} |
| 224 | + <VirtualHost *:80> |
| 225 | + |
| 226 | + ServerName example.com |
| 227 | + ServerAlias www.example.com |
| 228 | + DocumentRoot /var/www/example.com/public/ |
| 229 | + ErrorLog /var/www/example.com/logs/error.log |
| 230 | + CustomLog /var/www/example.com/logs/access.log combined |
| 231 | + </VirtualHost> |
| 232 | + ``` |
| 233 | +
|
| 234 | + This sets up a name-based virtual host for the `example.com` domain. You can host additional domain names by adding a configuration file for each. |
| 235 | +
|
| 236 | +1. Create the directories to be used by your website. The first holds the website's static content and the second holds its log files: |
| 237 | + |
| 238 | + ```command |
| 239 | + sudo mkdir -p /var/www/example.com/public |
| 240 | + sudo mkdir -p /var/www/example.com/logs |
| 241 | + ``` |
| 242 | + |
| 243 | +1. Enable the site in Apache: |
| 244 | + |
| 245 | + ```command |
| 246 | + sudo a2ensite example.com |
| 247 | + ``` |
| 248 | + |
| 249 | +1. Restart Apache for the configuration changes to take effect: |
| 250 | + |
| 251 | + ```command |
| 252 | + sudo systemctl restart apache2 |
| 253 | + ``` |
| 254 | + |
| 255 | +1. Open port `80` on your system's firewall. UFW is the front end typically used to manage firewall rules on Ubuntu. You can use the following command to open port `80` with UFW: |
| 256 | +
|
| 257 | + ```command |
| 258 | + sudo ufw allow http |
| 259 | + ``` |
| 260 | +
|
| 261 | + Refer to our [How to Configure a Firewall with UFW](/docs/guides/configure-firewall-with-ufw/) guide for more on how to use UFW for managing your firewall. |
| 262 | +
|
| 263 | +### Launching the Website |
| 264 | +
|
| 265 | +1. Give the website some content. Create a page for your website. The file should be named `index.html` and stored in your website's `DocumentRoot` directory: |
| 266 | + |
| 267 | + ```file {title="/var/www/example.com/public/index.html" lang="html"} |
| 268 | + <!doctype html> |
| 269 | + <html> |
| 270 | + <body> |
| 271 | + <h1>Hello, World!</h1> |
| 272 | + <p>This is an example website running on the Apache HTTP Web Server.</p> |
| 273 | + </body> |
| 274 | + </html> |
| 275 | + ``` |
| 276 | + |
| 277 | +1. In a browser, visit the domain you set up for your website — `example.com` above. |
| 278 | + |
| 279 | + You should see your website's "Hello, World!" page. |
| 280 | +
|
| 281 | +  |
0 commit comments