Seedy's Guide to Apache, episode 1. Getting Fired Up

Welcome to the first in a series of detailed Apache Web Server usage guides. These guides will cover just what, exactly, the Apache Web Server software is, some basic usage/configuration steps, and how to set up and maintain your own website(s).

In this first guide, we will cover the following topics.

You will need the following things in order to follow this guide and all subsequent guides in this series.

This guide, and all subsequent guides, assume you are using an Ubuntu-based Linux server. Steps might vary depending on which Linux distribution you're using.

Part 1. What the hell is this Apache Web Server thing anyway?

Apache, not to be confused with the Native American tribe or the Boeing AH-64 attack helecopter, is a free and open source web server solution that allows users to host one or more websites from a single machine. An Apache-hosted website can be as simple as a single static HTML page, or as complex as a dynamic search portal with interconnected MySQL databases, JavaScript files, maybe with a bit of Python andPerl thrown in there as well.

The Apache project was launched in 1995 by the Apache Group, now known as the Apache Software Foundation. Since then, it has become one of the most popular and widely used web server solutions out there, besides NGINX.

Apache is often seen to be the second part of a four-piece software package, with the operating system/distribution being the first piece, and MySQL and PHP being the final 2 pieces. Here is a list of the main AMP packages.

PHP can be substituted for Perl or Python if so desired, but PHP is the most common.

Part 2. Installing the Apache Web Server

Part 2.1. Installing the main Apache software

Let's install the main Apache stuff first, then we'll talk about some optional extras you can install if you wish.

Please ensure you're logged into your server via SSH before executing the below commands.

sudo apt update

This ensures that the apt package repository used by Ubuntu is fully up-to-date.

sudo apt dist-upgrade

This ensures that all packages installed on your server, including dependencies that these packages might rely on, are fully up-to-date.

sudo reboot

For good measure, it's always a good idea to reboot your server after applying an update.

sudo apt install apache2

This installs the Apache Web Server software, and associated libraries, on to your server. This could take a short while, depending on the speed of your server and its connection to the internet.

To verify that Apache is installed and running fine, enter your domain name or the IP address of your server into a web browser. If everything went to plan, you should see a page that says: "It works!"

Part 2.2. Enabling some additional Apache modules.

Depending on your needs and environment, you might have to enable certain Apache modules that add some extra features to the web server. Modules can be enabled and disabled with the a2enmod and a2dismod commands respectively.

sudo a2enmod rewrite

This enables the Apache Rewrite module, which allows you to build custom URLs for your web pages, as well as carry out some htaccess manipulation tasks. We will discuss htaccess a bit more in the next guide.

sudo a2enmod ssl

This enables the Apache SSL module, which allows for SSL/TLS support on your website.

Part 2.3. Optional Extras

These are some additional PHP goodies you can add to your Apache installation. These aren't required for static websites where basic HTML/JavaScript/CSS is concerned, but they are needed if you want to run something like a WordPress blog, a forum site, or anything else that relies on PHP and/or MySQL.

To install the extensions, execute the following command.

sudo apt install php php-curl php-gd php-imagick php-mysql php-xml php-zip

Part 3. Virtual hosts

Virtual hosts are possibly one of my favourite things about Apache. They basically allow you to have multiple websites run under a single Apache instance via configuration files that tell Apache various things about a website, including where its files are, where to find SSL/TLS certificates, what hostname to use for the site, etc. What would you rather do? Buy 6 virtual private servers, set up 6 Apache installations on each server and have to deal with 6 separate server bills, or get 1 server, set up Apache on it, create 6 website directories and have 6 virtual host files for each site? If I were you, I'd choose option 2. But even if you only have 1 site, I still recommend that you create a virtual host file. It'll save you from having to mess around with the Apache config file (/etc/apache2/apache2.conf) if you decide to change the site's configuration later on and potentially frying your entire Apache installation.

Part 3.1. Creating the virtual host file

A virtual host file simply consists of a set of configuration directives that provide Apache Web Server with various pieces of information about your site.

To create the virtual host file, copy and paste the following lines of code into your text editor of choice, such as the free and open source Notepad++, and save the file as a .conf file. The name of the file doesn't matter, so long as the file extension is .conf.

Note: be sure to modify these lines to suit your specific site environment.

<VirtualHost *:443>
ServerName my.site
ServerAlias www.my.site
ServerAdmin [email protected]
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /certs/MySite/certificate.pem
SSLCertificateKeyFile /certs/MySite/private.key
SSLCertificateChainFile /certs/MySite/chain.pem
</VirtualHost>
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Part 3.2. The breakdown

Let's break down this file line by line before we, ourselves, have a breakdown!

Part 3.2.1. The main V host configuration

<VirtualHost *:443>

The <VirtualHost part tells Apache that this is, indeed, a virtual host configuration, much like the <!doctype html> line at the top of an HTML file tells web browsers that the file is an HTML file. The *:443 part means to listen out for requests on any network interface on port 443. Port 443 is the standard port used for secure HTTPS connections. If you were making a V host for a plain HTTP site, you would use

<VirtualHost *:80>
. Port 80 is the standard port used for HTTP.

ServerName my.site

This line specifies the domain/hostname for your site. This is what visitors will type into the address bar of their browsers to access your site. Note: this line must reference a valid hostname that points to the IP address of your server.

ServerAlias www.my.site

This line is optional, and simply allows visitors to access your site by another name, other than your main domain name. You'd only really need this line if you wanted something like www before your domain name. A valid CNAME or other related record in your DNS configuration is required for this to work properly. See your DNS provider for more details.

ServerAdmin [email protected]

This line specifies an email address that will be shown to visitors that can be used to contact you in the event of an error with your site, such as a 404 not found error or some other error that prevents your site from loading. This information is only seen in Apache's default error pages, and doesn't apply to custom error documents that you've created yourself. You'd most likely use your own contact details in those pages anyway.

DocumentRoot /var/www/html

This line simply tells Apache where all the files for your website are housed. By default, /var/www/html is the root directory for website pages and files.

SSLEngine on

This line tells Apache to use the SSL module for SSL/TLS support on the site.

SSLCertificateFile /certs/MySites/certificate.pem

This line points to the location of your pem encoded SSL certificate file. Don't worry if your certificate filename ends in .crt instead of .pem, they are basically the same thing. So long as the file is pem encoded, everything should be fine.

SSLCertificateKeyFile /certs/MySite/private.key

This line points to the location of your pem encoded SSL private key file. Again, the extension doesn't really matter, so long as the file is pem encoded.

SSLCertificateChainFile /certs/MySite/chain.pem

This line points to the location of, you guessed it, your SSL certificate authority chain file!

Note: If you're making a plain HTTP host, the above 4 lines are not needed and can therefore be skipped.

You can obtain an SSL/TLS certificate and related files for free using a service like Let's Encrypt. There are multiple ways of doing this, depending on OS version and distribution.

</VirtualHost>

Signifies the end of the VirtualHost directive.

Part 3.2.2. Allowing htaccess

<Directory /var/www/html>

The configuration applies to this directory. Remember, /var/www/html is the default website root directory for Apache sites. Replace /var/www/html with the root directory you've chosen for your website.

Options Indexes FollowSymLinks
AllowOverride All

Allows .htaccess files to override any setting specified in the Apache configuration file.

Require all granted

Allows access to your website from any IP address.

</Directory>

Signifies the end of the Directory directive.

Note: These lines are completely optional, and can be skipped if you don't wish to allow .htaccess on your site.

Part 3.3. Activating the V host

To activate your newly created virtual host, follow the following steps.

  1. Find the virtual host file stored on your PC, and, using your favourite SFTP client, upload it to the following directory on your server.
    /etc/apache2/sites-enabled
    . How you'd do this depends on the client you're using.
  2. Log into your server via SSH, and execute any of the following commands; they both do the same thing. . If these commands produce no output, most likely, the process was successful.
  3. Verify that the V host has been successfully activated by entering the hostname you specified in the V host file into your browser's address bar and pressing Enter. If your site loads without any errors, your new virtual host works!

Part 4. Your website and its files

By default in Apache, the root directory for a website is

/var/www/html
. This is fine if you only want to host 1 site, but for multiple sites, you'd generally create separate directories inside the /var/www directory and set those directories as root directories for the sites.

The root directory is at the very top of a file system or directory structure. In Unix terms, the root directory is denoted by a forward slash (/) character. In the context of website directory structures, the root directory is the main directory where your website's files, including HTML, JavaScript, CSS and PHP files, as well as other directories and/or downloadable files, are stored. A true URL is a URL that ends with a forward slash, such as

https://www.houseoffireseed.ml/
, or
https://christopherw.me/
. The forward slash at the end of the URL signifies the root directory. Directories housed within the root directory are called subdirectories. To access a subdirectory, you'd simply add its name at the end of the URL. For example, accessing the downloads subdirectory of your site's root directory is as simple as entering
https://my.site/downloads
. .

Manipulating items on your website is as easy as adding, modifying and/or deleting them in the site's root directory on your server, or a subdirectory therein. If you were to have a directory index listing on your site, that is, a directory without a home/index page, open in your browser, and you uploaded a file to the directory you're currently viewing, you'd simply press F5 in the browser and the change you just made would be instantly visible! Of course, if there was a web page there, you'd have to manually update the code of that page to link to the item you just added if you wanted it to be seen on the page, but the same principle of pressing F5 in the browser to have the update(s) you made to the page be visible immediately still remains.

Part 5. Conclusion

I hope this guide has helped you to understand the basics of setting up your own Apache server and getting a website or 2 heated up. It's a very technical process, with some very complicated principles and concepts to try to get your head around, but I'm more than confident that with a bit of practice, messing around and reviewing this guide or certain parts therein, you'll get the hang of it eventually!

In the next guide, we'll discuss some steps you can take to further configure your website(s) through the power of the almighty htaccess file. Until then, I wish you the best of luck in getting your Apache server working!

See thee!