Python on a shared hosting server - php

I have a hosting plan through Godaddy that only supports python 2.6.6. I have been able to install python 2.7 and 3.6 through SSH and run scripts, pip, no problems.
When I try and run a PHP script that calls a python script from SSH, it works just fine using my new python installs, but when I open the PHP script in a browser, it will only run 2.6.6.
Why is this? Is there a way to get around this without getting a VPS?

I think what is happening here is that you are able to manually run Python3 from your SSH session by calling it directly.
However, your PHP installation probably isn't aware that you have more than one instance of Python installed. At a guess, your PHP installation is defaulting to it's environment path (or other predetermined library directory) where it can find a Python installation (this installation being the original 2.7).
I'm not sure how you are calling your Python scripts but there is an answer here: Calling specific version of python from PHP that talks about changing the python version in the script.
Another possible solution is to add the directory containing Python3 to your $PATH variable. Word of warning, if this is a shared system this might be disabled or potentially COULD get you in some trouble. Since altering the path might start other python scripts (belonging to other people) being called by Python3, which could break them (due to deprecated syntax etc)
When you want to start messing with system configuration, you're getting into VPS territory rather than shared hosting.

I have found a sneaky way around this. I used SSH2 PHP extension to call the python3.

Related

Possible to "tunnel" PHP extension from Docker to outside?

On a CentOS 7 Virtual Machine, I have a PHP script running in Apache. That PHP script requires a PHP extension which I cannot install on the Virtual Machine. However, I have found a Docker image which contains the extension I need.
Now, how do I proceed from here?
Is it somehow possible to "tunnel" the PHP extension from inside the Docker to its host?
Or is it wise to make a detour over the command line? I.e. in the outer PHP script, call a system command which runs the Docker image, which runs a PHP script with the extension I need.
possible? yeah, make some api doing the required actions inside the VM, serialize the result, and deserialize it where your main app is running. the serialization process may be done with serialize() or json_encode() or var_export(), the transfer protocol can trivially be implemented using HTTP or HTTPS (just use apache or nginx?). PS, you may want to add some authentication or ip whitelist.
am curious why you can't install the extension on your main system tho, i guess it's' a windows<->linux compatibility issue?

Run a C++ Application on a Webserver with WordPress

I have a C++-library (.so) for some calculations that I would like to call from Wordpress/PHP via an input formular. The promising idea to build the .so-library as a PHP extension using PHP-CPP has been fine locally on Ubuntu 14.04. But on the webserver this method failed because my webhoster doesn't support changing the extension directive in the php.ini/.user.ini. I see the following alternatives:
Build an exutable application and run it from PHP via proc_open() and send a lot of variables to the stdin of the application. Wordpress itself offers PHP plugins.
Redirect to another server where my own php extensions are supported.
Is there a way using python/web2py for that purpose?
Which would be best?
Or any other ideas?
Probably the simplest way is to create command line utility in C++ and execute it from php with shell_exec. I tried that in past and the performance was not too bad.
"Probably the simplest way is to create command line utility in C++ and execute it from php with shell_exec. I tried that in past and the performance was not too bad."
This did help. Finally I managed a build on Linux which was portable to the webserver where the website and wordpress are located. The call to the binary built from C++ was done with shell exec or popen in PHP (which one I don't remember, it was in 2018). The PHP code was finally migrated to an own wordpress plugin. Unforunately, I could not use PHP-CPP due to missing admin rights for the webderver. But the integration via shell exec or popen works fine.

Executing java from CLI via exec() under Windows

I have a PHP-script originally developed on Ubuntu, which now has to run on a Windows machine, executing a java program like this:
exec("java -jar {$filename}");
// Process output
This does not work as expected on Windows. I already found out, that although I can use java -version from the command prompt I can't use it in exec(), i.e. the problem is java can not be found.
I have a workaround in place, pointing to java.exe using the complete path to C:\Program Files\Java\...\java.exe if the script runs on Windows. Unfortunately though this is hardcoded to the path on the current machine, which might change or vary on a different system, e.g. when installing Java to a different location or a different version (JRE/JDK/6/7) is installed.
How do I call Java on Windows without having to refer to the exact location of java.exe?
You need to set enviroment variable on windows, to be able access java without path
http://www.java.com/en/download/help/path.xml
Even if this Question is a little older, I ran into the same problem and I found a pretty neat solution for it without the PATH requirement.
There is a symlinks to all java executabled located in this folder:
C:\ProgramData\Oracle\Java\javapath
for example: just call
C:\ProgramData\Oracle\Java\javapath\java.exe -jar XYZ.jar

Run a PHP CLI script from a webpage

I have a (possibly dumb) question.
I have a script made in php, constructed for cli usage. Works fine when I run it from the command line, no problem there. The problem is that the site I'm working on has ssh restrictions on the hosting server and I cannot ssh there to run it. Hence my question: how can I run the script from another php that is web-accessible? Already tried with exec(), system(), etc.
The main problem is that I need he $_SERVER['SHELL'] variable set, and when the call is comming from a web browser of course php doesn't set it.
Any ideeas will be greatly apreciated, thanx.
There are many possibilities why exec() and related function calls are not working for you.
Your webhost does not have PHP-CLI installed. Just a webserver module
You need to use the full path to the php binary for lack of a decent shell environment. E.g. /usr/bin/php <script> instead of php <script>.
Your webhost has installed PHP-CLI on a non-standard path (e.g. /usr/local/bin/php, or /opt/php5/php)
The webserver user does not have rights to access the php binary
Et cetera..
maybe update the php script to be both an include and a cli script.
use
__FILE__
to check if it's a file, then read the params. otherwise do nothing.
and as an include just call the function you want directly.

How do I find out the currently running PHP executable?

From inside a PHP program I want to know the location of the binary executing it. Perl has $^X for this purpose. Is there an equivalent in PHP?
This is so it can execute a child PHP process using itself (rather than hard code a path or assume "php" is correct).
UPDATE
I'm using lighttpd + FastCGI, not Apache + mod_php. So yes, there is a PHP binary.
eval/include is not a solution because I'm spawning a server which has to live on beyond the request.
Things I've tried and don't work:
$_SERVER['_'] looks like what I want from the command line but its actually from an environment variable set by the shell of the last executed program. When run from a web server this is the web server binary.
which php will not work because the PHP binary is not guaranteed to be the same one as is in the web server's PATH.
Thanks in advance.
The PHP_BINDIR constant gives you the directory where the php binary is
Yeah, $_SERVER['_'] is what you're talking about, or as near as exists. The reason you're getting a Web server binary when it's run from the web is that /usr/bin/php has nothing to do with the Web server's execution; what it's running is a separate SAPI. There's nothing from the web PHP instance to point to /usr/bin/php because there's no reason for there to be.
The PHP_BINDIR constant is probably the easiest thing to use; the next best thing I could come up with is basically re-creating that bindir path from the extension_dir configuration setting:
$phpbin = preg_replace("#/lib(64)?/.*$#", "/bin/php", ini_get("extension_dir"));
It has a regex in it, so it feels more like your native perl(!) but otherwise is not especially optimal.
In PHP5.4 you can use the PHP_BINARY constant, it won't work via mod_php or similar but will via CGI etc.
For earlier versions of PHP readlink('/proc/self/exe'); will probably be fine, again it won't work via mod_php.
Depending on the way php is installed you CANT find the php executable.
if php is running as a module for the webserver like apache module, then there is no binary you can call.
you can take a look into php_info() it lists everything.
may also the path to php. within that path you can assume a php binary.
but why do you want to call a extra process?
you can execute other php files by include command or eval.
there is no reason to spawn a new process.
what about:
<?php
exec("which php");
?>
But, it's unix/linux only:D
I've been looking for the php7 executable on my mac (OSX El Capitan) in order to configure and install xdebug (needed to find the right version of phpize to run). None of the solutions I found worked for me, so I just ended out searching for it:
find / -name php -print
I knew (from phpinfo()) that I was running php7, so I was able to infer the correct directory from the options presented by find.

Categories