CodeIgniter4/general/environments.html
2019-02-18 11:53:51 -08:00

375 lines
14 KiB
HTML

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Handling Multiple Environments &mdash; CodeIgniter4 4.0.0-alpha.5 documentation</title>
<link rel="shortcut icon" href="../_static/ci-icon.ico"/>
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
<link rel="top" title="CodeIgniter4 4.0.0-alpha.5 documentation" href="../index.html"/>
<link rel="up" title="General Topics" href="index.html"/>
<link rel="next" title="Controllers and Routing" href="../incoming/index.html"/>
<link rel="prev" title="Managing your Applications" href="managing_apps.html"/>
<script src="../_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../index.html" class="icon icon-home"> CodeIgniter4
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a></li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">General Topics</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="configuration.html">Working With Configuration Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="urls.html">CodeIgniter URLs</a></li>
<li class="toctree-l2"><a class="reference internal" href="helpers.html">Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="common_functions.html">Global Functions and Constants</a></li>
<li class="toctree-l2"><a class="reference internal" href="logging.html">Logging Information</a></li>
<li class="toctree-l2"><a class="reference internal" href="errors.html">Error Handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="caching.html">Web Page Caching</a></li>
<li class="toctree-l2"><a class="reference internal" href="modules.html">Code Modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="managing_apps.html">Managing your Applications</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Handling Multiple Environments</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../license.html">The MIT License (MIT)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">CodeIgniter4</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> &raquo;</li>
<li><a href="index.html">General Topics</a> &raquo;</li>
<li>Handling Multiple Environments</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="handling-multiple-environments">
<h1>Handling Multiple Environments<a class="headerlink" href="#handling-multiple-environments" title="Permalink to this headline"></a></h1>
<p>Developers often desire different system behavior depending on whether
an application is running in a development or production environment.
For example, verbose error output is something that would be useful
while developing an application, but it may also pose a security issue
when &#8220;live&#8221;. In development environments, you might want additional
tools loaded that you don&#8217;t in production environments, etc.</p>
<div class="section" id="the-environment-constant">
<h2>The ENVIRONMENT Constant<a class="headerlink" href="#the-environment-constant" title="Permalink to this headline"></a></h2>
<p>By default, CodeIgniter comes with the environment constant set to use
the value provided in <code class="docutils literal"><span class="pre">$_SERVER['CI_ENVIRONMENT']</span></code>, otherwise defaulting to
&#8216;production&#8217;. This can be set in several ways depending on your server setup.</p>
<div class="section" id="env">
<h3>.env<a class="headerlink" href="#env" title="Permalink to this headline"></a></h3>
<p>The simplest method to set the variable is in your <a class="reference internal" href="configuration.html"><span class="doc">.env file</span></a>.</p>
<div class="highlight-ini"><div class="highlight"><pre><span></span><span class="na">CI_ENVIRONMENT</span> <span class="o">=</span> <span class="s">development</span>
</pre></div>
</div>
</div>
<div class="section" id="apache">
<h3>Apache<a class="headerlink" href="#apache" title="Permalink to this headline"></a></h3>
<p>This server variable can be set in your <code class="docutils literal"><span class="pre">.htaccess</span></code> file, or Apache
config using <a class="reference external" href="https://httpd.apache.org/docs/2.2/mod/mod_env.html#setenv">SetEnv</a>.</p>
<div class="highlight-apache"><div class="highlight"><pre><span></span><span class="nb">SetEnv</span> CI_ENVIRONMENT development
</pre></div>
</div>
</div>
<div class="section" id="nginx">
<h3>nginx<a class="headerlink" href="#nginx" title="Permalink to this headline"></a></h3>
<p>Under nginx, you must pass the environment variable through the <code class="docutils literal"><span class="pre">fastcgi_params</span></code>
in order for it to show up under the <cite>$_SERVER</cite> variable. This allows it to work on the
virtual-host level, instead of using <cite>env</cite> to set it for the entire server, though that
would work fine on a dedicated server. You would then modify your server config to something
like:</p>
<div class="highlight-nginx"><div class="highlight"><pre><span></span><span class="k">server</span> <span class="p">{</span>
<span class="kn">server_name</span> <span class="s">localhost</span><span class="p">;</span>
<span class="kn">include</span> <span class="s">conf/defaults.conf</span><span class="p">;</span>
<span class="kn">root</span> <span class="s">/var/www</span><span class="p">;</span>
<span class="kn">location</span> <span class="p">~</span><span class="sr">*</span> <span class="s">\.php</span>$ <span class="p">{</span>
<span class="kn">fastcgi_param</span> <span class="s">CI_ENVIRONMENT</span> <span class="s">&quot;production&quot;</span><span class="p">;</span>
<span class="kn">include</span> <span class="s">conf/fastcgi-php.conf</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Alternative methods are available for nginx and other servers, or you can
remove this logic entirely and set the constant based on the server&#8217;s IP address
(for instance).</p>
<p>In addition to affecting some basic framework behavior (see the next
section), you may use this constant in your own development to
differentiate between which environment you are running in.</p>
</div>
<div class="section" id="boot-files">
<h3>Boot Files<a class="headerlink" href="#boot-files" title="Permalink to this headline"></a></h3>
<p>CodeIgniter requires that a PHP script matching the environment&#8217;s name is located
under <strong>APPPATH/Config/Boot</strong>. These files can contain any customizations that
you would like to make for your environment, whether it&#8217;s updating the error display
settings, loading additional developer tools, or anything else. These are
automatically loaded by the system. The following files are already created in
a fresh install:</p>
<ul class="simple">
<li>development.php</li>
<li>production.php</li>
<li>testing.php</li>
</ul>
</div>
</div>
<div class="section" id="effects-on-default-framework-behavior">
<h2>Effects On Default Framework Behavior<a class="headerlink" href="#effects-on-default-framework-behavior" title="Permalink to this headline"></a></h2>
<p>There are some places in the CodeIgniter system where the ENVIRONMENT
constant is used. This section describes how default framework behavior
is affected.</p>
<div class="section" id="error-reporting">
<h3>Error Reporting<a class="headerlink" href="#error-reporting" title="Permalink to this headline"></a></h3>
<p>Setting the ENVIRONMENT constant to a value of &#8216;development&#8217; will cause
all PHP errors to be rendered to the browser when they occur.
Conversely, setting the constant to &#8216;production&#8217; will disable all error
output. Disabling error reporting in production is a
<a class="reference internal" href="../concepts/security.html"><span class="doc">good security practice</span></a>.</p>
</div>
<div class="section" id="configuration-files">
<h3>Configuration Files<a class="headerlink" href="#configuration-files" title="Permalink to this headline"></a></h3>
<p>Optionally, you can have CodeIgniter load environment-specific
configuration files. This may be useful for managing things like
differing API keys across multiple environments. This is described in
more detail in the Handling Different Environments section of the
<a class="reference internal" href="configuration.html"><span class="doc">Working with Configuration Files</span></a> documentation.</p>
</div>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="../incoming/index.html" class="btn btn-neutral float-right" title="Controllers and Routing" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="managing_apps.html" class="btn btn-neutral" title="Managing your Applications" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2014-2019 British Columbia Institute of Technology.
Last updated on Feb 18, 2019.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'../',
VERSION:'4.0.0-alpha.5',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: false,
SOURCELINK_SUFFIX: ''
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>