Linux

How to Install Apache on Ubuntu 20.04

How to Install Apache on Ubuntu 20.04

Apache is one of the most popular web servers in the world. This is an open-source and cross-platform HTTP server that supports most Internet websites. Apache provides many advanced features that can be expanded through additional modules.

This tutorial explains how to install and manage an Apache web server on Ubuntu 20.04.

Precondition

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

Install Apache

Apache is included in Ubuntu’s default repository.

Installation is quite easy. On Ubuntu and Debian systems, the Apache package and its service are called apache2.

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

sudo apt update
sudo apt install apache2

During the installation process, completing the Apache service will automatically start.

You can verify that Apache is running by typing:

sudo systemctl status apache2

The output will tell you that the service is running and is activated to start on system boot:

Output :

 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 have successfully installed Apache on your Ubuntu 20.04 server, and you can start using it.

Open HTTP and HTTPs Ports

Apache listens on ports 80 (HTTP) and 443 (HTTPS). You must open the port on your firewall so that the web server can be accessed from the Internet.

Assuming you are using UFW, you can do this by activating the ‘Apache Full’ profile which includes rules for both ports:

sudo ufw allow 'Apache Full'

Verify the change:

sudo ufw status

Output :

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)

Verifying the Apache Installation

To verify that everything is functioning correctly, open your browser, type your server’s IP address http: // YOUR_IP_OR_DOMAIN /, and you will see the default Apache 20.04 Apache welcome page as shown below:

1

This page includes some basic information about Apache configuration files, helper scripts, and directory locations.

Setting up a Virtual Host

Virtual Host is an Apache configuration directive that allows you to run more than one website on one server. Usually a virtual host describes one website.

Apache is shipped with one virtual host that is activated by default. All domains that point to the server’s IP address will match the default virtual host. If you are hosting a single website, you can upload the contents in / var / www / html and edit the virtual host configuration found in /etc/apache2/sites-enabled/000-default.conf file.

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

The first step is to create a document’s root directory where website files for the domain name will be stored and presented in response to requests. Run the following command to create a directory:

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

For testing purposes, create an index.html file in the domain document’s root directory:

/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 finished.

To avoid permission issues, change ownership of the domain document’s root directory to Apache users (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. The best practice is to save each vhost configuration in a separate file.

Apache vhosts files are stored in the / etc / apache2 / sites-available directory. The standard naming convention is to give a file name 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 webmaster@example.com
    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 configuration files found in the / etc / apache2 / sites-available directory unless they are connected to the / etc / apache2 / sites-enabled directory.

To enable virtual host configuration, create a symlink using the a2ensite utility:

sudo a2ensite example.com

Test the configuration for each syntax error by:

$ sudo apachectl configtest

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

Output :

Syntax OK

Restart the Apache service for the changes to take effect:

$ sudo systemctl restart apache2

Finally, to verify that everything works as expected, open http://example.com in your browser, and you will see something like this:

1

Conclusion

We have shown you how to install Apache on Ubuntu 20.04. You are now ready to start using your application and use Apache as a web or proxy server.

If you have questions or feedback, don’t hesitate to leave a comment.

Related posts

How to Install GCC (build-essential) on Ubuntu 20.04

Linux

How to install Lightworks on Ubuntu

Linux

How to Add Elements to a List in Python (add, add and enter)

Linux

How to use the chmod command on Linux

Howto

How to Install Elasticsearch, Logstash, and Kibana (ELK Stack) on Ubuntu 20.04

Linux

How to Install a Plex Media Server on Ubuntu 20.04 / Linux Mint 20

Linux

How to Install Ruby On Rails on Ubuntu 20.04

Linux

How to Install Steam on Ubuntu 20.04

Linux

How to Destroy / Erase Data Safely on Hard Drive with Damaged on Linux

Linux