How to Install Apache 2 on Ubuntu 18.04
A free and open-source web server known as Apache HTTP Server transmits web content over the internet. The most frequently used web server worldwide is Apache HTTP. It offers a variety of potent features, such as dynamically loadable modules, extensive integration with other well-known programs, and intense media support.
You will discover how to set up an Apache web server on your server running Ubuntu 18.04 in this guide.
Prerequisites for Making the Installation
You must take care of a few prerequisites before installing Apache.
- Ubuntu 18.04 running on a device.
- SSH access to the server is available.
- Access to a sudo-privileged user account.
- A firewall is installed on the server to prevent access to unnecessary ports.
Step 1: Installing Apache
Because Apache is part of Ubuntu’s default software repositories, it can be installed using conventional package management tools. Let’s start by updating the local package index with the most recent upstream changes.
sudo apt update
After that, set up the apache2 package.
sudo apt install apache2
apt will set up Apache and all necessary dependencies after the installation is confirmed.
To check the installed Apache version, you can use the following command.
apache2 -V
Here is the output:
~$ apache2 -V
Server version: Apache/2.4.29 (Ubuntu)
Step 2: Adjusting the Firewall
It is necessary to change the firewall settings to permit external access to the default web ports before testing Apache. A UFW firewall should be set up to limit access to your server if you follow the instructions in the prerequisites. In order to provide a few application profiles that can be used to enable or disable access to Apache through the firewall, Apache registers with UFW during installation.
Run the following command to list the ufw application profiles:
sudo ufw app list
Here is the output:
~$ sudo ufw app list
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
There are three profiles for Apache, according to this list:
- Apache: Only port 80 is open with this profile (normal, unencrypted web traffic)
- Apache Full: This profile allows both port 443 (TLS/SSL encrypted traffic) and port 80 (typical, unencrypted web traffic) to be used.
- Using Apache Secure, only port 443 (for TLS/SSL-encrypted traffic) is opened.
It’s advised that you activate the profile with the tightest restrictions while still allowing the configured traffic. You only need to permit traffic on port 80 since you haven’t set up SSL for your server yet in this guide:
sudo ufw allow 'Apache'
Here is the output:
$ sudo ufw allow 'Apache'
Rules updated
Rules updated (v6)
Now, the output will show the allowed HTTP traffic:
~$ sudo ufw status
Status: active
To Action From
-- ------ ----
Apache ALLOW Anywhere
OpenSSH ALLOW Anywhere
Apache (v6) ALLOW Anywhere (v6)
OpenSSH (v6) ALLOW Anywhere (v6)
To enable access to the web server, the Apache profile has now been activated.
Step 3: Checking your Web Server
Ubuntu 18.04 launches Apache after the installation process is complete. The web server should already be operational. To ensure that the service is running, check the systemd init system:
sudo systemctl status apache2
Here is the output:
~$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Thu 2022-11-24 20:49:14 UTC; 45s ago
Main PID: 2766 (apache2)
Tasks: 55 (limit: 1134)
CGroup: /system.slice/apache2.service
├─2766 /usr/sbin/apache2 -k start
├─2769 /usr/sbin/apache2 -k start
└─2770 /usr/sbin/apache2 -k start
Nov 24 20:49:14 ip-172-31-42-243 systemd[1]: Starting The Apache HTTP Server...
Nov 24 20:49:14 ip-172-31-42-243 systemd[1]: Started The Apache HTTP Server.
Step 4: Managing the Apache Process
Using the following command, you can stop the Apache web server.
sudo systemctl stop apache2
If the Apache web server has stopped, use the following command to start it.
sudo systemctl start apache2
Using the following command, you can restart the Apache web server.
sudo systemctl restart apache2
Apache can frequently reload without losing connections if you are only making configuration changes. Run the following command to accomplish this.
sudo systemctl reload apache2
When the server boots, Apache is set up by default to launch automatically. You can stop this behavior by doing the following if that’s not what you want.
sudo systemctl disable apache2
Alternately, run the following command to enable or re-enable the service to start up at boot:
sudo systemctl enable apache2
Now, whenever the server boots up again, Apache should launch automatically.
Optional Step: Install and Enable PHP
Our setup’s PHP component will process code to render dynamic content. It has the ability to execute scripts, connect to our MySQL databases to retrieve data, and then pass the finished product to our web server for display.
We can use the apt system to install our components once more. We’ll also add a few helper packages so that PHP code can communicate with our MySQL database while running on the Apache web server:
sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql
That’s it 🙂