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
Update all packages on the server
Install an Apache Web Server
Check Apache installation
Enable Apache Web Server
Configure the firewall
Personalize the HTML page
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.
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
Step 3: Check Apache installation
We can confirm it by checking the version of the installed package:
apache2 -v
And the status of the service:
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
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
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
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
Then display the available applications:
sudo ufw app list
Use the following command to allow access to ssh:
sudo ufw allow ssh
And the following command to allow access to port 80:
sudo ufw allow Apache
Now we are safe to enable it and check the status again:
echo "y" | sudo ufw enable
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
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
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)