Tuesday, March 24, 2009

Apache restart with Sudo Permission - Issue

Unfortunately, this issue ate my 5 days to fix this issue.
and the resolution was very very small compare to time take by it.

Issue :
While restarting apache with sudo access it was throwing some exception, while same with root was working fine.

Exception :
$ sudo /etc/init.d/apache restart
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable LD_LIBRARY_PATH was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JAVA_BINDIR was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JAVA_HOME was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JAVA_ROOT was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JDK_HOME was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JRE_HOME was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JRUBY_HOME was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable ORACLE_HOME was undefined
Syntax OK
Shutting down httpd2 (waiting for all children to terminate) done
Starting httpd2 (prefork) [Tue Mar 17 17:01:47 2009] [warn] PassEnv variable LD_LIBRARY_PATH was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JAVA_BINDIR was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JAVA_HOME was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JAVA_ROOT was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JDK_HOME was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JRE_HOME was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JRUBY_HOME was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable ORACLE_HOME was undefined
done

Resolution :

Since apache is not able to find above environment variable of user shell, its throwing the exception, so to restart the apache use option -i ( to preserver exception)

The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the user that the command is being run as. The comâmand name argument given to the shell begins with a - to tell the shell to run as a login shell. sudo attempts to change to that user's home directory before running the shell. It also initializes the environment, leaving TERM unchanged, setting HOME, SHELL, USER, LOGNAME, and PATH, and unsetting all other environment variables. Note that because the shell to use is determined before the sudoers file is parsed, a runas_default setting in sudoers will specify the user to run the shell as but will not affect which shell is actually run.

Make sure you have to execute the below command, if you are facing issue while executing this add " (root) NOPASSWD: /bin/bash /etc/init.d/apache2 *" in /etc/sudoers file

$ sudo -i /etc/init.d/apache restart

Syntax OK
Shutting down httpd2 (waiting for all children to terminate) done
Starting httpd2 (prefork) done

WOW !!

2 comments:

Rahul Sinha said...

Wonderful !!! This was really great.

Unknown said...

Great!! Thanks