Having Difficulty getting Mongodb to work with PHP - php

I have installed mongodb on a newly installed Ubuntu 18.04 server, and while I am able to work with mongo on the command line, I cannot seem to connect to it using PHP.
I've been following the process as per https://www.youtube.com/watch?v=RQcQ5tvb5E8&t=620s:
apt install mongodb-server
apt install php-pear # to get PECL
apt install php7.2-dev # to get phpize
pecl install mongodb
phpinfo() shows extension_dir=/usr/lib/php/20170718, and mongodb.so is indeed in that directory, with the same ownership and permissions as all other files. php -i | grep extension_dir shows extension_dir => /usr/lib/php/20170718 => /usr/lib/php/20170718
I then added extension=mongodb.so to /etc/php/7.2/apache2/php.ini, in the same location as the other extensions are listed. There is also a php.ini file under /etc/php/7.2/cli/, so I added the line there to. I then restarted the apache2 service.
I created a test file under /var/www/html/mongo.php
<?php
$m = new Mongo();
var_dump($m);exit;
When I browse to that page I get a 'This page isnt working; HTTP ERROR 500' message in Chrome. I have also tried Mongodb();, MongoClient();, MongodbClient();, mongo();, mongodb();, and mongodbclient();, but all to no avail.

Please make sure you have also installed the mongodb php driver. Please confirm the php version first.
On Ubuntu you can just do:
sudo apt-get install php-mongodb
or the same for specific PHP version:
sudo apt-get install php5.6-mongo
or
sudo apt-get install php7.0-mongodb
And then restart the service.
sudo systemctl reload nginx

You should to try some of these
https://docs.mongodb.com/ecosystem/drivers/php/
https://www.php.net/manual/en/mongodb.tutorial.library.php
they should work fine with php7 + composer
Actually with composer is pretty easy(something like this):
`$ composer require mongodb/mongodb
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing mongodb/mongodb (1.0.0)
Downloading: 100%
Writing lock file
Generating autoload files
Create a File: test.php (in the same location where you ran composer)
<?php
require 'vendor/autoload.php'; // include Composer's autoloader
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;
$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
echo "Inserted with Object ID '{$result->getInsertedId()}'";
?>
That should be enough

Related

OLS error while loading shared libraries libargon2.so.0

I am a newbie in Openlitespeed and while configuring OpenLiteSpeed to run my wordpress website, I checked the error.log and found out this error:
No request delivery notification has been received from LSAPI application, possible dead lock
and here it is the content of my stderr.log
/usr/local/lsws/lsphp73/bin/lsphp: error while loading shared libraries: libargon2.so.0: cannot open shared object file: No such file or directory
It means your OS/System need libargon2 package but not exist.
For Debian/Ubuntu, please run
apt-get update -y
apt-get install -y libargon2-0
For CentOS, please run
yum install epel-release
Best

Class "Memcached" not found on Heroku

I am faced with this problem when I run any artisan commands on Heroku. Its a Lumen-PHP Project. I have added the "ext-memcached" to the require section of the composer.json but still gets the same result.
Screenshot of my terminal
I finally got my hands around this and thanks to the Heroku Docs on Memcached. I quote Heroku Docs:
The php-memcached client is not a pure PHP client but a PECL extention that makes use of libmemcached. You thus need to install php-memcached via your OS package manager.
Make sure you have Memcached Installed on your local machine. You can check a gist I created on how to do this for Mac OSx. I will post it here anyways.
brew install libevent
brew install autoconf
brew install libmemcached
//go to
cd /Applications/MAMP/bin/php/php{{VERSION}}/bin
//compile memcached
./pecl install memcached
//go back
cd ../
//Add the memcached.so extension to your php.ini file
echo -e "\n[memcached]\nextension=memcached.so" >> conf/php.ini
//start memcached server
memcached -m 24 -p 11211 -d
//restart MAMPP
You’ll need to modify your composer.json file to include the module:
{
"require": {
"php": ">=7.0.0",
"ext-memcached": "*"
}
}
Ensure that your new requirements are “frozen” to composer.lock by running:
composer update
Afterwards, commit your changes and run
git push heroku master
Thats it!!

Why cant I run bin/behat?

I am trying to run behat on my vendor folder. I have installed composer globally, have installed the behat package, but every time I run bin/behat I keep getting this message from composer
You must set up the project dependencies, run the following commands:
curl -s http://getcomposer.org/installer | php
php composer.phar install
I am not sure how to fix this. I see the files are in the vendor folder, and when I type "composer" on the terminal, I see the manual.
If anyone can help me resolve this I would really appreciate it. Thanks!
There are several possible problems leading to this situation:
Make sure that composer is installed in your $PATH. That is, running composer at a command prompt should work, and you shouldn't need to run an explicit path like ~/Downloads/composer.phar
Execute the composer install instruction as the error message suggests. A common error is the message
Mcrypt PHP extension required
in which case you need to install the specified extension. For example, brew install php56-mcrypt on a Mac or sudo apt-get install php5-mcrypt on Ubuntu.

PHP extensions dependency issue when installing from puppet

does anybody knows how to resolve issue with dependencies for php extensions ?
default.pp
class { 'php':
package => 'php5',
service => 'apache',
version => '5.2.10.dfsg.1-2ubuntu6.5',
require => Package['apache'],
}
package { "PhpModule_mhash":
ensure => '5.2.10.dfsg.1-2ubuntu6.5',
name => 'php5-mhash',
require => [Package['php'], Package['PhpModule_common'], ],
}
when i vagrant up i'm receiving err
err: /Stage[main]//Package[PhpModule_mhash]/ensure: change from purged to 5.2.10.dfsg.1-2ubuntu6.5 failed: Could not update: Execution of '/usr/bin/apt-get -q -y -o DPkg::Options::=--force-confold --force-yes install php5-mhash=5.2.10.dfsg.1-2ubuntu6.5' returned 100: Reading package lists...
Building dependency tree...
Reading state information...
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
php5-mhash : Depends: php5 but it is not going to be installed or
phpapi-20060613+lfs
E: Unable to correct problems, you have held broken packages.
at /tmp/vagrant-puppet/manifests/default.pp:79
My virtual box repository is up to date and all required packages are available. PHP is also installed without problems.
What's very interesting is fact that when i vagrant ssh and execute following command:
/usr/bin/apt-get -q -y -o DPkg::Options::=--force-confold --force-yes install php5-mhash=5.2.10.dfsg.1-2ubuntu6.5
it's installed without any notice...
Is there a way to tell Package that php5 is already installed ?
Is there a way to omit dependencies if i have already required package ?
What options do i have on Puppet or Debian side?
Thanks in advance
Puppet Chaining is the answer !
Even though i've specified that *PhpModule_mhash* package`requires php class i had to insert chaining in my manifest.
Class['php'] -> Package['PhpModule_common'] -> Package['PhpModule_cli'] -> Package['PhpModule_mhash']

Install phpunit on windows

How to install phpunit?
I read documentation https://github.com/sebastianbergmann/phpunit, but have an error:
>pear upgrade PEAR
Nothing to upgrade
>pear config-set auto_discover 1
config-set succeeded
>pear install pear.phpunit.de/PHPUnit
No releases available for package "pear.phpunit.de/PHPUnit"
install failed
How can I fix this error?
Try the following instructions:
In the command prompt, switch to the directory that you installed PHP to by running cd C:\php\
Then install PEAR by running php go-pear.phar
Press Enter to accept the default when it asks you “Are you installing a system-wide PEAR or a local copy?”
Press Enter again to accept the file layout.
Press Enter to finish.
Run the following commands (they may take a while to update, be patient):
pear channel-update pear.php.net
pear upgrade-all
pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear channel-discover pear.symfony-project.com
pear update-channels
Clear your pear cache pear clear-cache
To install PHPUnit, run pear install --alldeps --force phpunit/PHPUnit
To test that PHPUnit was successfully installed, run phpunit -v
Old answer (2014):
It's said that phpunit will not be available via PEAR since December 2014.
So it's easy to install it using composer:
composer global require "phpunit/phpunit=4.1.*"
Update 2019: it should be installed as a local (for your project) development package:
composer require --dev phpunit/phpunit ^8
Update 2020: it should be installed as a local (for your project) development package:
composer require --dev phpunit/phpunit ^9.3
As said by #Wilt Installation via pear doesn't works any longer. Follow below steps instead
Step I: Create a directory named bin in C drive.
Step II: Now add the path C:\bin to your environment.
To do this click on Windows icon and right click on Computer and then
select Properties.
Then click on Advanced system settings -> Advanced
-> Environment Variables.
In the System variables section scroll down and select the line where the Variable column value is Path. Click on
Edit.
Now add (append at the end) ;C:\bin at the end.
Step III: Download phpunit phar file to C:\bin folder.
If you are PHP 7 then download the phar file from
https:// phar.phpunit.de/phpunit-6.2.phar.
Else if you are using PHP
5.6 then download the phar file from https:// phar.phpunit.de/phpunit-5.7.phar.
Once downloaded rename the
file to phpunit.phar and move it to C:\bin folder.
Step IV: Create a batch script phpunit.cmd
Open command prompt. Type cd C:\bin and hit enter.
Then type
echo #php "%~dp0phpunit.phar" %* > phpunit.cmd and hit enter.
To verify PHPUnit has been installed type phpunit --version in command prompt. You should get something like PHPUnit x.y.z by Sebastian Bergmann and contributors.
Reference: https://perials.com/installing-phpunit-windows/
I use Windows XP.
I wasted a lot of time trying to use pear (the proscribed method to get MakeGood working with Eclipse IDE), only to discover the repository for phpunit is no longer available. This information should be nearer the top of this page so people do not waste their time too.
I installed phpunit via the download at https://phpunit.de/ and following the accompanying instructions. By adding the path to the command file to PATH I can now run phpunit from the command line. I have not however got phpunit running in MakeGood/Eclipse. Much of the information on that is out-of-date as it requires pear.
Try this in Windows Subsystem for Linux (wsl):
sudo apt install phpunit

Categories