Can't access api

I have installed Cockpit in a subfolder on a shared hosting and followed the information provided in https://github.com/agentejo/cockpit/issues/704#issuecomment-405016371 to setup the paths. Now I am able to access the admin interface at https://mydomain.com/cockpit and everything seems to work. But I am not able to accss API. If I am not logged in, every request is redirected to the login and if I am logged in, every request returns status 404. I try to reach the api at https://…n.com/cockpit/api/… What am I doing wrong?

EDIT: Sorry, I had to shorten the last link as the forum allows new users only to post two links in a post. It should also start with mydomain…

EDIT 2:
Some more information about my setup:

  • Cockpit is located at /home/user/public_html/dir/cockpit
  • The document root points to /home/user/public_html
  • A htaccess file is used to redirect everyting to “dir”
  • I use a “defines.php” file like provided in the above link to avoid having “dir” in the URL
  • If I remove the “defines.php”, the “dir” is in the url, but I can access the API.

You need to create an API key in the Cockpit settings for API access and append a ?token=apikey to every api call.

I’m still not sure how to do this with the user account API keys though.

The key is not the problem, as described in the last point of my edit. My collection is public, so no key is needed.

I collected a few defines from tests on different hosts before. Maybe, there is one for you. If you nest your cockpit deeper, you may adjust some paths to something like basename(basename(__DIR__))

Setup (I used PHP and not the API, but I tested the API for the forms endpoint):

- cockpit/... --> Backend
- index.php   --> Frontend
- .htaccess
- ...

strato (shared host):

subdomain points to /dir

Frontend:

define('BASE_URL', '/' . basename(__DIR__));
define('BASE_ROUTE', '/' . basename(__DIR__));
$options['route'] = preg_replace('#'.preg_quote(BASE_URL, '#').'#', '', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), 1);

Backend:

define('COCKPIT_BASE_URL', '/' . basename(__DIR__));
define('COCKPIT_BASE_ROUTE', '/' . basename(__DIR__));
define('COCKPIT_DOCS_ROOT', dirname(__DIR__));

Uberspace6, Uberspace7 (shared host)

works out of the box if using var/www/virtual/$USER/html

If using subomain var/www/virtual/$USER/blog.example.com:

Frontend: .htaccess needs RewriteBase /

Backend: .htaccess needs RewriteBase /cockpit

@jrast I fixed it. Try it with this branch. https://github.com/raffaelj/cockpit/tree/cp-next_fix-api-call-redirect-to-login

If it works for you, I’ll send a pull request.

I moved the COCKPIT_BASE_URL definition above the COCKPIT_API_REQUEST definition and removed one $ sign in bootstrap.php.

See also this discussion: https://github.com/agentejo/cockpit/issues/873

1 Like

Hi @raffaelj! I’ve read that you successfully used Cockpit with a subdomain on Uberspace. Maybe you can help me with it because I did not get it to work. I tried it with RewriteBase / and also with the file defines.php but nothing did really work. The best result until now was a successful installation of cockpit and getting redirected to the desired address /auth/login but there I got only an error from the browser saying there are too many redirects :frowning:

Do you use Uberspace6 or 7? If I remember correctly, I only tested a subdomain in /var/www/$USER/virtual/sub.domain.com/ on an old U6 host and I had to set RewriteBase /. I think, I never tested it on an U7 with a subdomain, but I’m not sure anymore.

Is Cockpit in your root .../sub.domain.com/ or in a subfolder .../sub.domain.com/cockpit/?

Does Cockpit work in /var/www/$USER/virtual/html/?

Thanks for the quick reply.
I’m on Uberspace 7. Cockpit works in the default document root (/virtual/html/) but not for my subdomain (cms.$USERNAME.uber.space/) where I put it in root. So no extra folder.

When only including the RewriteBase rule I get redirected to cms.$USERNAME.uber.space/var/www/virtual/$USERNAME/cms.$USERNAME.uber.space/auth/login and the site is really broken:

Sorry for the late reply. I was very busy.

Maybe that’s the problem:

The DOCUMENT_ROOT variable set by Apache always points to the one and only DocumentRoot /var/www/virtual/<username>/html so you will will get a misleading value. There is no way to change that behaviour.
DocumentRoot — Uberspace 7 manual 7.15.4 documentation

So you would have to adjust some constants via defines.php


Or just create a second Uberspace and split the bill. I think, I never tested subdomains on an U7 host because they disabled wild card subdomains, which was a very fast way for quick tests.

We strongly suggest to use different accounts for different projects due to security reasons. If one of the DocumentRoots gets compromised (e.g. because of a CVE), all other files within all other DocumentRoots can be compromised as well.

(de) German cite from the old U6 wiki about multiple hosts

[…] uns ist schließlich egal, ob du z.B. monatlich 10 Euro für einen Uberspace bezahlst, oder ob du monatlich je 2 Euro für 5 Uberspaces bezahlst. Es macht ja auch keinen Unterschied vom Ressourcenbedarf her, ob du die gleichen Programme und die gleichen Besucherzahlen auf mehrere Accounts aufteilst oder nicht. […]
Uberspace 7 manual — Uberspace 7 manual 7.15.4 documentation

No problem. After including every constant in the defines.php (also with the help of your debug snippet) the login page is now showing up correctly but strangely the login is not working after executing the install script and trying to login with admin / admin. It always states: Login failed.
Maybe you have an idea what the issue could now be? If not, thank you very much for your effort nevertheless. :slight_smile:

Can you open your developer console and look at the response of /auth/check, that should be called after pressing the login button. Maybe it works already, but there is an error message on top of the json response that will stop the page from reading it as success and redirecting to the dashboard.
I don’t assume, that you use Internet Explorer, but if so, the login fails because of a missing Promise polyfill.

And if you get it running, please share your setup.

@Tummerhore I tried it myself now.

Steps to reproduce:

Install Cockpit in the default docs root to see the expected paths.

  • username.uber.space points to /var/www/virtual/username/html
  • cd ~/html
  • git clone https://github.com/agentejo/cockpit.git .
  • nano bootstrap.php --> add debugging snippet directly after the constants defines and before function cockpit(...) starts
echo "<pre style='position:absolute;top:0;left:0;'>"; //avoid pre to get centered (body CSS)
print_r($_SERVER['DOCUMENT_ROOT']);                   // may differ from __DIR__
echo "\r\n";
print_r(get_defined_constants(true)['user']);         // list all user constants
print_r($app->config['paths']);                       // list all config paths
echo "</pre>";
die;
  • open https://username.uber.space in browser to see the expected constants

Now to the sub domain.

  • add sub domain uberspace web domain add test.myurl.de
  • test.myurl.de points to /var/www/virtual/username/test.myurl.de
  • cd /var/www/virtual/username/
  • mkdir test.myurl.de
  • cd test.myurl.de
  • git clone https://github.com/agentejo/cockpit.git .
  • nano bootstrap.php --> add debugging snippet from above
  • open https://test.myurl.de in browser to see the wrong constants
  • change .htaccess (see below)
  • add custom defines (see below)

.htaccess

  • nano .htaccess
  • uncomment RewriteBase /

defines.php

  • nano defines.php
  • copy the following snippet and save
<?php
define('COCKPIT_BASE_URL', '');
define('COCKPIT_BASE_ROUTE', '');
define('COCKPIT_DOCS_ROOT', __DIR__);
define('COCKPIT_SITE_DIR',  dirname(__DIR__));

Don’t forget to delete the debugging snippet in bootstrap.php.

Call /install in browser or use the cli to create a new user

I was able to login and to logout, I created a collection from the posts template and I saved one entry. I didn’t test anything else. So it seems to work with this setup.

1 Like

Thanks for checking it again and sorry for not responding earlier. Hmm strange, I think I did the same things like you described and the developer console (on Chrome) didn’t show anything as far as I know. But I will have a look at it again later this week, when I have some time left.

I think, I found your issue:

I can’t remember, if I read it somewhere, but I’m pretty sure, that you can’t setup a “sub-subdomain”, which technically is a subdomain. $USER.uber.space is registered automatically and cms.$USER.uber.space would be a wildcard subdomain, that isn’t supported. In my test I used my own (sub)domain and it worked on the first try.