end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

init → systemd 移行メモと、apache httpd と nginx の設定例

linux自動起動において、init → systemd 移行は避け気味で過ごしていましたが、 amazon linux2が発表され、systemd による自動起動が殆どなるので、メモ。

サービス一覧の確認

init.d systemd
chkconfig --list systemctl list-units --type=service または
systemctl list-unit-files --type=service --no-pager

以下、実行例。まずは、init.d の場合

$ /sbin/chkconfig --list
mysql.server    0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

次にsystemd.

※「SUB=exited」 は必要な処理が完了したものを表します.

※「--type=service」はservice以外に mount,automount,socket,path,device,target がありますが、 自動起動(daemon化)では気にする必要はないかと思います.

$ /usr/bin/systemctl list-units --type=service
  UNIT                       LOAD   ACTIVE SUB     DESCRIPTION
  auditd.service             loaded active running Security Auditing Service
  crond.service              loaded active running Command Scheduler
  dbus.service               loaded active running D-Bus System Message Bus
  getty@tty1.service         loaded active running Getty on tty1
  kmod-static-nodes.service  loaded active exited  Create list of required static device no
* lvm2-monitor.service       loaded failed failed  Monitoring of LVM2 mirrors, snapshots et
* lvm2-pvscan@8:2.service    loaded failed failed  LVM2 PV scan on device 8:2
  mysql.server.service       loaded active running LSB: start and stop MySQL
* network.service            loaded failed failed  LSB: Bring up/down networking
      :                    

サービスの自動起動:登録や起動,終了,状態確認

以下の通り.

- init.d systemd
自動起動:登録 chkconfig --add sshd systemctl enable sshd
自動起動:削除 chkconfig --del sshd systemctl disable sshd
起動 service sshd start systemctl start sshd
停止 service sshd stop systemctl stop sshd
再起動 service sshd restart systemctl restart sshd
状態確認 なし? ps -efで確認? systemctl status sshd

apache httpd 2.4 の登録例

参考url

https://qiita.com/pb_tmz08/items/68a58979ca22748e8502 https://exfield.jp/View/pid:589/

$ vi /home/end0tknr/local/apache24/conf/apache24.service
[Unit]
Description=The Apache2.4 HTTP Server
After=network.target

[Service]
Type=forking
ExecStart=/home/end0tknr/local/apache24/bin/apachectl start
ExecReload=/home/end0tknr/local/apache24/bin/apachectl restart
ExecStop=/home/end0tknr/local/apache24/bin/apachectl stop

User=root
Group=root

# KillMode=???
# PIDFile=???
# EnvironmentFile=/usr/local/etc/apache/env.sh (ExecStartでのみ有効?)
# Environment=    ## 個別に定義する場合

[Install]
WantedBy=multi-user.target  # = run level

# 以下で /etc/systemd/system/ 以下に ln -s されます 
$ sudo systemctl enable /home/end0tknr/local/apache24/conf/apache24.service
$ sudo systemctl start apache24.service
$ sudo systemctl status apache24.service
$ sudo systemctl stop apache24.service

nginx の登録例

本家 www.nginx.com で紹介されていますので、以下をご覧下さい

https://www.nginx.com/resources/wiki/start/topics/examples/systemd/