Linux下设置apache httpd服务为自动启动

linux服务器搭建svn+apache+ssl部署环境:
1、linux下svn安装与使用参考链接:linux服务器svn安装与版本控制.
2、linu下svn配置http访问服务参考链接:linux服务器svn配置http访问.
3、linu下svn配置https访问服务参考链接: linux服务器部署svn https访问.
4、linux svn设置开机自启动参考链接: linux svn设置开机自启动.

一、介绍

   svn经常都是搭建在本地。而本地服务器的环境也是极为不稳定的, 断电就很难避免,到服务器启动时,各种服务重启很麻烦。
   以下为linux下开启svn自启动设置,记录下,方便后续自己使用 。

二、linux自启动设置

查看服务器版本命令:uname -a
先说明下我的版本,不同版本可能会略有差异
1

linux 服务启动脚本多放在etc/init.d文件夹下
1

2.1 创建httpd自启动文件

# 进入目录
cd /etc/init.d
# 创建自启动脚本文件
touch appache-httpd
ls -lt

1

2.2 编辑appache-httpd脚本文件

编辑文件appache-httpd:

gedit appache-httpd

写入启动脚本:

#!/bin/bash
#
# created by rmling, 2022/05/05
#
# apache-httpd    Startup script for the Apache HTTP Server
#
# chkconfig: 345 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#           HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# pidfile: /run/httpd/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Path to the apachectl script, server binary, and short-form for messages.
apahectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=${PIDFILE-/run/httpd/httpd.pid}
lockfile=${LOCKFILE-/run/lock/subsys/httpd}
RETVAL=0
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}
# When stopping httpd a delay of >10 second is required before SIGKILLing the
# httpd parent; this gives enough time for the httpd parent to SIGKILL any
# errant children.
stop() {
    echo -n $"Stopping $prog: "
    killproc -d 10 $httpd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc $httpd -HUP
        RETVAL=$?
    fi
    echo
}
# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
        status $httpd
    RETVAL=$?
    ;;
  restart)
    stop
    start
    ;;
  condrestart)
    if [ -f ${pidfile} ] ; then
        stop
        start
    fi
    ;;
  reload)
        reload
    ;;
  graceful|help|configtest|fullstatus)
    $apachectl $@
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
    exit 1
esac
exit $RETVAL

第一行#!/bin/bash 格式标准
以下选项根据自己实际安装情况赋值:
#config: /etc/httpd/conf/httpd.conf
apahectl=/usr/sbin/apachectl
httpd= H T T P D − / u s r / s b i n / h t t p d p i d f i l e = {HTTPD-/usr/sbin/httpd} pidfile= HTTPD/usr/sbin/httpdpidfile={PIDFILE-/run/httpd/httpd.pid}
lockfile=${LOCKFILE-/run/lock/subsys/httpd}
1

2.3 分配权限

#执行如下命令,将该脚本标记为可执行文件(添加可执行的权限)
 chmod +x /etc/init.d/appache-httpd

2

2.4 设置开机启动

chkconfig --add appache-httpd
#执行如下命令,查看是否添加成功了
chkconfig httpd on 

1

注:如果出现->服务不支持 chkconfig,仔细核对上面的启动脚本

2.5 查看状态

#查看状态
chkconfig --list appache-httpd

此时的所有level状态都为关,设置其在所有level级别下运行:
1

chkconfig --level 0123456 appache-httpd on

在这里插入图片描述

2.6 服务启动

service httpd start

1

三、测试

立刻重启服务器:

shutdown -r now 

重启后执行命令:

#查看https是否启动成功
netstat -ntpl|grep 443 

1

猜你喜欢

转载自blog.csdn.net/weixin_44462773/article/details/124590390
今日推荐