Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http-vhosts.conf virtual host environment variables not working

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??

like image 355
David Avatar asked Sep 18 '25 12:09

David


1 Answers

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 [email protected]
  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.

like image 96
David Avatar answered Sep 21 '25 05:09

David