How To Install the Apache Web Server on Ubuntu 20.04
How To Install the Apache Web Server on Ubuntu 20.04?
Introduction
The Apache HTTP server is the world’s most widely-used web server, renowned for its powerful features, including dynamically loadable modules, robust media support, and extensive integration with other popular software.
In this guide, we will walk you through the process of installing an Apache web server on your Ubuntu 20.04 server.
Step 1 — Installing Apache
Apache is included in Ubuntu’s default software repositories, allowing for installation using standard package management tools.
First, update the local package index to ensure it reflects the latest upstream changes:
Next, install the apache2 package
Managing Apache 2 service on Ubuntu
We will use the systemctl
command to start and enable the Apache
service.
Start the Apache Service, use the systemd
init system by typing:
The web server should already be up and running.
To confirm the service is running, use the systemd
init system by typing:
Output
● apache2.service – The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2024-07-19 06:32:23 UTC; 6h ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 3018563 (apache2)
Tasks: 12 (limit: 14213)
Memory: 33.0M
CPU: 2.544s
CGroup: /system.slice/apache2.service
├─3018563 /usr/sbin/apache2 -k start
├─3018566 /usr/sbin/apache2 -k start
├─3018569 /usr/sbin/apache2 -k start
Step 2 — Setting Up Virtual Hosts
Virtual hosts allow you to run multiple websites on a single server by configuring separate domain names or IP addresses. Here’s how to set up virtual hosts for Apache on your server:
We will configure a domain named your_domain_name, which you should replace with your actual domain name.
By default, Apache on Ubuntu 20.04 comes with a single server block enabled, which is configured to serve documents from the /var/www/html directory.
Create the directory for your_domain_name as follows:
Then, assign ownership of the directory using the $USER
environment variable: init system by typing:
If you haven’t changed your umask value, the permissions of your web roots should be correct by default. To verify and set the appropriate permissions, allowing the owner to read
, write
, and execute
the files while granting only read and execute permissions to groups and others, you can use the following command:
Next, create a sample index.html
page using nano
or your preferred text editor: the files while granting only read and execute permissions to groups and others, you can use the following command:
Add the following sample HTML
inside the file:
<html>
<head>
<title>Welcome to Your_domain_name!</title>
</head>
<body>
<h1>Success! The your_domain_name Apache Setup is working!</h1>
</body>
</html>
To have Apache serve this content, you need to create a virtual host file with the appropriate directives. Rather than altering the default configuration file found at /etc/apache2/sites-available/000-default.conf create a new file at /etc/apache2/sites-available/your_domain_name.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain_name
ServerAlias www.your_domain_name
DocumentRoot /var/www/your_domain_name
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file once you are done.
Let’s enable the file with the a2ensite
tool:
Disable the default site defined in 000-default.conf
:
Next, test for configuration errors:
You should see the following output:
Output
Syntax OK
Restart Apache to apply your changes:
http://your_domain_name.com
, where you should see something like this: