How to Install and Use Apache on Ubuntu 20.04
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
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 20.04 and learn how to get started using it.
Before You Begin
Familiarize yourself with our Getting Started with Linode guide, and complete the steps for setting your Linode’s hostname and timezone.
This guide uses
sudo
wherever possible. Complete the sections of our How to Secure Your Server guide to create a standard user account, harden SSH access, and remove unnecessary network services.Update your system:
sudo apt update && sudo apt upgrade
sudo
. If you’re not familiar with the sudo
command, see the
Linux Users and Groups guide.Installing Apache
Install Apache from the package manager using the following command:
sudo apt install apache2
Start the Apache service using the following command:
sudo systemctl start apache2
To have Apache begin running at system startup, enable the Apache service using the following command:
sudo systemctl enable apache2
Managing Apache
Apache Service
The Apache service runs on systemd
, which can be used to manage the Apache service.
View the current status of the Apache service using the following command:
sudo systemctl status apache2
To stop the Apache service, use the following command:
sudo systemctl stop apache2
You can then start the Apache service backup using the command below:
sudo systemctl start apache2
To disable the Apache service, preventing it from beginning automatically at system startup, use the command below:
sudo systemctl disable apache2
You can enable the Apache service again using the command below:
sudo systemctl enable apache2
Restart the Apache service using the command below:
sudo systemctl restart apache2
To reload Apache’s configuration files, use the command below:
sudo systemctl reload apache2
Apache Modules
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).
Apache modules are typically installed via the package manager. After that, you can manage modules through Apache.
To search for available modules, use the command below:
sudo apt search libapache2-*
To install a module, use a command like the following. In this and the following examples, the guide uses the
php7.4
module, which is available in the package manager aslibapache2-mod-php7.4
:sudo apt install libapache2-mod-php7.4
You can enable a module as follows. When managing a module via Apache, you use the module name, rather than the package name:
sudo a2enmod php7.4
Usually, the module name is the last part of the package name, as in
libapache-mod-{module_name}
orlibapache2-{module_name}
.You can subsequently disable a module using the command below:
sudo a2dismod php7.4
To list the modules currently enabled, use the command below
sudo apache2ctl -M
Multi-processing Modules
Apache supports several models for handling connections through a particular kind of module: Multi-processing Modules (MPMs). On Ubuntu and many other Linux distributions, the event
module is Apache’s default MPM. This section provides an overview of each of the three MPMs available and gives you the necessary commands for using them.
Prefork Module
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.
Enable the
prefork
MPM. Be sure to first disable your current MPM — theevent
MPM in this example:sudo a2dismod mpm_event sudo a2enmod mpm_prefork
Find the configuration file for the
prefork
MPM here:/etc/apache2/mods-available/mpm_prefork.conf
. Modify the defaults as needed.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 theMaxRequestWorkers
value. You should ensure that it is large enough to handle the expected request volume but small enough not to exceed hardware memory limits.Restart the Apache service:
sudo systemctl restart apache2
Worker Module
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.
Enable the
worker
MPM. Be sure to first disable your current MPM — theevent
MPM in this example:sudo a2dismod mpm_event sudo a2enmod mpm_worker
Find the configuration file for the
worker
MPM here:/etc/apache2/mods-available/mpm_worker.conf
. Modify the defaults as needed.Restart the Apache service:
sudo systemctl restart apache2
Event Module
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.
The
event
MPM is enabled by default. Use the following command to re-enable it if needed. Remember to usea2dismod
to disable any other enabled MPM first, as shown for theprefork
andworker
MPMs above:sudo a2enmod mpm_prefork
Find the configuration file for the
event
MPM here:/etc/apache2/mods-available/mpm_event.conf
. Modify the defaults as needed.Restart the Apache service:
sudo systemctl restart apache2
Using Apache
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.
Apache Configuration
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:
sudo a2dissite 000-default.conf
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.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 seeexample.com
from here on:- File: /etc/apache2/sites-available/example.com.conf
1 2 3 4 5 6 7 8
<VirtualHost *:80> ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public/ ErrorLog /var/www/example.com/logs/error.log CustomLog /var/www/example.com/logs/access.log combined </VirtualHost>
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.Create the directories to be used by your website. The first holds the website’s static content and the second holds its log files:
sudo mkdir -p /var/www/example.com/public sudo mkdir -p /var/www/example.com/logs
Enable the site in Apache:
sudo a2ensite example.com
Restart Apache for the configuration changes to take effect:
sudo systemctl restart apache2
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 port80
with UFW:sudo ufw allow http
Refer to our How to Configure a Firewall with UFW guide for more on how to use UFW for managing your firewall.
Launching the Website
Give the website some content. Create a page for your website. The file should be named
index.html
and stored in your website’sDocumentRoot
directory:- File: /var/www/example.com/public/index.html
1 2 3 4 5 6 7
<!doctype html> <html> <body> <h1>Hello, World!</h1> <p>This is an example website running on the Apache HTTP Web Server.</p> </body> </html>
In a browser, visit the domain you set up for your website —
example.com
above.You should see your website’s “Hello, World!” page.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on