joppot

The program absolutely runs in copy paste

Server

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

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

Pocket

setapacheenvhtumb

Abstract

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.


Premise

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

SPONSORED LINK


Setting for environment variables

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

/usr/local/etc/apache2/2.4/httpd.conf

Open this file and make sure that mod_env is enabled.

LoadModule env_module libexec/mod_env.so

You find out this line and comment out it.

commentinmodenv

Next, This is the sample virtual host.

alicelocal

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
  </Directory>
</VirtualHost>

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'
</Directory>

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”.

getenv('HELLO_WORD');
$_SERVER['HELLO_WORD']

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.

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

echozaemon

Save it and access.

showenvdata-1

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


Conclusion

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

-Server
-, ,

執筆者:


comment

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

関連記事

Monitor the memory state of ec2 with watch and free command

English 日本語 Abstract Hello everyone, It’s candle. In this time, I will show you how to monitor ec2 memory in real time. EC2 server of t2.micro has only 1GB of memory, If you run composer, rails, mysql on it, memory may be barely. It is useful if you can monitor the memory and tune the server. Precondition EC2 server

Build up td-agent server on the EC2 of aws

English 日本語 Abstract Hello everyone, It’s candle. In this time we will build up td-agent environment on the ec2. Precondition You have a EC2.

Build up a td-agent server on centos 6.5

English 日本語 Abstract Hello everyone, It’s candle. In this time, we build up a td-agent server on the centos 6.5. Td-agent is service based on fluentd. It daemonizes fluentd and creates a GUI. Premise You have Centos 6.5 server. I use Centos 6.5 on the vagrant for explanation.

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.

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.

Profile


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

twitter

スポンサードリンク

Archives