Shiny Server 설치와 운영
shinyapps.io는 편리하지만 서버를 직접 제어할 수 없습니다. 회사 내부망에서 운영하거나, 월 비용을 줄이고 싶거나, 앱 설정을 세밀하게 조정해야 한다면 직접 서버를 운영하는 방법이 맞습니다. Shiny Server는 Ubuntu 서버에 무료로 설치할 수 있는 오픈소스 소프트웨어입니다.
준비 사항
- Ubuntu 22.04 LTS 서버 (AWS EC2, GCP Compute Engine, Azure VM 등)
- 최소 1 vCPU, 1 GB RAM (앱 규모에 따라 조정)
- 포트 3838 오픈 (보안 그룹 또는 방화벽 설정)
R 설치
# Ubuntu 22.04 기준sudo apt update && sudo apt upgrade -y# CRAN 공식 저장소 추가wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | \ sudo gpg --dearmor -o /usr/share/keyrings/r-project.gpgecho "deb [signed-by=/usr/share/keyrings/r-project.gpg] \ https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/" | \ sudo tee /etc/apt/sources.list.d/r-project.listsudo apt updatesudo apt install -y r-base r-base-dev
버전을 확인합니다.
R --version
필수 패키지 설치
# 시스템 의존성 먼저 설치sudo apt install -y \ libcurl4-openssl-dev \ libssl-dev \ libxml2-dev \ libfontconfig1-dev \ libharfbuzz-dev \ libfribidi-dev# R 패키지sudo Rscript -e "install.packages(c( 'shiny', 'bslib', 'tidyverse', 'plotly', 'DT', 'rmarkdown', 'arrow', 'memoise'), repos='https://cloud.r-project.org/')"
sudo로 설치해야 Shiny Server가 실행하는 shiny 계정이 패키지를 쓸 수 있습니다.
Shiny Server 설치
# 최신 버전은 https://posit.co/download/shiny-server/ 에서 확인wget https://download3.rstudio.org/ubuntu-18.04/x86_64/shiny-server-1.5.22.1017-amd64.debsudo gdebi shiny-server-1.5.22.1017-amd64.deb
설치 후 서비스 상태를 확인합니다.
sudo systemctl status shiny-server
active (running)이 표시되면 설치가 완료된 것입니다.
설정 파일 구조
설정 파일 위치는 /etc/shiny-server/shiny-server.conf입니다.
# /etc/shiny-server/shiny-server.conf
run_as shiny; # 앱 실행 계정
server {
listen 3838; # 포트 번호
location / {
site_dir /srv/shiny-server; # 앱 루트 디렉토리
log_dir /var/log/shiny-server; # 로그 디렉토리
directory_index on; # 폴더 목록 표시
}
}
설정을 변경하면 서비스를 재시작합니다.
sudo systemctl restart shiny-server
앱 배포
앱 폴더를 /srv/shiny-server/ 아래에 복사합니다.
sudo cp -r ~/modular-app /srv/shiny-server/sudo chown -R shiny:shiny /srv/shiny-server/modular-app
브라우저에서 http://서버IP:3838/modular-app으로 접속합니다.
여러 앱을 동시에 호스팅할 때는 폴더별로 분리합니다.
/srv/shiny-server/
├── modular-app/ → http://서버IP:3838/modular-app
├── stat-report/ → http://서버IP:3838/stat-report
└── realtime-app/ → http://서버IP:3838/realtime-app
다중 앱 별도 포트 설정
앱마다 포트를 분리하고 싶을 때는 location 블록을 추가합니다.
server {
listen 3838;
location /dashboard {
app_dir /srv/shiny-server/modular-app;
log_dir /var/log/shiny-server;
}
location /report {
app_dir /srv/shiny-server/stat-report;
log_dir /var/log/shiny-server;
}
}
로그 확인
앱 오류가 발생하면 로그를 먼저 봅니다.
# Shiny Server 로그sudo journalctl -u shiny-server -f# 앱별 로그 (앱 이름과 세션 ID로 구분된 파일)ls /var/log/shiny-server/tail -f /var/log/shiny-server/modular-app-shiny-20240101-120000-12345.log
Nginx 리버스 프록시 설정
3838 포트 대신 기본 80/443 포트로 서비스하려면 Nginx를 리버스 프록시로 씁니다.
sudo apt install -y nginx
# /etc/nginx/sites-available/shiny
server {
listen 80;
server_name 내도메인.com;
location / {
proxy_pass http://127.0.0.1:3838/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 600;
}
}
sudo ln -s /etc/nginx/sites-available/shiny /etc/nginx/sites-enabled/sudo nginx -tsudo systemctl reload nginx
이제 http://내도메인.com으로 접속하면 Nginx가 Shiny Server로 연결합니다. Let's Encrypt로 HTTPS도 간단히 적용할 수 있습니다.
sudo apt install -y certbot python3-certbot-nginxsudo certbot --nginx -d 내도메인.com