如何在AWS上设置RStudio服务器以使用SSL运行?

10

我想在AWS实例上运行RStudio服务器,并通过SSL加密连接访问该服务器。

如何设置呢?

1个回答

16

使用Ubuntu作为操作系统,在安全组中添加一个入站连接端口为443的HTTPS连接,同时还要通过端口22进行SSH连接。您的实例还必须具有公共DNS。

一旦机器启动并运行,使用SSH登录。

按照提供的这里的说明安装RStudio服务器,执行以下命令:

sudo apt-get update
sudo apt-get install r-base
sudo apt-get install gdebi-core
wget https://download2.rstudio.org/rstudio-server-1.1.463-amd64.deb 
sudo gdebi rstudio-server-1.1.463-amd64.deb

注意:RStudio服务器的.deb文件的确切名称会随着新版本的发布而更改。
我们将遵循此处此处提供的说明来配置nginx Web服务器,将RStudio服务器反向代理到Web浏览器并使用SSL。要安装nginx,请执行以下操作:
sudo apt-get install nginx

创建SSL证书:
sudo mkdir /etc/nginx/ssl
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

后面的命令会创建两个文件:一个密钥文件和一个SSL证书文件。

/etc/nginx/conf.d/rstudio.conf下创建一个文件并编辑它(注意:您需要使用sudo nano /etc/nginx/conf.d/rstudio.conf或类似的方式进行编辑),添加以下内容:

server {
        listen 80;
        listen [::]:80;

        listen 443 ssl;
        ssl_certificate /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key /etc/nginx/ssl/nginx.key;

        server_name ec2-11-22-333-444.us-west-2.compute.amazonaws.com;

        location / {
             proxy_pass http://localhost:8787/;
             proxy_redirect http://localhost:8787/ $scheme://$host/;
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection $connection_upgrade;
             proxy_read_timeout 20d;
        }
}

您需要将server_name字段替换为AWS实例的公共DNS IP。

此外,您需要编辑/etc/nginx/nginx.conf文件,在http块中添加以下行:

http {
       # All you other settings up here... 

       server_names_hash_bucket_size 128;

       map $http_upgrade $connection_upgrade {
              default upgrade;
              ''      close;
                  }

}

server_names_hash_bucket_size设置为128是重要的,原因在这里解释。

最后编辑您的/etc/rstudio/rserver.conf配置文件,添加以下行:

www-address=127.0.0.1

接下来为您的用户创建用户账户。例如:

sudo adduser arokem

现在您应该能够重新启动nginx和rstudio-server:

sudo rstudio-server restart
sudo systemctl restart nginx

将您的浏览器指向https://ec2-11-22-333-444.us-west-2.compute.amazonaws.com。您的浏览器可能会收到一个警告,表示它无法识别您的SSL证书。在这种情况下,可以安全地忽略此警告,并继续访问RStudio服务器登录窗口。使用刚创建的用户登录信息来访问RStudio。


因此,我认为使用“sudo nginx -t”进行nginx配置检查绝对有助于发现任何错误。而且,针对Ubuntu,服务器重启命令可以简化。我使用了“sudo service rstudio-server restart”和“sudo service nginx restart”。 - Nirmal
我建议设置 proxy_redirect http://localhost:8787/ $scheme://$host:$server_port/; 进行替换。 - Marcel Boldt
你有没有关于在使用docker本地运行rstudio-server时使其工作的任何提示?我尝试了按照步骤输入我的计算机IP作为服务器名称,但是没有成功。 - user3084100
1
截至2022年6月,对于非Docker实例的RStudio-server,这仍然适用于Marcel Boldt的建议。 - mikemtnbikes
这对我非常有用(谢谢!),但是为了使此方法适用于机器上的现有用户,我必须在最后执行两个额外的步骤: sudo cp /etc/pam.d/login /etc/pam.d/rstudio 然后使用 passwd 设置用户密码。 - dyoung

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接