设为首页 收藏本站
查看: 688|回复: 0

[经验分享] PHP run as CGI or Apache Module-IT博客

[复制链接]

尚未签到

发表于 2018-11-26 07:15:41 | 显示全部楼层 |阅读模式
  1. There are two ways to configure Apache to use PHP:

  •   Configure Apache to load the PHP interpreter as an Apache module
  •   Configure Apache to run the PHP interpreter as a CGI binary
  (PS: Windows IIS normaly configures as CGI by the way)
  2. What is the difference between CGI and apache Module Mode?
  An Apache module is compiled into the Apache binary, so the  PHP interpreter runs in the Apache process, meaning that when Apache  spawns a child, each process already contains a binary image of PHP. A  CGI is executed as a single process for each request, and must make an  exec() or fork() call to the PHP executable, meaning that each request  will create a new process of the PHP interpreter. Apache is much more  efficient in it's ability to handle requests, and managing resources,  making the Apache module slightly faster than the CGI (as well as more  stable under load).
  CGI Mode on the other hand, is more secure because the server  now manages and controls access to the binaries. PHP can now run as your  own user rather than the generic Apache user. This means you can put  your database passwords in a file readable only by you and your php  scripts can still access it! The "Group" and "Other" permissions (refer Where can you learn more about file permissions?)  can now be more restrictive. CGI mode is also claimed to be more  flexible in many respects as you should now not see, with phpSuExec  (refer Permissions under phpSuExec)  issues with file ownership being taken over by the Apache user,  therefore you should no longer have problems under FTP when trying to  access or modify files that have been uploaded through a PHP interface,  such as Joomla! upload options.
  If your server is configured to run PHP as an Apache module, then you  will have the choice of using either php.ini or Apache .htaccess files,  however, if your server runs PHP in CGI mode then you will only have  the choice of using php.ini files locally to change settings, as Apache  is no longer in complete control of PHP.
  3. Running PHP as an Apache module
  To configure Apache to load PHP as a module to parse your PHP  scripts, the httpd.conf needs to be modified, typically found in  "c:\Program Files\Apache Group\Apache\conf\" or "/etc/httpd/conf/".
  Search for the section of the file that has a series of commented out  "LoadModule" statements. (Statements prefixed by the hash "#" sign are  regarded as having been commented out.) If PHP is running in "Apache  Module" Mode you should see something very similar to the following;
LoadModule php4_module "c:/php/php4apache.dll"
Apache 2.x
For PHP5
LoadModule php5_module C:/php/php5apache2.dll   or (platform dependent)
LoadModule php5_module /usr/lib/apache/libphp5.so   and
  AddModule mod_php5.c
  4. Running PHP as a CGI binary
  To configure PHP to run as a CGI, again you will need to configure  the httpd.conf, but confirm that the above settings are not also  configured, unless you know what you are doing you can generate yourself  "HTTP 500" errors. Search your Apache configuration file for the  "ScriptAlias" section.
Add the following line below after the ScriptAlias for "cgi-bin". Note:  The location will depend on where PHP is installed on your system, you  should substitute the appropriate path in place of "c:/php/" (for  example, "c:/Program Files/php/"). ScriptAlias /php/ "c:/php/" Apache again needs to be configured for the PHP MIME type. Search for  the "AddType" section, and add the following line after it: AddType application/x-httpd-php .php As in the case of running PHP as an Apache module, you can add  whatever extensions you want Apache to recognise as PHP scripts, such  as: AddType application/x-httpd-php .php3  AddType application/x-httpd-php .phtml
Next, you will need to tell the server to execute the PHP executable  each time it encounters a PHP script. Add the following below any  existing entries in the "Action" section. Action application/x-httpd-php "/php/php.exe" If you notice, we have used the "ScriptAlias" reference, "/php/"  portion will be recognised as the scriptAlias configured above, this is  sort a path alias which will correlate to your PHP installation path  configured previously. In other words, don't put "c:/php/php.exe" or  "c:/Program Files/php/php.exe" in that directive, put "/php/php.exe",  Apache WILL work it out if correctly configured.PHP as a CGI or a module?
  When running PHP through your web server, there are two distinct options: running it using PHP's CGI SAPI, or running it as a module for the web server. Each have their own benefits, but, overall, the module is generally preferred.
  Running PHP as a CGI means that you basically tell your web server the location of the PHP executable file, and the server runs that executable, giving it the script you called, each time you visit a page. That means each time you load a page, PHP needs to read php.ini and set its settings, it needs to load all its extensions, and then it needs to start work parsing the script - there is a lot of repeated work.

  When you run PHP as a module, PHP literally sits inside your web server - it starts only once, loads its settings and extensions only once, and can also store information across sessions. For example, PHP accelerators>  The obvious advantage of using PHP as a module is speed - you will see a big speed boost if you convert from CGI to a module. Many people, particularly Windows users, do not realise this, and carry on using the php.exe CGI SAPI, which is a shame - the module is usually three to five times faster.
  There is one key advantage to using the CGI version, though, and that is that PHP reads its settings every time you load a page. With PHP running as a module, any changes you make in the php.ini file do not kick in until you restart your web server, which makes the CGI version preferable if you are testing a lot of new settings and want to see instant responses.





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-639577-1-1.html 上篇帖子: apache的php扩展名解析漏洞 下篇帖子: Apache与Tomcat整合(apache+tomcat+mod_jk)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表