I am able to easily call a python script from php using system(), although there are several options. They all work fine, except they all fail. Through trial and error I have narrowed it down to it failing on
import MySQLdb
I am not too familiar with php, but I am using it in a pinch. I understand while there could be reasons why such a restriction would be in place, but this will be on a local server, used in house, and the information in the mysql db is backed up and not to critical. Meaning such a restriction can be reasonably ignored.
But how to allow php to call a python script that imports mysql? I am on a Linux machine (centOs) if that is relevant.
The Apache user (www-data in your case) has a somewhat restricted environment. Check where the Python MySQLdb package is installed and edit the Apache user's env (cf Apache manual and your distrib's one about this) so it has a usable Python environment with the right PYTHONPATH etc.
Related
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.
I'm using Windows 10 Home Single Language 64-bit Operating System on my laptop.
I've installed the latest version of XAMPP on my laptop.
This has installed PHP 7.2.8 and Apache/2.4.34 (Win32) OpenSSL/1.1.0h PHP/7.2.8 on my laptop.
I come across following sentence from PHP Manual Page :
PHP can be used on all major operating systems, including Linux, many
Unix variants (including HP-UX, Solaris and OpenBSD), Microsoft
Windows, macOS, RISC OS, and probably others. PHP also has support for
most of the web servers today. This includes Apache, IIS, and many
others. And this includes any web server that can utilize the FastCGI
PHP binary, like lighttpd and nginx.
From the above text I didn't get the exact meaning of the term "PHP binary". This peculiar term is used very frequently at many places in the PHP Manual but nowhere the actual meaning of "PHP binary" has been given.
I googled it for the meaning and I come to know about the 'predefined constant' PHP_BINARY.
So, I tried to execute the below code in hope to clear my doubt over the actual meaning of the frequently used term "PHP binary" and checked the output in my web browser:
<?php
echo PHP_BINARY;
?>
Surprisingly, I got the below output :
C:\xampp\apache\bin\httpd.exe
I got surprised to see this output because in output I got complete address of the Apache file httpd.exe. I was expecting to get something about PHP but I got Apache file's address. Why so?
So, what does ultimately mean is I still don't understand what does actually mean by the frequently used term "PHP binary"?
I still don't have any idea about what does actually mean by the very frequently used term "PHP binary"?
Someone, please clear my doubt about the frequently used term "PHP binary" in an easy to understand, simple and lucid language.
I'm waiting for your help.
The compiled version of any program is commonly referred to as it's "binary".
PHP has multiple binaries for multiple purposes.
php.exe is used to run scripts locally in your Command Prompt;
php-cgi.exe is compliant with FastCGI protocol;
php7apache2_4.dll is PHP wrapped as an Apache 2.4 module;
And a few others too.
In this case the docs are referring to php-cgi.exe
The constant PHP_BINARY contains the path of the binary where PHP is currently executing from. Since XAMPP uses Apache, and Apache is most likely using PHP in it's Apache module form, it makes sense that httpd.exe is recognized as the binary, since it is loading php7apache2_4.dll.
PHP itself is running as a program on your computer (often referred to a binary).
Supposed we were on Windows, the PHP binary would probably be php.exe. Essentially, PHP itself.
Note that the PHP binary could be a server module... a DLL or similar.
I am calling a perl script from one php page. This perl script calls some other scripts inturn and a C binary file. The C binary uses mbstowcs function inside. This is not getting executed correctly.
But the same thing If I call from terminal(the perl script) everything works fine.
I have given proper permissions to all the files before executing(Even gave 777 permission).
Is apache uses some other terminal session/ something else to run the scripts.
I am using ubuntu 14.04 and apache webserver.
Without given any further information, I suspect you may have run across the following situation:
Use of non standard mbstowcs feature
This information is dated I realize but it's all I can offer at this time unless you can provide some more information like an error message from your Apache log file.
Sorry I could not be of more help.
The short version of the story is that when I import MySQLdb in a script that I run from the browser, I always get 500 Internal Server Error.
The longer version:
Trying to learn web programming concepts. So far I've done a lot of work with PHP and MySQL using MAMP. I have created a big Javascript/PHP app and a big MySQL db to go with it. Now I'd like to learn Python, and I'd like to start with a familiar environment, namely, my MAMP setup and the db I've created.
I run my PHP scripts by entering localhost:8888/the-script.php in my browser. Now I have python scripts that run the same way, so I know python is working in my MAMP environment. What I haven't figured out yet is how to get my python scripts to talk to the MySQL db.
I installed Django, and I got it to see my db, but it wants to run its own server. I don't want that. I want my python scripts to run on the same server as my PHP scripts (I suppose I could try to get my PHP scripts to run on the Django server, but I'd rather consider that a last resort, to be taken only in desperation).
I've tried installing various packages with pip and macports, and I can run python from the command line and import MySQLdb, and it works fine. I've been all over stackoverflow and tried many of the applicable suggestions. I found a straightforward set of installation instructions that I thought might be the answer. But if I import MySQLdb in a script that I run from the browser, I always get 500 Internal Server Error.
I was hoping to learn python without first having to get a PhD in a lot of other areas like how Apache works internally, rebuilding MAMP and MySQL from the source code, etc. I realize that learning web programming concepts will eventually include those things, but I'm not ready for that. Right now I just want to learn python.
Is there a simple way to get a python script to run in the same environment as my PHP scripts, and interact with the same MySQL db that I've created for use with my PHP scripts?
I've just started reading about php, it needs me to install php, apache and MySql to run any php script. can any one suggest me a simplest method to install php, apache and MySql so that i can sun those php script.
I've tried the zip files from php.net, Are those required to run the script offline for practicing the script? what do i do with them? i felt the things given on the same site a bit complex, and was unable to run the script. please help.
I'm assuming you're using Windows - get wamp - it has exactly what you need:
Apache
MySQL
PHP
Wamp comes with a nifty tray controller app which you can use to access the most common tasks, like restarting services, editing config, etc.
Equivalent to Artem's post, just a different organization which packages it differently. And my preference. It is called XAMPP.
If you are using Windows, you could install XAMPP to have the environment setup for you. You'll then need to learn how to bring up the server (basically executing the XAMPP control panel program), copy your PHP scripts into the correct directories (for XAMPP, it'd be C:/[xampp installation dir]/htdocs), access MySQL and creating the necessary MySQL databases (you could access the MySQL via the included PhpMyAdmin).
You can probably refer to a tutorial here (it's a YouTube video, BTW).
Don't have enough rep to comment but wanted to add something to Artem's recommendation of Wampserver. It's what I use on my windows machine, and one of the nicest things about it I find is the ability to have multiple versions of versions of apache, php and mysql installed alongside each other as plug-ins and then select which to have running at any time. It makes it easy for me to emulate the eventual hosting environment for any given project.