Nextcloud Nightmare - php code is shown instead of web interface

Installed Nextcloud on my Linux server following this guide: https://thenewstack.io/how-to-install-nextcloud-16-on-ubuntu-server-18-04/

Apache works fine, but when I got to http://localhost/nextcloud for the final part of the setup, I get this in my browser:

* @author Joas Schilling * @author Jörn Friedrich Dreyer * @author Lukas Reschke * @author Morris Jobke * @author Robin Appelman * @author Sergio Bertolín * @author Thomas Müller * @author Vincent Petry * * @license AGPL-3.0 * * This code is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, version 3, * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License, version 3, * along with this program. If not, see * */ require_once __DIR__ . '/lib/versioncheck.php'; try { require_once __DIR__ . '/lib/base.php'; OC::handleRequest(); } catch(\OC\ServiceUnavailableException $ex) { \OC::$server->getLogger()->logException($ex, array('app' => 'index')); //show the user a detailed error page OC_Template::printExceptionErrorPage($ex, 503); } catch (\OC\HintException $ex) { try { OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), 503); } catch (Exception $ex2) { try { \OC::$server->getLogger()->logException($ex, array('app' => 'index')); \OC::$server->getLogger()->logException($ex2, array('app' => 'index')); } catch (Throwable $e) { // no way to log it properly - but to avoid a white page of death we try harder and ignore this one here } //show the user a detailed error page OC_Template::printExceptionErrorPage($ex, 500); } } catch (\OC\User\LoginException $ex) { OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), 403); } catch (Exception $ex) { \OC::$server->getLogger()->logException($ex, array('app' => 'index')); //show the user a detailed error page OC_Template::printExceptionErrorPage($ex, 500); } catch (Error $ex) { try { \OC::$server->getLogger()->logException($ex, array('app' => 'index')); } catch (Error $e) { http_response_code(500); header('Content-Type: text/plain; charset=utf-8'); print("Internal Server Error\n\n"); print("The server encountered an internal error and was unable to complete your request.\n"); print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); print("More details can be found in the webserver log.\n"); throw $ex; } OC_Template::printExceptionErrorPage($ex, 500); }

Nothing on the Apache logs, /var/log/nextcloud contains 0 bytes, so I just don’t know how to proceed. Any suggestions?

Something definitely isn’t right there. Did you verify your download of Nextcloud with the SHA256 hash?

Good idea, but I double-checked with SHA256 and MD5SUM. All good.

How familiar are you with setting up a web server and PHP? Due to the fact that PHP code is displayed, I would tend to say that you haven’t correctly configured the necessary environment to run Nextcloud.
Please follow these steps to double-check that PHP is correctly installed and configured:

2 Likes

You miss generally a php extension.
Usually try :
sudo apt-get install libapache2-mod-php7

Restart apache2 service and see that working.

It’s not a PHP problem. php -v returns:

PHP 7.2.24-0ubuntu0.18.04.1 (cli) (built: Oct 28 2019 12:07:07) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

and I can run phpinfo(); successfully on the site.

That narrows it down to Nextcloud, so I did a grep on parts of that message and discovered it comes from nextcloud/index.php. Running http://localhost/nextcloud just produced the string of text (above) but running http://localhost/nextcloud/index.php brought up the GUI.

So is that it? Unfortunately not. I set up Nextcloud in the GUI, got through to the home screen with the default files, etc. then clicked on Settings. Bang: Internal Server Error. And I now get the same message when I try http://localhost/nextcloud/index.php again. (But http://localhost/nextcloud still gives me a string of text.

Conclusion: the latest version of Nextcloud is badly broken, at least on Ubuntu. Is there such a thing as a stable build?

Based on your description I still assume that your Apache setup needs some adjustments, otherwise it would automatically open the index.php file. Please check how the following Apache parameter is set on your server and make sure that index.php is listed:

DirectoryIndex index.html index.htm index.php
                                    ^^^^^^^^^
2 Likes

@GeoffP yes, @j-ed is right look at this command.
My confident it will work for you.

Thanks for the suggestion, but DirectoryIndex is definitely set Nextcloud’s .htaccess:
./.htaccess: DirectoryIndex index.php index.html

(It’s in the section labelled #### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####. I tried adding your line again at the bottom and restarting Apache, but no luck.)

PHP is also enabled in Apache. apache2ctl -M shows:
php7_module (shared)

Disabling and re-enabling the module makes no difference.

OK,

So I span up a AWS lightsail instance with Ubuntu 18.04 and followed the guide you referenced EXACTLY.

So, to troubleshoot, we need to replicate the issue. If there is an issue with Ubuntu, Nextcloud or the guide you used we should encounter the same error.

I completed the install and ended up looking at the setup page: http://3.8.127.87/nextcloud/

There was ONE correction I needed to make. To this end can you post the output of:
1.) apachectl configtest (when run from terminal).
2.) The exact contents of your /etc/apache2/sites-available/nextcloud.conf

3 Likes

i also tend to agree with @j-ed: to me it looks a bit like you’ve forgotten to enable some php-modules or didnt set the execution rights properly.

have you tried the following?

chown -R www-data:www-data /var/www/html/nextcloud

and/or:

a2enmod setenvif dir env mime ssl headers rewrite && systemctl reload apache2.service

if you have tried these steps and your system still isn’t running as expected, could you please post the following WITHOUT any critical informations?

  • config.php
  • apache2: nextcloud.conf for HTTP/HTTPS or combined
2 Likes

Thanks for the suggestions.

@NeptuneUK here’s what I get when I run apachectl configtest: Syntax OK
and here’s the contents of /etc/apache2/sites-available/nextcloud.conf:

Alias /nextcloud "/var/www/html/nextcloud/"
<Directory /var/www/html/nextcloud/>
    Options +FollowSymlinks
    AllowOverride All
      <IfModule mod_dav.c>
        Dav off
      </IfModule>
 
     SetEnv HOME /var/www/html/nextcloud
   SetEnv HTTP_HOME /var/www/html/nextcloud
</Directory>

@victorbw I’ve tried your two suggestions, both with no change. (Still getting Internal Server Error.)
apache2: nextcloud.conf is listed above, and here’s Nextcloud’s config.php:

<?php
$CONFIG = array (
  'instanceid' => 'oc1cyu2az99f',
  'passwordsalt' => 'XXX',
  'secret' => 'XXX',
  'trusted_domains' => 
  array (
    0 => '99.99.99.99',
  ),
  'datadirectory' => '/media/nextcloud-data',
  'dbtype' => 'mysql',
  'version' => '17.0.2.1',
  'overwrite.cli.url' => 'http://99.99.99.99/nextcloud',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'XXX',
  'dbpassword' => 'XXX',
  'installed' => true,
);

You’d need to add DirectoryIndex index.php above the Options line inside the Directory statement.

Depending on your Apache/PHP setup you have to tell Apache that .php files should be handled with PHP.

    <FilesMatch "\.(php|php[57]|phtml)$">
        SetHandler application/x-httpd-php
    </FilesMatch>

I tried that, restarted Apache, and Nextcloud did allow me to sign in. Then, clicking on Admin/Services it crashed back to displaying “Internal Server Error”.

So I removed your code and restarted Apache again. Still “Internal Server Error”. As a last resort, I stopped and started both MySQL and Apache and found I could access the site – for a while.

Nextcloud now stays up sometimes for minutes at a time, then – while in the middle of simple housekeeping tasks such as adding an email address for admin – it simply crashes out to the login screen. And won’t let me log in again. Nothing’s changed. There’s only one user and one password, but it insists they’re wrong. The only way to get back in is to restart MySQL and Apache, then I can log in again.

Just tried it again. Clicked on Admin/Apps. Bang! Out again to the login screen but no login accepted. Stop and start MySQL and Apache and I can get back in again … for a few more minutes.

There are no significant issues on the Overview – just a warning about using HTTP – and the NC Security Scan gives the site an A rating.

You shouldn’t try to change more than one thing at a time, when performing such kind of trouble shooting. First, I’d rule out the MySQL instance… no need to restart it, if it’s working. So before you restarted MySQL did you check to see, if it still runs and if you can connect to it using the credentials you have in your config.php?

1 Like

HI @GeoffP,

Thanks for the feedback on your nextcloud.conf and the apache syntax check.

They both seem fine in terms of configuration at the site level.

1.) Could you post the contents of your /etc/httpd/conf/httpd.conf?

2.) Was this install a fresh one, on a clean box? That is to say this box wasn’t in service prior to to your attempt to install NC?

3.) Does the server meet the recommended minimum installation requirements?

I noticed you deviated from the NC install guide you referenced.

As I installed on a virgin lightsail instance, using the same NC installation guide, OS (and version) and NC version, it would seem at this point to indicate that Ubuntu isn’t the issue, or NC. It would point to a configuration issue with Apache. Sometimes a small omission or typo can have bewildering effect.

It also seems that the original issue you posted is resolved. That is to say .php files are not being served as text/raw files. Correct us if this is not the case. It would seem you are now dealing with other issues. @budy gave some great advice, stick to the issue at hand, one at a time, implement a suggestion, test, evaluate, feedback and if not resolved move forward, just small steps one at a time.

My personal opinion at this point would be to do a completely fresh install from scratch on a clean box if this is possible. Follow exactly the guide you referenced, don’t change anything. Establish if the install and initial configuration is good. Then small steps to change data directory etc. Obviously remembering to backup your server at key points to enable you to roll-back if needed.

Hi @NeptuneUK,

Thanks for the response. In response to your points…

  1. I’m guessing you mean /etc/apache2/apache2.conf. (There’s no /etc/httpd/conf/httpd.conf in Ubuntu.) It’s a 7K file so rather than post it here, I’ve copied it to a Pastebin link: https://pastebin.com/yxEhWSLF

  2. Yes, this was a fresh install.

  3. Yes, the server meets the minimum requirements.

And yes, the original issue has been resolved, and that’s lead on to the current issue.

I’ve been developing and testing software for decades so I’m well aware of the incremental nature of the debugging process, and that is partly why I’m hesitant to do a fresh reinstall. NC is now working, sometimes for 10-15 minutes at a time. Then it simply crashes out to the login screen and refuses a login until I restart Apache. That suggests a problem in NC itself. (Or perhaps not. Having just typed the above, I had a crash out to an “Internal Server Error” screen which requires a restart of MySQL and Apache, then I’m back to login.)

You say you tried a virgin install on Lightsail. Did you also run it for a while? I’m back in again just now, looking at Logging, Apps, etc. then, after a few minutes, it crashes back to “Internal Server Error”.

I’m not sure what you mean when you say I deviated from the installation guide. The only deviation I’m aware of was to reference attached storage when configuring Nextcloud. The reason I did that was because I followed the advice detailed here, specifically:

Changing data directory after installation is not officially supported. Consider re-installing Nextcloud with new data directory…

(The original was also in bold type!)

Wiping the server and starting again is an option, but that will destroy any chance of learning what’s actually going wrong here – and perhaps finding a rare, gnarly bug that could affect someone else.

Hi @GeoffP,

So maybe it might be best to document which action you implemented that led to the original issue “Nextcloud Nightmare - php code is shown instead of web interface” being resolved, then marking this thread as closed and resolved, attributing whoever if anyone gave the solution that worked for you.

That done open a new thread with the new issue you are experiencing? It will make it easier for people to locate whichever issue it is they are experiencing and it’s resolution.

I ran the instance I span up for about 3 days, and it had a few visits other than myself. The instance remained available and stable during that time without crashing, displaying PHP as text/raw and there were no other issues in the error or sys logs. I obviously then terminated the instance as it’s billable.

Yes the deviation for the data directory is specifically what I was referring to. It wasn’t something you mentioned previously, and is important in terms of the operation of NC (NC checks the data directory and specifcally a text file written there). If NC is unable to read the text file for whatever reason (permissions, mountpoint availability etc) you can see the ‘internal server’ error message, depending on what / how you mounted your data directory could have a bearing (eg a physical disc that is slow to respond to read requests after idling.) Also the omission could indicate other deviations you felt weren’t relevant, but that possibly could be.

1.) What do the apache / NC logs have to say about the ‘internal server error’?

2.) What happens if you change the data directory to the default location (same physical media as you NC installation?) Do you get stability.

3.) Do a fresh install, following the guide you referenced EXACTLY and see if the issue/issues are replicated again. This could point to a issue with your data directory mountpoint / media etc depending on the result. (You can always backup your current installation using your preferred method dd, rsync, tar etc) and restore later to troubleshoot if you really want to find that gnarly bug.

Hi @GeoffP,

I just thought I’d mention I also just did a quick install on an MS Azure compute unit (alternative Hypervizor to AWS), using the guide you referenced. All worked OK and as expected.

@GeoffP if you want a quick setup (on a new machine).

the default is nginx/postgresql so you have to change nextcloud_webserver_type = 'apache' and nextcloud_db_type = 'mysql'. follow the readme.

sounds like trouble with the server.

yes. month/years. depends. i run two “baremetal” and two “dockerized”. on different hoster.