当前位置: 首页 > 工具软件 > Simple HTTPD > 使用案例 >

为编译安装的 httpd 服务,编写 service unit 文件

缑兴贤
2023-12-01
  • 编译安装 httpd
# 安装相关工具
[root@localhost ~]# yum -y install gcc openssl-devel pcre-devel apr-devel apr-util-devel

# 下载安装包
[root@localhost ~]# wget https://downloads.apache.org/httpd/httpd-2.4.43.tar.bz2

# 解压
[root@localhost ~]# tar -xvf httpd-2.4.43.tar.bz2

# 安装
[root@localhost ~]# cd httpd-2.4.43
[root@localhost httpd-2.4.43]# ./configure --prefix=/apps/httpd24 --sysconfdir=/etc/httpd --enable-ssl --enable-so
[root@localhost httpd-2.4.43]# make && make install

# 启动服务
[root@localhost ~]# /apps/httpd24/bin/apachectl start

# 测试
[root@localhost ~]# curl 127.0.0.1
<html><body><h1>It works!</h1></body></html>

# 启动服务(停止服务,否则后面用 systemctl 无法启动)
[root@localhost ~]# /apps/httpd24/bin/apachectl stop
  • 创建 unit 文件
[root@localhost ~]# vim /usr/lib/systemd/system/httpd.service

# 填写以下内容
[Unit]
Description=Start httpd
[Service]
Type=simple
EnvironmentFile=/etc/httpd/httpd.conf
ExecStart=/apps/httpd24/bin/httpd -k start -DFOREGROUND
ExecReload=/apps/httpd24/bin/httpd -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
[Install]
WantedBy=multi-user.target
# 重新加载 unit 文件
[root@localhost ~]# systemctl daemon-reload

# 启动服务
[root@localhost ~]# systemctl start httpd

# 查看状态
[root@localhost ~]# systemctl status httpd
● httpd.service - Start httpd
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-06-23 10:37:11 CST; 17min ago
 Main PID: 39492 (httpd)
   CGroup: /system.slice/httpd.service
           ├─39492 /apps/httpd24/bin/httpd -k start -DFOREGROUND
           ├─39493 /apps/httpd24/bin/httpd -k start -DFOREGROUND
           ├─39494 /apps/httpd24/bin/httpd -k start -DFOREGROUND
           └─39495 /apps/httpd24/bin/httpd -k start -DFOREGROUND

Jun 23 10:37:11 localhost.localdomain systemd[1]: Started Start httpd.


# 访问(运行正常)
[root@localhost ~]# curl 127.0.0.1
<html><body><h1>It works!</h1></body></html>

# 停止服务
[root@localhost ~]# systemctl stop httpd

# 设置开机启动
[root@localhost ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /etc/systemd/system/httpd.service.
 类似资料: