monetization

Learn Linux: How to configure Apache on Debian

Apache Web Server is one of the most popular web servers in the world. It's very easy to setup, contains a plethora of extensions and can be customized to suit your website. This guide will walk you through how to install Apache on Linux and set it up to serve your domain names.

Installing Apache

First you'll want to update your packages to the latest version:

apt-get update
apt-get upgrade

Now you want to install apache:

apt-get install apache2

You have now installed the default installation of Apache. If you go to your server's IP address, you'll see the default Apache installation page. On Debian, your Apache default web directory is normally /var/www/ If you add a new index.html file and visit your IP address, you should see it being served by Apache.

Quick Start Commands

Here are some useful quick start commands for managing your Apache server. Stop Apache:

service apache2 stop

Start Apache:

service apache2 start

Restart Apache:

service apache2 restart

Apache Configuration

The default Apache configuration file can be found at:

/etc/apache2/apache2.conf

Generally you won't need to modify this file as most of our edits and additions will come in the form of separate virtual host files. Let's take a look at the default virtual host file:

/etc/apache2/sites-available/000-default.conf

A virtual host allows you to setup your websites to be served by Apache. When a user makes a request to your site, Apache processes it and tries to match it against any of your Virtual Hosts. By using the directive settings, Apache can look for the correct files to serve back to the user.

The 000-default.conf file should look something like this:

<VirtualHost>
     ServerAdmin webmaster@localhost
     DocumentRoot /var/www

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

You start by declaring your VirtualHost with an opening tag, specifying the host and port. The default setting is *:80 where * is a wildcard to indicate any IP/Domain address. The default port is 80 which is how most traffic will be directed to your site.

If you have a specific domain name, say example.com, you could make a VirtualHost to only recognise requests for that domain:

<VirtualHost example.com>
</VirtualHost>

Notice how we didn't indicate a port- that's because by default it assumes port 80. Let's look at some of the other directives:

  • ServerAdmin directive indicates the contact email address of the admin of the server.
  • DocumentRoot is where Apache looks for files to serve to the user. For example, if you tried to visit /test.php, Apache would look for /var/www/test.php
  • The ErrorLog and CustomLog are used to determine where and how your log files are stored. They are useful for debugging any errors you have on your server since Apache logs them to these files. The default directory is /var/log/apache2/.
  • ServerName - Allows you to configure Apache to respond to requests based on the domain name. ServerAlias - Allows you to specify an alternative name/address that will match the current address.

We can also make rules for specific directories and files for a VirtualHost.

  • <Directory> - Allows you to specify rules and permissions for certain directories and files within them.
  • <Files> - Allows you to specify rules and permissions for certain files (or types).

If we want to deny access to our .htaccess files, we could write something like this:

<Files .htaccess>
Order Allow, Deny
Deny From All
</Files>

The Order Allow, Deny line specifies the order in which the rules should be applied. From the Apache site:

"First, all Allow directives are evaluated; at least one must match, or the request is rejected. Next, all Deny directives are evaluated. If any matches, the request is rejected. Last, any requests which do not match an Allow or a Deny directive are denied by default."

Our Deny rule is Deny From All which will stop requests for the file.

These types of rules can also be applied to and directives, as well as a list of these sections: http://httpd.apache.org/docs/current/sections.html

Setting up your site

In this example, let's assume our domain is http://www.patchesoft.com/ and that we want Apache to serve our files from a new directory. Let's also assume that there are multiple domain names on our server, so we can't just use the default configuration.

Our first step is to create a new VirtualHost file. Navigate to your available sites directory:

cd /etc/apache2/sites-available/

Note that sites-available and sites-enabled shouldn't be confused. Sites-available are where our config files are stored and sites-enabled are symlink files pointing to our configuration files. This makes it easy for us to disable/enable different sites quickly.

Let's create our VirtualHost file:

touch patchesoft.com.conf

Modify it with our editor:

nano patchesoft.com.conf

And write our configuration details:

<VirtualHost patchesoft.com>
ServerAdmin patchesoft@patchesoft.com
DocumentRoot /var/www/patchesoft.com
ServerName patchesoft.com
ServerAlias www.patchesoft.com
ErrorLog ${APACHE_LOG_DIR}/patchesoft.com.error.log
CustomLog ${APACHE_LOG_DIR}/patchesoft.com.access.log combined

<Directory /var/www/patchesoft.com>
AllowOverride All
</Directory>

</VirtualHost>

So here we have specified our VirtualHost for our domain patchesoft.com. We've set the contact email to patchesoft@patchesoft.com (this email is displayed to the user when something goes wrong) using the ServerAdmin directive.

We specify the DocumentRoot to /var/www/patchesoft.com where our files will reside and also we add in the ServerAlias of www.patchesoft.com so that requests will match both http://patchesoft.com and http://www.patchesoft.com/.

Then come our Log files and finally we specify that we want the files in the directory /var/www/patchesoft.com to be allowed to be overwritten - this is so our FTP client can write files.

The next step is creating our directory where our website files reside:

mkdir /var/www/patchesoft.com

And we can create a default index.html or index.php file (if you have PHP installed) to serve when we visit http://www.patchesoft.com/.

touch index.html

Finally, we need to enable our site. Remember before where we spoke about symlinks, we need to use a special apache command to generate our symlink:

a2ensite patchesoft.com.conf

You can check the symlink that was generated in the /etc/apache2/sites-enabled folder.

There is just one more step left to do, and that's restart Apache:

service apache2 restart

Now when visiting your domain, you should see the index.html file you created. This is a simple tutorial on how you can use Apache to begin serving your webpages. You can customise through configuration files sub-domains, IP addresses and just about anything else you want served on your network.



Enjoyed that? Check These Posts Out

Sager NP8377 Gaming Laptop Review

Amazon Affiliate Sites: 10 Successful Examples

Acer Aspire 5 Slim A515-43-R19L Laptop Product Review

Setup Amazon Affiliate API with PHP - Product Advertising API 5.0 (2020)

...
monetization

Article Comments

Let us know your thoughts below by adding a quick comment!

Leave A Comment