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 will learn how to install an Apache web server on your Ubuntu 18.04 server. This guide also outlines information about important Apache files and directories.
Prerequisites
Before you begin this guide, you should have a regular, non-root user with sudo privileges configured on your server. Additionally, you will need to enable a basic firewall to block non-essential ports
When you have an account created, log in as your non-root user to begin.
Step 1 — Installing Apache
Apache is available within Ubuntu’s default software repositories, making it possible to install it using conventional package management tools.
Let’s begin by updating the local package index to reflect the latest upstream changes:
sudo apt update
Then, install the apache2 package:
sudo apt install apache2
After confirming the installation, apt will install Apache and all required dependencies.
Step 2 — Adjusting the Firewall
Before testing Apache, it’s necessary to modify the firewall settings to allow outside access to the default web ports. If you followed the instructions in the prerequisites, you should have a UFW firewall configured to restrict access to your server.
During installation, Apache registers itself with UFW to provide a few application profiles that can be used to enable or disable access to Apache through the firewall.
List the ufw application profiles by running the following command:
sudo ufw app list
This will return a list of the application profiles:
Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
This list indicates that there are three profiles available for Apache:
Apache: This 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)
OpenSSH: This profile enables us to use SSH into the Instance.
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 will only need to allow traffic on port 80:
sudo ufw allow 'Apache Full'
sudo ufw allow 'OpenSSH'
Verify this change by checking the status:
sudo ufw status
Now the allowed HTTP traffic will be displayed in the output:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
The Apache profile has now been activated to allow access to the web server.
Finally enable the firewall
sudo ufw enable
Step 3 — Checking your Web Server
At the end of the installation process, Ubuntu 18.04 starts Apache. The web server should already be up and running.
Check with the systemd init system to make sure the service is running:
Active: active (running) since Tue 2021-09-28 16:52:56 UTC; 1min 14s ago
Main PID: 9409 (apache2)
Tasks: 55 (limit: 4915)
CGroup: /system.slice/apache2.service
├─9409 /usr/sbin/apache2 -k start
├─9410 /usr/sbin/apache2 -k start
└─9411 /usr/sbin/apache2 -k start
This output demonstrates that the service has started successfully. However, the best way to verify 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.
Run the following at your server’s command prompt:
hostname -I
You will receive a few addresses separated by spaces. You can try each in your web browser to check if they work.
An alternative is running the following command, which should give you your public IP address as it is identified from another location on the internet:
curl -4 icanhazip.com
When you have your server’s IP address, enter it into your browser’s address bar:
http://your_server_ip
You should receive the default Ubuntu 18.04 Apache web page:
This web page indicates that Apache is working correctly. It also includes some basic information about important Apache files and directory locations.
Step 4 — Managing the Apache Process
Now that you have your web server up and running, let’s review some basic management commands.
You can stop your web server with the following command::
sudo systemctl stop apache2
To start the web server when it is stopped, run this command:
sudo systemctl start apache2
You can stop and then start the service again by running the following:
sudo systemctl restart apache2
If you are only making configuration changes, Apache can often reload without dropping connections. To do this, execute the following command:
sudo systemctl reload apache2
By default, Apache is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior with the following:
sudo systemctl disable apache2
Alternatively, to enable or re-enable the service to start up at boot, run this command:
sudo systemctl enable apache2
Apache should now start automatically when the server boots again.
Step 5 — Setting Up Virtual Hosts (Recommended)
When using the Apache web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. For our example, we will set up a domain called your_domain, but you should replace this with your own domain name.
Apache on Ubuntu 18.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:
sudomkdir /var/www/your_domain
Next, assign ownership of the directory with the $USER environment variable:
sudochown -R ubuntu:ubuntu /var/www/your_domain
The permissions of your web roots should be correct if you haven’t modified your unmask value, but you can make sure by typing the following:
sudochmod -R 755 /var/www/your_domain
Next, create a sample index.html page using nano or your favorite editor:
nano /var/www/your_domain/index.html
Inside, add the following sample HTML:
/var/www/your_domain/index.html
<html>
<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 used nano, you can exit 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>
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 should match for this virtual host definition, and ServerAlias, which defines further names that should match as if they were the base name.
Save and close the file when you are finished.
Next, enable the file with the a2ensite tool:
sudo a2ensite your_domain.conf
Disable the default site defined in 000-default.conf:
sudo a2dissite 000-default.conf
Now test for configuration errors:
sudo apache2ctl configtest
You should receive the following output:
Output
Syntax OK
Restart Apache to implement your changes:
sudo systemctl restart apache2
Step 6 – Getting Familiar with Important Apache Files and Directories
Now that you know how to manage the Apache service itself, you should take a few minutes to familiarize yourself with a few important directories and files.
Content
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (