CodeIgniter3: $_SERVER['CI_ENV'] not able to set of configured - php

Unable to set the environment of the CodeIgniter-3 framework. Please help.
I see that in their default installation, their index.php has this. Nothing is mention where to set the ENVIRONMENT value.
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
Why would CI_ENV ever already be set within the $_SERVER array?

Steps :
I : First create .htaccess file in the root of the project, parallel to index.php file.
II : Paste these code in .htaccess file.
<IfModule mod_rewrite.c>
SetEnv CI_ENV development # For Developement
#SetEnv CI_ENV testing # For Testing
#SetEnv CI_ENV production # For Production
</IfModule>
III : If mod_rewrite is not enabled then first enable it.
IV : Now run the project. You will get desired output.

Related

htaccess SetEnv directive is not working in MAMP pro 6

I have a solution develop with PHP
Framework Phalcon 3.4.5
macOS 12.6
PHP 7.3.33
MAMP Pro 6
I have this .htaccess that is in my root folder:
A SetEnv directive is set like that:
<IfModule mod_env.c>
SetEnv APPLICATION_ENV=development
</IfModule>
The right env module are loaded in Apache:
mod_env.so
mod_setenvif.so
My host config have AllowAoverride All
My AccessFileName is set to htaccess
I use this code in my index file to read the environment variable and I can't get the value.
defined('ENVIRONMENT') || define('ENVIRONMENT', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
echo ENVIRONMENT . PHP_EOL;
Considering the SetEnv in the htaccess, should get development has a value, but I keep getting production.
I did test to be sure that mod_env and mod_setenvif are loaded. There is no error in my apache log and php log.
I have no clue why. Any help will be welcome here?
Thank you!
Environment variables set with SetEnv in an htaccess file are available in the Apache environment, so you should use apache_getenv() or the $_SERVER superglobal to access the variable rather than getenv().
You appear to be using SetEnv correctly, and the rest of your setup looks fine.
Perhaps you may be interested in a .env file instead in your root directory, and using vlucas/dotenv to load it in, and use Dotenv in your code instead.
Here's an example of using that package:
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
It will look for an .env file in __DIR__ and load them into your environment, making them available via getenv()

PHP Include_path not working apache server

Currently in my PHP coding I have to put include($_SERVER['DOCUMENT_ROOT']"./ in front of every file path in my web code. I previously asked and discovered I could set include_path. I tried setting this in both .htaccess and the .conf for the virtual host, but it just fails out and doesn't work/load the include.
Code:
this is in the vhost .conf file:
<IfModule mod_php7.c>
php_value include_path ":/var/www/testdomains/domain-test1.local/php"
php_admin_flag engine on
</IfModule>
(I've tried putting this outside the IfModule area with no success.
My .php file on the webserver:
<?php require "/authcheck.php" ?>
What am I doing wrong?

How to properly use Git. Using CodeIgniter w/ DEVELOPMENT, TESTING, and PRODUCTION servers

Basically, I have a file index.php which should be different in each server. The file indicates which server it is on.
It has something like this:
On the DEVELOPMENT environment:
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
On the TESTING environment:
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'testing');
On the PRODUCTION environment:
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production');
What's the proper way to maintain these files on each server without Git touching it?
I've added the file to .gitignore now, and I've untracked it using git rm --cached index.php then git push origin master for TESTING server and stable for PRODUCTION server.
fyi,
if you use apache you don't really have to change the index.php file because you can use your htaccess file to define your environment
for example :
SetEnvIf Host yourdevdomain$ CI_ENV=development
SetEnvIf Host yourtestdomain$ CI_ENV=testing
SetEnvIf Host example.com$ CI_ENV=production
In this case, you can set your needed Environments in your htaccess file, and you don't have to worry about overwriting some stuff with different files from different environments.
i think there is an option on nginx too
Take a look here
You can get more information from the official CI Documentation here.
With Apache you can set CI_ENV like #sintakonte's answer. With nginx and php-fpm you can set CI_ENV like this:
location ~ \.php {
....
fastcgi_param CI_ENV production;
}
And leave the index.php file as default.
Cheers.

How Symfony activate automatically the prod mode en MacOS with MAMP

I use the global symfony command to create a project under my ~/tmp/ directory, and aliased it to my /Application/MAMP/htdoc/ Apache Server's webroot.
Surprise, the project was in prod mode. I know it because the underline debug tool known as the "profiler" is missing and all my bugs was non visible, and when I search in app/logs/dev.log, the file is missing too, but there is a app/logs/prod.log.
If I run app/console server:run, the project is in dev mode.
How is that possible ? Maybe all the software installed by MAMP?
open_ssl, mod_fastcgi, mod_perl, mod_ssl, mod_wsgi?
I usually only use the build-in server of PHP to run the project and I never set the prod mode before.
I don't recommend you to change the value of the second argument of AppKernel in web/app.php.
Instead I recommend you to configure in your local MAMP setup to use app_dev.php as the PHP index file, which is what the server:run command does too.
It's due to the .htaccess of the project's webdirectory.
When you browse a Symfony application through Apache, you are in production environment by default, no matter the OS used.
To go in dev mode, open the file web/app.php , find the following line :
$kernel = new AppKernel('prod', false); // Prod env, debug disabled
And change it to :
$kernel = new AppKernel('dev', true); // Dev env, debug enabled
It's the quicker way I know.
Otherwise, I made an override of the default a .htaccess adapted for the dev environment.
It rewrite URLs to the app_dev.php front-controller rather than app.php.
Update
You need to add the following in your apache configuration :
<Directory "path/to/your/project">
DirectoryIndex app_dev.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app_dev.php [QSA,L]
</IfModule>
</Directory>
You should create a vhost to make this configuration specific to your project, not your whole localhost.

.htaccess authentication with nginx

I've some issues with my configuration. I'm using Nginx as a reversed proxy for my Apache server. My website has .htaccess authentication but Nginx doesn't seem to understand it so I'm getting a 404 error.
I saw some translators on other answers but it doesn't translate this part.
AuthUserFile /var/www/mywebsite/private/.htpassword
AuthName "Acces Restreint"
AuthType Basic
Require valid-user
By the way, do you have any idea about how to translate this second part ?
php_value session.gc_maxlifetime 604800
SetEnv APPLICATION_ENV development
Thanks everyone for your help !
for perfomance related issues, nginx by default does not look up per-directory .htaccess files, which is a problem if your hosting does not let you edit webserver config.
if you can edit webserver config (or put new config into conf.d dir)
just add these lines, replacing /your/path correspondingly
location /your/path {
auth_basic "Acces Restreint";
auth_basic_user_file /var/www/mywebsite/private/.htpassword;
}
answering second part of your question needs more info on how you use PHP with nginx.
if you are tied to FastCGI (e.g. php-fpm), then you can use this method to set PHP' vars:
fastcgi_param PHP_VALUE "session.gc_maxlifetime=604800";
env APPLICATION_ENV development;

Categories