# yy_cmd.hlp
# 목적 : yyy_cmd.php + yy_cmd.py + Apache WebSocket + systemd + tmux 운영 메모
# 기준 : 현재 서버 점검 결과 반영본

[1] 구성 개요
- 웹 UI : /var/www/html/y/yyy_cmd.php
- WebSocket 서버 : /var/www/html/y/yy_cmd.py
- Python venv : /opt/venv
- systemd 서비스 : /etc/systemd/system/yy-cmd.service
- 내부 WS 리슨 : 127.0.0.1:8765
- Apache WS 프록시 경로 : /y/yy_cmd_ws
- 선택 기능 : tmux ON/OFF 토글 사용 가능

[2] 현재 확인된 버전 / 상태
- Python : 3.12.3
- pip : 26.0.1
- websockets : 16.0
- fastapi : 0.129.0
- uvicorn : 0.41.0
- tmux : 3.4
- yy-cmd.service 실행 계정 : data_www
- yy-cmd.service 작업 디렉터리 : /var/www/html/y
- yy-cmd.service 실행 명령 :
  /opt/venv/bin/python3 /var/www/html/y/yy_cmd.py --host 127.0.0.1 --port 8765

[3] Apache 필수 모듈
아래 모듈이 올라와 있어야 함.
- proxy_module
- proxy_http_module
- proxy_wstunnel_module
- rewrite_module
- ssl_module

확인:
apache2ctl -M | egrep 'proxy|proxy_http|proxy_wstunnel|rewrite|ssl'

[4] Apache 핵심 설정 예
파일 예:
- /etc/apache2/sites-available/nssb.green-net.or.kr-le-ssl.conf

핵심 구문:
ProxyPass        /y/yy_cmd_ws  ws://127.0.0.1:8765/y/yy_cmd_ws
ProxyPassReverse /y/yy_cmd_ws  ws://127.0.0.1:8765/y/yy_cmd_ws

같은 WS 경로가 다른 SSL vhost에도 있을 수 있으니 운영 시 문서에 같이 기록할 것.

[5] systemd 서비스 파일 예
파일:
- /etc/systemd/system/yy-cmd.service

내용:
[Unit]
Description=yy_cmd python service
After=network.target

[Service]
Type=simple
User=data_www
WorkingDirectory=/var/www/html/y
ExecStart=/opt/venv/bin/python3 /var/www/html/y/yy_cmd.py --host 127.0.0.1 --port 8765
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

[6] venv 준비 / 설치
기본 확인:
 /opt/venv/bin/python3 --version
 /opt/venv/bin/pip --version

패키지 설치/업데이트 예:
 /opt/venv/bin/pip install -U pip setuptools wheel
 /opt/venv/bin/pip install -U websockets fastapi uvicorn

tmux 설치:
 sudo apt-get update && sudo apt-get install -y tmux

[7] 서비스 등록 / 재시작 / 상태 확인
리로드 + 재시작:
 sudo systemctl daemon-reload && sudo systemctl restart yy-cmd.service

부팅 시 자동실행:
 sudo systemctl enable yy-cmd.service

상태 확인:
 systemctl status yy-cmd.service --no-pager -l

로그 확인:
 journalctl -u yy-cmd.service -n 200 --no-pager

서비스 원문 확인:
 systemctl cat yy-cmd.service

[8] 포트 / 연결 확인
리슨 확인:
 ss -lntp | grep 8765

실제 연결 확인:
 ss -ntp | grep 8765

정상 예:
- LISTEN 127.0.0.1:8765 에 python3 가 떠 있음
- 접속 중이면 apache2 <-> python3 ESTAB 1쌍 이상 보일 수 있음

[9] Apache 설정 점검
프록시 설정 찾기:
 grep -RIn --color=never -E 'yy_cmd_ws|8765|ProxyPass|ProxyPassMatch|RewriteRule|wss:|ws:' /etc/apache2 2>/dev/null

활성 사이트 목록:
 ls -l /etc/apache2/sites-enabled /etc/apache2/sites-available

vhost 구조:
 apache2ctl -S

문법 확인:
 apache2ctl configtest

리로드:
 sudo systemctl reload apache2

[10] yyy_cmd.php / yy_cmd.py 역할 분리
- yyy_cmd.php
  - xterm.js UI
  - /y/yy_cmd_ws 로 WebSocket 접속
  - 터미널 ON/OFF 상태 저장
  - 연결 의도 저장
  - tmux ON/OFF 토글 UI

- yy_cmd.py
  - PTY 생성
  - bash 실행
  - xterm.js 와 WebSocket 연결
  - resize/input 처리
  - tmux=1 일 때 tmux attach/new 분기 가능

[11] tmux 사용 메모
tmux ON 이면 같은 세션으로 복귀가 가능함.
대표 명령 예:
 tmux attach -t yy || tmux new -s yy

주의:
- 세션명을 yy 하나로 고정하면 여러 사용자가 같은 세션을 공유할 수 있음
- 단독 사용이면 편리하지만, 다중 사용자면 계정별/IP별/session_id 분리가 더 안전함

[12] 자주 쓰는 점검 명령 묶음
apache2ctl -M | egrep 'proxy|proxy_http|proxy_wstunnel|rewrite|ssl'
grep -RIn --color=never -E 'yy_cmd_ws|8765|ProxyPass|ProxyPassMatch|RewriteRule|wss:|ws:' /etc/apache2 2>/dev/null
apache2ctl configtest
systemctl cat yy-cmd.service
systemctl status yy-cmd.service --no-pager -l
journalctl -u yy-cmd.service -n 200 --no-pager
/opt/venv/bin/python3 --version && /opt/venv/bin/pip --version
/opt/venv/bin/pip list | egrep 'websockets|aio|pty|uvicorn|fastapi'
command -v tmux && tmux -V
ss -lntp | grep 8765
ss -ntp | grep 8765

[13] 이상 시 1차 판단
1. apache2ctl configtest 가 OK 인가?
2. 127.0.0.1:8765 에 python3 가 LISTEN 중인가?
3. /y/yy_cmd_ws ProxyPass 가 맞는가?
4. yy-cmd.service 가 restart loop 아닌가?
5. tmux ON 인데 tmux 가 실제 설치되어 있는가?
6. 접속 수보다 bash 가 과도하게 많으면 누수 의심. 이 경우:
   - systemctl restart yy-cmd.service
   - ss -ntp | grep 8765
   - ps -eo pid,ppid,stat,lstart,etime,cmd | grep '[b]ash -il'

[14] 권장 저장 위치
- 서버 운영 문서로 둘 경우 : /var/www/html/y/yy_cmd.hlp
- 임시 보관 / 배포 전 검토 : 원하는 작업 폴더에 별도 저장

[15] 메모
- 현재 구성은 /opt/venv Python 3.12.3 기반
- websockets 16.0 / tmux 3.4 확인됨
- 8765 LISTEN 정상
- Apache proxy_wstunnel 포함 필수 모듈 정상