How to install Apache on Ubuntu
20.04 LTS
Author: Vivek Gite Last updated: November 11, 2020
H ow 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
Requirements Ubuntu Linux
Root privileges Yes
Difficulty Easy
Est. reading time 10m
Table of contents
1 Installing Apache 2
2 Apache 2 service management
3 UFW Firewall configuration
4 Find server IP address
5 Test Apache 2
6 Configuring Apache 2
7 Conclusion
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
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
Step 2 – Make sure Apache service started on boot
We are going to use the systemctl command as follows to enable the
[Link]:
sudo systemctl is-enabled [Link]
If not enabled, enable it, run:
sudo systemctl enable [Link]
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 [Link]
Stop the apache2 server
sudo systemctl stop [Link]
Restart the apache2 server
sudo systemctl restart [Link]
Reload the apache2 server gracefully
sudo systemctl reload [Link]
Find the status of apache2 server
sudo systemctl status [Link]
* [Link] - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/[Link]; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-05-05 [Link] UTC; 7min ago
Docs: [Link]
Main PID: 1766 (apache2)
Tasks: 54 (limit: 4915)
Memory: 6.5M
CGroup: /[Link]/[Link]
??1766 /usr/sbin/apache2 -k start
??1767 /usr/sbin/apache2 -k start
??1768 /usr/sbin/apache2 -k start
May 05 [Link] db-host systemd[1]: Starting The Apache HTTP Server...
May 05 [Link] db-host apachectl[1765]: AH00558: apache2: Could not reliably determine the
server's fully qualified domain name, using [Link]. Set the 'ServerName' directive globally
to suppress this message
May 05 [Link] 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
-- ------ ----
[Link] 22/tcp ALLOW [Link]/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:
[Link]
You can also use the dig command/host command as follows to find your
public IPv4/IPv6 address from the CLI:
dig +short [Link] @[Link]
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 [Link]
Sample outputs:
HTTP/1.1 200 OK
Date: Tue, 05 May 2020 [Link] GMT
Server: Apache/2.4.41 (Ubuntu)
Last-Modified: Tue, 05 May 2020 [Link] 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:
[Link]
[Link]
Basic configuration
Edit the /etc/apache2/[Link] file, run:
sudo nano /etc/apache2/[Link]
At least set ServerName to [Link] or actual name such as your-dot-com or
server IP address:
ServerName [Link]
Save and close the file. Next, edit the /etc/apache2/[Link] file that include
list of ports to listen on Ubuntu box:
sudo nano /etc/apache2/[Link]
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 [Link].
Step 6 – Configuring Apache 2 virtual hosts
Create a config file for your domain as follows:
sudo nano /etc/apache2/sites-available/[Link]
Append the following config:
# Replace my-domain-name-here with actual domain name such as [Link] #
<VirtualHost *:80>
ServerAdmin webmaster@my-domain-name-here
ServerName my-domain-name-here
ServerAlias [Link]-domain-name-here
DocumentRoot /home/my-domain-name-here/html
DirectoryIndex [Link]
ErrorLog ${APACHE_LOG_DIR}/[Link]
CustomLog ${APACHE_LOG_DIR}/[Link] 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 commandsudo 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/[Link]
<html>
<head>
<title>[Link] - welcome</title>
</head>
<body>
<h2>[Link]</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@[Link]</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 [Link]
sudo a2dissite [Link]
Sample outputs:
Enabling site [Link].
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:
[Link]
[Link]