Install Apache Ubuntu
Install Apache Ubuntu
Learn more
Tutorials Questions Learning Paths For Businesses Product Docs Social Impact Search Community
Please select whether you consent to our use of cookies and related technologies (“Cookies”), as
described in our privacy policy. Expand each section below for more information. You can return at any
Tutorial Series: Getting Started With Cloud Computing
time from the same web browser to update your preferences. Please note that resetting your browser’s
cookies will reset your preferences.
eb Servers 10/39 How To Install the Apache Web Se… 11/39 How To Install Nginx on Ubunt
➔ Functional Cookies NO YES
// Tutorial //
SUBMIT ALL PREFERENCES
How To Install the Apache Web Server on Ubuntu
22.04 DECLINE ALL
Introduction
The Apache HTTP server is the most widely-used web server in the world. It provides many powerful
features including dynamically loadable modules, robust media support, and extensive integration with other
popular software.
In this guide, you’ll learn how to install an Apache web server on your Ubuntu 22.04 server.
Prerequisites
Before you begin this guide, you will need an Ubuntu 22.04 server set up with a non-root user with sudo
privileges and a firewall enabled to block non-essential ports. You can learn how to do this by following our
Initial server setup guide for Ubuntu 22.04.
Once you’re done setting this up, log in as your non-root user and proceed to the first step.
After confirming the installation, apt will install Apache and all required dependencies.
Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
As indicated by the output, there are three profiles available for Apache:
ApacheThis profile opens only port 80 (normal, unencrypted web traffic)
Apache Full This profile opens both port 80 (normal, unencrypted web traffic) and port 443
TLS/SSL encrypted traffic)
Apache Secure This profile opens only port 443 TLS/SSL encrypted traffic)
It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve
configured. Since you haven’t configured SSL for your server yet in this guide, you’ll only need to allow
traffic on port 80 :
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
As indicated by the output, the profile has been activated to allow access to the Apache web server.
Output
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
Active: active (running) since Tue 2022-04-26 15:33:21 UTC; 43s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 5089 (apache2)
Tasks: 55 (limit: 1119)
Memory: 4.8M
CPU: 33ms
CGroup: /system.slice/apache2.service
├─5089 /usr/sbin/apache2 -k start
├─5091 /usr/sbin/apache2 -k start
└─5092 /usr/sbin/apache2 -k start
As confirmed by this output, the service has started successfully. However, the best way to test this is to
request a page from Apache.
You can access the default Apache landing page to confirm that the software is running properly through
your IP address. If you do not know your server’s IP address, you can get it a few different ways from the
command line.
Try writing the following at your server’s command prompt:
$ hostname -I Copy
You will receive a few addresses separated by spaces. You can try each in your web browser to determine
if they work.
Another option is to use the free icanhazip.com tool. This is a website that, when accessed, returns your
machine’s public IP address as read from another location on the internet:
When you have your server’s IP address, enter it into your browser’s address bar:
http:// your_server_ip
You will see the default Ubuntu 22.04 Apache web page as in the following:
This page indicates that Apache is working correctly. It also includes some basic information about
important Apache files and directory locations.
If you are simply making configuration changes, Apache can often reload without dropping connections. To
do this, use the following command:
By default, Apache is configured to start automatically when the server boots. If this is not what you want,
disable this behavior by running:
Apache will now start automatically when the server boots again.
Inf o: If you are setting up a domain name with DigitalOcean, please refer to our Networking Documentation.
Apache on Ubuntu 22.04 has one server block enabled by default that is configured to serve documents
from the /var/www/html directory. While this works well for a single site, it can become unwieldy if you are
hosting multiple sites. Instead of modifying /var/www/html , create a directory structure within /var/www for
a your_domain site, leaving /var/www/html in place as the default directory to be served if a client request
doesn’t match any other sites.
Create the directory for your_domain as follows:
Next, assign ownership of the directory to the user you’re currently signed in as with the $USER environment
variable:
The permissions of your web root should be correct if you haven’t modified your umask value, which sets
default file permissions. To ensure that your permissions are correct and allow the owner to read, write, and
execute the files while granting only read and execute permissions to groups and others, you can input the
following command:
Next, create a sample index.html page using nano or your favorite editor:
/var/www/your_domain/index.html
<html> Copy
<head>
<title>Welcome to Your_domain !</title>
</head>
<body>
<h1>Success! The your_domain virtual host is working!</h1>
</body>
</html>
Save and close the file when you are finished. If you’re using nano , you can do this by pressing CTRL + X ,
then Y and ENTER .
In order for Apache to serve this content, it’s necessary to create a virtual host file with the correct
directives. Instead of modifying the default configuration file located at /etc/apache2/sites-
available/000-default.conf directly, make a new one at /etc/apache2/sites-
available/ your_domain .conf :
Add in the following configuration block, which is similar to the default, but updated for your new directory
and domain name:
/etc/apache2/sites-available/your_domain.conf
<VirtualHost *:80> Copy
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>
Notice that we’ve updated the DocumentRoot to our new directory and ServerAdmin to an email that the
your_domain site administrator can access. We’ve also added two directives: ServerName , which
establishes the base domain that will match this virtual host definition, and ServerAlias , which defines
further names that will match as if they were the base name.
Save and close the file when you are finished.
Now enable the file with the a2ensite tool:
Output
. . .
Syntax OK
Apache will now be serving your domain name. You can test this by navigating to http:// your_domain ,
where you will see something like the following:
Server Logs
/var/log/apache2/access.log By default, every request to your web server is recorded in this log
file unless Apache is configured to do otherwise.
/var/log/apache2/error.log By default, all errors are recorded in this file. The LogLevel directive in
the Apache configuration specifies how much detail the error logs will contain.
Conclusion
Now that you have your web server installed, you have many options for the type of content you can serve
and the technologies you can use to create a richer experience.
If you’d like to build out a more complete application stack, you can read this article on how to configure a
LAMP stack on Ubuntu 22.04
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute,
storage, networking, and managed databases.
Learn more about us
Still looking for an answer? Ask a question Search for more help
Comments
Leave a comment
Leave a comment
Featured on Community
Kubernetes Course Learn Python 3 Machine Learning in Python Getting started with Go Intro to Kubernetes
DigitalOcean Products
Cloudways Virtual Machines Managed Databases Managed Kubernetes Block Storage Object Storage Marketplace VPC
Load Balancers
Learn more