htaccess SetEnv directive is not working in MAMP pro 6 - php

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()

Related

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

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.

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.

PHP Symfony apps on OSX El Capitan apache

I have installed the symfony_demo application on El Capitan (10.11.2)
If I run the inbuilt PHP server as follows:
php app/console server:run 127.0.0.1:8111
the demo application is Live at localhost:8111
but if I access the application via El Capitan's inbuilt apache instance (by copying it into my <user>/Sites directory), I just get a directory listing.
Note that I do have PHP enabled on apache, and other PHP-based applications run through apache as currently configured (e.g. Joomla).
What more configuration of apache do I need?
Addendum:
I am running the Server version of the OS. I read that there exists another apache configuration file at: /Library/Server/Web/Config/apache2/httpd_server_app.conf
But uncommenting the vhosts .conf and .so directives and restarting 'Websites' via the Server app in this file made no difference to the behaviour
You'll need to uncomment few lines in your /etc/apache2/httpd.conf. Remove the # in front of the following lines if it's there (these lines are in different parts of the file):
LoadModule php5_module libexec/apache2/libphp5.so
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
Include /private/etc/apache2/extra/httpd-vhosts.conf
Next, you can edit /etc/apache2/extra/httpd-vhosts.conf to define your virtual hosts. For example if your project is located in /Users/username/Sites/symfony (replace "username" with your username, and "symfony" with you project directory):
<VirtualHost *:80>
ServerName symfony.dev
DocumentRoot "/Users/username/Sites/symfony/web/"
<Directory "/Users/username/Sites/symfony/web/">
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
</VirtualHost>
In the example I used a fake domain symfony.dev. If you want to use it you'll need to add it to your /etc/hosts file (add a new line there):
127.0.0.1 symfony.dev
After changing apache's configuration don't forget to restart it:
sudo /usr/sbin/apachectl restart
You might need to adapt the vhost config to your needs. Read more about configuring the web server in the Symfony documentation.
Success!
The pivotal information here is that the Server version of OSX has it's own configuration which disables .htaccess redirection by default.
After finding the setting in Server > Websites > Server Website (edit) > Edit Advanced Settings > "Allow overrides using .htaccess files
And restarting Websites, the symfony demo works fine at http://localhost/symfony_demo/web/
n.b.: No need for vhosts - that's merely a convenience

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.

SetEnv httpd for use in PHP

I'm attempting to set an environmental variable in Apache for later use in PHP. I'm doing this by including a file in /etc/httpd/conf.d/, which holds these contents:
SetEnv ENVIRONMENT "dev"
I've also added this line to httpd.conf file for testing:
SetEnv BATCHPATH /foo:/bar
Neither of these variables appear in PHP via $_SERVER or $_ENV, and I do not see them using getenv("ENVIRONMENT").
I'm running Centos 7, have the env_module listed as (shared) under httpd -M, but also have mod_suexec listed there as well, which I've read may be overriding any user variables.
Any help is appreciated.

Categories