我的系统:
我有很多我想添加到Apache的环境variables。
我目前正在通过添加到我的/etc/httpd/conf.d/vhosts.conf文件,如下所示:
SetEnv API_USERNAME 'my_special_username' SetEnv API_PASSWORD 'my_special_password'
我可以通过以下方式访问PHP中的两个环境variables:
echo $_SERVER['API_USERNAME']; echo $_SERVER['API_PASSWORD']; // or echo getenv('API_USERNAME'); echo getenv('API_PASSWORD');
最近 ,我发现我也可以通过systemd添加环境variables:
我创build了文件/etc/systemd/systemd/httpd.service.d/envvars.conf :
[Service] Environment="API_USERNAME=my_special_username" Environment="API_PASSWORD=my_special_password"
systemctl daemon-reload
systemctl restart httpd
我可以通过以下方式访问PHP中的两个环境variables:
echo getenv('API_USERNAME'); echo getenv('API_PASSWORD'); // $_SERVER does not work when I specify environment variables in systemd echo $_SERVER['API_USERNAME']; // returns blank echo $_SERVER['API_PASSWORD']; // returns blank
题
在安全性或最佳实践方面,最好是使用SetEnv或systemd将我的Apache环境variables添加到configuration文件中 ? 还是不重要?
我同意Tvon的说法,并且希望更具体地添加systemd实际上是放置这些Apache环境variables的错误地方。 其他使用Systemd的应用程序或进程不需要知道这些variables。
我也会考虑你是否会在同一个服务器上托pipe多个PHP站点,如果是的话,而不是坚持一切
/etc/httpd/conf.d/vhosts.conf
我也会考虑使用站点可用的方法,也就是“debian-way”
如何configurationApache(站点可用vs httpd.conf)
下面的方法基于CentOS的这个指南https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-centos-7#step-four-%E2%80 %94创build新的虚拟主机,文件
sudo mkdir /etc/httpd/sites-available sudo mkdir /etc/httpd/sites-enabled
告诉Apache在启用站点的目录中查找虚拟主机。 通过编辑Apache的主configuration文件并添加一行声明一个可选的目录来获取额外的configuration文件:
sudo nano /etc/httpd/conf/httpd.conf
将一行添加到文件末尾IncludeOptional sites-enabled / *。conf
保存并closures文件。 然后创build一个虚拟主机文件。
sudo nano /etc/httpd/sites-available/example.com.conf <VirtualHost *:80> </VirtualHost>
然后添加你的第一个网站的直播
<VirtualHost *:80> ServerName www.example.com ServerAlias example.com SetEnv API_USERNAME 'my_special_username' SetEnv API_PASSWORD 'my_special_password' DocumentRoot /var/www/example.com/public_html ErrorLog /var/www/example.com/error.log CustomLog /var/www/example.com/requests.log combined </VirtualHost>
保存并closures文件。 然后为第二个网站制作一个名为example2.com.conf的副本
sudo cp /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-available/example2.com.conf
编辑文件并进行相关更改
sudo nano /etc/httpd/sites-available/example2.com.conf <VirtualHost *:80> ServerName www.example2.com ServerAlias example2.com SetEnv API_USERNAME 'my_other_special_username' SetEnv API_PASSWORD 'my_other_special_password' DocumentRoot /var/www/example2.com/public_html ErrorLog /var/www/example2.com/error.log CustomLog /var/www/example2.com/requests.log combined </VirtualHost>
然后,您需要通过创build从启用网站的目录到网站可用目录的符号链接来启用网站
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf sudo ln -s /etc/httpd/sites-available/example2.com.conf /etc/httpd/sites-enabled/example2.com.conf
然后重新启动Apache(通常我首先做一个configtest,看看是否有任何错误)
sudo apachectl configtest
如果一切还好
sudo apachectl restart
这种方法的其他好处是你可以创build现有configuration的新版本,并使用符号链接来指向新的configuration或启用/禁用网站的任何问题,如有必要。