We help IT Professionals succeed at work.

Apache Web Servers

Difference Between Apachectl and Httpd .Is apachectl -k start or httpd -k start  both the same .we have a pid file by the name httpd.pid but not apachectl.pid what is the difference .


pgrep httpd|head -5
6894
7971
8439
8946
9038
$ pgrep apache|head -5
$ pgrep apachectl|head -5
$ ps -ef|grep httpd|wc -l
58
$ ps -ef|grep apache|wc -l
71
$ ps -ef|grep apachectl|wc -l
1

$ ps -ef|grep apache|head -5

root      6894     1  0 23:13 ?        apache-2.2.22//bin/httpd -k start
root      6895  6894  0 23:13 ?        apache-2.2.22/logs/error.log_%Y%m%d 10M
root      6896  6894  0 23:13 ?        apache-2.2.22/logs/access.log_%Y%m%d 10M
root      6897  6894  0 23:13 ?        apache-2.2.22/logs/access.log_%Y%m%d 10M
root      6898  6894  0 23:13 ?        apache-2.2.22/logs/ssl_request.log_%Y%m%d 10M
daemon    6900     1  0 23:13 ?        00:00:00 LLAWP /apache-2.2.22/conf/WebAgent.conf -APACH
root      6906  6894  0 23:13 ?        apache-2.2.22/logs/mod_jk.log_%Y%m%d 10M
daemon    8946  6894  0 23:25 ?        /apache-2.2.22//bin/httpd -k start
daemon    9255  6894  0 23:27 ?    apache-2.2.22//bin/httpd -k start
daemon    9617  6894  0 23:29 ?        /apache-2.2.22//bin/httpd -k start


 ps -ef|grep httpd|head -5
root      6894     1  0 23:13 ?            apache-2.2.22//bin/httpd -k start
daemon    8439  6894  0 23:21 ?        /apache-2.2.22//bin/httpd -k start
daemon    8946  6894  0 23:25 ?        apache-2.2.22//bin/httpd -k start
daemon    9038  6894  0 23:26 ?        apache-2.2.22//bin/httpd -k start
daemon    9255  6894  0 23:27 ?        apache-2.2.22//bin/httpd -k start

daemon is user having in httpd.conf

getent passwd daemon
daemon:x:2:2:daemon:/sbin:/sbin/nologin

 cat httpd.conf|grep -i daemon
# httpd daemons, you will need to change at least LockFile and PidFile.
User daemon
Group daemon



can anyone explain clearly  the different outputs regarding the each command.
Comment
Watch Question

Dr. KlahnPrincipal Software Engineer
CERTIFIED EXPERT

Commented:
apachectl is a front-end which controls starting, stopping and (to some extent) configuring the actual httpd executable.  It brings some convenience with its built-in help.  apachectl also checks the configuration files to ensure that they are consistent and valid.

In general Apache should always be started and stopped using apachectl, not by executing httpd directly.  The sole exception to that is at startup time, when systemd or SysVInit start it using their obscure startup files.

Author

Commented:
thanks but can you explain those each command output clearly I have difficulty in understanding them
Dr. KlahnPrincipal Software Engineer
CERTIFIED EXPERT

Commented:
httpd should never be used as a command.  Only apachectl should be used to control Apache.  And apachectl is indeed somewhat obscure.  Under normal conditions all that is needed are two variations:  "apachectl start" and "apachectl stop."

There are more variations and switches but they are only useful when there is trouble, and they are excessively (to my mind) obscure.  I've been using Apache for 20 years now, and only twice have I ever needed anything other than "start" or "stop."  So I recommend not going any further in that direction unless there is a problem which needs to be solved.

I do note that the "-k" switch does not exist on my Apache 2.4 system, though the documentation for it is still there in "apachectl -h".

root:/www> apachectl -k start
httpd: illegal option -- k

Open in new window

Murugesan NWe like KT across the universe.  C C++ shell script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT
CERTIFIED EXPERT

Commented:
@Srinivas

I have written my comments inline and tested the same at Linux and Cygwin
$ cat ./29176311.sh
#!/bin/bash
export PS4='$0 $LINENO: '
LOGNAME=$(id -nu)
if [ "Linux" = "$(uname -s)" ]
then
	if [ "root" = "$LOGNAME" ]
	then
		# Version 0001.0001
		# If apachectl not installed, install it => yum -y install httpd

		# /usr/sbin/apachectl is a shell script executable file
		# This file assigning the variable HTTPD='/usr/sbin/httpd'

		# $ /usr/bin/file /usr/sbin/apachectl
		# /usr/sbin/apachectl: Bourne shell script text executable

		# /usr/sbin/httpd is an executable

		# $ /usr/bin/file /usr/sbin/httpd
		# /usr/sbin/httpd: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, stripped

		if [ ! -f /usr/sbin/apachectl ]
		then
			/usr/bin/yum -y install httpd
			Ret=$?
		else
			Ret=0
		fi
		# If installed or /usr/sbin/apachectl is available, execute following statements
		if [ 0 -eq $Ret ]
		then
			# Before starting httpd
			# Check if a line starting with "^ServerName IPAddress" at /etc/httpd/conf/httpd.conf
			grep -E "^ServerName" /etc/httpd/conf/httpd.conf >/dev/null 2>&1
			if [ 0 -ne $? ]
			then
				grep -E "^#ServerName" /etc/httpd/conf/httpd.conf >/dev/null 2>&1
				if [ 0 -eq $? ]
				then
					# If a line starting with #ServerName followed by 1 or more characters => #ServerName.*
					# Replace #ServerName.* => \1 => first occurence of that line
					# With:
					# \1 new line
					# ServerName IPAddress:80
					# in same file /etc/httpd/conf/httpd.conf
					# You can change port number to required port 420 :)
					# Before assigning that port number
					# Validate that port number not in use before assigning that number.
					sed -i "s/\(^#ServerName.*\)/\1\nServerName $(hostname -i):80/;" /etc/httpd/conf/httpd.conf | grep ServerName
				else
					# If not present write following line at /etc/httpd/conf/httpd.conf
					echo "ServerName $(hostname -i)" >> /etc/httpd/conf/httpd.conf
					# Because of this change following error will not happen when we start httpd
					# httpd: Could not reliably determine the server's fully qualified domain name, using 123.456.78.910 for ServerName
				fi
			fi
			# Before strarting httpd validate if it is already at execution mode
			ps -eaf |\
			grep httpd |\
			grep -E -v "grep|ps"
			if [ 0 -ne $? ]
			then
				echo /usr/sbin/apachectl start
				/usr/sbin/apachectl start
				if [ 0 -eq $? ]
				then
					netstat -na | grep ":80"
					echo "httpd start PASS"
				else
					echo "httpd start FAIL"
				fi
			else
				echo -n "you can remove # at following lines to stop httpd." >/dev/null
				#echo "Stopping httpd"
				#echo /usr/sbin/apachectl stop
				#/usr/sbin/apachectl stop
			fi
		else
			echo "Execute this script using root user or using sudo"
		fi
	else
		echo "Update this script to execute at other OS: $(uname -s | sed "s/\-[0-9]\.[0-9]$//;")"
	fi
fi

Open in new window


Output at Linux:
murugesandins@123.456.78.910 /home/murugesandins [ 0 ]
$ ./29176311.sh
Execute this script using root user or using sudo
murugesandins@123.456.78.910 /home/murugesandins [ 0 ]
$ sudo ./29176311.sh
/usr/sbin/apachectl start
tcp        0      0 :::80                       :::*                        LISTEN
httpd start PASS
murugesandins@123.456.78.910 /home/murugesandins [ 0 ]
$ sudo ./29176311.sh
root      6663     1  0 08:40 ?        00:00:00 /usr/sbin/httpd -k start
apache    6664  6663  0 08:40 ?        00:00:00 /usr/sbin/httpd -k start
apache    6665  6663  0 08:40 ?        00:00:00 /usr/sbin/httpd -k start
apache    6666  6663  0 08:40 ?        00:00:00 /usr/sbin/httpd -k start
apache    6667  6663  0 08:40 ?        00:00:00 /usr/sbin/httpd -k start
apache    6668  6663  0 08:40 ?        00:00:00 /usr/sbin/httpd -k start
apache    6669  6663  0 08:40 ?        00:00:00 /usr/sbin/httpd -k start
apache    6670  6663  0 08:40 ?        00:00:00 /usr/sbin/httpd -k start
apache    6671  6663  0 08:40 ?        00:00:00 /usr/sbin/httpd -k start

Open in new window


Output at CYGWIN:
$ ./29176311.sh
Update this script to execute at other OS: CYGWIN_NT
$ file /cygdrive/c/Windows/System32/notepad.exe
/cygdrive/c/Windows/System32/notepad.exe: PE32+ executable (GUI) x86-64, for MS Windows
$ file /bin/ls
/bin/ls: PE32+ executable (console) x86-64, for MS Windows

Open in new window

Murugesan NWe like KT across the universe.  C C++ shell script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT
CERTIFIED EXPERT

Commented:
you can obtain the usage of apachectl using:
$ /usr/sbin/apachectl
Usage: /usr/sbin/httpd [-D name] [-d directory] [-f file]
                       [-C "directive"] [-c "directive"]
                       [-k start|restart|graceful|graceful-stop|stop]
                       [-v] [-V] [-h] [-l] [-L] [-t] [-S]
Options:
  -D name            : define a name for use in <IfDefine name> directives
  -d directory       : specify an alternate initial ServerRoot
  -f file            : specify an alternate ServerConfigFile
  -C "directive"     : process directive before reading config files
  -c "directive"     : process directive after reading config files
  -e level           : show startup errors of level (see LogLevel)
  -E file            : log startup errors to file
  -v                 : show version number
  -V                 : show compile settings
  -h                 : list available command line options (this page)
  -l                 : list compiled in modules
  -L                 : list available configuration directives
  -t -D DUMP_VHOSTS  : show parsed settings (currently only vhost settings)
  -S                 : a synonym for -t -D DUMP_VHOSTS
  -t -D DUMP_MODULES : show all loaded modules
  -M                 : a synonym for -t -D DUMP_MODULES
  -t                 : run syntax check for config files
$ man apachectl
$ man httpd

Open in new window

Author

Commented:
thanks
We like KT across the universe.  C C++ shell script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT
CERTIFIED EXPERT
Commented:
@Srinivas

If resolved/assisted you can accept/assist related answers here.

>> thanks
$ echo wELcOME | /usr/bin/wc > /dev/null

Author

Commented:
thanks but if we can connect on Skype or some way  that would be really helpful . but anyway this is the best answer I received among my questions raised so far .
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
1) Difference Between Apachectl and Httpd.

No difference. Some Distros use apache2 (Debian/Ubuntu) + some use httpd (RedHat/CentOS/Fedora).

Same code. Different name.

Whatever name is chosen, sometimes tools like apachectl change to httpdctl + pid file names also sometimes change.

2) Is apachectl -k start or httpd -k start  both the same.

Yes. See #1.

3) we have a pid file by the name httpd.pid but not apachectl.pid what is the difference .

See #1.