DEV Community

Cover image for How to Install an Apache Web Server on Ubuntu 18.04 LTS
lucianobm
lucianobm

Posted on

How to Install an Apache Web Server on Ubuntu 18.04 LTS

Introduction

This article will guide you install the Apache web server on Ubuntu 18.04 LTS Server (it has also been tested on version 20.04).

Apache web server is “an open-source web server creation, deployment and management software. Initially developed by a group of software programmers, it is now maintained by the Apache Software Foundation.” (Source)

Ubuntu “is a free operating system that uses the Linux kernel. It is one of the most popular Linux distributions and it is based on Debian Linux computer operating system. A new release of Ubuntu is released every six months, with long-term support (LTS) releases every two years.” (Source)

Prerequisites

  • A system running Ubuntu 18.04 LTS (Bionic Beaver) or Ubuntu 20.04 LTS (Focal Fossa)

  • A terminal or command line interface (CLI)

  • Access to a user account with sudo privileges

Summary

  1. Update all packages on the server

  2. Install an Apache Web Server

  3. Check Apache installation

  4. Enable Apache Web Server

  5. Configure the firewall

  6. Personalize the HTML page

  7. Create and run a Bash Script to automate the installation (optional)

Let the fun begin!

Step 1: Update all packages on the server

Before installing a new software, it’s a best practice to fetch the latest version of the package list from your distro’s software repository with the command below:

sudo apt update -y
Enter fullscreen mode Exit fullscreen mode

The *-y *option will automatically agree to user prompts generated by this command.

After that we can go to the next commands. The first one is to list the packages that can be upgraded and the second one to actually download and upgrade them.

sudo apt list --upgradable
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install an Apache Web Server

Now we can move forward and install Apache with the following command:

sudo apt install apache2 -y
Enter fullscreen mode Exit fullscreen mode

Apache installed

Step 3: Check Apache installation

We can confirm it by checking the version of the installed package:

apache2 -v
Enter fullscreen mode Exit fullscreen mode

And the status of the service:

“active” and “running”

Also we can open the Apache2 Ubuntu default page on the browser and confirm it’s working: http://ip_of_the_server

To grab the public IP of the server try the command below:

curl ifconfig.me; echo
Enter fullscreen mode Exit fullscreen mode

public IP address of the server

Depending on your network you might want the private IP of the server. If this is your case, you can get the IP using the following command:

hostname -I
Enter fullscreen mode Exit fullscreen mode

private IP address of the server

We should be able to see the following page on the browser:

Step 4: Enable Apache Web Server

If we want to start Apache Web Server as a service on the boot of the server, we should go through the command below:

systemctl enable apache2
Enter fullscreen mode Exit fullscreen mode

Apache will run on system startup

Step 5: Configure the firewall

We can install and configure a firewall on the server to expose to the internet only the needed ports. To do that we will use UFW (Uncomplicated Firewall).

Since we are currently connected to the server through ssh, our connection will be blocked preventing us from accessing the server if we enable ufw before allowing the ssh.

Start by checking the status of the firewall:

sudo ufw status
Enter fullscreen mode Exit fullscreen mode

ufw currently inactive

Then display the available applications:

sudo ufw app list
Enter fullscreen mode Exit fullscreen mode

list of all available apps

Use the following command to allow access to ssh:

sudo ufw allow ssh
Enter fullscreen mode Exit fullscreen mode

ssh allowed on ufw

And the following command to allow access to port 80:

sudo ufw allow Apache
Enter fullscreen mode Exit fullscreen mode

Apache allowed on ufw

Now we are safe to enable it and check the status again:

echo "y" | sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

ufw enabled

Step 6: Personalize the HTML page

Now that we have our Apache Web Server up and running, we can edit the index.html file and change the default page of our server.

First we need to change the owner of the file:

sudo chown -R $USER:$USER /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

And then we will be able to edit:

sudo cat > /var/www/html/index.html <<- "EOF"

<!DOCTYPE html>
<html><head>
<title>Apache Web Server</title>
</head><body>
<h1>Welcome to our Apache Web Server</h1>
</body></html>

EOF
Enter fullscreen mode Exit fullscreen mode

If we try again the page on the browser it will show “Welcome to our Apache Web Server”: http://ip_of_the_server

Success!

We have successfully installed the Apache Web Server software on our Ubuntu, configured the firewall and personalized the default index page.

Step 7: Create and run a Bash Script to automate the installation (optional)

Copy and paste the text below on the terminal of the server and then execute the script with the next command.

sudo bash apache.sh
Enter fullscreen mode Exit fullscreen mode

If this step-by-step guide was helpful to you, click the like button and drop a comment below! I can’t wait to hear from you!

Top comments (0)