This guide will take you through the installation of the Apache webserver on Ubuntu 20.04. It includes managing the Apache2 services, open webserver port in the firewall, testing the Apache2 installation, and configuring a Virtual Host environment.
Related Read: How to Install Nginx Web Server on Ubuntu 20.04
Requirements:
Installing Apache2 in Ubuntu 20.04
1. First, log into your Ubuntu 20.04 system and update your system packages using the following apt command.
$ sudo apt update
2. Once the update process is complete, install the Apache2 web server software as follows.
$ sudo apt install apache2
3. While installing the Apache2 package, the installer triggers systemd to automatically start and enable the apache2 service. You can verify that the apache2 service is active/running and is enabled to automatically start at system startup using the following systemctl commands.
$ sudo systemctl is-active apache2 $ sudo systemctl is-enabled apache2 $ sudo systemctl status apache2
Managing the Apache in Ubuntu 20.04
4. Now that your apache web server is running, it’s time to learn some basic management commands to manage the apache process using the following systemctl commands.
$ sudo systemctl stop apache2 #stop apache2 $ sudo systemctl start apache2 #start apache2 $ sudo systemctl restart apache2 #restart apache2 $ sudo systemctl reload apache2 #reload apache2 $ sudo systemctl disable apache2 #disable apache2 $ sudo systemctl enable apache2 #enable apache2
Configuring Apache in Ubuntu 20.04
5. All Apache2 configuration files are stored in the /etc/apache2
directory, you can view all files and subdirectories under it with the following ls command.
$ ls /etc/apache2/*
6. The following are the key configuration files and sub-directories you should take note of:
- /etc/apache2/apache2.conf – The main Apache global configuration file, that includes all other configuration files.
- /etc/apache2/conf-available – stores available configurations.
- /etc/apache2/conf-enabled – contains enabled configurations.
- /etc/apache2/mods-available – contains available modules.
- /etc/apache2/mods-enabled – contains enabled modules.
- /etc/apache2/sites-available – contains configuration file for available sites (virtual hosts).
- /etc/apache2/sites-enabled – contains configuration file for enabled sites (virtual hosts).
Note that if the server’s FQDN is not set globally, you will get the following warning every time you check the apache2 service status or run a configuration test.
apachectl[2996]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.2.15.
Set the 'ServerName'
directive globally in the main apache configuration file to suppress this message.
7. To set the web server’s FQDN, use the ServerName
directive in /etc/apache2/apache2.conf file, open it for editing using your favorite text editor.
$ sudo vim /etc/apache2/apache2.conf
Add the following line in the file (replacing webserver1.tecmint.com with your FQDN).
ServerName webserver1.tecmint.com
8. After adding the server name in the apache configuration, check the configuration syntax for correctness, and restart the service.
$ sudo apache2ctl configtest $ sudo systemctl restart apache2
9. Now when you check the apache2 service status, the warning should not appear.
$ sudo systemctl status apache2
Opening Apache Ports in UFW Firewall
10. If you have the UFW firewall enabled and running on your system, you need to open the HTTP (port 80) and HTTPS (port 443) services in the firewall configuration, to allow web traffic to the Apache2 web server via the firewall.
$ sudo ufw allow http $ sudo ufw allow https $ sudo ufw reload OR $ sudo ufw allow 80/tcp $ sudo ufw allow 443/tcp $ sudo ufw reload
Testing Apache on Ubuntu 20.04
11. To test if the Apache2 webserver installation is working fine, open a web browser, and use your server’s IP address to navigate:
http://SERVER_IP
To find out your server’s public IP address, use any of the following curl commands.
$ curl ifconfig.co OR $ curl ifconfig.me OR $ curl icanhazip.com
If you see the Apache Ubuntu default welcome web page, it means your web server installation is working fine.
Setting Up Virtual Hosts in Ubuntu 20.04
Although the Apache2 web server is configured by default to host one website, you can use it to host multiple web sites/applications using the concept of “Virtual Host”.
Therefore Virtual Host is a term that refers to the practice of running more than one web site/application (such as example.com and example1.com) on a single server.
Additionally, Virtual Hosts can be “name-based “(meaning that you have multiple domain/hostnames running on a single IP address), or “IP-based” (meaning that you have a different IP address for every web site).
Note that the default virtual host which serves the Apache Ubuntu default welcome web page which is used to test the Apache2 installation is located in the /var/www/html
directory.
$ ls /var/www/html/
12. For this guide, we will create a virtual host for the web site called linuxdesktop.info
. So let’s first create the web document root for the site which will store the site’s web files.
$ sudo mkdir -p /var/www/html/linuxdesktop.info
13. Next, set the appropriate ownership and permissions on the created directory.
$ sudo chown www-data:www-data -R /var/www/html/linuxdesktop.info $ sudo chmod 775 -R /var/www/html/linuxdesktop.info
14. Now create a sample index page for testing purposes.
$ sudo vim /var/www/html/linuxdesktop.info/index.html
Copy and paste the following html code in it.
<html> <head> <title>Welcome to linuxdesktop.info!</title> </head> <body> <h1>Congrats! The new linuxdesktop.info virtual host is working fine.</h1> </body> </html>
Save the file and exit it.
15. Next, you need to create a virtual host configuration file (which should end with the .conf
extension) for the new site under the /etc/apache2/sites-available directory.
$ sudo vim /etc/apache2/sites-available/linuxdesktop.info.conf
Then copy and paste the following configuration it the file (remember to replace www.linuxdesktop.info
with your FQDN).
<VirtualHost *:80> ServerName www.linuxdesktop.info ServerAlias linuxdesktop.info DocumentRoot /var/www/html/linuxdesktop.info ErrorLog /var/log/apache2/linuxdesktop.info_error.log CustomLog /var/log/apache2/linuxdesktop.info_access.log combined </VirtualHost>
Save the file and exit it.
16. Next, enable the new site and reload the Apache2 configuration to apply the new changes as follows.
$ sudo a2ensite linuxdesktop.info.conf $ sudo systemctl reload apache2
17. Finally, test if the new virtual host configuration is working fine. In a web browser, use your FQDN to navigate.
http://domain-name
If you can see the index page for your new website, it means the virtual host is working fine.
That’s all! In this guide, we have shown how to install the Apache webserver on Ubuntu 20.04. We also covered how to manage the Apache2 services, open the HTTP and HTTPS services/ports in the UFW firewall, tested the Apache2 installation, and configured and tested a Virtual Host environment. Do you have any queries, use the comment form below to reach us.
You might want to add that this won’t always work for a local web server that is to deliver a website from your filesystem. Curiously, it worked for me on Linux Mint 19 twice but eventually failed.
I then entered the name of my virtual host in /etc/hosts and connected it with 127.0.0.1 (alongside ‘localhost‘), and it worked. That might be a specialty of Linux Mint or not, I don’t know.
Very very goog. Great explanation :)