How To Install The Apache Web Server On Ubuntu 18.04 - DigitalOcean
How To Install The Apache Web Server On Ubuntu 18.04 - DigitalOcean
04 | DigitalOcean
Language: EN
Contents
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, we'll explain how to install an Apache web server on your Ubuntu 18.04 server.
Prerequisites
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 1/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
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. 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.
Let's begin by updating the local package index to reflect the latest upstream changes:
After confirming the installation, apt will install Apache and all required dependencies.
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.
Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
As you can see, 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)
It is recommended that you enable the most restrictive profile that will still allow the traffic you've configured. Since we
haven't configured SSL for our server yet in this guide, we will only need to allow traffic on port 80:
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
$ sudo ufw allow 'Apache' × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 3/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
As you can see, the profile has been activated to allow access to the web server.
Check with the systemd init system to make sure the service is running by typing:
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
Output
× SCROLL TO TOP
Enter your email address Sign Up
h 2 i Th A h HTTP S
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 4/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
● 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
As you can see from this output, the service appears to have 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.
$ hostname -I
You will get back a few addresses separated by spaces. You can try each in your web browser to see if they work.
An alternative is typing this, which should give you your public IP address as seen from another location on the internet:
$ curl -4 icanhazip.com
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
When youemail
have your server's IP address, enter it into your browser's
× SCROLL TO TOP
Enter your address Sign Up address bar:
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 5/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
http://your_server_ip
You should see the default Ubuntu 18.04 Apache web page:
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 6/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 7/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
This page indicates that Apache is working correctly. It also includes some basic information about important Apache files
and directory locations.
$ Sign
sudo upsystemctl start
for our newsletter. apache2
Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 8/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
If you are simply making configuration changes, Apache can often reload without dropping connections. To do this, use this
command:
By default, Apache is configured to start automatically when the server boots. If this is not what you want, disable this
behavior by typing:
Apache should now start automatically when the server boots again.
replace this with your own domain name. To learn more about setting up a domain name with DigitalOcean, see our
Introduction to DigitalOcean DNS.
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 , let's create a directory structure within /var/www for our example.com 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 example.com as follows, using the -p flag to create any necessary parent directories:
Next, assign ownership of the directory with the $USER environmental variable:
The permissions of your web roots should be correct if you haven't modified your unmask value, but you can make sure by
typing:
Next, create a sample index.html page using nano or your favorite editor:
$ nano /var/www/example.com/html/index.html
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your
Inside, add email
theaddress
following sample HTML: Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 10/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
/var/www/example.com/html/index.html
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com server block is working!</h1>
</body>
</html>
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, let's make
a new one at /etc/apache2/sites-available/example.com.conf :
Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain
name:
/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
ErrorLog ${APACHE_LOG_DIR}/error.log × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 11/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
Notice that we've updated the DocumentRoot to our new directory and ServerAdmin to an email that the example.com 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.
×
Output
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. SCROLL TO TOP
Syntax OK
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 12/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
Apache should now be serving your domain name. You can test this by navigating to http://example.com , where you
should see something like this:
Content
/var/www/html : The actual web content, which by default only consists of the default Apache page you saw earlier, is
served out of the /var/www/html directory. This can be changed by altering Apache configuration files.
Server Configuration
/etc/apache2 : The Apache configuration directory. All of the Apache configuration files reside here.
/etc/apache2/apache2.conf : The main Apache configuration file. This can be modified to make changes to the Apache
global configuration. This file is responsible for loading many of the other files in the configuration directory.
/etc/apache2/ports.conf : This file specifies the ports that Apache will listen on. By default, Apache listens on port 80
and additionally listens on port 443 when a module providing SSL capabilities is enabled.
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 13/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
/etc/apache2/sites-available/ : The directory where per-site virtual hosts can be stored. Apache will not use the
configuration files found in this directory unless they are linked to the sites-enabled directory. Typically, all server block
configuration is done in this directory, and then enabled by linking to the other directory with the a2ensite command.
/etc/apache2/sites-enabled/ : The directory where enabled per-site virtual hosts are stored. Typically, these are created
by linking to configuration files found in the sites-available directory with the a2ensite . Apache reads the
configuration files and links found in this directory when it starts or reloads to compile a complete configuration.
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.
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 14/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
If you'd like to build out a more complete application stack, you can look at this article on how to configure a LAMP stack on
Ubuntu 18.04.
Related Tutorials
How To Migrate your Apache Configuration from 2.2 to 2.4 Syntax.
How To Get Started With mod_pagespeed with Apache on a CentOS and Fedora Cloud Server
How To Use the .htaccess File
18 Comments
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter
Leaveyour a
email address
comment... Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 15/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
Log In to Comment
1 I have done everything exactly like that but when I access example.com it opens up the example.com website from google.
Although, when I access my localhost(127.0.0.1) it opens up the file which I have places in example.com/html directory!
I 'm facing this issue in Ubuntu 18.04, Everything was working fine in ubuntu 16.04.
0 can i know why i cant open /var/log/apache2 folder.it says permission denied even when i use sudo command.can someone
explain it to me?
We have different servers with different subdomains in our network and I need a SSL certificate only for one subdomain, e,g,
https://ww8.unidi.com/
BR,
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
zeokat October 12, 2018 × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 17/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
0
Are you sure that you the part of the tutorial that says Next, assign ownership of the directory with the $USER
environmental variable is secure?
This comand assigns root as user and i'm not sure if that is correct. Not will be better use www-data user?
0 Virtual directory server block instructions failed over permissions when accessing from web. This line sudo chmod -R 755
/var/www/example.com didnt work for me. Added /html to the end and it worked.
I followed along the tutorial and when I got to the part where I enabled the new site and disabled the default site, I still get the
html
Sign uppage
for ourfrom the default
newsletter. Get thesite...
latest not the on
tutorials shortened
SysAdminversion sitting
and open intopics.
source my × SCROLL TO TOP
virtual
Enter yourdomain. I noted what was said by ansargondal belowSign
email address Upcomment section and ensured that I added the line 127.0.0.1
in the
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 18/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
mydomainName (the directory name @ /etc/apache2/sites-available/mydomainName). Also, I'm thinking that I can name my
directory in the sites-available folder to whatever I want, without a .com or other extension. My structure is /etc/apache2/sites-
available/myVirtualSite/html. Again, I tried both ways just in case it mattered. What should be in the hosts file as that might be
where my problem is.
0 i have followed above steps and successfully created virtual hosts and added two domain names both are working fine without
www but when i open www.domain1.com website is not working where as domain1.com is working fine.
i have another problem if i open my ipaddress i am getting contents on my 1st domain name which i have added under
var/www/domain1/html but i want files which are under var/www/html
0 You'll have to ensure that you've set ServerAlias www.domain1.com in your virtual host's settings, located in the
/etc/apache/sites-available/ directory.
You can also change the virtual host's main directory from there.
It'll be great if you can post a snippet of the configuration file you have right now.
0 So good article, just was i needed, thanks a lot all works fine!
0
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics.
My default Ubuntu 18.04 Apache web page Not Open × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 19/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
0 Please help,
Apache2: could not reliably determine the servers fully qualified domain name using 127.0.1.1. Set ‘servername’.......
Var/www/siddiksaleh.com
Vitualhost file name is siddiksaleh.com.conf
I have tried ServerName in VH file as “siddiksaleh.com”, “www.siddiksaleh.com”, “localhost” but non of them worked for me.
Thanks.
0 Hey Kathleen,
Is there any other setup to be done to enable the access typing example.com?
Tks :)
0 Was able to breeze over the parts I knew, and drill down into what was new to me. Love the digression into the explanation of
sites, conf, mods. This article comes out of a mastery of the subject and a mastery of teaching.
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 20/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 21/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
Featured on Community Intro to Kubernetes Learn Python 3 Machine Learning in Python Getting started with Go
Migrate Node.js to Kubernetes
DigitalOcean Products Droplets Managed Databases Managed Kubernetes Spaces Object Storage Marketplace
Learn More
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 22/23
16/7/2019 How To Install the Apache Web Server on Ubuntu 18.04 | DigitalOcean
Contact
Support
Sales
Report Abuse
System Status
Sign up for our newsletter. Get the latest tutorials on SysAdmin and open source topics. × SCROLL TO TOP
Enter your email address Sign Up
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04 23/23