Debug School

rakesh kumar
rakesh kumar

Posted on

How to set and use environment variables by Apache with SetEnv

<VirtualHost *:80>
  ServerName motoshowcase.example
  ServerAlias motoshare.in motoshare.jp motoshare.us motoshare.co.uk

  DocumentRoot /var/www/motoshare-site

  # Enable SSI for .html files
  Options +Includes
  AddType text/html .html
  AddOutputFilter INCLUDES .html

  # Per-request conditional environment variables
  <If "%{HTTP_HOST} == 'motoshare.in'">
    SetEnv COUNTRY "India"
    SetEnv DOMAIN  "motoshare.in"
    SetEnv SUPPORT_EMAIL "support@motoshare.in"
  </If>
  <ElseIf "%{HTTP_HOST} == 'motoshare.jp'">
    SetEnv COUNTRY "Japan"
    SetEnv DOMAIN  "motoshare.jp"
    SetEnv SUPPORT_EMAIL "support@motoshare.jp"
  </ElseIf>
  <ElseIf "%{HTTP_HOST} == 'motoshare.us'">
    SetEnv COUNTRY "United States"
    SetEnv DOMAIN  "motoshare.us"
    SetEnv SUPPORT_EMAIL "support@motoshare.us"
  </ElseIf>
  <Else>
    SetEnv COUNTRY "Unknown"
    SetEnv DOMAIN  "default"
    SetEnv SUPPORT_EMAIL "support@default.com"
  </Else>

  # Logging for host and referer
  LogFormat "%h %l %u %t \"%r\" %>s %b HOST=\"%{Host}i\" REF=\"%{Referer}i\" UA=\"%{User-Agent}i\"" hostfmt
  CustomLog ${APACHE_LOG_DIR}/access.log hostfmt
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Explanation

<VirtualHost *:80>
Enter fullscreen mode Exit fullscreen mode

Defines a virtual host that matches all requests to port 80 (HTTP). Everything inside this block applies to requests matching the criteria.

ServerName and ServerAlias
ServerName motoshowcase.example: The main canonical domain for this virtual host.

ServerAlias motoshare.in motoshare.jp motoshare.us motoshare.co.uk: Additional domains that should be handled in this virtual host block (any of these will match here).

DocumentRoot /var/www/motoshare-site
All files will be served from this directory for requests that match this domain/alias.

Enabling Server Side Includes (SSI) for HTML
Options +Includes: Allows the use of SSI directives.

AddType text/html .html: Tells Apache .html files are HTML (needed for SSI).

AddOutputFilter INCLUDES .html: Enables the SSI processor for .html files, allowing server-side includes and echoes.

Per-request Conditional Environment Variables
This section sets custom variables based on the domain the visitor uses:

<If "%{HTTP_HOST} == 'motoshare.in'">
  SetEnv COUNTRY "India"
  SetEnv DOMAIN  "motoshare.in"
  SetEnv SUPPORT_EMAIL "support@motoshare.in"
</If>
Enter fullscreen mode Exit fullscreen mode

Checks the Host header; if it's motoshare.in, sets environment variables accordingly.

Uses for other domains, and for any domains not matched.

These variables (COUNTRY, DOMAIN, etc.) can be read by SSI, CGI scripts, PHP, or other backend for customization per domain.

Custom Logging

LogFormat "%h %l %u %t \"%r\" %>s %b HOST=\"%{Host}i\" REF=\"%{Referer}i\" UA=\"%{User-Agent}i\"" hostfmt
CustomLog ${APACHE_LOG_DIR}/access.log hostfmt
Enter fullscreen mode Exit fullscreen mode

Logs detailed information about each request, including client IP, request timestamp, requested URL, HTTP status, bytes sent, and values for Host, Referer, and User-Agent.

What this configuration achieves
Multiple domains, one codebase: All your domains are served from a single directory.

Dynamic per-domain variables: Apache will set COUNTRY, DOMAIN, and SUPPORT_EMAIL differently for each domain, letting you customize pages (with SSI, scripts, etc.).

Easy maintenance: Add more domains or change environment logic by editing only the config.

Enhanced logging: Every request logs the associated host and referer for audit and debugging.

How to use setenv

Access Variables in Laravel Application
A. Using the env() helper in config files:

// config/app.php (or any config file)
'country' => env('COUNTRY', 'DefaultCountry'),
'domain' => env('DOMAIN', 'defaultdomain.com'),
'support_email' => env('SUPPORT_EMAIL', 'support@default.com'),
Enter fullscreen mode Exit fullscreen mode

B. Using PHP directly in Controllers or Blade:

$country = getenv('COUNTRY');          // returns "India"
$domain = getenv('DOMAIN');            // returns "motoshare.in"
$supportEmail = getenv('SUPPORT_EMAIL'); // returns "support@motoshare.in"

Enter fullscreen mode Exit fullscreen mode

Another Example

<VirtualHost *:80>
  ServerName motoshare.in
  ServerAlias motoshare.jp motoshare.us

  DocumentRoot /var/www/motoshare-site

  # Enable SSI for .html files
  Options +Includes
  AddType text/html .html
  AddOutputFilter INCLUDES .html

  # Per-domain footer JS environment variable
  <If "%{HTTP_HOST} == 'motoshare.in'">
    SetEnv FOOTER_JS "/js/footer-in.js"
  </If>
  <ElseIf "%{HTTP_HOST} == 'motoshare.jp'">
    SetEnv FOOTER_JS "/js/footer-jp.js"
  </ElseIf>
  <ElseIf "%{HTTP_HOST} == 'motoshare.us'">
    SetEnv FOOTER_JS "/js/footer-us.js"
  </ElseIf>
  <Else>
    SetEnv FOOTER_JS "/js/footer-common.js"
  </Else>
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

apache-configuration-for-multi-domain-static-site

Top comments (0)