Shiny server gives "Page not found" selectively to a specific parent directory

I also have the same trouble.
As a summary my problem is:

  1. The shiny app runs successfully from Run app button on rstudio (on the server)
  2. Gives "Page not Found" when trying from browser ip-server:3838/customdir/shiny
  3. When I copy the app.R file from the ~/customdir/shiny to ~/ShinyApps/shiny the link ip-server:3838/ShinyApps/shiny starts working,

Have you set proper permissions for shiny user? if so you have to assign ownership of the folder to shiny-apps group

sudo chown -R root:shiny-apps /srv/shiny-server/customdir/shiny

The directory privileges are identical to the one where the app.R works:

rstudio@ip-172-31-30-19:~/nlp$ ls -ld shiny
drwxr-xr-x 2 rstudio rstudio 4096 Dec  9 13:38 shiny

And in both places the owner of the directory is the user: rstudio

shiny apps run under shiny user not rstudio user that is the problem

You can reset your shiny permissions with this

sudo groupadd shiny-apps
sudo usermod -aG shiny-apps #your-user#
sudo usermod -aG shiny-apps shiny
cd /srv/shiny-server
sudo chown -R #your-user#:shiny-apps .
sudo chmod g+w .
sudo chmod g+s .

Replace #your-user# with your actual user name

Sorry, I ran all the above commands successfully (the last one had a . missing and I added that). But still I get "Not Found" error on the URL. And the other URL is still working. Pasting all commands executed with a periodic ls -l. I have also found one peculiar thing: the app.R runs in the parent dir while fails inside any subdir of the main dir nlp.

shiny apps run under shiny user not rstudio user that is the problem

You can reset your shiny permissions with this

sudo groupadd shiny-apps
sudo usermod -aG shiny-apps #your-user#
sudo usermod -aG shiny-apps shiny
cd /srv/shiny-server
sudo chown -R #your-user#:shiny-apps .
sudo chmod g+w .
sudo chmod g+s

Replace #your-user# with your actual user name
[/quote]


studio@ip-172-31-30-19:~/nlp$ sudo usermod -aG shiny-apps shiny
rstudio@ip-172-31-30-19:~/nlp$ cd /srv/shiny-server
rstudio@ip-172-31-30-19:/srv/shiny-server$ ls -l
total 0
lrwxrwxrwx 1 root root 38 Oct 15  2017 index.html -> /opt/shiny-server/samples/welcome.html
lrwxrwxrwx 1 root root 37 Oct 15  2017 sample-apps -> /opt/shiny-server/samples/sample-apps
rstudio@ip-172-31-30-19:/srv/shiny-server$ sudo chown -R rstudio:shiny-apps .
rstudio@ip-172-31-30-19:/srv/shiny-server$ ls -l
total 0
lrwxrwxrwx 1 rstudio shiny-apps 38 Oct 15  2017 index.html -> /opt/shiny-server/samples/welcome.html
lrwxrwxrwx 1 rstudio shiny-apps 37 Oct 15  2017 sample-apps -> /opt/shiny-server/samples/sample-apps

rstudio@ip-172-31-30-19:/srv/shiny-server$ sudo chmod g+w .
rstudio@ip-172-31-30-19:/srv/shiny-server$ ls -l
total 0
lrwxrwxrwx 1 rstudio shiny-apps 38 Oct 15  2017 index.html -> /opt/shiny-server/samples/welcome.html
lrwxrwxrwx 1 rstudio shiny-apps 37 Oct 15  2017 sample-apps -> /opt/shiny-server/samples/sample-apps
rstudio@ip-172-31-30-19:/srv/shiny-server$ sudo chmod g+s
chmod: missing operand after ‘g+s’
Try 'chmod --help' for more information.
rstudio@ip-172-31-30-19:/srv/shiny-server$ sudo chmod g+s .
rstudio@ip-172-31-30-19:/srv/shiny-server$ ls -l
total 0
lrwxrwxrwx 1 rstudio shiny-apps 38 Oct 15  2017 index.html -> /opt/shiny-server/samples/welcome.html
lrwxrwxrwx 1 rstudio shiny-apps 37 Oct 15  2017 sample-apps -> /opt/shiny-server/samples/sample-apps

Update 1:

And by now I have experimented with all possible combinations of directory moving the app.R. And the results are very interesting.
The app.R runs in all combinations except when it is inside a subdirectory of nlp directory.
The ls -l is as follows.

rstudio@ip-172-31-30-19:~$ ls -l
total 24
drwxr-xr-x  6 rstudio rstudio 4096 Nov  1 02:59 embassy
drwxrwxr-x 18 rstudio rstudio 4096 Dec  9 12:52 nlp
drwxr-xr-x  4 rstudio rstudio 4096 Jun 16 16:26 nsdl
drwxr-xr-x  3 rstudio rstudio 4096 Dec  9 19:47 R
drwxr-xr-x  6 rstudio rstudio 4096 Dec  9 17:05 ShinyApps
-rw-r--r--  1 rstudio rstudio 2141 Oct 15  2017 Welcome.R

The app.R works from directory embassy, nlp, nsdl, R and ShinyApps. So 100% working at level -1 of depth.

The app.R works at level-2 depth in all other directories except the directory nlp.

This seems quite a weird problem.

this happens with your own apps or with sample apps? if you want to run sample apps you can try repairing the symlink with sudo ln -sfn /opt/shiny-server/samples/sample-apps /srv/shiny-server/sample-apps and setting ownership and permissions recursively to the actual folder /opt/shiny-server/samples/sample-apps

Are you running your apps from folders located outside /srv/shiny-server/? because none of your dirs has shiny-apps group as owner
port 3838 redirects to /srv/shiny-server/ sou if you are using dirs outside this folder you have to create symlinks for everyone of them.

hmm..yes indeed I am running from outside /srv/shiny-server/.
This I remember was a major reason the apps weren't working and then I got the solution to set run_as rstudio in some .profile file.

So which file do I set up symlinks for so that all sub directories in nlp are accessible from within the server dir. Seems a simple ln command should solve the problem.

You can try something like sudo ln -s ~/nlp /srv/shiny-server/nlp but also fix the ownership for the actual nlp folder sudo chown -R rstudio:shiny-apps ~/nlp

I also got the actual file where I had setup custom settings.

rstudio@ip-172-31-30-19:/opt/shiny-server$ cat /etc/shiny-server/shiny-server.conf
# adding this line to preserve logs
preserve_logs true;

# Instruct Shiny Server to run applications as the user "shiny"
run_as rstudio;

# Define a server that listens on port 3838
server {
  listen 3838;

  # Define a location at the base URL
  location / {

    # Host the directory of Shiny Apps stored in this directory
    site_dir /home/rstudio;

    # Log all Shiny output to files in this directory
    log_dir /var/log/shiny-server;

    # When a user visits the base URL rather than a particular application,
    # an index of the applications available in this directory will be shown.
    directory_index on;
  }
}

I have never changed those defaults, maybe your custom config is conflicting with nginx configuration

This chain of messages would definitely prove useful for anyone who has a similar problem as we discussed virtually all solutions. I have learnt my lesson but I want us to leave a decent trail for others.

Now here is the ls -la of the /srv/shiny-server dir.

To be safe I would request you to list out the permission changes (chmod) & symlinks that I need to setup. Are we going to revert everything to the default settings?

rstudio@ip-172-31-30-19:/srv/shiny-server$ ls -la
total 8
drwxrwsr-x 2 rstudio shiny-apps 4096 Dec  9 20:32 .
drwxr-xr-x 3 root    root       4096 Oct 15  2017 ..
lrwxrwxrwx 1 rstudio shiny-apps   38 Oct 15  2017 index.html -> /opt/shiny-server/samples/welcome.html
lrwxrwxrwx 1 root    shiny-apps   17 Dec  9 20:32 nlp -> /home/rstudio/nlp
lrwxrwxrwx 1 root    shiny-apps   37 Dec  9 20:00 sample-apps -> /opt/shiny-server/samples/sample-apps

I understand moving my files from ~/rstudio into the /srv/shiny-server directory is the best option but it would be nice if we can avoid large-scale file movements.

Relying on your expert advice. Many thanks.

Update: I am calling it a day today (it is 2 AM here) and will work on this with a fresh mind tomorrow. Hope in your time zone you have some time left for feeding me with the valuable next steps :slightly_smiling_face:

Will check your message after next 8 hours.

Thanks a lot.

I don´t know your particular setup or application, but I see no good reason for changing site_dir and run_as parameters, so I suggest you to change this parameters to default (i.e. run_as shiny; and site_dir /srv/shiny-server;), if you do so, remember that you have to install all r packages used in your shiny apps as superuser (e. g. sudo su - -c "R -e \"install.packages('shinyWidgets', repos='http://cran.rstudio.com/')\"")

If you don't want to move your files just keep making symlinks the same way you did with nlp folder, just remember to set correct ownership of files so shiny user can run it.

For reference these are my permissions in /srv/shiny-server folder (git stuff are there just because I develop on my laptop and do sudo git pull origin master later on the server, so just ignore those).

ubuntu@ip-172-31-X-XXX: /srv/shiny-server$ ls -la
total 40
drwxrwsrwx 7 ubuntu shiny-apps 4096 dic  9 13:03 .
drwxr-xr-x 3 root   root       4096 dic 14  2017 ..
drwxr-sr-x 2 andres shiny-apps 4096 dic  2 18:42 almacen
drwxrwsrwx 8 ubuntu shiny-apps 4096 dic  9 11:27 .git
-rwxrwxrwx 1 root   shiny-apps  526 dic  2 18:45 .gitignore
lrwxrwxrwx 1 root   shiny-apps   40 jun  5  2018 index.html -> C:/opt/shiny-server/samples/welcome.html
-rwxrwxrwx 1 ubuntu shiny-apps   14 dic 14  2017 README.md
drwxrwsrwx 4 andres shiny-apps 4096 oct  2 10:04 .Rproj.user
lrwxrwxrwx 1 root   shiny-apps   37 dic  9 13:03 sample-apps -> /opt/shiny-server/samples/sample-apps
-rwxrwxrwx 1 root   shiny-apps  205 dic  9 11:27 shiny-server.Rproj
drwxrwsrwx 2 root   shiny-apps 4096 ago 28 21:22 speed_test
drwxrwsrwx 2 root   shiny-apps 4096 dic  9 14:25 tienda

Hmm...comparing the effort needed to reinstall all the R packages with root privilege versus just adding a symlink, it is not justified to go with the former approach. My fear is, even after restoring all the defaults and reinstalling the R packages again, what if the problem persists?

This fear is substantiated by the fact, our settings here even if they aren't the default are indeed working perfectly fine in all other directories.

There is indeed something silly that I am missing and will be discovered suddenly in an aha moment. Let me move around the files a little more to narrow down to a reproducible problem.

We are not sure as of now if it is a permissions problem as we have run_as rstudio line forcing the shiny server to run as the user rstudio. We even have app.R working within nlp but it is not just running inside any sub-directory of nlp.

If you have any other sequence of diagnostics suggested I will be glad to follow those.

Thanks till now for struggling along with me.

All the things we have try so far were done with the premise that apps run as shiny user, have you tried to modify this commands for rstudio user?

sudo usermod -aG shiny-apps rstudio
cd /srv/shiny-server
sudo chown -R rstudio:shiny-apps .
sudo chmod g+w .
sudo chmod g+s .

As of now I have created a symlink to my subdirectory inside nlp and retained my run_as rstudio as well as site_dir /home/rstudio lines inside the /etc/shiny-server/shiny-server.conf file.
The question that still remains unanswered is why are symlinks not needed to other directories to run a shiny app from those directories when their set of permissions are identical to the one I had to create a symlink to make it work.

BTW @andresrcs - you are trying to help 2-3 people alternately on the same question but with different situations, so you might want to tag your answer with the name of the person or just create a sub-thread with a reply :slightly_smiling_face:

@Sanjmeh have you already added rstudio user to shiny-apps group?

No, but I am still able to work with the symlink to subdirectories that were not working and all other directories are already working. So as of now, I am not disturbing any permissions.

Fair enough, never the less, adding a user to a group doesn't take back his current permissions, and having to rely on this walk around solution it could complicate your life in the future.

Anyways, good luck with your job, and happy coding!

Oh yes these are harmless changes. So I did them. I reproduce the ls -l pre and post the changes.

rstudio@ip-172-31-30-19:/srv/shiny-server$ ls -l
total 0
lrwxrwxrwx 1 rstudio shiny-apps 26 Dec 10 13:48 annotate -> /home/rstudio/nlp/annotate
lrwxrwxrwx 1 rstudio shiny-apps 38 Oct 16  2017 index.html -> /opt/shiny-server/samples/welcome.html
lrwxrwxrwx 1 root    shiny-apps 17 Dec 10 02:02 nlp2 -> /home/rstudio/nlp
lrwxrwxrwx 1 root    shiny-apps 37 Dec 10 01:30 sample-apps -> /opt/shiny-server/samples/sample-apps
rstudio@ip-172-31-30-19:/srv/shiny-server$ sudo chown -R rstudio:shiny-apps .
rstudio@ip-172-31-30-19:/srv/shiny-server$ ls -l
total 0
lrwxrwxrwx 1 rstudio shiny-apps 26 Dec 10 13:48 annotate -> /home/rstudio/nlp/annotate
lrwxrwxrwx 1 rstudio shiny-apps 38 Oct 16  2017 index.html -> /opt/shiny-server/samples/welcome.html
lrwxrwxrwx 1 rstudio shiny-apps 17 Dec 10 02:02 nlp2 -> /home/rstudio/nlp
lrwxrwxrwx 1 rstudio shiny-apps 37 Dec 10 01:30 sample-apps -> /opt/shiny-server/samples/sample-apps

As you see the symlink created on Dec 10 13:48 is the one that I created on your advice on Dec 10 and that started the shinyapp inside the subdirectory. I also notice that the symlink was already with user: group setting of rstudio:shiny-apps. Hence it could be working because of that these setting already existing. Anyway once I am a little free I will try to reproduce the error with old settings and fix it again so that the problem resolution is well documented.

At the end I must say it was terrific support from you @andresrcs. Thanks a lot and keep it up. You are a star on the forum.