<!DOCTYPE html>
<html version="XHTML+RDFa 1.0" dir="ltr" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/terms/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:og="http://ogp.me/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:sioct="http://rdfs.org/sioc/types#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" lang="en">
<head profile="http://www.w3.org/1999/xhtml/vocab">
  
  
  <link rel="shortcut icon" href="/sites/default/files/bluemarine_favicon.ico" type="image/vnd.microsoft.icon">
  
  <link rel="shortlink" href="/node/5">
  <link rel="canonical" href="/Reload-httpd-via-PHP">
  <title>Reload httpd via PHP (sorta) | Adam Young</title>
  <style type="text/css" media="all">
  @import url("/modules/system/system.base.css?lmiz4s");
  @import url("/modules/system/system.menus.css?lmiz4s");
  @import url("/modules/system/system.messages.css?lmiz4s");
  @import url("/modules/system/system.theme.css?lmiz4s");
  </style>
  <style type="text/css" media="all">
  @import url("/modules/comment/comment.css?lmiz4s");
  @import url("/modules/field/theme/field.css?lmiz4s");
  @import url("/modules/node/node.css?lmiz4s");
  @import url("/modules/poll/poll.css?lmiz4s");
  @import url("/modules/search/search.css?lmiz4s");
  @import url("/modules/user/user.css?lmiz4s");
  </style>
  <style type="text/css" media="all">
  @import url("/themes/bartik/css/layout.css?lmiz4s");
  @import url("/themes/bartik/css/style.css?lmiz4s");
  @import url("/themes/bartik/css/colors.css?lmiz4s");
  </style>
  <style type="text/css" media="print">
  @import url("/themes/bartik/css/print.css?lmiz4s");
  </style>
  
  
  
  
  
</head>
<body class="html not-front not-logged-in one-sidebar sidebar-first page-node page-node- page-node-5 node-type-blog">
  <div id="skip-link">
    <a href="#main-content" class="element-invisible element-focusable">Skip to main content</a>
  </div>
  <div id="page-wrapper">
    <div id="page">
      <div id="header" class="without-secondary-menu">
        <div class="section clearfix">
          <a href="/" title="Home" rel="home" id="logo"><img src="/sites/default/files/bluemarine_logo.gif" alt="Home"></a>
          <div id="name-and-slogan">
            <div id="site-name">
              <strong><a href="/" title="Home" rel="home"><span>Adam Young</span></a></strong>
            </div>
          </div>
          <div class="region region-header">
            <div id="block-search-form" class="block block-search">
              <div class="content">
                
              </div>
            </div>
          </div>
          <div id="main-menu" class="navigation">
            <h2 class="element-invisible">Main menu</h2>
            <ul id="main-menu-links" class="links clearfix">
              <li class="menu-198 first last">
                <a href="/">Home</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
      <div id="main-wrapper" class="clearfix">
        <div id="main" class="clearfix">
          <div id="breadcrumb">
            <h2 class="element-invisible">You are here</h2>
            <div class="breadcrumb">
              <a href="/">Home</a> » <a href="/blog">Blogs</a> » <a href="/blog/1">adam's blog</a>
            </div>
          </div>
          <div id="sidebar-first" class="column sidebar">
            <div class="section">
              <div class="region region-sidebar-first">
                <div id="block-adsense-managed-0" class="block block-adsense-managed">
                  <div class="content">
                    <div style="text-align:center; display: block;">
                      <div class="adsense adsense_managed">
                        
                      </div>
                    </div>
                  </div>
                </div>
                <div id="block-user-login" class="block block-user">
                  <h2>User login</h2>
                  <div class="content">
                    
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div id="content" class="column">
            <div class="section">
              <a id="main-content"></a>
              <h1 class="title" id="page-title">Reload httpd via PHP (sorta)</h1>
              <div class="tabs"></div>
              <div class="region region-content">
                <div id="block-system-main" class="block block-system">
                  <div class="content">
                    <div id="node-5" class="node node-blog node-promoted node-full clearfix" about="/Reload-httpd-via-PHP" typeof="sioc:Post sioct:BlogPost">
                      <div class="meta submitted">
                        <span property="dc:date dc:created" content="2006-10-27T06:27:30-04:00" datatype="xsd:dateTime" rel="sioc:has_creator">Written by <span class="username" about="/user/1" typeof="sioc:UserAccount" property="foaf:name" lang="">adam</span> on Fri, 10/27/2006 - 06:27</span>
                      </div>
                      <div class="content clearfix">
                        <div class="field field-name-body field-type-text-with-summary field-label-hidden">
                          <div class="field-items">
                            <div class="field-item even" property="content:encoded">
                              <p><strong>The problem:</strong><br>
                              Apache forked processes (PHP, CGI, etc) runs as a different user (usually www or some variant) which doesn't have permission to restart Apache. To further compound the problem, PHP doesn't allow you to sudo to a user that does have the privileges (they call this a "security feature"). Also, if it did have the privileges, it cannot reload httpd directly because that's it's parent process.</p>
                              <p><strong>The Fix:</strong><br>
                              Have PHP invoke a binary that does have the ability to sudo. From there, put a reload request into the atq (this assumes you have atd running) to avoid the whole parent process problem.</p>
                              <p><strong>The code:</strong></p>
                              <pre>
/***********************************************************
 * make sure to chmod +s the binary after compiling:
 * cc -o httpd_reload httpd_reload.c ; chmod +s httpd_reload
 ***********************************************************/

#include <stdio.h>
#include <stdlib.h>

int main() {
  if (!setuid(geteuid())) {
    system("/bin/echo '/sbin/service httpd reload > /dev/null 2>&1' | /usr/bin/at now");
  } else {
    printf("Couldn't set UID to effective UID\n");
    return 1;
  }
  return 0;
}
</pre>
                              <p><strong>PHP code:</strong><br>
                              You can use other scripting languages here...</p>
                              <pre>
exec('/path/to/httpd_reload');
</pre>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="link-wrapper">
                        <ul class="links inline">
                          <li class="blog_usernames_blog first">
                            <a href="/blog/1" title="Read adam's latest blog entries.">adam's blog</a>
                          </li>
                          <li class="comment_forbidden last"><span> or  to post comments</span></li>
                        </ul>
                      </div>
                      <div id="comments" class="comment-wrapper">
                        <h2 class="title">Comments</h2>
<a id="comment-651"></a>
                        <div class="comment clearfix" about="/comment/651#comment-651" typeof="sioc:Post sioct:Comment">
                          <div class="attribution">
                            <div class="submitted">
                              <p class="commenter-name"><span rel="sioc:has_creator"><span class="username" typeof="sioc:UserAccount" property="foaf:name" lang="">Anonymous (not verified)</span></span></p>
                              <p class="comment-time"><span property="dc:date dc:created" content="2009-04-04T11:29:24-04:00" datatype="xsd:dateTime">Sat, 04/04/2009 - 11:29</span></p>
                              <p class="comment-permalink"><a href="/comment/651#comment-651" class="permalink" rel="bookmark">Permalink</a></p>
                            </div>
                          </div>
                          <div class="comment-text">
                            <div class="comment-arrow"></div>
                            <h3 property="dc:title" datatype=""><a href="/comment/651#comment-651" class="permalink" rel="bookmark">I still can't get this damned thing to work</a></h3>
                            <div class="content">
                              <span rel="sioc:reply_of" resource="/Reload-httpd-via-PHP" class="rdf-meta"></span>
                              <div class="field field-name-comment-body field-type-text-long field-label-hidden">
                                <div class="field-items">
                                  <div class="field-item even" property="content:encoded">
                                    <p>Thanks for the post. I build the file and it works at restarting apache so long as I am logged in as root but when I try and run it from exec command with php - nada?!?! I am not really adept at unix yet so I fumbled around just figuring out how to compile the source to begin with. I just did gcc to compile your code? Is that right? I just took your code copied it as httpd_reload.c did a gcc to it. Created a httpd_reload file - changed it to be executable and it runs and gives me this output...</p>
                                    <p>job 5 at 2009-04-04 13:09</p>
                                    <p>not sure what this is?</p>
                                    <p>Anyhow - I am stuck... any advice would be appreciated.</p>
                                  </div>
                                </div>
                              </div>
                            </div>
                            <ul class="links inline">
                              <li class="comment_forbidden first last"><span> or  to post comments</span></li>
                            </ul>
                          </div>
                        </div>
                        <div class="indented">
                          <a id="comment-2699"></a>
                          <div class="comment clearfix" about="/comment/2699#comment-2699" typeof="sioc:Post sioct:Comment">
                            <div class="attribution">
                              <div class="submitted">
                                <p class="commenter-name"><span rel="sioc:has_creator"><span class="username" typeof="sioc:UserAccount" property="foaf:name" lang="">Anonymous (not verified)</span></span></p>
                                <p class="comment-time"><span property="dc:date dc:created" content="2011-04-19T03:15:00-04:00" datatype="xsd:dateTime">Tue, 04/19/2011 - 03:15</span></p>
                                <p class="comment-permalink"><a href="/comment/2699#comment-2699" class="permalink" rel="bookmark">Permalink</a></p>
                              </div>
                            </div>
                            <div class="comment-text">
                              <div class="comment-arrow"></div>
                              <h3 property="dc:title" datatype=""><a href="/comment/2699#comment-2699" class="permalink" rel="bookmark">re: I still can't get this damned thing to work</a></h3>
                              <div class="content">
                                <span rel="sioc:reply_of" resource="/Reload-httpd-via-PHP" class="rdf-meta"></span><span rel="sioc:reply_of" resource="/comment/651#comment-651" class="rdf-meta"></span>
                                <div class="field field-name-comment-body field-type-text-long field-label-hidden">
                                  <div class="field-items">
                                    <div class="field-item even" property="content:encoded">
                                      <p>Did you use the compile commands from the source code?<br>
                                      cc -o httpd_reload httpd_reload.c</p>
                                      <p>Is the at deamon running?<br>
                                      /etc/init.d/atd start</p>
                                      <p>Did you chmod the binary?<br>
                                      chmod +s httpd_reload</p>
                                      <p>The binary returns the same output for me and it is reloading httpd.</p>
                                    </div>
                                  </div>
                                </div>
                              </div>
                              <ul class="links inline">
                                <li class="comment_forbidden first last"><span> or  to post comments</span></li>
                              </ul>
                            </div>
                          </div>
                        </div>
<a id="comment-2465"></a>
                        <div class="comment clearfix" about="/comment/2465#comment-2465" typeof="sioc:Post sioct:Comment">
                          <div class="attribution">
                            <div class="submitted">
                              <p class="commenter-name"><span rel="sioc:has_creator"><span class="username" typeof="sioc:UserAccount" property="foaf:name" lang="">Anonymous (not verified)</span></span></p>
                              <p class="comment-time"><span property="dc:date dc:created" content="2011-01-21T14:49:18-05:00" datatype="xsd:dateTime">Fri, 01/21/2011 - 14:49</span></p>
                              <p class="comment-permalink"><a href="/comment/2465#comment-2465" class="permalink" rel="bookmark">Permalink</a></p>
                            </div>
                          </div>
                          <div class="comment-text">
                            <div class="comment-arrow"></div>
                            <h3 property="dc:title" datatype=""><a href="/comment/2465#comment-2465" class="permalink" rel="bookmark">Hello There</a></h3>
                            <div class="content">
                              <span rel="sioc:reply_of" resource="/Reload-httpd-via-PHP" class="rdf-meta"></span>
                              <div class="field field-name-comment-body field-type-text-long field-label-hidden">
                                <div class="field-items">
                                  <div class="field-item even" property="content:encoded">
                                    <p>Saat ini  menjadi perhatian tidak hanya keluarga indonesia, melainkan segmen di Indonesia. <a href="<a href="http://idnyul.com/newport-beach-houses/%22>Newport" rel="nofollow">http://idnyul.com/newport-beach-houses/">Newport</a> Beach Houses</a> is a charming coastal community in Orange County, California. It's also well known as the wealthiest city in the United States.</p>
                                  </div>
                                </div>
                              </div>
                            </div>
                            <ul class="links inline">
                              <li class="comment_forbidden first last"><span> or  to post comments</span></li>
                            </ul>
                          </div>
                        </div>
<a id="comment-2749"></a>
                        <div class="comment clearfix" about="/comment/2749#comment-2749" typeof="sioc:Post sioct:Comment">
                          <div class="attribution">
                            <div class="submitted">
                              <p class="commenter-name"><span rel="sioc:has_creator"><span class="username" typeof="sioc:UserAccount" property="foaf:name" lang="">Anonymous (not verified)</span></span></p>
                              <p class="comment-time"><span property="dc:date dc:created" content="2011-05-17T02:34:30-04:00" datatype="xsd:dateTime">Tue, 05/17/2011 - 02:34</span></p>
                              <p class="comment-permalink"><a href="/comment/2749#comment-2749" class="permalink" rel="bookmark">Permalink</a></p>
                            </div>
                          </div>
                          <div class="comment-text">
                            <div class="comment-arrow"></div>
                            <h3 property="dc:title" datatype=""><a href="/comment/2749#comment-2749" class="permalink" rel="bookmark">Thanks for the post.</a></h3>
                            <div class="content">
                              <span rel="sioc:reply_of" resource="/Reload-httpd-via-PHP" class="rdf-meta"></span>
                              <div class="field field-name-comment-body field-type-text-long field-label-hidden">
                                <div class="field-items">
                                  <div class="field-item even" property="content:encoded">
                                    <p>You really are an expert in your field. This is very helpful for web developers like me. Thanks a lot! </p>
                                  </div>
                                </div>
                              </div>
                            </div>
                            <ul class="links inline">
                              <li class="comment_forbidden first last"><span> or  to post comments</span></li>
                            </ul>
                          </div>
                        </div>
<a id="comment-2762"></a>
                        <div class="comment clearfix" about="/comment/2762#comment-2762" typeof="sioc:Post sioct:Comment">
                          <div class="attribution">
                            <div class="submitted">
                              <p class="commenter-name"><span rel="sioc:has_creator"><span class="username" typeof="sioc:UserAccount" property="foaf:name" lang="">Anonymous (not verified)</span></span></p>
                              <p class="comment-time"><span property="dc:date dc:created" content="2011-05-24T08:14:41-04:00" datatype="xsd:dateTime">Tue, 05/24/2011 - 08:14</span></p>
                              <p class="comment-permalink"><a href="/comment/2762#comment-2762" class="permalink" rel="bookmark">Permalink</a></p>
                            </div>
                          </div>
                          <div class="comment-text">
                            <div class="comment-arrow"></div>
                            <h3 property="dc:title" datatype=""><a href="/comment/2762#comment-2762" class="permalink" rel="bookmark">Interesting Code</a></h3>
                            <div class="content">
                              <span rel="sioc:reply_of" resource="/Reload-httpd-via-PHP" class="rdf-meta"></span>
                              <div class="field field-name-comment-body field-type-text-long field-label-hidden">
                                <div class="field-items">
                                  <div class="field-item even" property="content:encoded">
                                    <p>You have a very interesting post that is worth sharing. Thanks! </p>
                                  </div>
                                </div>
                              </div>
                            </div>
                            <ul class="links inline">
                              <li class="comment_forbidden first last"><span> or  to post comments</span></li>
                            </ul>
                          </div>
                        </div>
<a id="comment-2767"></a>
                        <div class="comment clearfix" about="/comment/2767#comment-2767" typeof="sioc:Post sioct:Comment">
                          <div class="attribution">
                            <div class="submitted">
                              <p class="commenter-name"><span rel="sioc:has_creator"><span class="username" typeof="sioc:UserAccount" property="foaf:name" lang="">Anonymous (not verified)</span></span></p>
                              <p class="comment-time"><span property="dc:date dc:created" content="2011-05-25T04:44:50-04:00" datatype="xsd:dateTime">Wed, 05/25/2011 - 04:44</span></p>
                              <p class="comment-permalink"><a href="/comment/2767#comment-2767" class="permalink" rel="bookmark">Permalink</a></p>
                            </div>
                          </div>
                          <div class="comment-text">
                            <div class="comment-arrow"></div>
                            <h3 property="dc:title" datatype=""><a href="/comment/2767#comment-2767" class="permalink" rel="bookmark">Very interesting post worth</a></h3>
                            <div class="content">
                              <span rel="sioc:reply_of" resource="/Reload-httpd-via-PHP" class="rdf-meta"></span>
                              <div class="field field-name-comment-body field-type-text-long field-label-hidden">
                                <div class="field-items">
                                  <div class="field-item even" property="content:encoded">
                                    <p>Very interesting post worth mentioning. Thanks for sharing this code. </p>
                                  </div>
                                </div>
                              </div>
                            </div>
                            <ul class="links inline">
                              <li class="comment_forbidden first last"><span> or  to post comments</span></li>
                            </ul>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div id="footer-wrapper">
        <div class="section"></div>
      </div>
    </div>
  </div>
</body>
</html>
