티스토리 뷰

반응형




포스트 설명 

  • 최근 shiny 앱을 호스팅 할 방법을 찾다가 AWS 에 호스팅 하는 방법을 알려주는 좋은 글이 있어 저자의 허락을 받고 번역 공유 합니다. 좋은 글을 올려주신 Venkat Raman 님께 다시한번 감사드립니다.

  • 스크린샷은 최근 aws 의 ui 가 변경된 관계로 최신화 합니다.

  • https://towardsdatascience.com/how-to-host-a-r-shiny-app-on-aws-cloud-in-7-simple-steps-5595e7885722



시작하며 

  • R shiny app is an interactive web interface. R shiny app has two components user interface object (UI.R) and server function (Server .R). The two components are passed as arguments to the shiny app function that creates a shiny app object. For more info on how to build Shiny apps please refer this link. Basics of R Shiny App

  • R shiny app 은 인터렉티브한 웹 인터페이스 입니다. shiny 는 ui.R 이라고 불리는 UI 부분과 server.R 이라고 불리는 SERVER 단의 부분으로 나뉩니다. 이 두 스크립트는 shiny app 을 구동하는 shinyapp 이라는 함수의 인자로 사용됩니다. shiny app 을 구축하기 위한 더 많은 정보를 얻고 싶으신 분은 다음 링크를 참조해주세요. Basics of R Shiny App



Step 1 : EC2 인스턴스 만들기 

  • Log in to AWS account, click on EC2 under the ‘Compute’ header or click on EC2 under ‘Recently visited services’

  • AWS 에 로그인하고 EC2 인스턴스를 만드세요. 

  • 주석 : 이때 중요한 것은 인스턴스를 서울로 만드셔야한다는 것입니다. 만들고 나서 지역을 옮기는 것은 까다로우니 처음부터 서울로 만드시는 편이 좋습니다. 상단부에 보면 지역선택란이 있는데 이 부분을 서울로 설정해주세요.

  • 주석 : 저는 결제 과정을 이미 거쳤지만 처음 등록할 때는 신용카드를 등록하는 과정이 필요합니다.

  • 주석 : EC2 관련 정보 - https://aws.amazon.com/ko/ec2/?nc2=h_m1



  • 로그인을 한 뒤 보이는 화면입니다. 여기서 솔루션 구축탭에 가상머신 시작 이라는 메뉴를 클릭하세요.


  • Choose a Machine Image of your choice, here I have chosen Ubuntu server 16.04 LTS (HVM)

  • 클릭하고 난 후 ubuntu server 16.04 AMI 를 선택해주시면 됩니다.


  • Choose an Instance type; one can start out with t2.micro, t2.small or t2.medium instances. For larger apps one can use t2.large and beyond.

  • 인스턴스 유형을 선택하세요. t2.micro, small, medium 으로 선택할수 있지만 큰 앱이라면 large 나 그 이상도 좋겠죠.

  • 저는 프리티어이기 때문에 t2.micro 를 선택했습니다.



  • Click on Edit security groups, you will be directed to below page (Configure security group). In the SSH row, change source to ‘My IP’. Click on add a Rule, custom TCP Rule would be added. Under the ‘Port range’ enter 3838. This is the port for R shiny server.

  • 인스턴스 유형 선택이후로는 6번 보안그룹 구성 탭으로 바로 이동해주세요. 

  • 룰 추가 버튼을 클릭하고 유형은 사용자지정 TCP, 포트 범위는 3838 로 해주시고 소스는 위치 무관으로 변경해주세요. 3838은 R shiny server 포트 입니다.

  • 꼭 이 설정으로 하실 필요는 없습니다. 




  • Click ‘Review and Launch’ and then click ‘Launch’. Upon doing this you will get a dialogue box like below. The dialogue box helps in creating a private key which will enable us to ssh into the EC2 instance. Give a name to the key and click ‘Download Key Pair’. You will get .pem file. Save the .pem file/key securely.

  • 모든 설정을 마친 뒤에 시작을 누르면 위와 같이 대화상자가 뜰겁니다. 이 대화상자는 EC2 인스턴스에 접속할 수 있는 키 페어를 생성하기 위한 상자입니다. 새 키페어 생성을 선택하고 이름을 입력한 뒤 다운로드 버튼을 누르세요. 그러면 pem 확장자의 파일이 생성됩니다. 잘 보관 하세요.



  • 이후 인스턴스 시작 버튼을 누르면 다음과 같은 창을 확인할 수 있습니다. 이때 i-0a~~~~ 부분을 클릭하시면 인스턴스 상태를 확인하실 수 있습니다.





Step 2 : Putty 를 통해 EC2 인스턴스 SSH 접속하기 (윈도우) 

  • Download putty, after downloading, convert the .pem file into ppk file. To convert .pem file to ppk, type puttygen in the windows start dialog box. Click on ‘puttygen’. The below box should appear.

  • putty 를 받고나서 pem 파일을 ppk 파일로 변환하세요. 변환하기 위해서는 윈도우 시작창에 있는 puttygen 프로그램을 사용해야합니다.

  • putty 다운로드 url : https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html


  • 받아논 pem 파일을 로드하고 save private key 버튼을 통해 ppk 파일로 변환합니다.



  • 이제 putty 를 실행시켜서 session 란의 hostname 과 auth 란의 private key 란을 채워주세요. hostname 은 밑의 그림처럼 인스턴스 정보에서 확인하실 수 있습니다.

  • 또한 이때 정보를 입력하시고 session 을 save 해두시면 차후 이용시 편리합니다.






  • 정보를 입력하시고 접속하시면 대화상자가 하나 뜰 수 있는데 무시하시고 ubuntu 로 로그인 해주시면 다음과 같은 화면을 확인하실 수 있으실 겁니다.


Step 3 : 파일 통신위해 WinSCP 설치하기 

  • WinSCP 를 설치해주세요.

  • WinSCP 다운로드 url : https://winscp.net/eng/download.php


  • 호스트 이름과 사용자이름, 고급란에서 private key 까지 설정해주신 뒤 로그인을 눌러주세요.


Step 4 : 인스턴스에 R 과 shiny server 설치하기 

  • The first prerequisite to run R shiny app is to install r base, shiny server, shiny package and associated packages.

  • R shiny app 을 사용하기 위해 우선 되어야할 것은 R base, shiny server, shiny package 와 관련 패키지들을 설치하는 것 입니다.


  • To install the above, the first step is to go to the root and install them. The reason being if you are logged in as non root user in Ec2, you will have your own library path and probably the R packages, r base, shiny server may not get installed system wide. To install it system wide, go to root and install the above

  • 상기의 것들을 설치하기 위해선 일단 루트로 이동해야합니다. 왜냐하면 현재 사용자계정으로 로그인 했기 때문에 이 상태에서 설치를 하면 전체에 적용되지 않을 수 있기 때문입니다.


# putty 에서 입력할 명령어 목록

sudo -i

sudo apt-get update


sudo apt-get install r-base-dev

sudo su -c "R -e \"install.packages('shiny', repos = 'http://cran.rstudio.com/')\""


sudo apt-get install gdebi-core

wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.9.923-amd64.deb

sudo gdebi shiny-server-1.5.9.923-amd64.deb






  • 전부 제대로 설치 되었다면 shiny server 시작 메시지가 나옵니다.



Step 5 : shiny app 구성요소 전송 

  • After execution of above steps a directory(folder) by the name ‘shiny-server’ would have been created in the path /srv/shiny-server/ 

  • 위의 단계를 마치고 나면 shiny-server 라는 이름의 폴더가 srv/shiny-server/ 라는 경로로 생성되었을 것입니다.

  • The next step is to create a folder inside the directory shiny-server where we can place our R shiny app components (UI.R, Server. R, configuration file, R workspace, data files or R programs).

  • 다음 단계는  shiny-server 경로에 우리가 shiny app 구성요소들을 저장할 폴더를 만드는 것입니다.

  • At first we may not be able to create folder inside the shiny-server folder, to do this execute the below commands first.

  • 권한문제로 인해 shiny-server 폴더에 폴더를 만들지 못할 수 있습니다. 먼저 이 명령어들을 입력하세요.

sudo chmod 777 /srv/shiny-server
sudo mkdir /srv/shiny-server/myapp
sudo chmod 777 /srv/shiny-server/myapp
  • In the above command I have created a folder ‘myapp’ to place all the R shiny app components.

  • mkdir 명령은 shiny 앱의 구성요소들을 저장할 폴더를 만드는 명령어 입니다.



Step 6 : WinSCP 를 이용해서 shiny app 을 옮기기 

  • Now copy the R shiny app components from your local machine to Ec2 instance under the following path /srv/shiny-server/myapp/ 

  • 이제 R shiny app 요소들을 로컬 pc 에서 EC2 인스턴스의 srv/shiny-server/myapp 으로 복사해볼까요

  • One important thing to be taken into consideration is to configure the shiny-server.conf 

  • 이 과정에서 고려해야할 것은 shiny-server.conf 파일을 구성하는 것 입니다.

  • The shiny-server.conf is available in the location /etc/shiny-server/ 

  • shiny-server.conf 는 etc/shiny-server/ 에 있습니다.

  • Again you may not be able to access the shiny-server directory under /etc/.  

  • 아까처럼 etc 폴더에 접근할 수 없을 수 있으니 다음과 같은 명령어를 실행하겠습니다.

sudo chmod 777 /etc/shiny-server 

  • After executing the above command you can copy the configuration file into local system, edit it and then transfer back the edited configuration file in the location /etc/shiny-server. The edition to be made is as follows; please note the words after # are comments.

  • 위의 명령어를 실행한 뒤에야 etc/shiny-server/ 에 있는 conf 파일을 로컬로 복사한뒤 수정한 후에 재전송할 수 있습니다. 파일을 받고 난 뒤 다음과 같이 변경해주세요. ; 참고로 # 는 주석표시입니다.



  • 이후 srv/shiny-server/myapp 폴더에 shiny app 과 conf 파일을 복사해주세요.




Step 7 : app 호스팅 하기 

  • Now the final step. In the amazon console, go to your running EC2 instance. Copy the Public DNS (Ipv4) e.g. : ec2–34–215–115–68.us-west-2.compute.amazonaws.com.    

  • 이제 마지막 단계입니다. 아마존 콘솔에 있는 EC2 인스턴스의 public DNS(ipv4) 주소를 복사하세요. : ec2-52-79-233-69.ap-northeast-2.compute.amazonaws.com

  • Copy this in the browser and suffix: 3838/myapp like below and press enter.

  • 복사한 url 뒤에 :3838/myapp 을 붙여 접속해보세요.

  • ec2-52-79-233-69.ap-northeast-2.compute.amazonaws.com:3838/myapp



마치며... 

  • 가끔은 그냥 회색화면으로 나오거나 찾을 수 없다는 메시지가 뜨기도 합니다만 새로고침 하시면 보통 해결됩니다.

  • 좋은 글을 작성해주신 Venkat Raman 님께 다시한번 감사드립니다.  

  • Thanks Venkat. 







부족한 블로그에 방문해 주셔서 감사합니다.

잘못된 부분이나 질문이 있으시면 

댓글로 말씀해주세요.


금방 확인하고 피드백 드리겠습니다.


좋은 하루 되세요. ^^



반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함