Ubuntu Lamp php script with mySQL connection not using Apache - php

Hi I got a strange problem with my LAMP installation
I'm using an Ubuntu Server with PHP5 Apache and mySQL, all runs great when accessing via http.
But the thing is, I also want to trigger some scripts using bash, basically so I can trigger them at will and prevent them being used via Apache/HTTP
The scripts themselves run when called with php5, all includes are found.
But for some unknown reason mySQL does not seem to initiate the Database the way it does via Apache. I just get a message back saying no database is connected.
Is there some special setup for bash related php calls?

Figured it out. There are no special DB settings for PHP run from Bash, However because I work on several Servers I was using the $_SERVER['HTTP_HOST'] variable to determin which login credentials should be used. Therefore it only worked on the server where the default credentials were correct.
For now I just put in an extra option in the code that uses credentials based on the value of the dirname(__FILE__) variable. Works great.

Related

How to open an external GUI application from web browser using PHP?

I'm new to web development. I'm trying to execute a shell script using PHP's shell_exec(). Inside the script, I'm trying to invoke a GUI application(Qt). When I executed the PHP script from a terminal the application started as expected. But when I opened it from browser an empty blank page appeared.
I'm using Ubuntu with apache2 server running as service. When I searched in google, the similar problem is solved in the Windows environment by allowing apache service to interact with the desktop.
PHP Script:
<?php
$log = shell_exec('sh testcmd.sh');
?>
testcmd.sh:
./Program1
Any help provided will be highly appreciated.
It is somewhat unclear what you're asking.
If you wish that browsing to a certain web site will run a PHP script that will open a GUI app for the client to interact with, the answer is "you can't". The reason is that the way the setup works is that the server and the client run on different machines, and your PHP runs on the server machine. As such, the client never gets to see the running program.
The above is true also for Windows. The answer you quote in your question does not apply to a server running on a different machine than the client.
If, for whatever reason, you want something that works only when the server and client run on the same machine (or there is someone watching the server's display), then you need to do the equivalent of the Windows answer.
The graphics display on Linux (assuming you're not running wayland) is using a protocol called X11. In order for the display to appear, your GUI program needs two things. The first is to know which display it needs to use. This is supplied with an environment variable called DISPLAY. The second is an authorization to actually use that display.
So in order for your PHP script to run a GUI app that will show its GUI, you will need to first do the following steps:
Set the DISPLAY variable to the correct value (copy from your desktop environment).
Run xauth add something, where you can get what something is by running xauth list on your desktop environment.
If you do these two things (in this order), your GUI should show up.

Openshift: databases environment variables are not set in PHP

I've created a php application on Openshift. Then I added a MySQL database to my app. Afterward I can see database related variables in SSH shell with env command.
However when I tried to use environment varibles to setup db connection, it does not retrun them to my php code. As a example:
<?php
echo('App: '.$_ENV["OPENSHIFT_APP_NAME"].'<br/>');
echo('Host: '.$_ENV["OPENSHIFT_MYSQL_DB_HOST"]);
OPENSHIFT_APP_NAME returns Application name correctly however OPENSHIFT_MYSQL_DB_HOST is null and host information is not available.
I also restarted my application in console.
As a notice I used WinScp to directly upload my application and I'm not sure if deployment of php application needs anything more or less (like some require stuff)?
Update:
When I checked phpMyAdmin codes, I saw it have a file 'conf.d/passenv.conf' with syntax like this:
PassEnv OPENSHIFT_MYSQL_DB_PORT
My be it help for resolving my problem.
You need to stop and start (NOT restart) your application, since you added MySQL after your application was created.
use the rhc app stop and rhc app start commands.

Testing my php scripts and forms

I have two html forms and two php scripts. The first one is a form which is supposed to submit a users email to a .txt file, and the other is a Stripe payment form which uses php code to charge the customer.
Now my problem is that there are some issues with the two php scripts, that I can't figure out how to fix, because I am not really sure how to test the scripts. Normally when testing the html scripts I would just open the html files in my browser, but that doesn't work on my php scripts as the site just shows what is written in the scripts when called/submitted.
So my question is how do I test my scripts, without having to use a hosting account and can I even test it like this?
You need to run a web server on your local computer to test it out. I would suggest looking into something called Vagrant, which allows you to fairly easily create virtual machines on your computer in which you can install anything you like without fear of messing up anything else. If you go here you can even find a "box" to create a virtual machine that already has apache and php installed.
Depending on your version of PHP there's a webserver builtin right into PHP:
http://php.net/manual/en/features.commandline.webserver.php
You probably have to install a web server locally with php enabled. Since php is a server side language it needs a server to run on, in order to send you back the html, after execution. If you need to see the result on a browser.
http://en.wikipedia.org/wiki/LAMP_%28software_bundle%29.
Or run your script from terminal (you must have php installed) if you don't need to see the result in the browser and just want to run your script.
$ php myscript.php

Google Chrome can't connect to localhost

I'm trying to follow a tutorial for my first php project in netbeans. I select new php application name it and then simply type echo("anything") in the php portion of index.php and when I run it I always get the error Oops! Google Chrome could not connect to localhost. I don't understand why because I also have a Java web application(selection from netbeans) and when I put stuff in the html I can get things to run just fine. How can I get rid of this issue?
Make sure that PHP is running on your machine (XAMP or WAMP) and it should be echo "anything";
PHP requires that the PHP parser is running on a server. The error comes from a lack of this. HTML requires no parser so it can be sent as is. The java web application may have a module to enable PHP, and you likely need to either enable it or tell it where a PHP binary is; if not both.
Alternatively, you can use XAMPP/MAMPP to host the server; just make sure it is not running on the same port as your java host.
You can get XAMPP/MAMPP here:
www.apachefriends.org

Executing PHP script based on Outlook rule

I am getting daily data dump via e-mail, which is being processed by Access (based on the Outlook rule, VBA is extracting the attachment and running Access procedures, so I get a report).
As data dump is getting bigger and bigger, and having in mind that Access is run locally which consumes my resources, I want to set up a PHP/MySQL server to make it more efficient.
The first challenge I face is how to connect Outlook rule with PHP execution? (as I will have Outlook set up on the Windows based machine, with apache/mysql set up (WAMP))
Anyone can share some insights on how to start PHP execution from Outlook?
Thanks for the help!
Srdjan
If you have an existing setup using Access and VBA, and you just need a better database behind it, could you not just use MySQL without PHP and use the MySQL ODBC driver instead of the Access connection you are using at the moment (assuming you are currently using ODBC to connect to the access database)
This way you wouldn't even have to have mysql running locally if the hit is too high on your local machine and it should be pretty straightforward in that you shouldn't have to make to many changes to your vba code.
Of course, whether this will do it depends on what else is going on in your access db
If not you could try using php and using the PHP Command Line Interface (CLI) which you should be able to call as an external executable from Outlook. Just pass php.exe the name of the php script you want to run
UPDATE:
I am not a VBA expert by any means but it looks like the shell function would let you run the PHP CLI from within VBA
Shell("path/to/php.exe phpscript.php")
http://msdn.microsoft.com/en-us/library/xe736fyk(VS.71).aspx
Of course this will only work if you can get to php.exe on the wamp machine (ie is a local machine or you have network access to the appropriate folder)
Alternatively, if the Wamp server is to be a separate machine then you could trigger the php script to be run by calling a url. I think the XMLHTTP object will do this for you
Dim xmh As Object
Set xmh = CreateObject("MSXML2.XMLHTTP")
xmh.Open "GET", "http://urlofphpserver/script.php", False
xmh.Send

Categories