How to Install Apache on Ubuntu 18.04

r00t June 12, 2018

Install Apache on Ubuntu 18.04

In this tutorial we are going to learn how to install Apache on Ubuntu 18.04. We will also install and configure its prerequisites. Apache web server is among the most popular and powerful web servers in the world, as a result of its ease of administration and flexibility. In this tutorial we will install Apache on a host which doesn’t have a web server or database server installed.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Ubuntu 18.04 Bionic Beaver minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Apache on Ubuntu 18.04

Step 1. First, ensure your system and apt package lists are fully up-to-date by running the following:

apt-get update -y
apt-get upgrade -y

Step 2. Installing Apache web server.

Installing Apache is as simple as running just one command:

sudo apt install apache2

Start the Apache service and make it start on system boot:

sudo systemctl start apache2.service
sudo systemctl enable apache2.service

Step 3. Configuring Apache HTTPS.

In order to serve your web pages safely, you’ll need to setup HTTPS. You will need to get SSL certificates so as to achieve this. There are a couple of ways of getting SSL certificates. If your site will be a publicly available web site, then you should probably spring a few bucks and buy SSL certs.

The second choice will be self-signed certificates. These you produce in your web server and utilize to deploy HTTPS. This is not as secure as the first option but is very good for testing or running inner web sites. I shall cover this procedure in this part.

First thing we need to do is generate our certificates:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/selfsigned.key -out /etc/ssl/certs/selfsigned.crt
It will ask you several questions. You can either accept the defaults (which can be empty) or enter the information. Now we need to tell Apache to serve HTTPS traffic on port 443 (the standard HTTPS port) and utilize our newly created SSL certificates.
Next we need to modify the default SSL file /etc/apache2/sites-available/default-ssl.conf:
<IfModule mod_ssl.c>
         <VirtualHost _default_:443>
         ServerAdmin webmaster@localhost

         DocumentRoot /var/www/html

         ErrorLog ${APACHE_LOG_DIR}/error.log
         CustomLog ${APACHE_LOG_DIR}/access.log combined

         SSLEngine on

         SSLCertificateFile    /etc/ssl/certs/selfsigned.crt
         SSLCertificateKeyFile /etc/ssl/private/selfsigned.key

         <FilesMatch "\.(cgi|shtml|phtml|php)$">
         SSLOptions +StdEnvVars
         SSLOptions +StdEnvVars

The critical parts are the SSLCertificateFile and SSLCertificateKeyFile that stage into the SSL certs we have made before. Save the file and leave:

Now we need to enable HTTPS:

a2enmod ssl
a2enmod headers
a2ensite default-ssl

Finally steps, Browse to https://{your-server-ip} and you should see the same default web page as before. You will have to click through a warning from your browser first.

Congratulation’s! You have successfully install and configured Apache on your Ubuntu 18.04 server. Thanks for using this tutorial installing Apache web server on Ubuntu 18.04 LTS (Bionic Beaver) system.

The Tags:

Leave a Comment

Comments are closed.