Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assets rerouted Instatlation Issue #873

Closed
PiIsFour opened this issue Sep 28, 2018 · 11 comments · Fixed by #878
Closed

Assets rerouted Instatlation Issue #873

PiIsFour opened this issue Sep 28, 2018 · 11 comments · Fixed by #878

Comments

@PiIsFour
Copy link

Hello,

i have some trouble installing cockpit. Probablly something i missed, but i would be really thankfull if someone can point me in the right direction. Don't even know how to diagnose it further.

The installation locally worked fine, but now i tried to upload it to my webserver (strato). The install went without error but the login page is brocken. It seems to me some problem with mod rewrite because all the assets (like jquery) get reroutet with a 302 to /api/auth/login.

I have installed it under a subdomain, if that makes a difference.

Thank you for your time and help.

@raffaelj
Copy link
Contributor

You don't need to change the .htaccess.

Create a file defines.php in your cockpit root directory with

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

And I wrote an install script for strato, because it was very annoying to have such a limited console:
https://github.com/raffaelj/cockpit-scripts/blob/master/install-cockpit-on-strato/install.sh

@PiIsFour
Copy link
Author

Thank you @raffaelj,

that sounds good. Will try it tomorrow.

@PiIsFour
Copy link
Author

Finally got time to do it and it worked. Just added the defines.php and put in my pathnames.

Thanks again @raffaelj

@PiIsFour
Copy link
Author

PiIsFour commented Oct 1, 2018

Sorry, but maybe i closed this to early. Should i open a new one?

Last time i only checked that the frontend of Cockpit worked on my server. But now i filled in my data and tried to fetch it. but now i get a 404 on the api request.

i can access the frontend at: beta.MYDOMAIN/cms
so shouldn't i get the api with: beta.MYDOMAIN/cms/api/collections/get/test
that is what worked locally.

I'm really sorry for asking all this questions.

@PiIsFour PiIsFour reopened this Oct 1, 2018
@raffaelj
Copy link
Contributor

raffaelj commented Oct 1, 2018

Sorry, I used cockpit as a library to have a backend for a simple php frontend. I didn't notice, that the api doesn't work. It worked on my localhost and apparently I never checked, if the api works on the server.

I tested it again. The backend and frontend work. When trying to call the api through https://example.com/admin/api/..., I got redirected to the login page. If calling https://www.example.com/admin/api/..., I get a {"error": "404", "message":"File not found"}.

There was some weird config, I had to add to load cockpit as library (found it here):

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

$app = new Lime\App($options);

Maybe this is helpful.

Otherwise - If you don't have to use strato shared hosting (because a client already has a cheap account and wants to stay...), you could consider to choose a better webhoster.

@PiIsFour
Copy link
Author

PiIsFour commented Oct 2, 2018

No reason to be sorry, I'm thankfull you spend the time on my problem. And yes i'm bound to strato.

But im not sure i understand your solution. I'm on the 0.8.0 stable relase. I don't seem to have a line:
$app = new Lime\App($options);

so i tried putting it in the bootstrap.php because the LimeExtra\App there seems to just inherit from Lime\App. but i don't really understand the project yet and have no idea where i could kind of check if anything changes.

// load config
$config = array_replace_recursive([
  'debug'        => preg_match('/(localhost|::1|\.dev)$/', @$_SERVER['SERVER_NAME']),
  'app.name'     => 'Cockpit',
  'base_url'     => COCKPIT_BASE_URL,
  'base_route'   => COCKPIT_BASE_ROUTE,
  'docs_root'    => COCKPIT_DOCS_ROOT,
  'session.name' => md5(__DIR__),
  'sec-key'      => 'c3b40c4c-db44-s5h7-a814-b4931a15e5e1',
  'i18n'         => 'en',
  'database'     => ['server' => 'mongolite://'.(COCKPIT_STORAGE_FOLDER.'/data'), 'options' => ['db' => 'cockpitdb'] ],
  'memory'       => ['server' => 'redislite://'.(COCKPIT_STORAGE_FOLDER.'/data/cockpit.memory.sqlite'), 'options' => [] ],

  'paths'         => [
    '#root'     => COCKPIT_DIR,
    '#storage'  => COCKPIT_STORAGE_FOLDER,
    '#pstorage' => COCKPIT_PUBLIC_STORAGE_FOLDER,
    '#data'     => COCKPIT_STORAGE_FOLDER.'/data',
    '#cache'    => COCKPIT_STORAGE_FOLDER.'/cache',
    '#tmp'      => COCKPIT_STORAGE_FOLDER.'/tmp',
    '#thumbs'   => COCKPIT_PUBLIC_STORAGE_FOLDER.'/thumbs',
    '#uploads'  => COCKPIT_PUBLIC_STORAGE_FOLDER.'/uploads',
    '#modules'  => COCKPIT_DIR.'/modules',
    '#addons'   => COCKPIT_DIR.'/addons',
    '#config'   => COCKPIT_CONFIG_DIR,
    'assets'    => COCKPIT_DIR.'/assets',
    'site'      => COCKPIT_SITE_DIR
  ],
  'filestorage' => [],
], is_array($customconfig) ? $customconfig : [], [
  // FIXME: hack to get strato to work?
  'route' => preg_replace('#'.preg_quote(BASE_URL, '#').'#', '', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), 1),
]);

$app = new LimeExtra\App($config);

@raffaelj
Copy link
Contributor

raffaelj commented Oct 2, 2018

Your fixme hack might work... Try to replace BASE_URL with COCKPIT_BASE_URL.

For debugging create a file path/to/cockpit/config/bootstrap.php and add

<?php
// view the debug info on top of the layout
$app->on('app.layout.contentbefore', function(){
    
    $vars = [
        'app_important_routes' => [
            'route' => $this['route'],
            'base_url' => $this['base_url'],
            'base_route' => $this['base_route'],
            'base_host' => $this['base_host'],
            'base_port' => $this['base_port'],
            'docs_root' => $this['docs_root'],
            'site_url' => $this['site_url'],
        ],
        'DOCUMENT_ROOT' => $_SERVER['DOCUMENT_ROOT'],
        'cockpit_DIR' => dirname(dirname(__DIR__)), // may differ from DOCUMENT_ROOT (symlinks)
        'user_constants' => get_defined_constants(true)['user'],
        'app_paths' => $this['paths'],
        // 'SERVER' => $_SERVER,
        // 'app_config' => $this->config, // config.yaml + app defaults
        // 'app' => $this, // the whole app, needs a few seconds to load/print
    ];
    
    echo '<pre>' . print_r($vars, true) . '</pre>';
    
});

My setup was:

- /dir/cockpit   // backend
- /dir/index.php //frontend
- /dir/...

content of frontend index.php:

//include cockpit
include_once("cockpit/bootstrap.php");

// start Lime
$app = new Lime\App($options);

// some logic to bind paths and render output as html

@raffaelj
Copy link
Contributor

raffaelj commented Oct 2, 2018

Wait, the route is already set this way:
https://github.com/agentejo/cockpit/blob/next/index.php#L18

Let me know, if you find a solution. I really thought, I was able to run cockpit on strato and I'm sure, I'll run into this issue again in the future.

@raffaelj
Copy link
Contributor

raffaelj commented Oct 2, 2018

I found the issue, and maybe the solution. The constant COCKPIT_API_REQUEST is set to 0, so the api controller doesn't start and the backend returns a 404.

Add this debug code here: https://github.com/agentejo/cockpit/blob/next/bootstrap.php#L67

$vars = [
    'DOCUMENT_ROOT' => $_SERVER['DOCUMENT_ROOT'],
    'cockpit_DIR' => __DIR__, // may differ from DOCUMENT_ROOT (symlinks)
    'user_constants' => get_defined_constants(true)['user'],
    'SERVER' => $_SERVER,
];
print_r($vars);
die;

to return your SERVER vars and constants to the api output.

In the defines.php define COCKPIT_API_REQUEST with a mixup from

define('COCKPIT_API_REQUEST'    , COCKPIT_ADMIN && strpos($_SERVER['REQUEST_URI'], $COCKPIT_BASE_URL.'/api/')!==false ? 1:0);

and

if (COCKPIT_ADMIN && !defined('COCKPIT_ADMIN_ROUTE')) {
    $route = preg_replace('#'.preg_quote(COCKPIT_BASE_URL, '#').'#', '', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), 1);
    define('COCKPIT_ADMIN_ROUTE', $route == '' ? '/' : $route);
}

If it works - Let's start a new thread in the Cockpit Community Forum with best practices for defines.php and supported webhosters.

@raffaelj
Copy link
Contributor

raffaelj commented Oct 2, 2018

I fixed it. Try it with this branch. If it works for you, I'll send a pull request.
https://github.com/raffaelj/cockpit/tree/cp-next_fix-api-call-redirect-to-login

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

@PiIsFour
Copy link
Author

PiIsFour commented Oct 3, 2018

I tried your branch and it just works. I tested the Interface, got my data via the api and even tested the email submit via forms. Everything I need.

That is just awesome, thank you so much. I would have been lost without you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants