Bootstrap

如何在openEuler中编译安装Apache HTTP Server并设置服务管理(含Systemd和Init脚本)

准备工作:

1、更新系统

        dnf update -y

2、安装必要的依赖(安装编译Apache所需的工具和库)

        dnf groupinstall "Development Tools"

        dnf install pcre-devel openssl-devel expat-devel apr apr-util-devel

方法一:使用Systemd服务单元文件

1、下载并解压httpd源码

        访问Apache官网下载最新版本的httpd源代码包

        wget https://downloads.apache.org/httpd/httpd-2.4.62.tar.gz


        tar xzvf httpd-2.4.62.tar.gz
        cd httpd-2.4.62

2、编译与安装

        配置、编译并安装httpd:

        ./configure --prefix=/usr/local/apache2
        make
        make install


3、创建Systemd服务单元文件

        创建一个名为httpd.service的文件于/etc/systemd/system/目录下:

        vim /etc/systemd/system/httpd.service
        内容如下:

        [Unit]
        Description=The Apache HTTP Server
        After=network.target
        [Service]
        Type=forking
        ExecStart=/usr/local/apache2/bin/apachectl start
        ExecStop=/usr/local/apache2/bin/apachectl stop
        ExecReload=/usr/local/apache2/bin/apachectl graceful
        PrivateTmp=true
        [Install]
        WantedBy=multi-user.target

4、启用并启动服务

        重新加载systemd配置并启动httpd服务:

        systemctl daemon-reload
        systemctl enable httpd
        systemctl start httpd

5、验证服务

 

方法二:使用传统的Init脚本

1、前提条件

        同方法一,先完成httpd的安装

2、创建Init脚本

        创建一个位于/etc/init.d/下的脚本文件,例如命名为httpd:

        vim /etc/init.d/httpd

        脚本:

        #!/bin/sh

        APACHE_HOME=/usr/local/apache2

        case "$1" in
        start)
            echo "Starting Apache..."
            $APACHE_HOME/bin/apachectl start
            ;;
        stop)
            echo "Stopping Apache..."
            $APACHE_HOME/bin/apachectl stop
            ;;
        restart)
            echo "Restarting Apache..."
            $APACHE_HOME/bin/apachectl restart
            ;;
        status)
            echo "Checking Apache status..."
            $APACHE_HOME/bin/apachectl status
            ;;
        *)
            echo "Usage: $0 {start|stop|restart|status}"
            exit 1
            ;;
        esac

        exit 0

        给脚本添加执行权限:

        chmod +x /etc/init.d/httpd 


3、管理服务

        启动服务:service httpd start

        停止服务:service httpd stop

        重启服务:service httpd restart

4、验证服务

;