When comes into web development, Configuration is essential and need to adjust server configuration to meet overall project requirements and aspects.
- Configuration Files
- Basic Configuration
- Configuring Virtual Hosts
- HTACCESS Configuration
- Additional Configuration Options
- Key points to remember
The primary configuration file for Apache is usually named httpd.conf
and httpd-vhosts.conf
. This file contains directives that control various aspects of the server behavior.
Configuring apache server locally is possible due to full access by the user. How ever, on the web mostly, shared web hosting is used to host multiple websites on single server. Thus it will not allow us to edit those configuration httpd.conf
and httpd-vhosts.conf
.
But apache server gives possibility to configure with certain option through .htaccess
files. In order to install and use wordpress
, laravel
or any other PHP frameworks then it need .htaccess
to work properly.
From the configuration files, It is possibilities to configure following options of the server.
ServerRoot
: It specifies the root directory of the Apache installation.DocumentRoot
: It specifies the document root directory, where website files are located.ServerName
: It sets the server domain name.ErrorLog and CustomLog
: To configure error and access logs to monitor server activity.
<VirtualHost *:80>
ServerName example-host.com
DocumentRoot /var/www/example-host.com
<Directory "/var/www/example-host.com">
AllowOverride All
</Directory>
ErrorLog "/var/log/apache2/example-host.com-error.log"
CustomLog "/var/log/apache2/example-host.com-access.log" combined
</VirtualHost>
To configure multiple virtual hosts to serve different websites from the same server. Each virtual host has own DocumentRoot, ServerName, and other settings.
Example:
<VirtualHost *:80>
ServerName example-host.com
DocumentRoot /var/www/example-host.com
</VirtualHost>
<VirtualHost *:80>
ServerName another-host.com
DocumentRoot /var/www/another-host.com
</VirtualHost>
Shared hosting environments restricts direct access to configuration files httpd.conf
and httpd-vhosts.conf
. These configuration affects overall server behavior and primarily due to security concerns and resource management. However, most of shared hosting environments allow their own configuration through .htaccess
files.
In order to allow .htaccess
file on specific virtual host to work properly, then it need to edit configuration for virtual host and add AllowOverride All
property for the particular directory of virtual host. That tells .htaccess
can override the properties for current virtual host directory.
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/C:/xampp/htdocs/site1"
<Directory "/C:/xampp/htdocs/site1">
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
- Directory Index: It specifies the default document to serve when a directory is requested.
DirectoryIndex index.php index.html
- Error Documents: It customizes error pages for specific HTTP status codes.
ErrorDocument 404 /error.html
- Rewrite Rules: It allows to rewrite URLs. It makes more user-friendly and SEO-friendly.
RewriteEngine On
RewriteRule ^articles/([^/]+)$ blog/articles.php?post=$1 [L]
- File and Directory Permissions: It controls access to files and directories.
<Directory /var/www/html/protected>
Require valid-user
</Directory>
- MIME Types: It specifies the MIME type for different file extensions.
AddType application/x-httpd-php .php
- IP-Based Access Control: It restrict access to specific IP addresses or ranges.
<Limit GET POST>
Order deny,allow
Deny from all
Allow from 192.168.1.0/24
</Limit>
Other Options:
- Directory Directives: It sets control access permissions for files such as index files, and other settings for specific directories.
- FileType-Specific Directives: It controls Apache to handles different file types.
- Security Headers: Implement security headers to protect website.
- Modularity: Extend the functionalities by using Apache modules such as
mod_rewrite
for URL rewriting andmod_ssl
for SSL/TLS.
Note: To refer useful and variety of .htaccess
code snippets from this third-party repository
- Always test configurations before implement to the production server.
- Check with the official documentation and get expert advices.
- Use a configuration management tool to simplify deployment and management.