httpd.conf, .htaccess, and php.ini (The difference)

Yes, these are all configuration files. Two of them even refer to the same things. But in reality they’re quite different.

Published Categorized as Domains & Hosting

Whether you’re a junior web developer or are in the process of building out your first website, understanding how to install and set up an Apache web server is one of the most important technical skills you will need to learn.

If you’re learning Apache server configuration—which is probably the case since you’re reading this post—one of the first things you need to know is the difference between the httpd.conf, .htaccess and php.ini files.

All of these files are text-based configuration files. That is, they store settings for how resources are accessed and how code is executed.

httpd.conf and .htaccess are for configuring the Apache web server (we’ll talk about the similarities and differences between them in a moment). php.ini, on the other hand, is for configuring the PHP runtime, which allows you to execute PHP code for dynamic web applications, on it.

Every Apache web server has an httpd.conf and .htaccess files. However, only Apache web servers with the PHP runtime installed will have a php.ini file. (Given the popularity of the PHP scripting language, these days, almost every web server has PHP.)

Configuring your Apache web server:

Both httpd.conf and .htaccess are text-based configuration files for an Apache web server. The configurations in httpd.conf apply to the entire server, and those in htaccess only to the folder it’s located in (and all of its subfolders).

The httpd.conf file is read at server startup. When you make changes to it, you need to restart Apache. .htaccess files, on the other hand, are read at every HTTP request, so changes to them don’t require restarting the server.

Configuring the PHP runtime:

php.ini is a text-based configuration file for the PHP runtime on a web server. It contains the default configuration for web applications written in the PHP server-side scripting language.

What is httpd.conf?

httpd.conf is a text-based configuration file for Apache web servers. (On some systems, like those powered by Debian, the name of the file is apache2.conf, and not httpd.conf.)

The httpd.conf file contains the main configuration of the Apache web server. Unlike the .htaccess file, which applies only to the folder it is stored in (and its subfolders), the httpd.conf file applies to the entire web server.

An httpd.conf file is written in statements, called directives, with each directive on a separate line in the text file. The syntax is the Apache Directive dialect of the Perl Compatible Regular Expressions (PCRE) language.

For example, the two directives below determine the locations of the server root and the document root folders:

ServerRoot   "/usr/local/apache"
DocumentRoot "/home/httpd/docs"

Sometimes a directive is too long for a single line, or its author has divided it into several lines to make it easier to read for himself and others. In such a case, the last character of each line is a backslash (\) to indicate that the directive continues on the next line.

A directive can also contain conditional statements. Usually, these conditional statements refer to whether a particular Apache module is enabled or not. If it is, the directive is executed; if it isn’t, it’s not executed.

For example, the directive below is only executed if Apache’s dir_module is enabled:

<IfModule dir_module>
    DirectoryIndex index.html index.php index.aspx

An example of a full httpd.conf file can be found at the University of Virginia’s website.

Some of the most important configurations contained in the httpd.conf file include the locations of specific root folders, the options for Apache modules, as well as directives in the <Directory>, <File>, and <Location> sections, which apply, respectively, to directories, files, and specific URIs.

What is .htaccess?

.htaccess is a text-based configuration file for folders (and their subfolders) on Apache web servers. As such, it contains directory-level configurations that override the server-level Apache configurations in httpd.conf.

Generally speaking, most webmasters use .htaccess files for access control, URL redirection, and URL rewriting, although there are a number of other use cases for them depending on the hosting, website/web app, and requirement.

With that being said, it’s only recommended to create and edit .htaccess files if you don’t have the option of editing the httpd.conf file on your server. (Many shared hosting service providers, for example, forbid the editing of the latter for security or for pricing reasons.)

The reason? Performance.

The .htaccess files in your website’s (or web application’s) directory structure are read on every HTTP request. If that structure is complex and based on many folders and subfolders, the requests can easily pile up and add precious milliseconds to your server’s response times.

(In contrast, the httpd.conf file is read once at server startup.)

This performance hit when using .htaccess is explained really well at technical SEO and web developer Daniel Morell’s blog.

Virtually anything that can be configured in an .htaccess file can also be configured in the Apache server’s httpd.conf file. And you can restrict any given directive to a specific folder (and its subfolder structure) by using the <Directory> section.

So, if you have the option, it’s better to do your access control, URL redirection, and URL rewriting in the httpd.conf file, and not in .htaccess files for every folder.

What is php.ini?

php.ini is the main configuration file for the PHP runtime on an Apache web server. It’s read at PHP startup and contains important settings for your PHP apps, including resource limits, maximum file size, maximum execution time, and others.

If you’re using shared hosting for your website or web application, it’s 99.9% likely that you’ll not be able to edit the php.ini file for your server.

However, you can try creating your own php.ini file with the server-level PHP configurations that you want to override, then upload it to the root directory of your hosting account. On most shared hosting service providers, this will do the trick.

Summing It All Up

httpd.confServer-level configuration for ApacheOn Apache startup
.htaccessDirectory-level configuration for ApacheOn every HTTP request
php.iniServer- and/or directory-level configuration for PHPOn PHP startup

If you want to configure Apache, you can do so in the server-level httpd.conf file or directory-level .htaccess file. If you want to edit your PHP runtime’s settings, you can do so by editing the php.ini file for the server or root directory in question.

Leave a comment

Your email address will not be published.