3

I am running Apache 2.2.26 with PHP 5.4.24 on Mac OS X 10.9.4. I have several virtual hosts running on this system, and all of them run successfully. I'm trying to add environment variables to one of the virtual hosts, and I don't want them to be in .htaccess so my only option is adding them to the in httpd-vhosts.conf.

The problem is that the environment variables don't appear after I install them in the httpd-vhosts.conf file (and, of course, after I restart Apache "sudo apachectl restart"). If, however, I add them to .htaccess (just for comparison purposes), they appear just fine. What am I missing?

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName mysite.local
  DocumentRoot "/path/to/mysite.com"
  SetEnv siteid 1234
  ErrorLog "/var/log/apache2/mysite.local-error_log"
</VirtualHost>

I've also double-checked my php.ini file to be sure there's nothing prohibitive there, and the only relevant line looks good:

variables_order = "GPCSE"

Again, just to be clear, my virtual hosts RUN FINE. It's just that the environment variables don't appear when called in my PHP code. I've tried each of these three commands:

echo $_SERVER["siteid"];
echo getenv("siteid");
echo apache_getenv("siteid");

What can I be doing wrong??

David
  • 446
  • 1
  • 7
  • 18
  • 1
    If you're going to downvote this question, at least tell me why. I've outlined my problem very succinctly, with detailed platform and versioning info. How dare you. – David Nov 06 '14 at 04:52
  • I agree, have an upvote. Any reason why you've changed from the default `variables_order` (EGPCS)? FYI, this works fine on my server. – Phil Nov 06 '14 at 04:58
  • Thanks @Phil. It was previously set as GPCS so I just added E to the end, without giving it any thought. I see the "Default Value: 'EGPCS'" instruction line now, and I updated accordingly. Unfortunately, I restarted Apache and it didn't impact my problem above. – David Nov 06 '14 at 05:02
  • Are you checking a script in the right virtual-host? ie, are you going to `http://mysite.local/path/to/script.php`? – Phil Nov 06 '14 at 05:05
  • @Phil Yes, I've triple-verified it, and I have this virtual-host listed first in my httpd-vhosts.conf file just to be sure there's nothing else interfering. – David Nov 06 '14 at 05:08
  • 1
    Try making a noticeable change (like change the `DocumentRoot`) and make sure that it applies when you restart / reload Apache – Phil Nov 06 '14 at 05:12
  • Well, my question may have now become different, because you were dead on - the test changes didn't apply. So I investigated, and found that the DocRoot and other settings are handled by OSX Server application. If you post your suggestion as an answer, I'll mark it, because I should re-post my question as an OSX Server application question - how to add environment variables using OSX Server. – David Nov 06 '14 at 05:26
  • This is why I use Vagrant for PHP dev environments on OS X. Could it be that you have simply failed to set the [`NameVirtualHost`](http://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost) directive? – Phil Nov 06 '14 at 05:28
  • Is the main configuration file set to include `httpd-vhosts.conf`? See http://coolestguidesontheplanet.com/set-virtual-hosts-apache-mac-osx-10-9-mavericks-osx-10-8-mountain-lion/ – Phil Nov 06 '14 at 05:34
  • Yes, thanks for the link. My httpd.conf file does indeed include my httpd-vhosts.conf correctly. I guess that's what has spoofed me this whole time thinking my vhosts file was in charge, when it seems now that OSX Server has been... – David Nov 06 '14 at 05:45
  • @Phil, please note my answer below, as a wrap-up to our discussion above. THANK YOU again for pointing me in the right discussion with your questions, and even though it still took me a couple more hours of investigation to figure this out, your tip was a massive clue for me. – David Nov 06 '14 at 07:14

1 Answers1

5

This has been an excruciating quest for me, but I have found a very simple yet obscure answer. First of all, let me clarify my earlier question, that what I didn't realize is my Mac runs OS X Server, which has effectively altered the way Apache runs on my system.

With that said, below is the equivalent httpd-vhosts.conf file PER VIRTUAL HOST installed on my system via OS X Server application.

/Library/Server/Web/Config/apache2/sites/0000_any_80_mylocalsite.conf

Clearly, "80" is the port for regular http requests and "mylocalsite" is the name of the local virtual host that I created (within the OS X Server application). Now that this file has been produced by OS X Server, I can now edit it as follows:

sudo vi /Library/Server/Web/Config/apache2/sites/0000_any_80_mylocalsite.conf

Which opened a config file looking similar and almost exactly like a normal httpd-vhosts.conf file (note the SetEnv testid "1234" line which is the solution I have been searching for, and this can only be done manually and not through the OS X Server application)...

<VirtualHost *:80>
  ServerName mysite.local
  ServerAdmin admin@example.com
  DocumentRoot "/my/local/docroot/path/to/mysite.com"
  SetEnv testid "1234"
  DirectoryIndex index.html index.php /wiki/ /xcode/ default.html
  CustomLog /var/log/apache2/access_log combinedvhost
  ErrorLog /var/log/apache2/error_log
  <IfModule mod_ssl.c>
      SSLEngine Off
      SSLCipherSuite "ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM"
      SSLProtocol -ALL +SSLv3 +TLSv1
      SSLProxyEngine On
      SSLProxyProtocol -ALL +SSLv3 +TLSv1
  </IfModule>
  <Directory "/my/local/docroot/path/to/mysite.com">
      Options All -Indexes -ExecCGI -Includes +MultiViews
      AllowOverride All
      <IfModule mod_dav.c>
          DAV Off
      </IfModule>
      <IfDefine !WEBSERVICE_ON>
          Deny from all
          ErrorDocument 403 /customerror/websitesoff403.html
      </IfDefine>
  </Directory>
</VirtualHost>

PLEASE, I hope this helps someone else someday. I have spent far too much time searching for this solution, only to stumble on it in brute-force tactics, and I would like to know this hopefully saves someone hours of searching themselves.

David
  • 446
  • 1
  • 7
  • 18