<p><strong>E.g.</strong> Is it more secure to use <code>mod_php</code> instead of <code>php-cgi</code>?
Or is it more secure to use <code>mod_perl</code> instead of traditional <code>cgi-scripts</code>?</p>

<p>I'm mainly interested in security concerns, but speed might be an issue if there are significant differences.</p>

[{'answer_id': 78131, 'author': 'Leon Timmermans', 'author_id': 4727, 'author_profile': 'https://Stackoverflow.com/users/4727', 'pm_score': 2, 'selected': False, 'text': "<p>Using a builtin module is definitely going to be faster than using CGI. The security implications depend on the configuration. In the default configuration they are pretty much the same, but cgi allows some more secure configurations that builtin modules can't provide, specially in the context of shared hosting. What exactly do you want to secure yourself against?</p>\n"}, {'answer_id': 78138, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 3, 'selected': False, 'text': "<p>Most security holes occur due to lousy programming in the script itself, so it's really kind of moot if they are ran as cgi or in modules. That said, apache modules can potentially crash the whole webserver (especially if using a threaded MPM) and mod_php is kind of famous for it.</p>\n\n<p>cgi will be slower, but nowadays there are solutions to that, mainly FastCGI and friends.</p>\n\n<p>What is your threat model?</p>\n"}, {'answer_id': 78188, 'author': 'X-Istence', 'author_id': 13986, 'author_profile': 'https://Stackoverflow.com/users/13986', 'pm_score': 5, 'selected': True, 'text': "<p>Security in what sense? Either way it really depends on what script is running and how well it is written. Too many scripts these days are half-assed and do not properly do input validation.</p>\n\n<p>I personally prefer FastCGI to mod_php since if a FastCGI process dies a new one will get spawned, whereas I have seen mod_php kill the entirety of Apache.</p>\n\n<p>As for security, with FastCGI you could technically run the php process under a different user from the default web servers user.</p>\n\n<p>On a seperate note, if you are using Apache's new worker threading support you will want to make sure that you are not using mod_php as some of the extensions are not thread safe and will cause race conditions.</p>\n"}, {'answer_id': 78271, 'author': 'Scott Swezey', 'author_id': 9439, 'author_profile': 'https://Stackoverflow.com/users/9439', 'pm_score': 2, 'selected': False, 'text': '<p><strong>From the PHP install.txt doc for PHP 5.2.6:</strong></p>\n\n<p>Server modules provide significantly better performance and additional\n   functionality compared to the CGI binary.\n<br /><br /></p>\n\n<p><strong>For IIS/PWS:</strong></p>\n\n<p>Warning</p>\n\n<p>By using the CGI setup, your server is open to several possible\n   attacks. Please read our CGI security section to learn how to defend\n   yourself from those attacks.</p>\n'}, {'answer_id': 78291, 'author': 'djn', 'author_id': 9673, 'author_profile': 'https://Stackoverflow.com/users/9673', 'pm_score': 3, 'selected': False, 'text': "<p>If you run your own server go the module way, it's somewhat faster.\nIf you're on a shared server the decision has already been taken for you, usually on the CGI side. The reason for this are filesystem permissions. PHP as a module runs with the permissions of the http server (usually 'apache') and unless you can chmod your scripts to that user you have to chmod them to 777 - world readable. This means, alas, that your server neighbour can take a look at them - think of where you store the database access password. Most shared servers have solved this using stuff like phpsuexec and such, which run scripts with the permissions of the script owner, so you can (must) have your code chmoded to 644. Phpsuexec runs only with PHP as CGI - that's more or less all, it's just a local machine thing - makes no difference to the world at large.</p>\n"}, {'answer_id': 79279, 'author': 'DGM', 'author_id': 14253, 'author_profile': 'https://Stackoverflow.com/users/14253', 'pm_score': 2, 'selected': False, 'text': "<p>A module such as mod_php or FastCGI is incredibly faster than plain CGI.. just don't do CGI.    As others have said, the PHP program itself is the greatest security threat, but ignoring that there is one other consideration,  on shared hosts.  </p>\n\n<p>If your script is on a shared host with other php programs and the host is not running in safe mode, then it is likely that all server processes are running as the same user. This could mean that any other php script can read your own, including database passwords.  So be sure to investigate the server configuration to be sure your code is not readable to others.</p>\n\n<p>Even if you control your own hosting, keep in mind that another hacked web application on the server could be a conduit into others.</p>\n"}]
['https://Stackoverflow.com/questions/78108', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1994377/']