Results 1 to 15 of 15
  1. #1
    Join Date
    Apr 2008
    Location
    India
    Posts
    227

    Question Restart httpd and mysql service through php code ?

    Hi,

    I want to create a php program to restart httpd and mysql services....

    <?php
    $output=shell_exec('/etc/init.d/httpd restart');
    echo $output;
    exit;
    ?>

    when i run this program i m getting below 3 errors :-

    /etc/init.d/httpd: line 15: ulimit: open files: cannot modify limit: Operation not permitted
    /etc/init.d/httpd: line 16: ulimit: open files: cannot modify limit: Operation not permitted
    httpd: Could not open configuration file /usr/local/apache/conf/httpd.conf: Permission denied
    ================================================
    (13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
    no listening sockets available, shutting down
    Unable to open logs
    /etc/init.d/httpd: line 13: ulimit: open files: cannot modify limit: Operation not permitted
    ================================================
    Syntax error on line 884 of /usr/local/apache/conf/httpd.conf:
    SSLCertificateKeyFile: file '/etc/ssl/private/domain.com.key' does not exist or is empty
    ================================================

    Can any one guide me how to solve this problem ?


    Thank you.
    =============
    Map007

    Linux Blog

  2. #2
    Quote Originally Posted by map007 View Post
    Hi,

    I want to create a php program to restart httpd and mysql services....

    <?php
    $output=shell_exec('/etc/init.d/httpd restart');
    echo $output;
    exit;
    ?>

    when i run this program i m getting below 3 errors :-

    /etc/init.d/httpd: line 15: ulimit: open files: cannot modify limit: Operation not permitted
    /etc/init.d/httpd: line 16: ulimit: open files: cannot modify limit: Operation not permitted
    httpd: Could not open configuration file /usr/local/apache/conf/httpd.conf: Permission denied
    ================================================
    (13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
    no listening sockets available, shutting down
    Unable to open logs
    /etc/init.d/httpd: line 13: ulimit: open files: cannot modify limit: Operation not permitted
    ================================================
    Syntax error on line 884 of /usr/local/apache/conf/httpd.conf:
    SSLCertificateKeyFile: file '/etc/ssl/private/domain.com.key' does not exist or is empty
    ================================================

    Can any one guide me how to solve this problem ?

    Thank you.
    PHP scripts run with www-data user priileges. So: you have to do sudo to root to perform this operation (read: man sudoers).

    Anpother problem may be with killing process that started your PHP script (next instructions will not be called).
    Homer knows about his website downtime, what about you?
    site-uptime.net site uptime monitor

  3. #3
    Join Date
    Apr 2009
    Location
    Islamabad , pakistan
    Posts
    128
    You can also try by granting apache the root previliges. You can do so by adding it in /etc/sudoers file.
    Aun Muhammad Raza
    Linux System Administrator
    Personal Web: http://linuxpitstop.com

  4. #4
    Join Date
    Apr 2008
    Location
    India
    Posts
    227
    Hi,

    Which user i have to add under sudoers file, Please guide me..

    I have already tried with nobody and apache users.

    Thank you.
    =============
    Map007

    Linux Blog

  5. #5
    Quote Originally Posted by map007 View Post
    Hi,

    I want to create a php program to restart httpd and mysql services....
    BTW, why do you need a PHP script for that? It can be achieved using a simple shell script as well.
    | LinuxHostingSupport.net
    | Server Setup | Security | Optimization | Troubleshooting | Server Migration
    | Monthly and Task basis services.
    | MSN : madaboutlinux[at]hotmail.com | Skype : madaboutlinux

  6. #6
    Join Date
    Apr 2003
    Location
    San Jose, CA.
    Posts
    1,624
    I'll put $2 on his "I got rooted, what do I do now" post appearing in less than 4 weeks of implementation

    In otherwords, this is a rather dumb idea.

  7. #7
    Right, the function "shell_exec" should be turned off server wide for security reasons and only use it when it is utmost needed.
    | LinuxHostingSupport.net
    | Server Setup | Security | Optimization | Troubleshooting | Server Migration
    | Monthly and Task basis services.
    | MSN : madaboutlinux[at]hotmail.com | Skype : madaboutlinux

  8. #8
    Join Date
    Apr 2008
    Location
    India
    Posts
    227
    Hi,

    Yes i know this is dumb idea. But i m trying to do this task through php for my local R & D network. I have already created shell script for that and its working perfectly.

    So if any body knows how to do through php please guide me....

    Thanks to all.... -
    =============
    Map007

    Linux Blog

  9. #9
    Join Date
    Apr 2003
    Location
    San Jose, CA.
    Posts
    1,624
    How are you trying to run the php program?

    CLI as in:
    php ./this_is_retarded.php

    or via accessing a URL:
    https://gonnagethackedsoon.com/this_is_retarded.php

  10. #10
    Join Date
    Jun 2006
    Location
    Ahh' UnderPants
    Posts
    1,013
    I believe he is trying to access via httpd, if it was on CLI, he could have just written the command without using a script.

  11. #11
    You could just run a .sh file by using shell_exec.

  12. #12
    Join Date
    Apr 2008
    Location
    India
    Posts
    227
    Hi,

    Yes Woooo you are right. I am giving you all the details.

    [root@linuxboy R&D]# php test.php
    Stopping httpd: [ OK ]
    Starting httpd: [ OK ]
    [root@linuxboy R&D]# cat test.php
    <?php
    $output=shell_exec('service httpd restart');
    echo $output;
    exit;
    ?>
    [root@linuxboy R&D]# cat php.php
    <?php
    $out= exec('/opt/test.sh');
    echo $out;
    exit;
    ?>
    [root@linuxboy R&D]# ./test.sh
    Stopping httpd: [ OK ]
    Starting httpd: [ OK ]
    [root@linuxboy R&D]# cat test.sh
    /etc/init.d/httpd restart

    Thanks to all..... -
    =============
    Map007

    Linux Blog

  13. #13
    so finally a shell script (test.sh) is restarting the httpd service
    | LinuxHostingSupport.net
    | Server Setup | Security | Optimization | Troubleshooting | Server Migration
    | Monthly and Task basis services.
    | MSN : madaboutlinux[at]hotmail.com | Skype : madaboutlinux

  14. #14
    Join Date
    Apr 2008
    Location
    India
    Posts
    227
    Hi,

    Not from web...

    http://127.0.0.1/php.php
    =======================
    <?php
    $out= exec('/opt/test.sh');
    echo $out;
    exit;
    ?>
    =======================
    <?php
    $out= shell_exec('/opt/test.sh');
    echo $out;
    exit;
    ?>
    =======================


    Thanks to all....
    =============
    Map007

    Linux Blog

  15. #15
    Join Date
    Oct 2006
    Location
    /usr/src/linux/
    Posts
    700
    OK there is rather semi-clean way of doing this, however I'm probably missing something (security wise) but nothing that's on top of my head.
    Use a binary as a wrapper to restart apache and mysql rather than directly from the init scripts such as for example this
    Code:
    int main() {
            system("/etc/init.d/mysqld restart;/etc/init.d/httpd restart");
            return 1;
    }
    You have to restart mysqld first because if you restart httpd, all apache's children will be killed along with your script which should restart mysqld as well so that will fail
    Compile the binary using gcc -o test test.c
    Then use visudo to edit /etc/sudoers, make sure to remove
    Code:
    Defaults requiretty
    From /etc/sudoers or you will get errors from php such as these
    Code:
    sudo: sorry, you must have a tty to run sudo
    Which is a good idea IMO but breaks the entire "system" in this scenario
    Check under which user apache runs, nobody, apache, www-data etc. In my case it was apache, add the following line in /etc/sudoers
    Code:
    apache  ALL=NOPASSWD: /var/www/html/test
    Where /var/www/html/test is your compiled binary which restart httpd and apache
    Then you may use a php script such as this to execute the binary and restart mysql and httpd
    Code:
    <?php passthru("/usr/bin/sudo /var/www/html/test"); ?>
    That should do the trick, if someone notices any security concerns please enlighten us.
    VPSnoc.com offers high quality Xen® OpenVZ & Windows® Virtual Private Servers at affordable prices.
    99.95% Uptime | 24/7/365 Support | Unmetered bandwidth.
    Follow us: twitter.com/VPSnoc

Similar Threads

  1. shutdown -r now OR service httpd restart? Difference?
    By kohkindachi in forum Hosting Security and Technology
    Replies: 4
    Last Post: 09-22-2009, 11:40 AM
  2. failed to restart mysql service
    By kotlt in forum Hosting Security and Technology
    Replies: 4
    Last Post: 04-13-2009, 09:21 AM
  3. Cannot restart MySQL, HTTPD and server
    By DelPierro in forum Hosting Security and Technology
    Replies: 16
    Last Post: 11-22-2007, 11:24 PM
  4. How to install service httpd restart ?
    By mouseattack in forum Hosting Security and Technology
    Replies: 2
    Last Post: 07-21-2006, 07:02 PM
  5. error when restart MYSQL service
    By AndyJ in forum Hosting Security and Technology
    Replies: 4
    Last Post: 03-02-2005, 05:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •