-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path09-server-admin.Rmd
307 lines (210 loc) · 11.3 KB
/
09-server-admin.Rmd
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# Server admin
```{r , include=FALSE}
knitr::opts_chunk$set(
message = FALSE,
warning = FALSE
)
```
## RStudio (argonaut and argosafe)
### Scheduled script (CRON)
Most scheduling is best done using RStudio Connect (argoshare), see below.
For certain applications, it may be desirable to do this on argonaut or argosafe.
For instance, if a scheduled job is required to be done in a trusted research environment.
This can be done by adding a cron job.
The following will execute the `R` script at 7AM everyday. A log file is written within the project.
```{bash eval=FALSE, include=TRUE}
crontab -e
# Add line (using e.g. nano editor)
00 07 * * * Rscript /home/user/project/script.R &>> /home/user/project/cron.log
```
The cronjob does not load the user environment.
If you are using environment variables in R, e.g. API tokens, then these can be loaded within the R script.
Include this at the top of your script.
```{r eval=FALSE, include=TRUE}
# Log
print(paste("Start:", Sys.time()))
# Environment variables
readRenviron("/home/user/.Renviron")
```
## RStudio Connect: argoshare
### Delete user
```{bash eval=FALSE, include=TRUE}
# Delete user from argoshare
## Connect must be stopped first
sudo systemctl stop rstudio-connect
## List all users
sudo /opt/rstudio-connect/bin/usermanager list
## Find guid for the user you want to delete, looks like below.
## Delete user - need to say y/n
sudo /opt/rstudio-connect/bin/usermanager delete --users --user-guid 46cb5adb-4036-451f-9f08-0da3197dbc6c
## Restart connect
sudo systemctl start rstudio-connect
```
## Shiny server opensource setup - UoE Eleanor
This guide assumes you’re already on the University of Edinburgh local network. If you’re working from home, you’ll first need to connect the UoE VPN. It also assumes you already have storage allocated to your project on Eleanor, and that your own computer is either Linux-based or a Mac.
### Setting up Ubuntu Instance
- Log into the Openstack dashboard on Horizon - https://horizon.ecdf.ed.ac.uk/dashboard/
- Click on Instances and launch a new instance (Ubuntu 18.4, t1.small (free tier))
- Click on key pairs and create an SSH keypair, for securely logging on to the new instance.
- Save the private key to a secure place on your computer (~/.ssh is the usual spot for these things). Change the files permissions to make it secure. Eleanor will not let you SSH without this step. In a local terminal, type:
```{bash eval=FALSE, include=TRUE}
chmod 400 PATH-TO-PRIVATE-KEY
```
- Click on Network > Floating IPs and create a new floating IP. Associate it with your new instance. Make a note of your floating IP – this is the address you’ll use to access your instance from now on.
- Click on Security Groups and create a new security group. Add new rules to this group, allowing TCP ingress and egress on the following ports: 80 (HTTP), 22 (SSH), 3838 (Shiny Server).
- Log into your instance using SSH. In a local terminal type:
```{bash eval=FALSE, include=TRUE}
ssh -i PATH-TO-PRIVATE-KEY ubuntu@YOUR-FLOATING-IP
#All being well, you’ll now have a terminal for your lovely new Ububtu instance.
#Get your instance up-to-date. Run:
sudo apt-get update && sudo apt-get upgrade
```
### Install Nginx
Nginx is the fast and light webserver that will host you Shiny code.
```{bash eval=FALSE, include=TRUE}
#Install nginx
sudo apt-get -y install nginx
```
- In a browser, enter your floating IP address. All being well, you should now get a "Welcome to Nginx" page.
### Installing R
Installing the latest version of R isn’t as easy as apt-get R. Sources and keys need to be added to the source.list file, then R can be installed (the latest version). The first command sets the sources to the RStudio mirror, then grabs keys to authenticate the installation. Enter the following commands in order:
```{bash eval=FALSE, include=TRUE}
sudo sh -c 'echo "deb http://cran.rstudio.com/bin/linux/ubuntu bionic-cran35/" >> /etc/apt/sources.list'
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo apt-get update
sudo apt-get -y install r-base
#Now load R
R
#Enter the following in the R console
sessionInfo()
quit()
```
### Installing Shiny Server
There are a few external libraries that are required to use Shiny Server. Let’s install those, the R package shiny then move on to installing Shiny Server itself.
```{bash eval=FALSE, include=TRUE}
sudo apt-get -y install gdebi-core
sudo su - -c "R -e \"install.packages('shiny', repos='http://cran.rstudio.com/')\""
wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.13.944-amd64.deb
sudo gdebi shiny-server-1.5.13.944-amd64.deb
```
Back in your browser, go to: YOUR-FLOATING-IP:3838
All being well, you’ll get a Shiny Server test page.
### Installing other Linux packages
You’ve got base-R installed and running now, but it’s likely your Shiny app will have other unmet dependencies. Here are a few common packages you’ll probably need, and the command needed to install them. Depending on your app, there may be others.
```{bash eval=FALSE, include=TRUE}
sudo apt-get install -y \
pandoc \
pandoc-citeproc \
libssl-dev \
libcurl4-gnutls-dev \
libcairo2-dev \
libgdal-dev \
libgeos-dev \
libproj-dev \
libxml2-dev \
libxt-dev \
libv8-dev \
git
```
### Installing R packages
You’ll probably also need a bunch of extra libraries in R (we’ve already installed Shiny, but that’s it). You install these from within R, rather than via apt-get. This is the syntax for installing R packages on the server – just replace the package names with the ones your app requires.
```{bash eval=FALSE, include=TRUE}
sudo su - -c "R -e \"install.packages(c('rmarkdown', 'ggplot2', 'dplyr', 'sp', 'rgdal', 'rgeos'), repos='http://cran.rstudio.com/')\""
```
NB: Installing R packages can take a long time. As we’re only using the small, free instance in this guide, you may find it grinds to a halt completely when trying to compile large libraries like seurat. This probably means you’ve run out of system memory. If that happens - see Setting up a swap file at the end of this guide.
### Loading your app
Assuming you’ve developed your app on a local R-Studio server, getting it onto its new home can be surprisingly tricky. I’ve found by far the easiest way is to use an SFTP client, such as Cyberduck, giving it the username and key pair you’ve already set up and just sending the files straight into your home directory.
Once you’ve transferred them, you’ll need to move them, including subdirectories, into /srv/shiny-server/.
Again, in your browser, go to YOUR-FLOATING-IP:3838. You’ll either see your working app, or an error page listing missing dependencies. If it’s the latter, follow the instructions above for installing R packages.
If you’re not getting any useful error messages on the site, check out the app-specific log files in /var/log/shiny-server/
### Make your app the default index page
You probably don’t want visitors to your site to specify that they want port 3838, so now we’re going to tell nginx to send visitors directly to your app.
First, we’re going to edit the nginx config file.
```{bash eval=FALSE, include=TRUE}
sudo nano /etc/nginx/sites-available/default
# Comment out (#) any other active server config lines, and add this…
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:3838/;
proxy_redirect http://127.0.0.1:3838/ $scheme://$host/;
# note, the following 3 lines added 2016-07-11, see updates section
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
```
Next, we’re going to update the Shiny server config file
```{bash eval=FALSE, include=TRUE}
sudo nano /etc/shiny-server/shiny-server.conf
#Add the following (if it’s not already there, which it may well be):
server{
listen 3838 127.0.0.1;
location / {
site_dir /srv/shiny-server;
log_dir /var/log/shiny-server;
directory_index on;
}
}
```
Go to your floating IP in the browser, without the 3838, and all should be well.
### Getting a user-friendly domain name
If you want to share your app with the outside world, you’ll need to open it up beyond the UoE network and, preferably, give it a catchy URL.
Making your site public: Back in OpenStack, under the Network menu, click on Floating IP and disassociate your floating IP.
Now go up to Router and click clear gateway. Click Set Gateway and select Floating Network Public.
Back in Floating IPs, click Associate and select the external IP address (probably starting 129.x.x.x). Under port, select your instance.
Enter your external IP address into your browser, and your website should appear. It is now available outside the University network.
Setting up your domain name:
Your first step here is obviously to buy a domain name from one of the many providers on the internet.Once you have this, use your domain provider’s dashboard to edit DNS settings. You’re looking to change the ANAME or CNAME settings – delete what’s already there and set up a new ANAME record, with the public IP address of your site.
### Appendix 1 – Password protecting your site
You may want to add a basic password to your site. This is easily accomplished (though there are more robust password protection mechanisms out there, if you’re dealing with potentially sensitive data).
First, make sure you have Apache2-utils installed:
```{bash eval=FALSE, include=TRUE}
#First, make sure you have Apache2-utils installed:
sudo apt-get install apache2-utils
#Now, stop nginx and shiny:
sudo service nginx stop
sudo stop shiny-server
#Next, you’ll need to go back into that Nginx config file, with
sudo nano /etc/nginx/sites-available/default
# Add the following two lines in the "location" section
location / {
proxy_pass http://127.0.0.1:3838/;
proxy_redirect http://127.0.0.1:3838/ $scheme://$host/;
auth_basic "Username and Password are required";
auth_basic_user_file /etc/nginx/.htpasswd;
}
```
Once that’s done, you’ll need to edit Shiny Server’s conf file so it only serves to loaclhost. Otherwise users would be able to creep around your authentication by going to port 3838.
```{bash eval=FALSE, include=TRUE}
sudo nano /etc/shiny-server/shiny-server.conf
#Copy and paste the below to your shiny-server.conf.
server{
listen 3838 127.0.0.1;
location / {
site_dir /srv/shiny-server;
log_dir /var/log/shiny-server;
directory_index on;
}
}
```
You’re now ready to create the username and password visitors must enter to view your site.
```{bash eval=FALSE, include=TRUE}
cd /etc/nginx
sudo htpasswd -c /etc/nginx/.htpasswd USERNAME
#Restart Nginx and Shiny and you’re good to go.
sudo service nginx start
sudo start shiny-server
```
### APPENDIX 2 – Setting up a swap file
If your instance grinds to a halt while compiling R packages, you’ve most likely run out of memory and will need to set up a swap file. Just enter the following commands:
```{bash eval=FALSE, include=TRUE}
sudo fallocate -l 2G /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
#Finally, make sure everything as it should be by running:
sudo free -h
```