Tutorial

How To Install the Apache Web Server on Ubuntu 18.04 [Quickstart]

Updated on March 18, 2022
English
How To Install the Apache Web Server on Ubuntu 18.04 [Quickstart]

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 will install the Apache web server on an Ubuntu 18.04 server. For a more detailed version of this tutorial, please refer to How To Install the Apache Web Server on Ubuntu 18.04.

Prerequisites

Before you begin this guide, you should have the following:

  • An Ubuntu 18.04 server and a regular, non-root user with sudo privileges. Additionally, you will need to enable a basic firewall to block non-essential ports. You can learn how to configure a regular user account and set up a firewall for your server by following our initial server setup guide for Ubuntu 18.04.

When you have an account available, log in as your non-root user to begin.

Step 1 — Installing Apache

Apache is available within Ubuntu’s default software repositories. You can install it using conventional package management tools.

Update your local package index:

  1. sudo apt update

Install the apache2 package:

  1. sudo apt install apache2

Step 2 — Adjusting the Firewall

Check the available ufw application profiles:

  1. sudo ufw app list
Output
Available applications: Apache Apache Full Apache Secure OpenSSH

Enable the most restrictive profile that will still allow the traffic you’ve configured, permitting traffic on port 80 (normal, unencrypted web traffic):

  1. sudo ufw allow 'Apache'

Verify the change:

  1. sudo ufw status
Output
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) ALLOW Anywhere (v6)

Step 3 — Checking your Web Server

Check with the systemd init system to make sure the service is running by entering:

  1. sudo systemctl status apache2
Output
● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Tue 2018-04-24 20:14:39 UTC; 9min ago Main PID: 2583 (apache2) Tasks: 55 (limit: 1153) CGroup: /system.slice/apache2.service ├─2583 /usr/sbin/apache2 -k start ├─2585 /usr/sbin/apache2 -k start └─2586 /usr/sbin/apache2 -k start

Access the default Apache landing page to confirm that the software is running properly through your IP address:

http://your_server_ip

You should see the default Ubuntu 18.04 Apache web page:

Apache default page

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. You will set up a domain called your_domain, but you should replace this with your own domain name. To learn more about setting up a domain name with DigitalOcean, see our introduction to DigitalOcean DNS.

Create the directory for your_domain:

  1. sudo mkdir /var/www/your_domain

Assign ownership of the directory:

  1. sudo chown -R $USER:$USER /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:

  1. sudo chmod -R 755 /var/www/your_domain

Create a sample index.html page using nano or your preferred text editor:

  1. 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.

Make a new virtual host file at /etc/apache2/sites-available/your_domain.conf:

  1. sudo nano /etc/apache2/sites-available/your_domain.conf

Add in the following configuration block, updated to include your new directory and domain name in place of your_domain:

/etc/apache2/sites-available/your_domain.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName your_domain
    ServerAlias your_domain
    DocumentRoot /var/www/your_domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file when you are finished.

Enable your virtual host file with a2ensite:

  1. sudo a2ensite your_domain.conf

Disable the default site defined in 000-default.conf:

  1. sudo a2dissite 000-default.conf

Test for configuration errors:

  1. sudo apache2ctl configtest

You should see the following output:

Output
Syntax OK

Restart Apache to implement your changes:

  1. sudo systemctl restart apache2

Apache should now be serving your domain name. You can test this by navigating to http://your_domain, where you should see something like this:

Apache virtual host example

Conclusion

Now that you have your Apache web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience.

If you’d like to build out a more complete application stack, check out this article on how to configure a LAMP stack on Ubuntu 18.04.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
3 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

That’s a nice article. For non tech people check out EasyApacheServer (https://github.com/realpvn/EasyApacheServer) it basically sets up everything for you, just ssh to the server and run this script

Somehow I am getting a 403 Forbidden.

sudo chown -R $USER:$USER /home/username/www
sudo chmod -R 755 /home/username/www


ls -la /home/username/www
total 12
drwxr-xr-x 2 username username 4096 Jul 10 10:32 .
drwxr-xr-x 5 username username 4096 Jul 10 10:48 ..
-rwxr-xr-x 1 username username   11 Jul 10 10:32 index.html

I used this to set up a web server, walked away from my laptop. My laptop restarted when I was away (blame Windows). Anyways, now I can no longer ssh into the web server I just set up. Any troubleshoot?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.