How to install Apache on Ubuntu 20.04 LTS

How do I install the Apache on Ubuntu 20.04 LTS Linux server?

The Apache (also known as the “HTTPD”) web server is one of the most popular web servers for serving dynamic and static web pages. It is free and open-source software released under Apache License 2.0. Let us see how to install and the Apache on Ubuntu 20.04 LTS Linux server.
Tutorial requirements
Operating system/appUbuntu Linux
Root privileges required Yes
Difficulty Easy (rss)
Estimated completion time 10m
Table of contents

ADVERTISEMENTS

How to install Apache on Ubuntu 20.04 LTS

Make sure your system is up to date and patched. To do that, type the following apt command:
sudo apt update
sudo apt upgrade

Installing Apache with apt

Step 1 – Installing Apache 2 server

Now that system updated with the latest patches, it is time to install Apache 2 software. In other words, type the following command and press the [Enter] key:
sudo apt install apache2
Install Apache on Ubuntu 20.04 LTS Linux

Step 2 – Make sure Apache service started on boot

We are going to use the systemctl command as follows to enable the apache2.service:
sudo systemctl is-enabled apache2.service
If not enabled, enable it, run:
sudo systemctl enable apache2.service

Managing Apache 2 service on Ubuntu cloud server

To start, stop, restart and then find the service status again use the following commands.

Start the apache2 server

sudo systemctl start apache2.service

Stop the apache2 server

sudo systemctl stop apache2.service

Restart the apache2 server

sudo systemctl restart apache2.service

Reload the apache2 server gracefully

sudo systemctl reload apache2.service

Find the status of apache2 server

sudo systemctl status apache2.service

* apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-05-05 19:49:41 UTC; 7min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 1766 (apache2)
      Tasks: 54 (limit: 4915)
     Memory: 6.5M
     CGroup: /system.slice/apache2.service
             ??1766 /usr/sbin/apache2 -k start
             ??1767 /usr/sbin/apache2 -k start
             ??1768 /usr/sbin/apache2 -k start
 
May 05 19:49:41 db-host systemd[1]: Starting The Apache HTTP Server...
May 05 19:49:41 db-host apachectl[1765]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
May 05 19:49:41 db-host systemd[1]: Started The Apache HTTP Server.

Step 3 – Open the Apache port 80 and 443 using UFW firewall

Execute the following ufw command to port TCP port 80 and 443 for everyone
sudo ufw allow 80/tcp comment 'accept Apache'
sudo ufw allow 443/tcp comment 'accept HTTPS connections'

Verify it:
sudo ufw status
Sample outputs:

Status: active

To                         Action      From
--                         ------      ----
10.105.28.158 22/tcp       ALLOW       192.168.2.0/24            
80/tcp                     ALLOW       Anywhere                   # accept Apache
443/tcp                    ALLOW       Anywhere                   # accept HTTPS connections
80/tcp (v6)                ALLOW       Anywhere (v6)              # accept Apache
443/tcp (v6)               ALLOW       Anywhere (v6)              # accept HTTPS connections

See “How To Configure Firewall with UFW on Ubuntu 20.04 LTS” for more info.

Step 4 – Find your Ubuntu 20.04 LTS server IP address

Run any one of the following command:
hostname -I
ip a
ip a s eth0

My IP Address:

10.105.28.158

You can also use the dig command/host command as follows to find your public IPv4/IPv6 address from the CLI:
dig +short myip.opendns.com @resolver1.opendns.com
See How To Find My Public IP Address From Command Line On a Linux for more info.

Step 5 – Test your Apache 2 installation on Ubuntu

At this stage you can use the curl command as follows:
curl -I http://10.105.28.158
Sample outputs:

HTTP/1.1 200 OK
Date: Tue, 05 May 2020 20:08:32 GMT
Server: Apache/2.4.41 (Ubuntu)
Last-Modified: Tue, 05 May 2020 19:49:22 GMT
ETag: "2aa6-5a4ebf1b4b8bf"
Accept-Ranges: bytes
Content-Length: 10918
Vary: Accept-Encoding
Content-Type: text/html

Another option is to fire a web browser such as Chrome or Firefox and type the URL as follows:
http://your-server-ip
http://10.105.28.158

Verify and check your web Server

Basic configuration

Edit the /etc/apache2/apache2.conf file, run:
sudo nano /etc/apache2/apache2.conf
At least set ServerName to 127.0.0.1 or actual name such as your-dot-com or server IP address:
ServerName 10.105.28.9
Save and close the file. Next, edit the /etc/apache2/ports.conf file that include list of ports to listen on Ubuntu box:
sudo nano /etc/apache2/ports.conf
By default, the Apache version 2 on Ubuntu Linux will listen on the TCP port 80 (HTTP) and 443 (HTTPS). There is no need to change them; however, if you run many sites in Linux containers, then we change ports as follows:

########################################################################################
## Typically you don't have to change the defaults. These are for advance usage/users ##
########################################################################################
# Change HTTP port 80 to 86
Listen 86
 
# Change HTTPS port 443 to 449
<IfModule ssl_module>
        Listen 449
</IfModule>
 
<IfModule mod_gnutls.c>
        Listen 449
</IfModule>

You must update your virtual host/domain config to match the port number listed in ports.conf.

Step 6 – Configuring Apache 2 virtual hosts

Create a config file for your domain as follows:
sudo nano /etc/apache2/sites-available/my-domain-name.conf
Append the following config:

# Replace my-domain-name-here with actual domain name such as cyberciti.biz #
<VirtualHost *:80>
    ServerAdmin webmaster@my-domain-name-here
    ServerName my-domain-name-here
    ServerAlias www.my-domain-name-here
    DocumentRoot /home/my-domain-name-here/html
    DirectoryIndex index.html
    ErrorLog ${APACHE_LOG_DIR}/my-domain-name-here-error.log
    CustomLog ${APACHE_LOG_DIR}/my-domain-name-here-access.log combined
</VirtualHost>
<Directory /home/my-domain-name-here/html>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

Create a new Ubuntu user for website

Type the following useradd command sudo useradd -d /home/my-domain-name-here -m -k /dev/null -s /usr/sbin/nologin usernamehere
Where,

  • -d /home/my-domain-name-here : Set home directory of the new account to serve documets.
  • -m : Make sure we create the user’s home directory set by the -d option.
  • -k /dev/null : Avoid creating dot files for Apache virtual domain DocumentRoot which can expose senstive information by using the /dev/null as alternative skeleton directory.
  • -s /usr/sbin/nologin : Set login shell of the new account to /usr/sbin/nologin, so that web server user can not login into our system using the ssh or any other method. Again this is a security feature.
  • usernamehere : User name that will store files for our virtual domain

Lock the Linux user account, enter:
sudo passwd -l usernamehere
Create html folder using the mkdir command:
sudo mkdir -pv /home/my-domain-name-here/html
Create a sample html page as follows:
sudo nano /home/my-domain-name-here/html/index.html

<html>
<head>
 <title>www.cyberciti.biz - welcome</title>
</head>
<body>
 <h2>www.cyberciti.biz</h2>
 <p>This is a test page running on:</p>
 <ul>
	 <li>Ubuntu Linux 20.04 LTS</li>
	 <li>Apache 2.x</li>
 </ul>
 <hr>
 <small>webmaster@cyberciti.biz</small>
</body>
</html>

Set permission using the chown command:
sudo chown -R usernamehere:usernamehere /home/my-domain-name-here/
Turn on newly created virtual domain, run:
sudo a2ensite my-domain-name.conf
sudo a2dissite 000-default.conf

Sample outputs:

Enabling site my-domain-name.conf.
To activate the new configuration, you need to run:
  systemctl reload apache2

Test config:
sudo apache2ctl configtest
You must get “Syntax OK” message and then restart the Apache server on Ubuntu Linux:
sudo systemctl reload apache2
Set your domains’s A and AAAA records to server’s public IPv4/IPv6 address and test it:
http://my-domain-name-here
http://www.cyberciti.biz

Testing Apache 2 on Ubuntu 20.04

Conclusion

Now you have your Apache 2 web server installed on Ubuntu Linux 20.04 LTS. You can configure PHP/Python apps or install TLS/SSL certificates to secure traffic. I strongly urge that you read Apache 2.4 docs here.

This entry is 1 of 2 in the Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 20.04 Tutorial series. Keep reading the rest of the series:
  1. How to install Apache on Ubuntu 20.04 LTS
  2. Secure Apache with mod_md Let’s Encrypt on Ubuntu 20.04 LTS
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
2 comments… add one
  • Anonymous Sep 13, 2020 @ 18:27

    There is a typo in the phrase, If not enabled, enable it, run:
    sudo systemctl enabled apache2.service"
    The correct command is:
    sudo systemctl enable apache2.service
    Where the “.service” part can be even omitted. Moreover is not raccomended to make a Linux user for each domain and use the user home directory as the domain root.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.