How can I force PHP Version for Command Line?

35,001

Solution 1

If both are installed, all you need to do is run the script using the relevant PHP binary.

So for example:

 // Runs using the PHP binary located at /usr/bin/php
 * * * * * root /usr/bin/php -n "/path/to/script.php"

or

 // Runs using the PHP binary located at /var/php5
 * * * * * root /var/php5 -n "/path/to/script.php"

All you need to know is the full file system path of the PHP CLI binaries, and call the relevant one to run your code.

Solution 2

In accordance to https://community.1and1.com/using-php-composer-at-1and1/, create .profile in your root projet and add the line :

alias php='/usr/bin/php5.5-cli'

It works for me. Log out and log back in SSH and do :

php -v

Result :

PHP 5.5.32 (cli) (built: Feb 15 2016 16:13:44)

Solution 3

If you can execute PHP scripts directly in the shell like:

$ script.php

you can specify the binary that will execute the script in it's first line:

#!/usr/bin/php
<?php

That line is called shebang. The line might differ on your system, you need to know the full file system path of the PHP CLI binary you want to use for that script.

If you execute that file in the shell, the specified binary will be used. Same for cron.

If you execute that file via your webserver, PHP will drop that line silently.

See as well: Features: Using PHP from the command line

Solution 4

there must be two PHP directories and one of them should be the default one. try to find out php5's path from the root of your server and use full path at your cron job.

Solution 5

This is another approach to force PHP from Command Line on 1and1.

Log in with ssh on your server and create a new file called .profile in there you will write the following line alias php='/usr/local/bin/php5' Log back in with ssh and check php version with php -v you should see the version is now 5.

Share:
35,001
Bluemagica
Author by

Bluemagica

Updated on August 16, 2020

Comments

  • Bluemagica
    Bluemagica over 3 years

    I am hosted with 1and1.com, and I have setup my files to be parsed with php5 using .htaccess.

    But that only works in apache, and not in command line, which defaults to the server default php4.

    So currently I can not setup cron jobs to run my code as php5. Any ideas?

  • Zombaya
    Zombaya almost 12 years
    whereis php can help with finding your php-locations
  • Ashith
    Ashith over 11 years
    You don't need to use the full path for php to run in CLI (having run php from terminal on my Mac using MAMP, XAMPP, and OSX's stock install, I am sure of this.) The reason you did was either (depending on the error you received) : a) You don't have the bin directory in your PATH env variable (which gets me every time), or b) you have multiple versions of php installed and the first one in the path was not the one you wanted. For the former, you can confirm by running the following before the php call: PATH=$PATH:/MAMP/bin
  • 0z88
    0z88 about 8 years
    This doesn't answer the question at all. OP was asking about 1and1's environment, which has nothing to do with MAMP.
  • 0z88
    0z88 about 8 years
    which php or which php5.5-cli as 1and1 command line doesn't have whereis