SetEnv httpd for use in PHP - 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.

Related

php 7 $_ENV && getenv() empty with EGPCS set

I have the folowing settings for my server,
ec2 -> centos7
httpd 2.4
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
php 7 ->
php.ini
variables_order=EGPCS
and an ENV var set in centos with
export myTestDir=/home/myuser
[myUser#.....]$ echo $myTestDir
/home/myuser
however I cannot get any system ENV var to be available in php.
echo exec('whoami'); // returns -> myUser
$_SESSION['test1']=$_SERVER['myTestDir']; // returns -> null
$_SESSION['test2']=$_ENV['myTestDir']; // returns -> null
$_SESSION['test3']=getenv("myTestDir"); // returns -> false
Am i missing an additional step or doing something wrong with variables_order=EGPCS
PHP's environment is isolated from the user environment you're using in your shell. This is by design, because PHP requires that you explicitly import variables into its environment. It will not do that implicitly since that would likely be a huge security risk.
From the manual
These variables are imported into PHP's global namespace from the environment under which the PHP parser is running. Many are provided by the shell under which PHP is running and different systems are likely running different kinds of shells, a definitive list is impossible. Please see your shell's documentation for a list of defined environment variables.
So in order to load the environment variables into PHP you must do so at the process level that loads the PHP interpreter. For example, in Apache httpd, using mod_php, this would be done by specifying the SetEnv directives in your apache.conf or virtual host files.
For PHP-FPM this would be done in your pool config files like env[myTestDir] = "/home/myuser"

File not being found on server Apache

I'm simply trying to open a php file I have created and keep getting this.
The requested URL /~Wade/HelloWorld.php was not found on this server.
I just recently did a reinstall on my Mac. Which shouldn't have affected my .conf files. With that being said, I can still typein localhost in my URL and it returns "It Works" so I know the server is still up? I'm confused
(Note: Assuming you use a recent version of OS X)
Since a couple of versions (not sure which, but at least since Mavericks), userdir Sites are disabled in the httpd.conf shipped with OS X.
Assuming you know how to edit files owned by root, remove the # from these two lines in /etc/apache2/httpd.conf:
#LoadModule userdir_module libexec/apache2/mod_userdir.so
#Include /private/etc/apache2/extra/httpd-userdir.conf
Next, create a file in /etc/apache2/users/. Its name doesn't matter, as long as it ends in .conf.
Its contents should be:
<Directory "/Users/Wade/Sites">
Require all granted
# The next two lines are optional and may bee too lax for
# production, but I deem them good for a development environment.
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
Also, in every httpd.conf that has shipped with OS X for as long as I can remember, PHP has been disabled by default. So you'll also have to remove the # from this line in httpd.conf:
#LoadModule php5_module libexec/apache2/libphp5.so
You probably also want to set the ServerName directive, to suppress a startup warning.
There's a commented out line somewhere that reads
#ServerName www.example.com:80
Either change it (and remove the #), or just add a new one after it that reads
ServerName 127.0.0.1:80
If you want your server to be accessible from other devices in your local network, replace 127.0.0.1 with 0.0.0.0, and if you use a different post, adjust accordingly.
After that, run sudo apachectl -k restart to restart Apache and you should be good to go.

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

PHP stopped working on Apache after upgrade to OS x 10.10.1 Yosemite - Owncloud broken

Context: I'm trying to run Apache on my local Mac27" (2009) so that I can use OwnCloud as an alternative to Apple's iCloud (I just want to do local sync of my calendar like in the good-old "OS X big cat" days. For paranoid privacy reasons I don't want Cupertino to know who I know or what I'm doing - probably misplaced, but there you go). My foray into this were based on a blog post by Michael Gracie at http://michaelgracie.com/2013/11/13/getting-ios-7-calendar-and-contacts-syncing-directly-with-os-x-10-9-mavericks/ (for which many thanks are due).
When I upgraded to Yosemite a few weeks ago (remind me why I did this?) I spent an age mending my very simple setup and I was back . Yesterday I upgraded (again, remind me why I did this?) to 10.10.1 and it broke again. Everything seems like it should work, but PHP is not running.
The symptom is that, when I run OwnCloud (localhost/owncloud/index.htm), I get the OwnCloud logo and styling and then an error:
PHP module GD is not installed.
Please ask your server administrator to install the module.
This, however, is a diversion, because it's not the GD module which is the problem - I think that Apache can't see PHP at all. The reason is as follows:
1) Does PHP run on Apache?
When I go to:
http://localhost
I see the Apache "It Works!" message.
Adapting the index.html.en file in /Library/Webserver/documents/ allows me to modify that message, however, when I call PHP from within that file e.g.
<h1>It works!</h1>
<h2>This file is in /Library/Webserver/Documents</h2>
<h3>Trying to printout phpinfo()</h3>
<?php
echo phpinfo();
?>
Then I see three lines of text but no PHP info.
So PHP is not available when rendering that webpage.
2) Is PHP available at all?
Investigating this, I run the following command from the terminal:
sudo echo <?php phpinfo() ?> | php
and I get four-thousand lines of PHP info, which includes
'--with-gd'
in the configure command.
3) Has Apache loaded the PHP module?
I then run:
httpd -t -D DUMP_MODULES
Apache tells me that, among many other modules it is using:
php5_module (shared)
4) Is the httpd.conf file compatible with this:
I've been through httpd.conf in my /etc/apache2 directory and have uncommented the following lines:
LoadModule php5_module libexec/apache2/libphp5.so
LoadModule authn_core_module libexec/apache2/mod_authn_core.so
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
5) What else?
Just to complete the picture, in my /etc/apache2/users file I have configured my own user settings to:
<Directory "/Users/DJBrown/Sites/">
Options Indexes MultiViews FollowSymLinks
Require all granted
AllowOverride All
Order allow,deny
Allow from all
</Directory>
(I confess, at this point - as well as others - I'm really not quite sure what I'm doing since I've following much of this from some other answers in StackOverflow).
6) Conclusion
Apache is running with PHP is loaded and both httpd and PHP can be interrogated from the command line ... but Apache won't run PHP.
Is that possible?
What have I missed?
Any help gratefully received.
DJ
Here's a partial answer.
Changing the line in httpd.conf
AddType application/x-httpd-php .php
to
AddType application/x-httpd-php .php .htm .html
Permits apache to render in-line PHP code in HTML files.
(I got this from http://php.about.com/od/advancedphp/p/html_php.htm)
I still have an OwnCloud problem now, which I will take to their forum
Thanks for your help birdspider
DJ

PHP $_ENV vs Linux environment (Ubuntu)

In my PHP code I am trying to access my linux system environment variable that I export in /etc/profile.
When I type "printenv" in my terminal, I can see that the variable (called MEMCACHED_1) set.
But my PHP doesn't see that variable, it's neither in $_ENV array nor can I access it with getenv function.
In my php.ini file I set variables_order = "EGPCS". That added a lot of variables to $_ENV, but MEMCACHED_1 is still not there.
What php.ini exactly do you mean? There are /etc/php5/apache2/php.ini (configuration used by scripts run by Apache module) and /etc/php5/cli/php.ini (configuration used by command line scripts). The latter has variables_order = "GPCS" as default setting on Ubuntu.
If you are running your code from command line, try changing /etc/php5/cli/php.ini and setting variables_order = "EGPCS".
If you are using Apache module, you may want to access needed variables as $_SERVER instead of $_ENV, or call getenv() before as comments here suggest: http://php.net/manual/en/reserved.variables.environment.php
UPDATE
Another thing to remember: PHP processes started by Apache module are run with user www-data (or whatever your User variable in Apache configuration says). So, you should also check printenv for correct user:
sudo su -l www-data -c 'printenv'
-- this should have line USER=www-data or something similar (not root, not your login). Make sure MEMCACHED_1 is set there.

Categories