Recently, after some software and hardware changes, I was forced to rebuild a CentOS 6.5-based virtual machine. Because the CentOS repository does not include Apache HTTP Server 2.4, and I’m not a big fan of third-party repositories, I decided to compile httpd from source. When compiling the HTTP server from source, the system administrator is not provided with any service management script; thus, I began my search.
I found a simple service script in an archived version of an old Red Hat user guide, which made things a little easier.
#!/bin/sh # # Startup script for the Apache Web Server # # chkconfig: 345 85 15 # description: Apache is a World Wide Web server. It is used to serve # HTML files and CGI. # processname: httpd # pidfile: /var/run/httpd.pid # config: /etc/httpd/conf/httpd.conf # Source function library. . /etc/rc.d/init.d/functions # See how we were called. case "$1" in start) echo -n "Starting httpd: " daemon httpd -DSSL echo touch /var/lock/subsys/httpd ;; stop) echo -n "Shutting down httpd: " killproc httpd echo rm -f /var/lock/subsys/httpd rm -f /var/run/httpd.pid ;; status) status httpd ;; restart) $0 stop $0 start ;; reload) echo -n "Reloading httpd: " killproc httpd -HUP echo ;; *) echo "Usage: $0 {start|stop|restart|reload|status}" exit 1 esac exit 0
To create the service script file, follow these directions:
# Create httpd script touch /etc/init.d/httpd # Upload, or copy and paste the script contents nano /etc/init.d/httpd # Set desired permissions chmod 0755 /etc/init.d/httpd # Start httpd on boot, if desired chkconfig --add httpd chkconfig httpd on
Upon careful review, you’ll notice the service script assumes that the httpd binary is globally accessible. Under my minimal CentOS installation, httpd was installed in /usr/local/apache2
. While there are more than a few methods for accomplishing this goal, we’ll use a symbolic link.
# Create symbolic link to apachectl ln -s /usr/local/apache2/bin/apachectl /usr/sbin/httpd
After that, I executed the script which caused a “bad interpreter” error.
[root@localhost]# /etc/init.d/httpd -bash: ./httpd: /bin/sh^M: bad interpreter: No such file or directory
This error occurs when a file contains CRLF-style line endings (Windows) instead of the LF-style line endings required by Unix-based systems. Luckily, someone invented a utility that fixes this problem quickly and painlessly.
# Install dos2unix yum install dos2unix # Fix "bad interpreter" error dos2unix /etc/init.d/httpd
After that, I could start, stop, and restart the httpd server without any problems. On the other hand, when I tried to retrieve the status directly from the httpd binary (apachectl), I encountered another problem. Luckily, this problem was also easily solvable.
httpd status # Output: /usr/sbin/httpd: line 95: lynx: command not found # Install Lynx browser yum install lynx
And… that’s it!