{"id":75,"date":"2007-02-27T23:47:32","date_gmt":"2007-02-27T23:47:32","guid":{"rendered":"http:\/\/www.dereckson.be\/blog\/2007\/02\/27\/apache-2-php-5-cgi-suexec\/"},"modified":"2007-02-27T23:47:32","modified_gmt":"2007-02-27T23:47:32","slug":"apache-2-php-5-cgi-suexec","status":"publish","type":"post","link":"https:\/\/www.dereckson.be\/blog\/2007\/02\/27\/apache-2-php-5-cgi-suexec\/","title":{"rendered":"Apache 2 + PHP 5 \/ CGI + SuEXEC"},"content":{"rendered":"<p>Want a very secure Apache \/ PHP setup? <\/p>\n<p><strong>SuEXEC<\/strong> allows CGI execution under user own accounts and not webserver one. So, if a security hole is exploited through a script, that&#8217;s normally &#8211; if your FreeBSD server is correctly chmoded &#8211; gives access to resources. <\/p>\n<p>The genuine SuEXEC drawback is you&#8217;ve to prefix each, as any other CGI script (remember #!\/usr\/bin\/perl ?). We&#8217;ll slightly edit the SuEXEC.c code to avoid that. <\/p>\n<p>SuEXEC will force you to chmod correctly and securely your web content: 700 the scripts, 711 the directories (755 to allow list them). If that&#8217;s sounds too paranoid or you&#8217;re tired of your users&#8217; complaints, you can ask SuEXEC to ignore permissions check (but what&#8217;s the interest of this method in this case? You should consider chroot instead.). If you&#8217;re a console guru, I&#8217;ve coded an <strong>autochmod<\/strong> script to make our life paranoid but easier \ud83d\ude09<br \/>\n<!--more-->\n<\/p>\n<p>Okay, let&#8217;s begin with the usual stuff: PHP, the libraries, MySQL \u2026 You should compile them from scratch one day, it&#8217;s a very great experience but today, but if I were you, I&#8217;d waited not have any other solution (e.g. test PHP 6 beta). So use ports (or packages or rpm or what you want) Oh, and you&#8217;ll find excellent precompiled binaries versions MySQL prepared with Intel compiler, in theory more optimized than cc or gcc compilation. Give them a try. <\/p>\n<p>Oh, don&#8217;t forget <a href=\"http:\/\/uk.php.net\/manual\/en\/configure.php\">&#8211;enable-discard-path<\/a> in PHP! <\/p>\n<p><strong>Now, Apache 2.<\/strong> Okay, we can prepare a patch for the port\/package\/what you want system but Apache is very quick and easy to compile: <\/p>\n<ul>\n<li>wget <a href=\"http:\/\/apache.multidist.com\/httpd\/httpd-2.0.59.tar.bz2\">http:\/\/apache.multidist.com\/httpd\/httpd-2.0.59.tar.bz2<\/a> \t\t<\/li>\n<li>tar x<strong>j<\/strong>f httpd-2.0.59.tar<strong>.bz2<\/strong> \t\t<\/li>\n<li>cd httpd-2.0.59 <\/li>\n<li>.\/configure &#8211;help <em>to see all the options<\/em> \t\t<\/li>\n<\/ul>\n<p>SuEXEC wants you specify at least one parameter as a proof <a href=\"http:\/\/httpd.apache.org\/docs\/2.0\/suexec.html\">you&#8217;ve read documentation<\/a>. <\/p>\n<p>Here, I wish full mod rewrite support, so I enable mod_proxy, mod_proxy_http and mod_rewrite. Speling (yes, with only one l) try to suggest URLs spelling correction instead when a 404 error occurs. <\/p>\n<ul>\n<li>.\/configure &#8211;enable-deflate &#8211;enable-mime-magic &#8211;enable-proxy &#8211;enable-proxy-http &#8211;enable-ssl &#8211;enable-http &#8211;enable-info &#8212;<strong>enable-suexec<\/strong> &#8211;enable-vhost-alias &#8211;enable-speling &#8211;enable-so &#8211;enable-rewrite &#8211;with-suexec-docroot=\/home\/wwwroot<em> \t\t\t<\/em>&#8211;with-suexec-uidmin=1000 &#8211;with-suexec-gidmin=1000 &#8211;with-suexec-logfile=\/var\/log\/httpd\/suexec.log<em> \t\t\t<\/em> \t\t<\/li>\n<li>make<em> \t\t\t<\/em> \t\t<\/li>\n<li>make install<em> \t\t\t<\/em> \t\t<\/li>\n<\/ul>\n<p>To access to our websites, we need 711 or 755 directories the entire path. I like \/home\/wwwroot\/mydomain.com\/subdomain structure (e.g. \/home\/wwwroot\/dereckson.be\/www or \/home\/wwwroot\/espace-win.org\/pastebin): <\/p>\n<ul>\n<li>chmod 711 \/home <em>(on a private server, with trusty users interacting with others, chmod 755 have a sense) <\/em> \t\t<\/li>\n<li>mkdir \/home\/wwwroot <\/li>\n<li>chmod 711 \/home\/wwwroot <\/li>\n<\/ul>\n<p><strong>Try your webserver.<\/strong> It&#8217;s important as now you&#8217;re sure the following bugs come from SuEXEC configuration error path permissions problems (httpd main or vhost error log and suexec.log are your best friends). <\/p>\n<p>When all is okay, let&#8217;s hack support\/suexec.c (in your httpd source directory): <\/p>\n<p>Find (near the end of file): <\/p>\n<p style=\"margin-left: 36pt\">execv(cmd, &amp;argv[3]); <\/p>\n<p>Replace by: <\/p>\n<ol>\n<li>\n<div style=\"background: #e0e0e0\"><span style=\"font-family:Courier New; font-size:13pt\"><span style=\"color:#b1b100\">if<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><span style=\"color:#66cc66\">(<\/span><span style=\"color:#000060\">strstr<\/span><span style=\"color:#66cc66\">(<\/span><span style=\"color:#000060\">cmd, <\/span><span style=\"color:red\">&#8220;.phps&#8221;<\/span><span style=\"color:#66cc66\">))<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><span style=\"color:#66cc66\">{<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: #e0e0e0\"><span style=\"font-family:Courier New; font-size:13pt\"><span style=\"color:#000060\">        execl<\/span><span style=\"color:#66cc66\">(<\/span><span style=\"color:red\">&#8220;\/usr\/local\/bin\/php-cgi&#8221;<\/span><span style=\"color:#000060\">, <\/span><span style=\"color:red\">&#8220;php-cgi&#8221;<\/span><span style=\"color:#000060\">, <\/span><span style=\"color:red\">&#8220;-s&#8221;<\/span><span style=\"color:#000060\">, cmd, <\/span><span style=\"color:black\"><strong>NULL<\/strong><\/span><span style=\"color:#66cc66\">)<\/span><span style=\"color:#000060\">; <\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: #e0e0e0\"><span style=\"font-family:Courier New; font-size:13pt\"><span style=\"color:#66cc66\">}<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><span style=\"color:#b1b100\">else<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><span style=\"color:#b1b100\">if<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><span style=\"color:#66cc66\">(<\/span><span style=\"color:#000060\">strstr<\/span><span style=\"color:#66cc66\">(<\/span><span style=\"color:#000060\">cmd, <\/span><span style=\"color:red\">&#8220;.php&#8221;<\/span><span style=\"color:#66cc66\">))<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><span style=\"color:#66cc66\">{<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: #e0e0e0\"><span style=\"font-family:Courier New; font-size:13pt\"><span style=\"color:#000060\">        execl<\/span><span style=\"color:#66cc66\">(<\/span><span style=\"color:red\">&#8220;\/usr\/local\/bin\/php-cgi&#8221;<\/span><span style=\"color:#000060\">, <\/span><span style=\"color:red\">&#8220;php-cgi&#8221;<\/span><span style=\"color:#000060\">, cmd, <\/span><span style=\"color:black\"><strong>NULL<\/strong><\/span><span style=\"color:#66cc66\">)<\/span><span style=\"color:#000060\">; <\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: #e0e0e0\"><span style=\"font-family:Courier New; font-size:13pt\"><span style=\"color:#66cc66\">}<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><span style=\"color:#b1b100\">else<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><span style=\"color:#66cc66\">{<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: #e0e0e0\"><span style=\"font-family:Courier New; font-size:13pt\"><span style=\"color:#000060\">        execv<\/span><span style=\"color:#66cc66\">(<\/span><span style=\"color:#000060\">cmd, &amp;argv<\/span><span style=\"color:#66cc66\">[<\/span><span style=\"color:#cc66cc\">3<\/span><span style=\"color:#66cc66\">])<\/span><span style=\"color:#000060\">; <\/span><\/span><\/div>\n<\/li>\n<li>\n<div style=\"background: #e0e0e0\"><span style=\"font-family:Courier New; font-size:13pt\"><span style=\"color:#66cc66\">}<\/span><span style=\"color:#000060\"> \t\t\t\t\t<\/span><\/span><\/div>\n<\/li>\n<\/ol>\n<p style=\"background: #f0f0ff\"><span style=\"color:gray; font-family:Verdana; font-size:9pt\"><strong>suexec.c PHP friendly hack &#8211; Dereckson <\/strong><\/span><\/p>\n<\/p>\n<p>Now, in httpd.conf you&#8217;ve to configure php and phps as CGI. <\/p>\n<p>[ This is a rough draft, drop a comment if you want any precision or the sequel ]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Want a very secure Apache \/ PHP setup? <\/p>\n<p><strong>SuEXEC<\/strong> allows CGI execution under user own accounts and not webserver one. So, if a security hole is exploited through a script, that&#8217;s normally &#8211; if your FreeBSD server is correctly chmoded &#8211; gives access to resources. <\/p>\n<p>The genuine SuEXEC drawback is you&#8217;ve to prefix each, as any other CGI script (remember #!\/usr\/bin\/perl ?). We&#8217;ll slightly edit the SuEXEC.c code to avoid that. <\/p>\n<p>SuEXEC will force you to chmod correctly and securely your web content: 700 the scripts, 711 the directories (755 to allow list them). If that&#8217;s sounds too paranoid or you&#8217;re tired of your users&#8217; complaints, you can ask SuEXEC to ignore permissions check (but what&#8217;s the interest of this method in this case? You should consider chroot instead.). If you&#8217;re a console guru, I&#8217;ve coded an <strong>autochmod<\/strong> script to make our life paranoid but easier \ud83d\ude09 <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[9],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","tag-freebsd"],"_links":{"self":[{"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":0,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dereckson.be\/blog\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}