How to solve this error on live server for laravel?

ぐ巨炮叔叔 提交于 2020-05-09 04:55:45

问题


I have installed this package success locally composer require spatie/laravel-image-optimizer, but when I tried to install on the live server I got this errors

no@zz607:/var/www/html$  composer require spatie/laravel-image- 
optimizer
Using version ^1.4 for spatie/laravel-image-optimizer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)

mmap() failed: [12] Cannot allocate memory

mmap() failed: [12] Cannot allocate memory

mmap() failed: [12] Cannot allocate memory
PHP Fatal error:  Out of memory (allocated 544395264) (tried to 
allocate 20480 bytes) in 
/usr/share/php/Composer/Repository/ComposerRepository.php on line 321

Fatal error: Out of memory (allocated 544395264) (tried to allocate 
 20480 bytes) in 
 /usr/share/php/Composer/Repository/ComposerRepository.php on line 321

server info


回答1:


Your problem is related to the probable fact of having only 1gb of RAM and not having memory swap, so let's create and enable it to take some time off at the time of composer execution.

Folow this steps:

1) Creating 1gb memory swapfile:

$ sudo fallocate -l 1G /swapfile

$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

This command created the /swapfile file with 1GB in size.

2) Configuring file to swap:

$ sudo chmod 600 /swapfile

$ sudo mkswap /swapfile

3) Enable swap:

$ sudo swapon /swapfile

To see result you can run free -m or see on htop

Example: $ sudo free -m

              total       usada       livre    compart.  buff/cache  disponível
Mem.:          7664        1052        3436          56        3175        6259
Swap:          1024           0        1024

Example: $ sudo swapon --show

NAME      TYPE SIZE USED PRIO
/swapfile file   1G   0B   -2

Test please.




回答2:


You should not call composer require or composer update on production server - you should run it locally, verify that everything works fine, and commit generated composer.lock with your project. Then on production server you need to run only composer install --no-dev - it will install all non-dev dependencies defined in lock file. composer install is much faster and uses fraction of RAM required by composer require or composer update, so it should work fine even on server with low memory. It also gives you more control on version of libraries installed on production server, so you can test app more reliable.



来源:https://stackoverflow.com/questions/55069471/how-to-solve-this-error-on-live-server-for-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!