服务器de novo部署shiny app到网页上

为了方便别人快速使用我们文章的数据,先前给自己的project开发了一个shiny app,现在需要把这个APP部署到网页上,因此记录下整个部署过程。使用的是个空白的服务器,centos系统。

第一步 安装R(因为我这个shiny app是基于R来的,因此R是必须的)安装方法我喜欢编译安装,当然也有很多人喜欢用conda安装,这个看个人偏好。

(1) 下载与解压
官网 https://cran.rstudio.com/src/base/R-4/
也可以自己从旧服务器copy过来,使用scp命令,注意如果端口不是默认的22的话,需要指定端口,使用参数 -P 即可

$ cd 
$ mkdir soft && cd soft
$ export R_VERSION=4.2.0

$ wget https://cran.rstudio.com/src/base/R-4/R-4.2.0.tar.gz
$ tar -xzvf R-${R_VERSION}.tar.gz
$ cd R-${R_VERSION}

(2) Build and install R

## 升级系统,补充一些包
$ tmux   #升级时间较长,防止意外中断
$ sudo yum install epel-release 
$ sudo yum update 
### 565M, 286个包,可能需要几个小时,取决于网速。中间 GPG key 选 y;  16:47--> 17:05 
### sudo shutdown -r now  #能跳过重启

## 安装build的依赖
$ sudo yum-builddep R 
## 188M, 281个包,耐心等待 

## 配置,主要是--prefix 指定安装位置
$ sudo mkdir -p /opt/R/
$ ./configure \
--prefix=/opt/R/${R_VERSION} \
--enable-memory-profiling \
--enable-R-shlib \
--with-blas \
--with-lapack

## 最后报警告,可以忽略,没啥影响。
## configure: WARNING: neither inconsolata.sty nor zi4.sty found: PDF vignettes and package manuals will not be rendered optimally

## 编译 | 可选多线程编译,提高速度
$ make ## 可多线程编译 make -j 64 

## 安装
$ sudo make install

## 清理残余
$ make clean

(3) checking

$ ls /opt/R/${R_VERSION}/
## bin lib64 share

$ /opt/R/${R_VERSION}/bin/R --version
R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

(4) Create a symlink to R 添加软链接

$ sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R
$ sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript

(5) 再次检查当前R

## 重新登录终端
$ ssh xxx

$ which R
/usr/local/bin/R

$ R --version
R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics"
## 已经是R最新版了

$ whereis R
R: /usr/local/bin/R

至此,R安装完成。这部分参考了我师兄先前写的一个推文R 安装

第二步 安装Shiny server

(1) 安装可参考shiny 官网 https://www.rstudio.com/products/shiny/download-server/redhat-centos/
我的是centos,如果是Ubuntu的,在官网选择Ubuntu即可。

## You’ll also need to install the Shiny R package before installing Shiny Server:
$ sudo su - \
-c "R -e \"install.packages('shiny', repos='https://cran.rstudio.com/')\""

## Download and Install shiny-server
$ wget https://download3.rstudio.org/centos7/x86_64/shiny-server-1.5.19.995-x86_64.rpm
$ sudo yum install --nogpgcheck shiny-server-1.5.19.995-x86_64.rpm

安装好shiny-server以后,会自动新建一个shiny的用户
(2) 需要对shiny用户做一些简单的设置

# 先切换到root模式
$ sudo -i 
# 然后设定密码
$ sudo passwd shiny 

# 添加组
$ sudo groupadd shiny-apps
$ sudo usermod -aG shiny-apps shiny
$ sudo usermod -aG shiny-apps webdep # 也允许主用户可以访问,这个webdep需要根据的用户名做适当修改

# change owner:将指定文件的拥有者改为指定的用户或组
$ sudo chown -R shiny:shiny-apps /srv/shiny-server
$ sudo chmod g+w /srv/shiny-server
$ sudo chmod g+s /srv/shiny-server
# g - the permissions that other users in the file's group have for it
# w - set user or group ID have right to write
# s - set user or group ID have right to execute

# 设置完成,查看【/srv/shiny-server这个目录就是存放各种shiny app的地方啦】
$ ls -l /srv/shiny-server
# total 0
# lrwxrwxrwx 1 shiny shiny-apps 38 Oct 11 19:35 index.html -> /opt/shiny-server/samples/welcome.html
# lrwxrwxrwx 1 shiny shiny-apps 37 Oct 11 19:35 sample-apps -> /opt/shiny-server/samples/sample-apps

# 之后我们都用shiny这个用户安装R包
$ su - shiny

接下来,你可以选择安装Rstudio,当然这个不是必要的,安装可参考官网https://www.rstudio.com/products/rstudio/download-server/redhat-centos/

## 非常简单
$ wget https://download2.rstudio.org/server/centos7/x86_64/rstudio-server-rhel-2022.07.2-576-x86_64.rpm
$ sudo yum install rstudio-server-rhel-2022.07.2-576-x86_64.rpm
第三步 安装shiny app 需要的R包及对R进行配置

Note 首先切换到shiny用户,然后再操作!
(1) 配置.Rprofile

# 命令
$ vi .Rprofile
# 然后输入下面👇
Sys.setenv(LANG="en_US.UTF-8")
options=(repo = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")
# 最后保存退出

(2) 配置.Renviron

$ mkdir /home/shiny/R_Library

# 命令
$ vi .Renviron
# 然后输入下面👇
R_LIBS=/home/shiny/R_Library
# 最后保存退出

(3) 因为是要配置shiny网页,所以接下来我们需要以shiny用户登陆

# 之后我们都用shiny这个用户安装R包
$ su - shiny

####################
# 下面👇均在R中进行  #
####################
$ R 
# 配置镜像 (前面配置过.Rprofile的话,可以跳过)
local({
  r <- getOption( "repos" ); 
  r[ "CRAN" ] <- "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"; 
  options( repos = r )
  BioC <- getOption( "BioC_mirror" ); 
  BioC[ "BioC_mirror" ] <- "https://mirrors.ustc.edu.cn/bioc/"; 
  options( BioC_mirror = BioC )
})
# 安装shiny需要的包 
## 定义个安装包的函数1
getPackage <- function(pkg, check = TRUE, load = TRUE, silent = FALSE, github = NULL) {
  if(check) {
    if(!suppressMessages(suppressWarnings(require(pkg, character.only = TRUE, quietly = TRUE)))) {
      if(is.null(github)){
        try(install.packages(pkg), silent = TRUE)
      }
      else{
        try(remotes::install_github(github))
      }
    }
  }
  if(load) suppressPackageStartupMessages(library(pkg, character.only = TRUE, quietly = TRUE))
  if(load & !silent) message("Loaded ", pkg)
}
packages <- c("dplyr","psych","Matrix","ggplot2", "png", "ggedit", "cowplot", "ggbeeswarm", "ggrepel", "corrplot", "grid", "RColorBrewer", "dplyr", "egg", "gtable", "scales", "reshape2")
lapply(packages, getPackage)

## 定义个安装包的函数2
all_pkgs <- c("BiocVersion","limma")
if (!requireNamespace("BiocManager", quietly = TRUE)){
  install.packages("BiocManager")
}
CRANpackages <- available.packages() # 全部的CRAN包
lapply(all_pkgs, 
        function( p ){
          if( !require( p, character.only = T ) ){
            if( p %in% rownames( CRANpackages) ){
              install.packages( p )
            }else{
              BiocManager::install( p, suppressUpdates = FALSE, ask = FALSE)
            }
          }
        }
)

(4) 开放端口及启动shiny app
shiny默认设置3838端口,rstudio默认是8787 端口

## 开放3838端口
$ sudo iptables -I INPUT -p tcp --dport 3838 -j ACCEPT

#保存修改到数据表
$ sudo service iptables save

### 如果保存报错,应该是防火墙拦截了 
### 解决方法:关掉 firewalld,并打开 iptables-services 服务。 
$ sudo systemctl stop firewalld     #关闭防火墙
$ sudo systemctl disable firewalld.service #禁止firewall开机启动

$ sudo yum install iptables-services   #安装或更新服务
$ sudo systemctl enable iptables        #开机启动iptables
$ sudo systemctl start iptables        #打开iptables
## 开放3838端口
$ sudo iptables -I INPUT -p tcp --dport 3838 -j ACCEPT
$ sudo service iptables save        #保存修改

### 重启iptables服务:
$ sudo service iptables restart
## 执行完毕之后 /etc/sysconfig/iptables 文件就有了

(5) 把shiny项目放在自己的家目录
5.1 首先把项目放在自己的家目录

# 方案1:项目在github上,可以直接git clone
$ cd /home/shiny
$ git clone 你的项目地址

# 方案2:本地上传
$ scp -r /本地目录/app shiny@你的IP地址:/home/shiny
# 然后在服务器上设置这个目录的权限
$ chmod 755 /home/shiny/app

5.2 然后在 /srv/shiny-server 中加入项目的快捷方式

$ sudo ln -s /home/shiny/app /srv/shiny-server
# 同时更改项目属主是shiny
$ sudo chown -R shiny:shiny app/ ## app是你自己的app名字,需要适当修改

5.3 之后重启

$ sudo systemctl restart shiny-server.service

5.4 最后输入:http://你的IP:3838/你的shiny项目名称

常见的报错
如果无法运行shiny app,去 /var/log/shiny-server/ 目录下看看log文件中的报错信息,一般是由于缺少包,解决后,立刻就能恢复运行。

(6) 可能的优化
6.1 删掉原来的shiny app
默认情况下,直接输入IP地址会显示自带的shiny app模板:

默认界面

只要测试shiny可以用了,就可以删掉它了:

$ sudo rm -rf /srv/shiny-server/sample-apps

6.2 服务器总是自动断线

$ sudo vim /etc/ssh/sshd_config
# 找到下面这两行
#ClientAliveInterval 0
#ClientAliveCountMax 3

# 然后去掉注释,并且修改
ClientAliveInterval 30 #意思是:服务端每隔多少秒向客户端发送一个信号
ClientAliveCountMax 86400 #意思是:客户端多少次没有相应,服务器自动断掉连接

# 最后重启ssh
$ service sshd restart

6.3 Shiny server 常用操作

启动:
$ sudo systemctl start shiny-server
$ sudo systemctl enable shiny-server

重启: 
$ sudo systemctl restart shiny-server.service

查看日志:
$ cat /var/log/shiny-server.log

这部分内容参考了刘小泽老师的推文

至此,服务器重头部署shiny app到网页的工作到此完成。由于我自己的是学校的IP,所以目前部署到的是内网上,目前在对接信息中心,到时候开放外网的IP的就可以让每个人都可以访问了,下面是自己的shiny app,等文章放到biorxiv就一起公开了。

自己的APP

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容