Install Apache Web Server
The Apache HTTP server is the most widely-used web server in the world.
Table of Contents
It provides the following powerful features:
- Loadable modules
- Robust media support
- Extensive integration
Moreover, it is customizable, and you can integrate it with other modules. Thus, installing and configuring Apache for basic setup is quite easy. This guide will walk you through the process of installing Apache on Ubuntu 20.04.
We will also cover the following in this guide:
- Manage Apache2 Services
- Opening Webserver port in Firewall
- Testing the Apache2 Installation
- Configure a Virtual Host Environment
Prerequisites
On your server, make sure you have a regular, non-root user with Sudo privileges configured. You must also enable a basic firewall to prevent the use of non-essential ports. Learn how to configure your server by reading our initial server setup guide for Ubuntu 20.04.
Install Apache Web Server on Ubuntu 20.04
Apache is available in Ubuntu’s default software repositories. Thus, making it possible to install using the conventional package management tools.
First, let us reflect on the latest upstream changes by updating the local package index:
sudo apt update
Install the apache2 package after that:
sudo apt install apache2
Apache2 installer during installation triggers systems
to start and enable the apache2 services. Use the following commands to verify apache2 services are active and enabled.
$ sudo systemctl is-active apache2
$ sudo systemctl is-enabled apache2
$ sudo systemctl status apache2
Configure Firewall
To access Apache from the outside, we will need to open up several ports on our server. Let’s start by listing the application profiles to which Apache needs access. To do so, use the following command:
$ sudo ufw app list
To enable network connectivity on port 80
, we will use the restrictive profile “Apache.”
$ sudo ufw allow ‘Apache.’
$ sudo ufw status
Check Web Server
Ubuntu 20.04 starts Apache at the end of the installation process. So the webserver should be up and running at this point.
To check that the service is up and running, use the systemd init command:
sudo systemctl status apache2
Output
apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2020-04-23 22:36:30 UTC; 20h ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 29435 (apache2)
Tasks: 55 (limit: 1137)
Memory: 8.0M
CGroup: /system.slice/apache2.service
├─29435 /usr/sbin/apache2 -k start
├─29437 /usr/sbin/apache2 -k start
└─29438 /usr/sbin/apache2 -k start
The service has started, as seen by this output. But, requesting a page via Apache is the best method to test this.
To verify that the software is running through your IP address, go to the default Apache landing page. If you do not know your server’s IP address, you can get it from the command line in a few different ways.
hostname -I
You will get a list of addresses separated by spaces as a response. You can test each one in your browser to see whether it works.
Another method is to use the Icanhazip tool. Which should provide you with your public IP address as read from another location:
curl -4 icanhazip.com
Enter your server’s IP address into the address bar of your browser:
http://your_server_ip
This page shows that Apache is up and running. And the basic information about the locations of important Apache files and directories.
Manage Apache Web Server in Ubuntu 20.04
Now that we have the webserver up and running. Let’s go through some basic systemctl
management commands.
sudo systemctl stop apache2
sudo systemctl start apache2
sudo systemctl restart apache2
When making configuration changes, Apache can often reload without losing connections. Use the following command to do this:
sudo systemctl reload apache2
Apache is set to start the moment your server boots up by default. If that’s not something you want, use the following command to disable the behavior:
sudo systemctl disable apache2
Use the following command to re-enable the behavior for the webserver to start at bootup:
sudo systemctl enable apache2
Configure Apache In Ubuntu 20.04
/etc/apache2
directory includes all Apache2 configuration files. And with the following is command, you can see all files and subdirectories under it.
$ ls /etc/apache2/*
- etc/apache2/apache2.conf – Main global Apache configuration file that includes all other configuration files.
- /etc/apache2/conf-available – stores the available configurations.
- /etc/apache2/conf-enabled – Includes all enabled configurations.
- /etc/apache2/mods-available – Includes all available modules.
- /etc/apache2/mods-enabled – Includes all enabled modules.
- /etc/apache2/sites-available – includes the configuration file for the sites available (virtual hosts).
- /etc/apache2/sites-enabled – contains a configuration file for sites enabled (virtual hosts).
apachectl[2996]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.2.15.
In the main apache configuration file, set the ‘ServerName’ Directive to remove this message globally.
/etc/apache2/apache2.conf file.
Use your preferred text editor to open the file to edit it.
$ sudo vim /etc/apache2/apache2.conf
webserver1.yehiweb.com
with your FQDN).
ServerName webserver1.yehiweb.com
$ sudo apache2ctl configtest
$ sudo systemctl restart apache2
$ sudo systemctl status apache2
Test Apache Web Server installation on Ubuntu 20.04
http://SERVER_IP
$ curl ifconfig.co
OR
$ curl ifconfig.me
OR
$ curl icanhazip.com
If the Apache Ubuntu default welcome web page appears, your web server is up and running.
Setup Virtual Hosts
You can use virtual hosts (like server blocks in Nginx) to encapsulate configuration. And you can also host more than one domain from a single Apache Web Server. We will create a domain named Your_Domain, but you should use your own domain name instead.
On Ubuntu 20.04, Apache is enabled by default and set to serve documents from the /var/www/html
directory. While this is fine for a single site, it might become difficult if you have many sites to host.
/var/www
for your domain instead of modifying /var/www/html.
And leave /var/www/html
as the default directory to serve if a client request does not match any other sites. your_domain
directory as follows:
sudo mkdir /var/www/your_domain
$USER
environment variable, assign ownership to the directory:
sudo chown -R $USER:$USER /var/www/your_domain
Ensure your permissions are correct and allow the owner to read, write, and execute the files. And grant only read and execute permissions to groups and others. To do so, use the following command.
sudo chmod -R 755 /var/www/your_domain
Next, using your preferred editor, create a sample index.html page as follows:
sudo nano /var/www/your_domain/index.html
Add the following HTML inside the file:
Welcome to Your_domain!
Success! The your_domain virtual host is working!
Instead of tweaking the default configuration file at /etc/apache2/sites-available/000-default.conf.
Let’s make a new one at /etc/apache2/sites-available/your_domain.conf:
sudo nano /etc/apache2/sites-available/your_domain.conf
Copy and paste the following configuration block. Which is identical to the default but updated to reflect our new directory and domain name:
ServerAdmin webmaster@localhost
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
You can see that we have updated the DocumentRoot to our new directory and the ServerAdmin to an Email address. One that your site administrator for your domain can access.
Moreover, we already added two directives: Servername and ServerAlias. Servername establishes the base domain, which should match for virtual host definition. ServerAlias defines more names that should match as if they were the base name.
sudo a2ensite your_domain.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
Output
Syntax OK
sudo systemctl restart apache2
Apache should now serve your domain name. You can verify this by going to http://your domain
to see something like this:
Bilal Shafqat
Related posts
New Articles
How To Use Your Finance Degree To Make Money With Crypto
So you have a finance degree. Congratulations! You are now in an elite group of people who have the skills…