Understanding Apache and website configurations

Before creating a Wordpress on your computer, let’s see the architecture of files and directories in a LAMP stack – the most important ones and their functions.

Our examples will be from MAMP installed on a MacOS computer, however the principles are the same in LAMP and WAMP, just the names of the folders and the paths to the files can be different. The most important thing is to understand the logic and not to know the exact file names or their paths.

The Apache Conf directory

mamp-apache-conf-directory

You can access this folder from your applications list > MAMP > conf.

Here you can see several folders:

  • Apache: This folder contains the Apache configuration files
  • Phpx.x: It contains all the files for PHP to work. Each PHP version has its own conversation files corresponding to its version.

Let’s see the Apache configuration files!

The Apache configuration main file

Open the httpd.conf file in your favourite text editor (if you are on Windows, you should use Notepad++). On MAMP for MacOS, this Apache configuration file is in conf > apache:

apache-conf-file

This file includes several things:

  • the global environment of the Apache server
  • the Apache server configuration
  • how your websites are managed

If you don’t have this file because you are using another OS and you want to follow this article based on the exact provided information, you can download the apache conf directory here.

The global environment of the Apache server

Note that all the lines that begin with “#” are just comments, they will never be executed unless you uncomment them (by removing the “#”). Based on this information the first line which is executed is:

ServerRoot "/Applications/MAMP/Library"

This line informs you where the root folder is that contains the other configuration files which Apache needs to run. Especially it contains all the different modules that Apache can use. Remember the example we have previously given concerning the URL rewriting that can be done thanks to an Apache module!

Of course, this directory can change from one distribution to another, and if you are on Windows you may have another path to these files.

The other global main directives that you can find in this file are:

Listen 8888

It informs your computer/server what the port is on which Apache is listening. If this notion of port is not clear for you, just keep in mind that it states from which entry number of the server Apache will handle the things. Usually, the default entry for a web server is 80, and the default secure entry is 443. When you enter an address in your browser you don’t define the port, as by default, if nothing is defined, your request will enter the server through the port number 80, and the server will have its port number 80 opened (in most of the cases if it’s a web server open to the world!).

In our example, it is on the entry number 8888. It is just to have a different workplace than the usual web workplace. We could have defined or added another port here.

LoadModule

This directive is telling Apache which modules to load. If you scroll down you will see our module for URL rewriting is loaded:

LoadModule rewrite_module modules/mod_rewrite.so
Include directory/>files

These “Include” directives that you will find commented near the bottom of this file are quite important if you want to add extra configurations to your server. To sum it up, if you uncomment one of the lines here, it will add the directives which are in the designed file to the global configuration. Just note, for now, the Include directive for Virtual Hosts as we will see this one in the paragraph about how your webspace is managed.

The Apache server configuration

ServerName localhost:8888

It indicates the server name. Usually it is the IP address of the server.

<Directory "directory name"/>
...
</Directory/>

All the things between the

<Directory>

tags are directives about access rights and functioning rules on the designed directory.

<IfModule "module_name" />
...
</IfModule/>

When the server uses a specific module we can give it specific instructions to execute.

AccessFileName .htaccess

It gives a priority to the use of the “.htaccess” file over the other files. You will often find these .htaccess files in different directories of your server and websites. It manages the permissions on the directory that contains it. The information about these kind of files in the Apache configuration are there to tell the server how to handle these .htaccess files.

<FilesMatch "^\.ht"/>
Order allow,deny
Deny from all
Satisfy All
</FilesMatch/>

This directive is adding some rights to the files which begin with “.ht”, thus every “.htaccess” file on the server. Here, for the order “allow, deny” it is marked “Deny from all”. That means if there are .htaccess files on your server they will never been taken into account (actually the directives they contain will never be taken into account).

“Satisfy All” means that all the instructions which are included in these <FilesMatch> tags must be satisfied. Here it is not really useful as all the users are denied to use the .htaccess files. But we could have other instructions, for example:

<FilesMatch "^\.ht"/>
Order allow,deny
Deny from all
Allow from 123.45.67.8
Require User from Matt John
Satisfy All
</FilesMatch/>

Here, all the instructions must be satisfied. That means only the authenticated users with username Matt, John and who make requests on the server from the IP address 123.45.67.8 will be able to use the .htaccess files directives.

We could also have “Satisfy Any” which would mean one of the instructions must be satisfied: either the requests from the defined IP or the defined authenticated users would be able to use the .htaccess files directives.

ErrorLog "/Applications/MAMP/logs/apache_error.log"

This one specifies where is the error log file. If you encounter a general error on your server (a 500 error), for example, you will be able to see the details of the error here.

How are your websites managed?

There are several options:

The Apache default option

Apache contains a default directory for your website. It is indicated thanks to this directive in the httpd.conf file:

DocumentRoot "/Applications/MAMP/htdocs"

It indicates the directory which contains the websites that are hosted on the server. It can be changed to another directory if you prefer to do so.

The VirtualHosts option

As previously seen in the paragraph related to “the global environment of the Apache server”, our httpd.conf file contains this line:

Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

If you uncomment it, your Apache server will also use the file httpd-vhosts.conf which is located in “/Applications/MAMP/conf/apache/extra/” in addition to the httpd.conf file.

The purpose of this file is to add several websites, located elsewhere than in the default directory of Apache, and to indicate their configurations to Apache .

Let’s take a look at this file (click on httpd-vhosts.conf if you don’t have it already). It contains:

NameVirtualHost *:80

This line informs Apache that there is a virtual host at the port 80. The “*” behind indicates that everything that points to this server on the default web port must be served with one of the virtual hosts – websites – indicated below.

And this file contains also several

<VirtualHost *:80/></VirtualHost/>

tags.

Between these tags, the configuration of one website is defined.

ServerAdmin webmaster@dummy-host.example.com

It indicates the email of the server admin.

DocumentRoot "/Applications/MAMP/Library/docs/dummy-host.example.com"

It defines the directory in which the files are hosted.

ServerName dummy-host.example.com

It indicates the URL of the website which is hosted there. Here it is “dummy-host.example.com”. Of course it is an example. If you have a real server you have to make your domain name associated to your server through the DNS options, otherwise your server will not recognize it. (That’s a thing we’ll see later).

ServerAlias www.dummy-host.example.com

That’s the optional other form of writing your domain name. Here it is with “www.” before the domain name.

ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log"

That’s the path to the error logs for this domain.

Depending on the distribution and the version of Apache, the file can be called apache.conf, or apache2.conf. Keep in mind that these file names and directories can change from one distribution to another, or from one version of Apache to another. However the logic behind always stays the same. Therefore just understand the logic: The configuration of Apache is located in a file which is often called httpd.conf or apache2.conf, and the instructions for a website are located either directly in the Apache configuration file or called through another file which is inside the Apache configuration folder (conf/apache/ or conf/httpd/).

How these information can be useful for you?

Let’s take several examples:

You have some errors and the error menu of your control panel doesn’t provide you with useful information. You just have to find the configuration file of your website and check where the error log file is located. Then you just have to open this file to see the exact error you encounter. You can even have no error in your control panel just because there is no error file declared in the configuration of your website. As you now know how to do it, you just have to find and open the configuration file of your website, and add the path to a file in which your server will write the errors.

Another example:

Let’s say you want to use the same files for several domain names. You want that domain2.com opens the same website than domain1.com which is already defined in your website configuration file. Open that file and just create a new entry for your domain2 which will have the exact same value for “DocumentRoot”:

<VirtualHost *:80/>
ServerAdmin email@admin.com
DocumentRoot "/directory/mywebsite"
ServerName domain1.com
ErrorLog "logs/domain1.com-error_log"
</VirtualHost/>
<VirtualHost *:80/>
ServerAdmin email@admin.com
DocumentRoot "/directory/mywebsite"
ServerName domain2.com
ErrorLog "logs/domain2.com-error_log"
</VirtualHost/>

As you can see the two domains, domain1.com and domain2.com, are using the same folder for the website “/directory/mywebsite”, and thus they will display the same website.

That are just some examples, but knowing these configuration files exist gives you more “behind the scene” understanding on how your website is working.

In all of these cases, if you want to see your changes being applied, you will have to restart Apache. Of course I presume you have done your changes through a command by being connected with SSH. So to restart Apache, you just have to enter this command:

service httpd restart

Or, depending on your distribution and Apache version,

service apache2 restart

You don’t use SSH already? Really? Let’s dive into the SSH world in a few moments! 🙂