Category Archives: PHP

Fix “Fatal error: Allowed memory size of X bytes exhausted (tried to allocate X bytes)” with composer update

I got this error today when running composer update for one of my Symfony2 projects:

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 3781182 bytes) in phar:///usr/local/bin/composer/src/Composer/Util/RemoteFilesystem.php on line 202

logo-composer-transparent

Composers troubleshooting tells that you can increase the memory PHP can allocate editing memory_limit parameter in php.ini. I changed it from 128M to 512M but still didn’t work.

Finally the solution was to run composer update with no memory limit this way:

php -d memory_limit=-1 /usr/local/bin/composer update

Install MongoDB in Ubuntu 12.04 with PHP driver

Install MongoDB:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install mongodb-10gen

Install MongoDB PHP driver:

sudo apt-get install php-pear php5-dev
pecl search mongo
sudo pecl install mongo

Configure MongoDB PHP driver: Create a file called /etc/php5/mods-available/mongo.ini with this content:

extension=mongo.so

And create a symlink to it in /etc/php5/conf.d/:

cd /etc/php5/conf.d
sudo ln -s ../mods-available/mongo.ini 20-mongo.ini

Then restart Apache:

sudo service apache2 restart

Start, stop and restart MongoDB:

sudo service mongodb start
sudo service mongodb stop
sudo service mongodb restart

Configuration file:

/etc/mongodb.conf

Log file:

/var/log/mongodb/mongodb.log

Data files:

/var/lib/mongodb

Ref: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

Nowdoc and heredoc in PHP

From the manual:

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.

Look that the difference between those 2 is that nowdocs uses single quotes when defining the tag (that can be anything you want) and heredocs doesn’t.

$foo = 'bar';

$now = <<<'NOW'
    I'm now, $foo!
NOW;

$here = <<<HERE
    I'm here, $foo!
HERE;

In this case:

$now is "I'm now, $foo!"

$here is "I'm here, bar!"

Ref: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
http://stackoverflow.com/a/11153164

Using MacPorts php from command line

Some time ago I had installed Apache+MySQL using MacPorts in Mac OS X. It was all OK as long as I used the web browser to navigate thru the web pages, but as I runned PHP via CLI to execute Symfony2 commands I started having problems. It said for example that my time zone was not set, and I knew for sure that I had configured it in the php.ini file.

$ php app/console doctrine:schema:update --force

  
[ErrorException] Warning: date_default_timezone_get(): It is not safe to rely on the system' s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those me thods and you are still getting this warning, you most likely misspelled th e timezone identifier. We selected 'Europe/Berlin' for 'CET/1.0/no DST' ins tead in /Users/enekochan/Documents/Webs/este.es/vendor/monolog/monolog/src/ Monolog/Logger.php line 199

          

Searching the web for a solution I realized that the problem was that I was running the binary php that comes with Mac OS X, and I didn’t have the timezone configured for it. You can see where the php.ini file is by running this:

$ php -i | grep 'Configuration File'
Configuration File (php.ini) Path => /etc
Loaded Configuration File => (none)

The first solution i came up with was to use the full path for the MacPorts php binary file:

$ /opt/local/bin/php app/console doctrine:schema:update --force
$ /opt/local/bin/php -i | grep 'Configuration File'
Configuration File (php.ini) Path => /opt/local/etc/php5
Loaded Configuration File => /opt/local/etc/php5/php.ini

A better solution consists on adding to the systems path /opt/local/bin before /usr/bin. This can be done adding this to ~/.bash_profile:

PATH=/opt/local/bin:$PATH;export PATH

Then you have to close and open a new terminal or source the newly edited ~/.bash_profile:

$ source ~/.bash_profile

Ref: Stackoverflow

Install and configure MongoDB and PHP driver with MacPorts

Installing and configuring MongoDB with MacPorts

Installing MongoDB with MacPorts is as easy as running this command:

$ sudo port install mongodb

MongoDB uses /data/db as the default path for storing its data files, but it’s likely that your won’t have that folder and running MongoDB server (mongod) will end with this error:

*********************************************************************
 ERROR: dbpath (/data/db/) does not exist.
 Create this directory or give existing directory in --dbpath.
 See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************

There are several options:

Create that folder and configure it with the correct permissions:

$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db

Supply another path as the data storage folder with the --dbpath option:

$ mongodb --dbpath /var/lib/mongo

And the best of all, create the necessary paths for data and log files and also the configuration file to specify those paths:

$ sudo mkdir /opt/local/var/db/mongodb
$ sudo mkdir /opt/local/var/log/mongodb
$ sudo touch /opt/local/var/log/mongodb/mongodb.log
$ sudo mkdir /opt/local/etc/mongodb/

Add this to /opt/local/etc/mongodb/mongodb.conf file:

# Store data alongside MongoDB instead of the default, /data/db/
dbpath = /opt/local/var/db/mongodb

# Only accept local connections
bind_ip = 127.0.0.1

# Running as daemon
fork = true

# Take log
logpath = /opt/local/var/log/mongodb/mongodb.log
logappend = true

You can then run mongod specifying that configuration file:

$ sudo mongod -f /opt/local/etc/mongodb/mongodb.conf

An easier way is to use the load/unload option that comes with MacPorts that defines all those configuration values in /opt/local/etc/LaunchDaemons/org.macports.mongodb/org.macports.mongodb.plist file. You can load and unload MongoDB with those commands (using them will make MongoDB autoload on boot!):

$ sudo port load mongodb
$ sudo port unload mongodb

More configuration options: http://docs.mongodb.org/manual/reference/configuration-options/

Installing the PHP driver for MongoDB

Before installing the driver we need to be sure that we have pear and pecl installed. Run this commands to see if they return some value:

$ pear version
$ pecl version

Now we have to install the PHP driver with pecl:

$ sudo pecl install mongo

When it finishes the installation you will see something like this:

Build process completed successfully
Installing '/opt/local/lib/php/extensions/no-debug-non-zts-20090626/mongo.so'
install ok: channel://pecl.php.net/mongo-1.3.4
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongo.so" to php.ini

That last line means that you have to edit yourself the php.ini file, which in my case is in /opt/local/etc/php5/php.ini, and add the extension load for mongo. You can add that line at the end of the file but it is supposed to be in the “Dynamic Extensions”.

Now restart Apache and you’ll be ready to use MongoDB in PHP.

$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start

Ref: http://api.mongodb.org/wiki/current/PHP%20Language%20Center.html
http://noteit.jiaeil.com/install-mongodb-via-macports-on-mac
http://stackoverflow.com/questions/8029064/new-to-mongodb-can-not-run-command-mongo

  • Page 1 of 2
  • 1
  • 2
  • >