Skip to main content

Getting Your Website Online Apache Setup for Ubuntu or Debian

· 13 min read
Yashwin Shankar
Front End Developer @ Tech4Biz Solutions Pvt Ltd.
Introduction:
Apache is like the engine that runs many websites you visit every day. It's super popular and used by over half of all websites online! While there are other web servers out there, Apache is a great one to learn about because it's everywhere.

In this article, we'll talk about some basic settings in Apache that you can tweak to customize how your website works. We'll focus on how things are set up in Ubuntu and Debian, which are types of operating systems. They have a particular way of organizing Apache's settings that might be a bit different from other systems, but don't worry, we'll guide you through it step by step!

Getting started:
Before we get started, it's important to make sure your Ubuntu system is up to date. If you're using an older version like 16.04 or below, it's a good idea to upgrade to a newer version. This is because older versions may not receive security updates anymore, which could make your system vulnerable.
Don't worry though! We've got guides to help you through the upgrade process. Just follow along, and we'll make sure your system is ready to go!
Before we begin, make sure you have the following:
  • A server running Ubuntu (an operating system).
  • A regular user account with special permissions (called sudo privileges).
  • A firewall set up to protect your server.
If you're not sure how to set these up, don't worry! We have guides to help you. Just choose your operating system (Ubuntu or Debian) from the list and follow our Initial Server Setup Guide.
Now, before we start, you need to have Apache installed on your server. We've got tutorials to help you with that too! Depending on your Ubuntu version, follow either the "How to Install the Apache Web Server on Ubuntu" or "How to Install the Apache Web Server on Debian 10" tutorial. They'll walk you through the installation process step by step.
Getting Started with Apache on Ubuntu: 5 Easy Steps
  1. Understanding Apache's Folders: Learn where Apache keeps its important files.
  2. Getting to Know the Apache2.conf File: Explore the main configuration file for Apache.
  3. Adjusting Apache's Settings: Make changes to how Apache behaves across your entire server.
  4. Creating Virtual Hosts: Set up separate configurations for different websites on your server.
  5. Activating Websites and Features: Enable your websites and additional functionalities in Apache.

Understanding Apache's Folders:
Apache stores its important files in a special folder called /etc/apache2. This folder is where you can find all the settings and configurations for your Apache web server. To see what's inside this folder, you can use a command that lists all the files.
ls -f /etc/apache2

Output

envars sites-available . apache2.conf .. sites-enabled mods-available ports.conf magic mods-enabled conf-enabled conf-available

Inside this folder, you'll see a bunch of simple text files and a few folders. Here are some key places you should know about:
  1. apache2.conf: This is the main settings file for the server. Most settings are managed here, but it's recommended to use separate files for easier organization. This file sets up defaults and is where the server looks for configuration details.

  2. ports.conf: This file sets the ports that virtual hosts should use. It's important to check this file if you're setting up SSL (secure connections).

  3. sites-available/ and sites-enabled/: In the sites-available folder, you'll find configurations for virtual hosts (websites). These configurations determine which content gets served for different requests. The sites-enabled folder contains active virtual host configurations. Apache reads these files when it starts up or reloads to compile the full configuration.

  4. conf-available/ and conf-enabled/: These folders hold extra configuration pieces that aren't specific to virtual hosts.

  5. mods-available/ and mods-enabled/: Here, you'll find optional modules that Apache can load. Files ending in .load load the modules, while those ending in .conf store module configurations.
In Apache, configuration isn't done in just one big file. Instead, it's set up in a modular way. This means you can add and change settings by working with different files, making it easier to manage your server.

Getting to Know the Apache2.conf File:
The main settings for your Apache server are stored in a file called apache2.conf, located in the /etc/apache2/ folder. This file is divided into three main parts:
  1. Settings for the overall Apache server process.
  2. Settings for the default server.
  3. Settings for virtual hosts.
You can open this file using any text editor you like. For example, you can use a simple one called nano.
sudo nano /etc/apache2/apache2.conf

In Ubuntu and Debian, the apache2.conf file is where you set up global settings for your server. Instead of cramming all settings into one file, you use the Include directive to bring in other configuration files. This helps keep things organized.

Inside apache2.conf, you'll see statements like Include and IncludeOptional. These tell Apache to read additional files, such as module definitions, ports.conf, configurations from the conf-enabled/ folder, and virtual host settings from the sites-enabled/ folder. This way, Apache puts together all the settings it needs when it starts up.
…
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
…
Include ports.conf
…
IncludeOptional conf-enabled/*.conf
…
IncludeOptional sites-enabled/*.conf

Adjusting Apache's Settings:
Now, let's make some changes to how Apache works on your server. We'll adjust settings that affect the entire server, so it's important to get these settings right.

When setting up your Apache server, it's essential to understand and adjust some key parameters to optimize its performance. Here's a breakdown of important settings:
  1. Timeout: By default, Apache gives itself 300 seconds to complete each request. You can safely lower this to between 30 and 60 seconds for faster responses.

  2. KeepAlive: When KeepAlive is turned on, it lets one connection handle multiple requests from the same client. If it's turned off, each request requires a new connection, which can increase overhead. Keeping KeepAlive on can save time, especially if you have a lot of traffic.

  3. MaxKeepAliveRequests: This setting determines how many requests each connection can handle before it's closed. A higher value allows Apache to serve content more efficiently. The default setting is 100, but you can set it to 0 for an unlimited number of requests per connection.

  4. KeepAliveTimeout: This specifies how long Apache should wait for the next request after completing the last one. If the timeout is reached, the connection is closed. The default is 5 seconds.

After making changes, you can close the configuration file by pressing CTRL+X.
Multi-Processing Modules (MPMs):
These extend Apache's capabilities to listen for, direct, and handle various network requests. You can find out which MPM your Apache uses by running a specific command.
apache2 -L

Output

Compiled in modules:
core.c
mod_so.c
mod_watchdog.c
http_core.c
mod_log_config.c
mod_logio.c
mod_version.c
mod_unixd.c

To find out the type of Multi-Processing Module (MPM) your server is using, you can use the command: a2query -M.
a2query -M

Output

event

The result shows that the event MPM is being used on this server. Your setup might offer different options, but you can only choose one at a time.
Creating Virtual Hosts:
Now, let's set up individual configurations for different websites on your server. This helps Apache know which content to serve for each website you host.
You'll find a default virtual host declaration in a file called 000-default.conf within the sites-available/ directory. You can take a look at this file to understand the basic format of a virtual host configuration.
To open the file, use the following command:
sudo nano /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
…
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
…
ErrorLog $ {APACHE LOG DIR}/error.log
CustomLog $ {APACHE LOG DIR}/access.log combined
…

The default virtual host is set up to deal with any requests on port 80, which is the standard for HTTP. You'll see this in the declaration header, where it says *:80, meaning it's listening on port 80 on any interface.

But just because it's set up this way doesn't mean it will handle every request to the server on this port. Apache looks for the most specific virtual host definition that matches the request. If it finds one that's more specific, it'll use that instead.

After you're done checking out the file, you can close it by pressing CTRL+X.

Exploring Apache Virtual Host Settings:

In the virtual host configuration, you'll find various options that apply to the entire virtual host. These settings are separate from any other specific configurations.

To begin, let's take a look at the security.conf file located in the conf-available/ directory.
sudo nano /etc/apache2/conf-available/security.conf

In this file, there's a setting called "Server Signature" which allows you to specify an email address for server-related issues. By default, it's set to "On," but switching it to "EMail" will display the server admin's email address.

Remember, if you make this change, be prepared to receive emails regarding server problems.
…
ServerSignature EMail
…

To exit the file, press CTRL+X. After you've made changes, a prompt will ask if you want to save them. Press Y to save or N to discard.

In your virtual host file, you can include a directive called ServerName. This specifies the domain name or IP address that this request should handle. If it matches the ServerName value, it'll override the default definition.

To open your virtual host file, use the following command, replacing "your_domain" with your actual domain name:
sudo nano /etc/apache2/sites-available/your_domain.conf

Add your domain name to the ServerName directive.Just include your actual domain name after the ServerName directive in the virtual host file.
…
ServerName your_domain
…

You can make the virtual host apply to multiple names using the ServerAlias directive. This allows you to specify alternate paths to access the same content. For example, a common use case is adding "www" before your domain name.
…
ServerAlias www.your_domain.com
…

The DocumentRoot directive tells Apache where to look for the content for this virtual host. On Ubuntu, the default setup serves content from the /var/www/ directory.

…
DocumentRoot /var/www/your_domain/public_html
…

Managing Directory Settings:
Within the virtual host configuration, there are rules for how the server handles different directories on the file system. Apache applies these rules in a specific order, from shortest to longest directory paths. This allows later rules to potentially override earlier ones.

To open the apache2.conf file, use this command:
sudo nano /etc/apache2/apache2.conf
…
<Directory />
      Options FollowSymLinks
      AllowOverride None
      Require all denied
</Directory>
<Directory /usr/share>
      AllowOverride None
      Require all granted
</Directory>
<Directory /var/www/>
      Options Indexes FollowSymLinks
      AllowOverride None
      Require all granted
</Directory>
…

The first directory definition sets rules for the root directory (/), which is the starting point for all files on the server. This configuration serves as the foundation for your virtual host, applying to all files on the file system.Take note of the directory settings and helpful comments in this file. By default, access to all content is denied unless specified otherwise in later directory definitions

The Require directive decides who can access different parts of your server. Meanwhile, the AllowOverride directive determines if an .htaccess file can change settings in the content directory. By default, this is not allowed, but it can be handy to enable in certain situations. After you're done looking at this file, you can close it by pressing CTRL+X.

Handling Aliases and Scripts:
Before defining directories, you might encounter Alias or ScriptAlias directives. These instructions help Apache manage specific paths on your server.To open your virtual host configuration file, use this command, and remember to replace "your_domain" with your actual domain name:
sudo nano /etc/apache2/sites-available/your_domain.conf

The Alias directive helps Apache connect a URL path to a directory path. For example, in a virtual host for your_domain, using this directive lets you access content stored in /usr/local/apache/content/ when you visit your_domain.com/content/.

Alias “/content/” “/usr/local/apache/content/”

Just like the Alias directive, the ScriptAlias directive connects a URL path to a directory path. However, it's specifically used for directories that contain executable components.
ScriptAlias "/cgi-bin/" "/usr/local/apache2/cgi-bin/"

After you finish making changes to the file, remember to save your edits by pressing CTRL+X. If you've made changes, you'll be asked if you want to save them. Press Y for yes, or N for no to discard them.And don't forget to define the directory with the appropriate access privileges as explained earlier!

Activating Websites and Features:
Once you've set up your virtual host file just the way you want it, you can use Apache's tools to turn it into a live website. To do this, you'll create a symbolic link in the sites-enabled directory that points to your virtual host file in the sites-available directory. Here's the command to do that. Just make sure to replace "your_domain" with the name of your own virtual host site configuration file:
sudo a2ensite your_domain

After you've activated a site, use this command to let Apache know to update its settings, making sure the changes you made take effect:
sudo systemctl restart apache2

There's a way to turn off a virtual host too. You do this by deleting the symbolic link from the sites-enabled directory. For instance, if you have the default 000-default site enabled, you can disable it like this:
sudo a2dissite 000-default

You can turn modules on or off using the a2enmod and a2dismod commands. They're similar to the a2ensite and a2dissite commands.For instance, if you want to activate the info module, you can use this command:
sudo a2enmod info

Similarly, if you want to turn off a module, you can use the a2dismod command:
sudo a2dismod info

Don't forget to restart Apache after you've made changes to configuration files or after enabling or disabling modules.

Conclusion

Apache is flexible and made up of many parts, so the settings you need might vary based on your setup. After looking at some common scenarios, you should have a good grasp of what the main configuration files do and how they work together.

If you want to learn about specific settings, the files we've talked about have helpful comments, and Apache offers great documentation. Hopefully, you're feeling less overwhelmed by the configuration files now, and you're ready to try making changes to fit your needs

Remember, if you have any questions or need further assistance, feel free to ask!