Monday, March 07, 2016

Install Shinny & Rstudio over NginX

Installing Shinny & Rstudio Open Source over Nginx


Today I will install in Centos 7:
  • Shinny and Rstudio web
  • NginX
  • And will Configure Nginx to serve as proxy for Shinny and Rstudio Web
  • All of this using the most basic setup in Digital Ocean
    
    
    #create user
    useradd admin/crodriguez
    passwd xxxxxx
    
    vi /etc/sudoers
    
    
    #Add user to wheel group for admin priviledges
    gpasswd -a crodriguez wheel
    
    
    #Or make him kindoff rootie User privilege speficication ;)
    admin ALL=(ALL) ALL
    
    
    # From now remember to use the created user to run all commands
    
    # 0. If you have less than 8 GB  RAM
    #lets make swap bigger in 1 GB
    
    sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
    sudo /sbin/mkswap /var/swap.1
    sudo /sbin/swapon /var/swap.1
    sudo sh -c 'echo "/var/swap.1 swap swap defaults 0 0 " >> /etc/fstab'
    
    #Now we're over with the preparations. Lets begin with the installation
    
    # 1. Lets install NgNx
    
    #install EPEL release
    sudo yum install epel-release
    sudo yum install nginx 
    
    #start the web server
    #in centos 7
     sudo systemctl start nginx
    
    # if you're using any flavor of ubuntu
    # sudo /etc/init.d/nginx start
    
    # Leave it as a service once the server starts
    sudo systemctl enable nginx
    
    # review if directory was created /usr/share/nginx/html
    
    # 2. install R from EPEL 
    sudo yum install R
    
    #3.  Download and install Rstudio for RH/Centos
    #change the version number if there's a newer release :) 
    
    wget http://download2.rstudio.org/rstudio-server-rhel-0.99.491-x86_64.rpm
    sudo yum install --nogpgcheck rstudio-server-rhel-0.99.491-x86_64.rpm
     
    
    #4. install "shinny" package from cran
    sudo su - -c "R -e \"install.packages('shiny', repos='http://cran.rstudio.com/')\""
    
    
    #5. Download shiny RPM
    
    wget https://download3.rstudio.org/centos5.9/x86_64/shiny-server-1.4.1.759-rh5-x86_64.rpm
    
    #6. Install shiny RPM :)
    
    sudo yum install --nogpgcheck shiny-server-1.4.1.759-rh5-x86_64.rpm
    
    
    #7. review shiny server status
    sudo systemctl status shiny-server
    
    #8 Create a group for shinny server config and server running
    
    sudo groupadd shiny-apps
    sudo usermod -aG shiny-apps crodriguez
    sudo usermod -aG shiny-apps shiny
    
    #change server directory owner and permissions
    #replace crodriguez for your user
    
    cd /srv/
    sudo  chown --recursive  crodriguez:shiny-apps shiny-server/
    #Change mode to all stuff in the dir
    cd shiny-server/
    sudo chmod g+w .
    sudo chmod g+s .
    
    #we're really changing permissions over some hard links. ( ls -l ) 
    
    lrwxrwxrwx 1 crodriguez shiny-apps 38 Jan 28 15:55 index.html -> /opt/shiny-server/samples/welcome.html
    lrwxrwxrwx 1 crodriguez shiny-apps 37 Jan 28 15:55 sample-apps -> /opt/shiny-server/samples/sample-apps
    
    #TODO everythin in  /srv/shiny-server will be served as an app
    
    #9. Config NGINX so we can use it as proxy for Rstudio and NGINX. For pretty URLs you know ;)
    
    sudo mkdir /etc/nginx/sites-available
    sudo mkdir /etc/nginx/sites-enabled
    
    
    #9.1 Include links to recently created config dirs in nginx.conf.
    # inside http{} block and before server{} block include the two lines *.conf and add some memory for server names domain 
    
    sudo vi /etc/nginx/nginx.conf
    
    include /etc/nginx/sites-enabled/*.conf;
    server_names_hash_bucket_size 64;
    
    
    
    #9.2 Add virtual locations on http{} block
    
            location /shiny/ {
               proxy_pass http://127.0.0.1:3838/;
               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "upgrade";
            }
    
            location /rstudio/{
               proxy_pass http://127.0.0.1:8787/;
               proxy_http_version 1.1;
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "upgrade";
            }
    
    #10. restart Nginx
     sudo systemctl restart  nginx
    
    
    #10.1 Install Rmarkdown
    # Two easy steps
    
    #rebuild yum cache ( just because we haven't done this yet )
    sudo yum makecache fast
    
    #install libcurl / libcurl-devel y openssl openssl-devel
    sudo yum -y install libcurl libcurl-devel openssl openssl-devel
    
    #install R devtools
     sudo su - -c "R -e \"install.packages('devtools', repos='http://cran.rstudio.com/')\""
    
    #install rmarkdown
     sudo su - -c "R -e \"install.packages('rmarkdown', repos='http://cran.rstudio.com/')\""
    
    
    #11. If everything it's ok
    
    #verify installation
    whereis rstudio-server
    sudo rstudio-server verify-installation
    sudo rstudio-server status
    
    
    #Bonus. with devtools and git you can install packages from github but "git" its allways a nice thing to have  :)
    sudo yum install git
    
    
    
    
    This is how it should look

    1. NGINX

    2. Rstudio

    3. Shinny

    No comments: