重启 Apache 服务器及测试配置

发布时间:2015-05-20 03:00:04
在 CentOS / RHEL / Fedora Linux 服务器上安装的 Apache 网页服务器程序在系统中的进程名称为 httpd。每次更改 Apache 服务器的配置,不管是 httpd.conf 还是 vhost.conf 或者自己配置的什么 .conf,只要有改动都需要重新加载配置或者重启 httpd 服务才能生效。重启 httpd 服务很简单,重要的是这里记录了一个在重启服务前测试配置是否有错误的指令。
 
重启 httpd 有两个方法,使用 service (服务)指令,CentOS 7 中会重定向到 systemctl 指令,或者使用 /etc/init.d/httpd 或者 /usr/sbin/httpd 脚本(script)。实际上 service / systemctl 指令只是调用 /etc/init.d/ 或 /usr/sbin/ 里的 httpd 的 script 的简单方法,所以这两个方法是等价的。
 
使用 root 帐户登录,查询 httpd 的服务指令:
 
service httpd
Usage: httpd {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}
{ } 中列举了可以使用的 httpd 服务指令。下面列出一些例子,同时在下一行给出对应的 CentOS 7 中 systemctl 指令格式。所以……
 
启动 httpd 服务器:
 
service httpd start
systemctl start httpd
停止 httpd 服务器:
 
service httpd stop
systemctl stop httpd
重新启动(停止后再启动) httpd 服务器(会停止响应访问请求):
 
service httpd restart
systemctl restart httpd
温和地加载新的 Apache 配置(注意这里的 CentOS 7 指令不再是 systemctl):
 
service httpd graceful
apachectl graceful
主进程会监视子进程的状态,如果子进程正在响应请求,则让它处理完当前请求然后结束;如果没有在执行任务,则直接结束。每个子进程结束之后就会有一个使用新配置的子进程来取代。该指令可以在不中断当前任务的情况下重新加载配置文件。
 
剩下的还有:
 
condrestart:如果 httpd 正在运行,则重新启动它(言下之意,如果没有在运行就不管它)
try-restart:尝试重新启动(仅仅是尝试,而并不真的重启)
force-reload:强制重新加载配置文件(即使处于锁定状态)会导致正在进行的任务中断)
reload:重新加载配置文件
status:查询服务的当前状态(是否在运行)
fullstatus:查询服务详细状态(需要 links 包的支持)
help:显示帮助
configtest:测试配置文件有无参数错误,同 /usr/sbin/httpd -t
有的时候不小心的话会在 Apache 的配置文件里出现错误,直接重新加载则会导致 httpd 出错。所以加载新配置前测试一下是很必要的:
 
service httpd configtest
apachectl configtest
或者
 
/usr/sbin/httpd -t
如果返回
 
Syntax OK
就表明配置中没有语法错误(只能检测语法参数,别的可不行)。
 
然后加载新的配置,当然,一种比较温和的方法就是:
 
service httpd graceful
apachectl graceful
上面都是使用 service 指令来执行的,也可以使用下面的脚本指令,例如:
 
/etc/init.d/httpd restart
/etc/init.d/httpd start
/etc/init.d/httpd stop
另外,如果是 Debian / Ubuntu Linux 系统,相应的指令应该是:
 
/etc/init.d/apache2 restart
/etc/init.d/apache2 stop
/etc/init.d/apache2 start
当然也可以使用 service 指令:
 
service apache2 restart
service apache2 stop
service apache2 start