Synology: PHP 7 CLI without errors (Nextcloud and PHP 7)

The PHP package is quickly installed on a Synology DiskStation, but setting everything up without issues might be a bit complex. Unfortunately PHP for Synology behaves a bit different than PHP on a Unix/Linux server. Especially PHP CLI causes some issues. Some commands/programs don’t run with PHP 7.0 on the command line. In this article I’ll explain how to fix it and how to run e.g. Nextcloud commands occ with PHP 7.

PHP has two sides, one is used by the web server (e.g. Apache). The other side is the CLI (Command Line Interface). With the CLI you can run php commands and scripts directly on you shell (e.g. via putty). To run PHP commands you simply type php following by a function name or a script name. If there are different versions of PHP installed, each of them can be addressed with its own command (php56, php70, …). For whatever reason (only Synology knows why), the PHP 7.0 CLI from Synology doesn’t have all modules included. That’s why some commands like sudo -u http php70 occ … result in errors. Nextcloud 14 is out and it requires PHP 7.0 or higher, so if you want to update, this is an issue.

To use PHP 7.0 properly, the correct profile has to be used.

  1. First, we need the name of the profile. Those are located under /var/packages/WebStation/etc/php_profile/ in a folder per profile. The name is not descriptive, so you have to look it up yourself. If you have more folders (at least 2, if you have installed PHP 5.6 and PHP 7.0) – and don’t know which one it is – you have to inspect the content. In each folder, there is a conf.d folder containing the profile config. Get sure that there is a difference in your PHP 7.0 profile:
    1. In DSM open the Web Station and go to PHP settings, look for a difference in the profiles (e.g. extensions that are activated in one but not in the other(s)).
    2. If you don’t fine one, create one by activating/deactivating a module in one of the profiles.
    3. Now open the profiles (under /var/packages/WebStation/etc/php_profile/<Garbage>/conf.d/) in vi or nano and inspect the listed modules. You’ll see the difference there and now you are able to tell which profile is the one you need.
    4. Remember the name of the PHP 7.0 profile.
  2. Create a script containing following lines:
    #!/bin/bash
    PHP_INI_SCAN_DIR=.:/usr/local/etc/php70/:/var/packages/WebStation/etc/php_profile/<Garbage>/conf.d/
    export PHP_INI_SCAN_DIR
    php70 $*

    Instead of <Garbage> enter the profile name you looked up as described above.

  3. Test your script by running this command on your command line (putty):
    <path_to_script>/<scriptname>.sh -i

    This line is the same as php(70) -i, the phpinfo() equivalent via command line. If you have made no mistake, the PHP 7.0 config – according to the profile – will be printen on the command line. Somewhere at the beginning, you should see the PHP version (in our case 7.0).

  4.  Now we can use the occ commands (or anything else that needs PHP 7.0 and certain modules). Instead of
    sudo -u http php56 occ ...

    use the modified PHP 7.0 CLI, by replacing php56:

    sudo -u http <path_to_script>/<scriptname>.sh occ ...
  5. Now you can also run the Nextcloud cron with PHP 7.0. In DSM go to the task scheduler and create a new task. Choose “own script” and let it run every 15 minutes. As command enter:
    sudo -u http <path_to_script>/<scriptname>.sh /var/services/web/nextcloud/cron.php

 

Why Synology doesn’t do the PHP 7.0 package properly? Who knows. But with a few extra steps it works fine. Many tanks to Lux007 from Synology-Forum for this solution.

Leave a Reply

Your email address will not be published. Required fields are marked *