How to define environment variables for each virtual host in apache 2.4 and use it in php

投稿日:2017年3月15日 更新日:



Hello everyone, It’s candle
This time I’ll show you how to define environment variables for each virtualhost in apache 2.4 and use it in php

For example, when you are developing with wordpress, you don’t want to put password or access key or anything else under the control of git.
So, you can define it on apache and require it with getenv for use.


anything php project
you have already built some kind of virtual host.

Setting for environment variables

There is the apache configuration file in the following directory when you installed apache by brew.


Open this file and make sure that mod_env is enabled.

LoadModule env_module libexec/

You find out this line and comment out it.


Next, This is the sample virtual host.


In this , write environment variables as follows.
We will define an environment variable called HELLO_WORD.

<VirtualHost *:8080>
  ServerName alice.localhost
  DocumentRoot /Users/candle/apache/alice_blog
  SetEnv HELLO_WORD 'hello'

  <Directory "/Users/candle/apache/alice_blog">
    AllowOverride All
    Order allow,deny
    Allow from all

If you want to use the environment variables to below any directory, use .
In above sample, DocumentRoot is /Users/candle/apache/alice_blog, but if you want to use environment variable in under the all of apache directory, you write following.

<Directory "/Users/candle/apache">
  SetEnv GOOD_BYE 'bye'

Thne GOOD_BYE=’bye’ will be applied to /Users/candle/apache only when you access to alice.localhsot’s virtual host.
Apache configuration is finished.

using environment variables in php

You can be received apache environment variable with “getenv” or “$_SERVER”.


Let’s display it.

You will create a file and try to receive environment variables.
The root directory is “/Users/candle/apache/alice_blog” in the above sample apache configuration.
You create info.php there.

touch /Users/candle/apache/alice_blog/info.php

Write this code.

echo "getenv: ".getenv('HELLO_WORD');
echo "<br />";
echo "<br />";
echo "ENV: ".$_ENV['HELLO_WORD'];


Save it and access.


How is it? It probaly gets the environment variable by getenv and $_SERVER.


If you define environment variables for each virtual host, it is useful because you can change the value depending on the site that accesses even the same environment variable name.


If you think this article is good, share it please

-, ,



Your email address will not be published. Required fields are marked *


React + S3 + Route53 + CloudFront HTTPS connection using SSL

Abstract Hello everyone it’s me candle. This time we would like to try SSL connection with the React web application deployed to S3. Programers recently develop web services on the API server and React front end. Then SSL connection is often required on the React side as well. Not only that, but recently https is being standardized. We try to taht. Condition React web is deployed to S3 You have the S3 bucket that name is same as Route53 domain name Create a CloudFront distribution First we will create the distribution of CloudFront. Go to the CloudFront management console, select …

Collect Rails 5 production.log with td-agent

English 日本語 Abstract Hello everybody It’s me candle. In this article I will show you how to collect rails 5 production.log with td-agent. Rails 5 was changed log format and it cause some problem. I will solve it and explain. relational article If you are using rails 4 please refer to the following article. td-agentでRails 4のproduction.logを取得する Precondition You can work ruby on rails in production mode Before read this article you setup the rails production environment such as webrick, apache, puma and nginx. You already installed td-agent or fluentd. You need td-agent or fluentd. It is installed on the ruby …

Setup of td-agent-ui and operation check

English 日本語 Abstract Hello everyone, It’s candle. In this time, I will show you how to set up td-agent-ui and operation check. Td-agent is easier than fluentd to operate on the server. Precondition Td-agent is installed Please refer to the following article when building td-agent to Centos 6.5. Build up a td-agent server on centos 6.5 For EC2, here Build up td-agent server on the EC2 of aws

How to get nginx error.log and access.log with td-agent

English 日本語 Abstract Hello everyone, It’s candle. In this time, we collect nginx log with td-agent. I explain with td-agent, but basic configuration is similer with fluentd. Precondition Td-agent or fluentd is installed You have a nginx server.

Collect the Rails 4 production.log with td-agent

English 日本語 Abstract Hello everybody, It’s me candle. In this time, let’s get the rails 4 production.log by td-agent. I have been written same article, but there was lack of explanation and etc, I will rewrite it again. Also, I will show you how to get Rails 5 production.log at a later date. Precondition You must be able to execute ruby on rails in production mode. It is ok in any environment as long as it has executable. Such as webrick, nginx, puma and apache. td-agent or fluentd is instlled. It is assumed that td-agent or fluentd is installed.

I work in the venture company as a CTO. I start to write program in University, first I learned java, C++ and PHP. In the company, I'm developing web services by Rails. I do like to automation.