Trending News
By using our website, you agree to the use of our cookies.

Blog

How to Install Apache Web Server on Ubuntu 20.04 [ Easy Guide ]
Installation & Upgrade

How to Install Apache Web Server on Ubuntu 20.04 [ Easy Guide ] 

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.

Log in as a non-root user when you have an account available.

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

				
			
Install Apache on Ubuntu 20 04 yehiweb
Install Apache on Ubuntu
Apt will install Apache and all needed dependencies once you confirm the installation.

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
				
			
Check Apache Service in Ubuntu 20.04 Yehiweb
Check Apache Service in Ubuntu 20.04
The commands ensure the apache2 service start at system startup.

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

				
			
You can see many different Apache profiles.
apache profile yehiweb
apache profile

To enable network connectivity on port 80, we will use the restrictive profile “Apache.”

				
					$ sudo ufw allow ‘Apache.’

				
			
check the status yehiweb
check the status
Check the status, which should indicate Apache allowed in the firewall.
				
					$ 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.

Try the following command at the command prompt of your server:
				
					hostname -I

				
			
host name yehiweb
host name

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


				
			
The default Ubuntu 20.04 Apache web page should appear:
apache ubuntu web page yehiweb
Apache ubuntu web page

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.

Use the following command to stop your server:
				
					sudo systemctl stop apache2

				
			
Use the following command to start the webserver when it stopped:
				
					sudo systemctl start apache2

				
			
Use the following command to stop and start the service again:
				
					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

				
			
Apache should now start when the server boots again.

Configure Apache In Ubuntu 20.04

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

				
			
apache configuration file yehiweb
apache configuration file
You should take note of the following key configuration files and sub-directories:
  • 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.

apache server name error yehiweb
apache server name error
To set FQDN of your web server, use ServerName directive in /etc/apache2/apache2.conf file. Use your preferred text editor to open the file to edit it.
				
					$ sudo vim /etc/apache2/apache2.conf 

				
			
In the file, add the following line: (replacing webserver1.yehiweb.com with your FQDN).
				
					ServerName webserver1.yehiweb.com

				
			
Set Server FQDN in Apache Yehiweb
Set Server FQDN in Apache
After adding the server name in the configuration, check syntax and restart the service.
				
					$ sudo apache2ctl configtest
$ sudo systemctl restart apache2
				
			
Check Apache Configuration Yehiweb
Check Apache Configuration
The warning should no longer appear when you check the apache2 service status.
				
					$ sudo systemctl status apache2

				
			
Check Apache Service Status Yehiweb
Check Apache Service Status

Test Apache Web Server installation on Ubuntu 20.04

To test if the Apache2 Web Server is running, use the server’s IP address to navigate on browser:
				
					http://SERVER_IP


				
			
Use any of the following curl commands to find out the public IP address of your server.
				
					$ curl ifconfig.co
OR
$ curl ifconfig.me
OR
$ curl icanhazip.com
				
			
Find Server IP in Ubuntu 20.04 Yehiweb
Find Server IP in Ubuntu 20.04

If the Apache Ubuntu default welcome web page appears, your web server is up and running.

Apache2 Default Page Yehiweb
Apache2 Default Page

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.

We will create a directory within /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.
Create your_domain directory as follows:
				
					sudo mkdir /var/www/your_domain

				
			
Then, using the $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:

				
					<html>
    <head>
        <title>Welcome to Your_domain!</title>
    </head>
    <body>
        <h1>Success!  The your_domain virtual host is working!</h1>
    </body>
</html>
				
			
When you are done, save and close the file.
For Apache to serve this content, create a virtual host file with correct directives.

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:

				
					<VirtualHost *:80>
    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
</VirtualHost>
				
			

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.

When you are done, save and close the file.
Now let’s use the a2ensite tool to enable the file:
				
					sudo a2ensite your_domain.conf

				
			
Use the following command to disable the default site defined in 000-default.conf:
				
					sudo a2dissite 000-default.conf

				
			
Let’s check for configuration errors next:
				
					sudo apache2ctl configtest

				
			
You will get the following output:

Output

				
					Syntax OK

				
			
To bring your modifications into effect, restart Apache:
				
					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:

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit
Share on vk
Share on tumblr
Share on skype
Share on stumbleupon
Share on whatsapp
Share on email

Related posts

Leave a Reply

Required fields are marked *