How to install Apache on Ubuntu 20.04

s2 0

Apache is one of the most popular web servers in the world. It’s an open source, cross-platform HTTP server that powers much of the internet’s websites. Apache offers many powerful functions that can be expanded with additional modules.

This tutorial describes how to install and manage the Apache web server on Ubuntu 20.04.

requirements

Before starting the tutorial, make sure you are logged in as a user with sudo permissions.

Install Apache

Apache is included in the standard Ubuntu repositories.

Installation is pretty straightforward. On Ubuntu and Debian systems the name of the package is Apache and the service apache2.

Run the following commands to update the package index and install Apache:

sudo apt updatesudo apt install apache2

When the installation process is complete, the Apache service will start automatically.

You can check if Apache is running by typing:

sudo systemctl status apache2

The output should tell you that the service is running and enabled to start at system startup:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-05-09 19:28:40 UTC; 36min ago
...

That’s it, you’ve successfully installed Apache on your Ubuntu 20.04 server and you’re ready to use it.

Open HTTP and HTTPs ports

Apache listens on port 80 (HTTP) and 443 (HTTPS). You have to open these ports in your firewall so that the web server can be reached from the Internet.

Assuming you use UFW
, you can do this by enabling the “Apache Full” profile, which contains rules for both ports:

sudo ufw allow 'Apache Full'

Check the change:

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)

Check the Apache installation

To check that everything is working properly, open your browser, enter your server IP address https://YOUR_IP_OR_DOMAIN/, and you will see the default Ubuntu 20.04 Apache welcome page as shown below:

The page provides some basic information about Apache configuration files, helper scripts, and directory locations.

Set up a virtual host

A virtual host is an Apache configuration statement that allows you to run more than one website on a single server. Typically a virtual host describes a website.

Apache comes with a virtual host enabled by default. All domains referencing the server IP address match the default virtual host. If you host a single website, you can upload its content to /var/www/html and edit the editing of the virtual host configuration that is in the /etc/apache2/sites-enabled/000-default.conf File.

If you plan to host more than one website, you will need to create a virtual host configuration for each website. In this section, we’ll set up a website for a domain called example.com. You should replace “example.com” with your domain name.

The first step is to create the document root that will store the website files for the domain name and serve them in response to requests. Run the following command to create the directory:

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

Create a index.html File in the root of the domain document:

/var/www/example.com/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

Save and close the file when you are done.

To avoid permissions problems, change ownership of the domain document root directory to the Apache user (www-data):

sudo chown -R www-data: /var/www/example.com

The next step is to create a virtual host configuration for the example.com domain. It is best to save each vhost configuration in a separate file.

Apache vhosts files are stored in /etc/apache2/sites-available Directory. The standard naming convention is to name the file according to the domain.

Open your text editor and create the following file:

/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Apache does not read the configuration files that are in the /etc/apache2/sites-available Directory unless they are with the /etc/apache2/sites-enabled Directory.

To activate the virtual host configuration, create a symlink with the a2ensite Utility:

sudo a2ensite example.com

Test the configuration for syntax errors with:

sudo apachectl configtest

If there are no errors, you will see the following output:

Syntax OK

Restart the Apache service for the changes to take effect:

sudo systemctl restart apache2

Finally, to check that everything is working as expected, open https://example.com in your browser and you will see something like this:

diploma

We showed you how to install Apache on Ubuntu 20.04. You can now start deploying your applications and using Apache as a web or proxy server.

If you have any questions or feedback, please feel free to leave a comment.

This post is part of the How to install the LAMP stack on Ubuntu 20-04 Series.
Further articles in this series:


How to install MySQL on Ubuntu 20.04

How to install Apache on Ubuntu 20.04

How to install PHP on Ubuntu 20.04

How to set up Apache virtual hosts on Ubuntu 20.04

Secure Apache with Let’s Encrypt on Ubuntu 20.04

This post is part of the How to install the LAMP stack on Ubuntu 20-04 Series.
Further articles in this series:


How to install MySQL on Ubuntu 20.04

How to install Apache on Ubuntu 20.04

How to install PHP on Ubuntu 20.04

How to set up Apache virtual hosts on Ubuntu 20.04

Secure Apache with Let’s Encrypt on Ubuntu 20.04

How to install MySQL on Ubuntu 20.04

How to install Apache on Ubuntu 20.04

How to install PHP on Ubuntu 20.04

How to set up Apache virtual hosts on Ubuntu 20.04

Secure Apache with Let’s Encrypt on Ubuntu 20.04