서버이전을 진행하면 nginx나 java application은 linux의 서비스로 등록하는 작업을 반복적으로 하게 된다.
그럴 때마다 찾아서 볼 수 있도록 정리해 보자.
linux 서비스 등록 or 관리
systemd라는 프로세스가 서비스를 감시, 시작, 중지를 담당
보통 systemctl 명령어로 관리
용어 |
의미 |
systemd |
Linux의 기본 서비스 관리자 (init 시스템). 시스템 부팅 시 자동으로 시작되고, 서비스(데몬)를 관리해 줌. |
systemctl |
systemd를 조작하는 명령어 (예: systemctl start nginx) |
서비스 등록 |
systemd에 프로그램을 등록해서 자동으로 실행되게 하는 것 (/etc/systemd/system/*.service 파일 만들기) |
서비스 등록의 장점
- 서버 부팅 시 자동으로 실행돼
- 죽으면 systemd가 다시 살려줌 (자동 재시작)
- 로그를 통합 관리 (예: journalctl -u nginx.service)
- 수동으로 시작/중지/재시작 가능 (systemctl start/stop/restart 서비스명)
등록
이미 등록되어 있는지 우선 확인
$ sudo systemctl status nginx
// 없다면
Unit nginx.service could not be found.
// 있다면
● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-07-18 11:50:15 KST; 10 months 10 days ago
Main PID: 15987 (nginx)
Tasks: 5 (limit: 9388)
Memory: 20.6M
CPU: 3min 52.145s
CGroup: /system.slice/nginx.service
├─ 15987 "nginx: master process /home/mmm/apps/nginx/sbin/nginx"
├─3998896 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
├─3998897 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
├─3998898 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
└─3998899 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
서비스 파일 생성
sudo nano /etc/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/home/apps/nginx/sbin/nginx -t -c /home/apps/nginx/conf/nginx.conf
ExecStart=/home/apps/nginx/sbin/nginx -c /home/apps/nginx/conf/nginx.conf
ExecReload=/home/apps/nginx/sbin/nginx -s reload
ExecStop=/home/apps/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
서비스 파일 등록
sudo systemctl enable nginx
sudo systemctl start nginx
// 상태 확인
sudo systemctl status nginx
사용자 명령어
명령어 |
설명 |
sudo systemctl start nginx |
nginx 서비스 시작 |
sudo systemctl stop nginx |
nginx 서비스 중지 |
sudo systemctl restart nginx |
nginx 서비스 재시작 |
sudo systemctl status nginx |
nginx 서비스 상태 확인 |
sudo systemctl enable nginx |
부팅 시 nginx 자동 실행 등록 |
sudo systemctl disable nginx |
부팅 시 nginx 자동 실행 해제 |
systemctl list-unit-files --type service |
서비스 리스트 상태 확인 |