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.
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.
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.
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!"
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.
To install the extensions, execute the following command.
sudo apt install php php-curl php-gd php-imagick php-mysql php-xml php-zip
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.
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>
Let's break down this file line by line before we, ourselves, have a breakdown!
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.
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.
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.
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.
This line tells Apache to use the SSL module for SSL/TLS support on the site.
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.
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.
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.
Signifies the end of the VirtualHost directive.
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
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.
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.
To activate your newly created virtual host, follow the following steps.
/etc/apache2/sites-enabled. How you'd do this depends on the client you're using.
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.
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
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.
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!