How to Install an Apache Web Server on Ubuntu 18.04 LTS
Tags: ubuntu, apache, linux, bash
Published: 2022-11-30 14:35:09


### 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](https://www.techopedia.com/definition/4851/apache-web-server))*

**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](https://simple.wikipedia.org/wiki/Ubuntu))*

### 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

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

![](https://cdn-images-1.medium.com/max/2000/1*HdV-RHaUvL1TduCViMOBKw.png)

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

**Step 2: Install an Apache Web Server**

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

    sudo apt install apache2 -y

![Apache installed](https://cdn-images-1.medium.com/max/2502/1*CPxL8jBlZSzc7_LaREMLEQ.png)

**Step 3: Check Apache installation**

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

    apache2 -v

![](https://cdn-images-1.medium.com/max/2000/1*wjEPLH25t56tZdUVhjv1WA.png)

And the status of the service:

![“active” and “running”](https://cdn-images-1.medium.com/max/2000/1*bEok9LZ7MmU9ROC4h7V7BQ.png)

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

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

    curl ifconfig.me; echo

![public IP address of the server](https://cdn-images-1.medium.com/max/2000/1*6WwV45S7G4dejdz2KXSYNg.png)

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

![private IP address of the server](https://cdn-images-1.medium.com/max/2000/1*KXRe1yoUa7VHftEJjgWjVg.png)

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

![](https://cdn-images-1.medium.com/max/2000/1*SNxSNpGYSAA2Sv8uTWTuOw.png)

**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

![Apache will run on system startup](https://cdn-images-1.medium.com/max/2000/1*awpq6G4GXxbdFj8059v2tg.png)

**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

![ufw currently inactive](https://cdn-images-1.medium.com/max/2000/1*4Q8TqUo3ANBblugaWQokiQ.png)

Then display the available applications:

    sudo ufw app list

![list of all available apps](https://cdn-images-1.medium.com/max/2000/1*bKybW-cznemyvaqvpr85aw.png)

Use the following command to allow access to ssh:

    sudo ufw allow ssh

![ssh allowed on ufw](https://cdn-images-1.medium.com/max/2000/1*ktPw8df6F8J9dC4yC1W1GA.png)

And the following command to allow access to port 80:

    sudo ufw allow Apache

![Apache allowed on ufw](https://cdn-images-1.medium.com/max/2000/1*Lj2w2bQCRcwJXIHc2QO0uw.png)

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

    echo "y" | sudo ufw enable

![ufw enabled](https://cdn-images-1.medium.com/max/2278/1*VuC8Db7cAIscTWRiCh6Yrg.png)

**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

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

![](https://cdn-images-1.medium.com/max/2000/1*yjgYpZrA59rHOurKNfT9Kw.png)

If we try again the page on the browser it will show “Welcome to our Apache Web Server”: [http://ip_of_the_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.
{% gist https://gist.github.com/lucianobm/2f8f0874ff6d2cd2db69bd2610a3cd4e.js %}

    sudo bash apache.sh

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!