Linux生信分析平台搭建教程——基于Ubuntu系统的实践

前言

生信分析在linux下的体验会更好,因此本教程旨在分享如何在Ubuntu环境下配置好Rstudio和Python的Jupyter lab。Linux系统的获取可以通过在windows端安装wsl或者购买云服务器。下面先以云服务器为例子(WSL涉及系统安装和ssh配置对linux新手不友好):

当我们购买了云服务器(华为、阿里、腾讯)之后,可以登陆服务器提供商的网站查看IP地址及用户和密码,下载FinalShell或MobaXterm工具进行远程登录服务器进行配置及使用。
FinalShell打开后显示界面如下,点击中间的文件夹按钮, 打开连接管理器。

编辑后保存,在连接管理器中点击你想登录的服务器即可转到命令台输入用户名及密码登录,注意密码不会以明文显示,尽管输入即可。

sudo useradd 用户名
sudo usermod -aG sudo 用户名 #设置该用户为管理员
su 用户名
passwd #回车之后设置密码

宝塔安装

Linux服务器管理如果仅通过ssh连接shell在命令行下进行对新手来说并不友好,劝退新手。面对这种困境,可以安装可视化面板工具如宝塔面板、1Panel面板来帮助运维。
可以Bing/百度搜索宝塔进入其官网,复制一键安装命令:

wget -O install.sh https://download.bt.cn/install/install_lts.sh
sudo bash ./install.sh

利用FinalShell或MobaXterm通过开启SSH服务的端口(默认是22)登陆服务器,黏贴上面的一键安装命令,按回车执行:

安装与部署 RStudio Server

安装必要依赖

确保安装了必要的依赖:

sudo apt update
sudo apt install -y gdebi-core libssl-dev

安装 R 和 RStudio Server

  1. 安装 R:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo apt install --no-install-recommends software-properties-common dirmngr
sudo add-apt-repository "deb [arch=amd64] https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
sudo apt update
sudo apt install r-base r-base-dev
  1. 下载并安装 RStudio Server:
wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2024.09.1-394-amd64.deb
sudo gdebi rstudio-server-2024.09.1-394-amd64.deb
  1. 启动 RStudio Server 服务:
sudo systemctl start rstudio-server
sudo systemctl enable rstudio-server

RStudio Server 将在 http://your_server_ip:8787 提供服务,用户可以用他们的 Linux 用户名和密码登录。

if(!require('BiocManager')) install.packages('BiocManager',update = F,ask = F)

#### 设置CRAN的镜像源为北外
options(repos = c(CRAN = 'https://mirrors.bfsu.edu.cn/CRAN/'))

#### 设置Bioconductor的镜像源为北外
options(BioC_mirror='https://mirrors.bfsu.edu.cn/bioconductor')
cran_packages=c('magrittr',
                'dplyr',
                'tibble',
                'ggpubr',
                'stringr',
                'reshape2',
                'psych',
                'limma',
                'circlize',
                'grid',
                'fmsb',
                'survival',
                'survminer',
                'forestplot',
                'pROC',
                'ggplot2',
                'patchwork',
                'ggsci',
                'RColorBrewer',
                'pheatmap')

Biocductor_packages=c('edgeR',
                      'org.Hs.eg.db',
                      'clusterProfiler',
                      'enrichplot',
                      'ComplexHeatmap',
                      'GSVA')
Biocductor_packages=c(
                      'org.Mm.eg.db',
                      'Seurat',
                      'monocle'
                  )
# install packages in CRAN
for (pkg in cran_packages){
  if (!require(pkg,character.only=T)){
    install.packages(pkg,ask = F,update = F)
    require(pkg,character.only=T) 
  }
}

# install packages in Biocductor
for (pkg in Biocductor_packages){
  if (!require(pkg,character.only=T)) {
    BiocManager::install(pkg,ask = F,update = F)
    require(pkg,character.only=T) 
  }
}

安装Python——以Mamba为虚拟环境管理器

下载并安装 Mambaforge
  1. 下载 Mambaforge:
wget https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh
  1. 安装 Mambaforge:
bash Mambaforge-Linux-x86_64.sh -b -p /opt/mambaforge

● -b 选项表示静默安装
● -p /opt/mambaforge 指定安装路径

  1. 创建共享虚拟环境
    你可以为所有用户创建一个共享虚拟环境,供所有人使用:
conda create -n myenv python=3.9 -y

然后激活该环境:

source ~/bin/mambaforge/bin/activate myenv
  1. 部署 Jupyter Lab
    4.1 安装 Jupyter Lab
    在已经创建的共享虚拟环境中安装 Jupyter Lab:
conda install -n shared_env jupyterlab -y

4.2 配置 Jupyter Lab 服务
为了让 Jupyter Lab 能作为一个服务运行,我们需要创建一个 systemd 服务文件。
创建 Jupyter Lab 服务文件 /etc/systemd/system/jupyterlab.service:

sudo nano /etc/systemd/system/jupyterlab.service

在jupyterlab.service文件中添加以下内容:

[Unit]
Description=Jupyter Lab

[Service]
Type=simple
PIDFile=/run/jupyterlab.pid
ExecStart=/home/liuyuchen/.conda/envs/scenv/bin/jupyter-lab --no-browser --port=8888 --ip=0.0.0.0
User=nobody
Group=nogroup
WorkingDirectory=/opt/mambaforge/envs/shared_env
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

启动并启用 Jupyter Lab 服务:

sudo systemctl daemon-reload
sudo systemctl start jupyterlab
sudo systemctl enable jupyterlab

Jupyter Lab 将在 http://your_server_ip:8888 提供服务。可以为每个用户生成单独的访问 token 来控制访问权限。

端口与防火墙设置
确保开放 RStudio Server 和 Jupyter Lab 所需的端口:

sudo ufw allow 8787
sudo ufw allow 8888
sudo ufw enable

总结

● 将 Mambaforge 的路径和共享虚拟环境添加到 /etc/profile.d/mambaforge.sh,确保每个用户登录时自动加载。
● RStudio Server 运行在端口 8787,Jupyter Lab 运行在端口 8888。
● 如果个人使用wsl部署上述服务,可以使用frp进行内网穿透,通过具有外网IP的云服务器访问内部资源:

以下是使用FRP进行内网穿透的技术重点

在云服务器和本地wsl虚拟机都下载FRP软件

 wget https://github.com/fatedier/frp/releases/download/v0.60.0/frp_0.60.0_linux_amd64.tar.gz

然后分别在云服务器和wsl虚拟机进行以下配置:
服务器端在解压缩文件夹中新建frps.toml文件

假设服务器端IP: IPXXX
编辑服务器配置文件内容(frps.toml):
bindPort = 8024 # 可以自定义,记得在安全组打开访问权限
vhostHTTPPort = 28416 # 可以自定义
vhostHTTPSPort = 1443 # 可以自定义

保存文件后,

 nohup ./frps -c frps.toml &  #启动服务器端 

在WSL虚拟机中新建客户端配置文件(frpc.toml):

serverAddr = "IPXXX" # 填写服务器的ip地址
serverPort = 8024  # 上述的bindPort


[[proxies]]
# 用于ssh链接
name = 'ssh' 
type = 'tcp'
localIP = '127.0.0.1'
localPort = 22
remotePort = 6000 # 可以自定义


[[proxies]]
name = 'rstudio'
type = 'http'
localIP = '127.0.0.1'
localPort = 8787
customDomains = ['119.45.13.149']

保存文件后在,客户端用:

nohup ./frpc -c frpc.toml & #启动客户端

在浏览器中输入IPXXX:6000进行ssh访问,在浏览器中输入 公网IPXXX:8787访问Rstudio。

写在最后

今天的分享先到这里,后续还会更新更多生信相关技能,欢迎关注我的博客,B站和微信公众号。

B站:

小路Espiteme

微信公众号(医嘉SCI):

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值