php page gives 500 error but executed via ssh works - php

I'm missing something easy I think:
Testing a mailgun install on an EC2 Linux instance.
The following code works when I use a putty session:
php /var/www/html/[thefilebelow.php]
But fails when I go to a browser and use
http://myexample.com/[thefilebelow.php]
That gives a 500 error
[thefilebelow.php]:
# Include the Autoloader (see "Libraries" for install instructions)
require '/home/ec2-user/vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('kxxxxxxxxxx');
$domain = "mg.myexample.com";
# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
'from' => 'bob <info#lxxxxx.com>',
'to' => 'Steve <xxxxx#gmail.com>',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomness!'
));
ERROR LOG:
PHP Fatal error: require(): Failed op ning required '/home/ec2-user/vendor/autoload.php' (include_path='.:/usr/share/pear7:/usr/share/php7') in /var/www/html/myfilebelow.php on line 3
Just to be clear - the location of the require file is correct.
Permissions for /var/www/htmlmyfilebelow.php ec2-user:www
Permissions for /home/ec2-user/vendor/ ec2-user:www
(permissions are the same for include file and script)

So the answer is that when installing composer on an ec2 instance (and generally on other servers), be sure to navigate out of the landing directory when opening a an ec2-user session in putty, if the landing directory is your user directory.
Move up to /var/www/ or some other directory owned by the www group.

Related

PHP fails to access the docker socket via curl

My Code fails to reach the Docker Socket
$client = new GuzzleHttp\Client();
$test = $client->request('GET','http://v1.40/containers/json',[
'curl' => [CURLOPT_UNIX_SOCKET_PATH => '/var/run/docker.sock']
]);
I only get a generic cURL error 7 from that and I´ve checked that the socket is available and working inside the Container with the cURL command from the cmd. Its only when i try to connect via PHP it fails ominously and frankly im out of ideas.
So just in case someone stumbles upon this in the future and has the same or a similar problem.
Guzzle was not the problem in this case but phpfpm. I did not realize that the phpfpm workers in the official php docker image used the www-data user by default. The way I used is to change the user in the www.conf (default /usr/local/etc/php-fpm.d/www.conf in docker)
user = root
group = root
you will have to append the -R flag to the run command to allow running the workers as root.

Error GRPC Spanner Google Cloud With PHP

I'm using PHP to try using the Google Cloud Spanner. I already did the gCloud settings and everything, and that's right. Now I need to make the connection via PHP to do a CRUD with the database that is in Spanner, but the code below always returns the error:
PHP Fatal error: Undefined constant 'Grpc\STATUS_UNKNOWN' in
/xxx/xxxx/www/vendor/google/cloud-spanner/Connection/Grpc.php on line
129
The code I have is:
<?php
require 'vendor/autoload.php';
use Google\Cloud\Spanner\SpannerClient;
/* Error start here */
$spanner = new SpannerClient([
'projectId' => 'my-project-id'
]);
$db = $spanner->connect('instance', 'database');
$userQuery = $db->execute('SELECT * FROM usuario WHERE login = #login', [
'parameters' => [
'login' => 'devteam'
]
]);
$user = $userQuery->rows()->current();
echo 'Hello ' . $user['login'];
The requirements I use in the composer are:
"require": {
"google/cloud": "^0.32.1",
"google/cloud-spanner": "^0.2.2"
}
I noticed that if I enter through the browser, the error presented above continues to appear. If I run the command php teste.php on the terminal, it runs the script correctly, ie, the terminal works and the browser does not.
Google Cloud PHP's spanner client is gRPC only. This means to use it you will need to install the gRPC PHP extension:
pecl install grpc
Once you have done that, add google/proto-client-php and google/gax to your composer.json and run composer update. After this is done, the error will be resolved.
For those wanting more detailed instructions, see this page for installing and enabling gRPC for PHP!
Since you mentioned that it works on CLI but not on browser, I can say that you need to enable the grpc extension on your php web server config.
E.g. Add
extension=grpc.so to your /etc/php/5.6/apache2/php.ini

How to load php_redis.dll when running post deployment script in Kudu Console

I have a webapp running on Azure App Service. I am trying to test a Post deployment script in Kudu Debug CMD console.
php -d extension=php_redis.dll -f postdeploy.php
Here is my postdeploy.php file
<?php
ini_set('error_reporting', -1);
ini_set("display_errors", 1);
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
set_exception_handler('exception_handler');
$redis = new Redis();
...
?>
I get the following error
Fatal error: Class 'Redis' not found in D:\home\site\deployments\tools\PostDeploymentActions\postdeploy.php on line 13
PHP Warning: PHP Startup: Unable to load dynamic library 'D:\Program Files (x86)\PHP\v5.6\ext\php_redis.dll' - The specified module could not be found.
in Unknown on line 0
I uploaded the proper version of php_redis.dll to the PostDeploymentActions folder. This is the same binary that the webapp is using via the app setting in portal PHP_EXTENSIONS = bin\php_redis.dll. However, I am not sure how to load this for this script.
Is there a way that I can load the php_redis.dll in Kudu post deployment script?
I tried php -d extension=./php_redis.dll -f postdeploy.php, php -d extension=%~dp0php_redis.dll -f postdeploy.php and other weird combinations with no luck.
Please try to add the PHP extension via ini settings, refer to https://azure.microsoft.com/en-us/documentation/articles/web-sites-php-configure/#how-to-enable-extensions-in-the-default-php-runtime at the Configure via ini settings section.
Generally:
Add an App Setting to your Web App with the key PHP_INI_SCAN_DIR and value d:\home\site\ini
Create an configuration file in d:\home\site\ini called extensions.ini
Add configuration settings to the extensions.ini file using the same
syntax you would use in a php.ini file. For example: extension =
php_redis.dll.
Restart Web Apps service.
Via this approach, you can configure the PHP extension into Kudu console site's PHP runtime. And configure via the App Settings will only configure the extension into IIS.
Any further concern, please feel free to let me know.

Installing Slim, PHP, Ubuntu

I'm trying to install Slim on my local LAMP server on Ubuntu only I get stuck at second base.
From my tutorial and various documentation found online:
You now have access to the composer command. Sure enough if I go to
the terminal and enter:
$ composer
Composer version b474944155429eb4cce186c746d55287ee6bb3f4
Usage:
[options] command [arguments]
The next step is to specify Slim as a required package for your app.
This can be accomplished, via a composer.json file within the root of
your project.
Where is the root of my project? I thought it would be
/var/www/slim
I've tried adding composer.json to:
/var/www/slim
and stood in /slim define an index.php script with:
<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
?>
Go to
http://localhost/var/www/slim
and the browser returns:
Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in /var/www/slim/index.php on line 2
Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/slim/index.php on line 2
Well, there seems to be more then one question in your post, but regardless ...
"Where is the root of my project?"
The root of your project should be in /var/www if you haven't changed your hosts / Apache settings.
Then on to your 2nd question, which I will take the liberty to rephrase :)
"How to create your web App, and include the composer-installed packages inside?"
go to your respective web-root directory, likely in your case /var/www and in it create "index.php". Then, there, in the console, run the command:
composer install
This should install the packages defined in your composer.json, which should in the same web root dir.
If everything went OO, you will have in the following a new directory: /var/www/vendor
Now, go to your web-root directory and create your index.php and in it, in the beginning add the following lines:
require 'vendor/autoload.php';
// to load all the packages installed with composer, not only slim. If
//composer was run in the right directory and w/o problems of course :)
$app = new \Slim\Slim();
// to instantiate Slim script instance and assign it to the pointer $app
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
})
//to create your 1st route ...
you need to run
composer install
from terminal. After that, add
$app->run();
in index.php.

PHP Error Class 'SoapClient' not found

I was trying to install Wordpress on our server to test, but in order to do that I had to upgrade our current PHP version to 5.3. I upgraded our PHP and I am now finding that our Purolator shipping module is no longer working. According to our host support the "purolator.php" file is missing data that the new version of PHP requires. Unfortunately, I can't revert our PHP version back and our programmer is away.
This is the error message I was able to get from our website host support team:
tail /var/www/vhosts/phantomcables.com/statistics/logs/error_log -f
[Thu Jul 19 08:34:28 2012] [error] [client 70.51.168.201] PHP Fatal
error: Class 'SoapClient' not found in
/var/www/vhosts/phantomcables.com/httpdocs/shippings/purolator.php on line
49, referer: https://phantomcables.com/index.php?dispatch=checkout.cart
These are lines 40 to 72:
function createPWSSOAPClient()
{
/** Purpose : Creates a SOAP Client in Non-WSDL mode with the appropriate authentication and
* header information
**/
//Set the parameters for the Non-WSDL mode SOAP communication with your Development/Production credentials
//echo DIR_SHIPPING_FILES."estimatingservice.wsdl";
$url = "https://webservices.purolator.com/PWS/V1/Estimating/EstimatingService.asmx";
$client = new SoapClient( DIR_SHIPPING_FILES."estimatingservice.wsdl",
array (
'trace' => true,
'location' => $url,
'uri' => "http://purolator.com/pws/datatypes/v1",
'login' => PRODUCTION_KEY,
'password' => PRODUCTION_PASS
)
);
//Define the SOAP Envelope Headers
$headers[] = new SoapHeader ( 'http://purolator.com/pws/datatypes/v1', 'RequestContext',
array (
'Version' => '1.0',
'Language' => 'en',
'GroupID' => 'xxx',
'RequestReference' => 'Rating Example'
)
);
//Apply the SOAP Header to your client
$client->__setSoapHeaders($headers);
return $client;
}
Any help would be greatly appreciated.
It seems like you did not have installed the php-soap package on your server.
Type
phpinfo();
in your code and check if 'Soap Client' is 'enabled'.
If not, I don't know your distribution and package manager, but here is the code for Fedora I use:
$ yum install php-soap
I suggest to do it with your programmer back or a sys admin by your sides, you never know what could happen. A backup is also useful before any installation!
You will need to add (or remove any comment characters before)
extension=soap.so
to your php.ini file, and then restart the web server.
If you are having trouble finding your php.ini file, create a page with the following:
<?php phpinfo();
and load that file in your browser.
On some systems, this setting may not actually live in php.ini. For example, if you are using Zend Server, the setting would live in /usr/local/zend/etc/conf.d/soap.ini.
So we reverted back to an older version of PHP, and re-enabled SOAP. apparently that was the issue, not sure why that didn't work with the newer version of PHP.
Again thank you for all of your help. This is a great community.
It is already posted here
Do the following:
Locate php.ini in your apache bin folder, I.e Apache/bin/php.ini
Remove the ; from the beginning of extension=php_soap.dll
Restart your Apache server
Look up your phpinfo(); again and check if you see a similar picture to the one above
If you do, problem solved!
Enable the SOAP extension, require it in your composer.json like so:
{
"require": {
"ext-soap": "*"
}
}

Categories