If you use AWS Free Tier (12 Month Introductory Period) and did all steps according to our article " Setting Up WordPress on Amazon EC2" you may see "-bash: fork: Cannot allocate memory" issue. How can you resolve it?

Elastic Compute Cloud (EC2) characteristics

750 hours of Amazon EC2 Linux t2.micro instance usage (1 GiB of memory and 32-bit and 64-bit platform support) – enough hours to run continuously each month*

750 hours of Amazon EC2 Microsoft Windows Server† t2.micro instance usage (1 GiB of memory and 32-bit and 64-bit platform support) – enough hours to run continuously each month*

750 hours of an Elastic Load Balancer shared between Classic and Application load balancers, 15 GB data processing for Classic load balancers, and 15 LCUs for Application load balancers*

30 GB of Amazon Elastic Block Storage in any combination of General Purpose (SSD) or Magnetic, plus 2 million I/Os (with EBS Magnetic) and 1 GB of snapshot storage*

500 MB-month of Amazon EC2 Container Registry storage for new customers*

How you may see the issue?

  1. When you login via SSH with my user with sudo privileges, every command you run results in a “Cannot allocate memory” error
  2. Website doesn't work
  3. In logs you may see this issue (httpd log).

First aid

Reboot your EC2 - it will help you for the first time, but later you will see this issue gain (just go to your client control panel and select "Reboot"),ssh in and then run top or htop. Keep an eye on the memory usage and see what process is using up all the memory. Some process is leaking memory. To get an idea of which process this might be, run

ps --sort -rss -eo rss,pid,command | head

In our case it was httpd service.

Steps to resolve it

  1. Something good to do is to add swap memory, as it allocates more memory if you're running out.
  2. Increase memory limit for your php and wordpress.
  3. Add Wordpress cache plugin.
  4. Create rules to deny access for ip addresses who want to hack your website.

Swap memory

One of the easiest way of increasing the responsiveness of your server and guarding against out of memory errors in your applications is to add some swap space. Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM.

Check is the system is using swap

sudo swapon -s

 

Filename Type Size Used Priority

If you only get back the header of the table, as I've shown above, you do not currently have any swap space enabled. Another way to check using of swap memory is free command:

free -m
             total       used       free     shared    buffers     cached
Mem:          3953        154       3799          0          8         83
-/+ buffers/cache:         62       3890
Swap:            0          0          0

Swaps uses 0. Ok. We can setup swap using. Before we should be aware of our current disk usage. We can get this information by typing:

df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        489M   56K  488M   1% /dev
tmpfs           497M     0  497M   0% /dev/shm
/dev/xvda1      7.8G  2.2G  5.6G  28% /

As you can see on the first line, our hard drive partition has 448M Gigabytes available, so we have a huge amount of space to work with. e can sreate swap file for 256M. We will create a file called swapfile in our root (/) directory

sudo dd if=/dev/zero of=/swapfile bs=256M count=1
sudo chmod 600 /swapfile

We can see that 256M have been allocated by typing:

ls -lh /swapfile
-rw------- 1 root root 256M Jul  8 18:47 /swapfile

Now that our file is more secure, we can tell our system to set up the swap space by typing:

sudo mkswap /swapfile
sudo swapon /swapfile

We can verify that the procedure was successful by checking whether our system reports swap space now:

sudo swapon -s
Filename				Type		Size	Used	Priority
/swapfile                              	file	255284	44380	-1

We have our swap file enabled, but when we reboot, the server will not automatically enable the file. We can change that though by modifying the fstab file.

vi /etc/fstab

At the bottom of the file, you need to add a line that will tell the operating system to automatically use the file you created:

/swapfile   none    swap    sw    0   0

Now we need to configure our swap. There are a few options that you can configure that will have an impact on your system's performance when dealing with swap. The swappiness parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage.With values close to zero, the kernel will not swap data to the disk unless absolutely necessary. Values that are closer to 100 will try to put more data into swap in an effort to keep more RAM space free. For a Desktop, a swappiness setting of 60 is not a bad value. For a VPS system, we'd probably want to move it closer to 0.

cat /proc/sys/vm/swappiness
60

We can setup any numbers:

sudo sysctl vm.swappiness=10

This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our /etc/sysctl.conf file:

sudo vi /etc/sysctl.conf

At the bottom, you can add:

vm.swappiness=10

Another related value that you might want to modify is the vfs_cache_pressure. This setting configures how much the system will choose to cache inode and dentry information over other data. Basically, this is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it's an excellent thing for your system to cache. You can see the current value by querying the proc filesystem again:

cat /proc/sys/vm/vfs_cache_pressure
100

As it is currently configured, our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:

sudo sysctl vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50

Again, if want to save these settings after reboot we should add row at the bottom of sysctl.conf file:

sudo vi /etc/sysctl.conf
vm.vfs_cache_pressure = 50

 Increase memory limit

 Now we will talk how to increase the maximum upload and PHP memory limit.

    • Upgrade your php.ini file.

find / -name "php.ini"

vi /etc/php.ini

Check and update next variables:

upload_max_filesize = 1000M
post_max_size = 2000M
memory_limit = 512M
file_uploads = On
max_execution_time = 180
    • Update your .htaccess file

Add to the bottom of your .htaccess file

php_value upload_max_filesize 1000M
php_value post_max_size 2000M
php_value memory_limit 3000M
php_value max_execution_time 180
php_value max_input_time 180

Update your wp-config.php File

Add the row:

define('WP_MEMORY_LIMIT', '512M');
    • Restart your httpd service:
service httpd restart

Update your .htaccess file

Check you httpd log

more /var/log/httpd/access_log

Find code like that:

83.247.31.129 - - [09/Jul/2017:13:54:26 +0000] "HEAD http://52.14.87.117:80/mysql/admin/ HTTP/1.1" 301 - "-" "Mozilla/5.0 Jorgee"
83.247.31.129 - - [09/Jul/2017:13:54:28 +0000] "HEAD http://52.14.87.117:80/mysql/sqlmanager/ HTTP/1.1" 301 - "-" "Mozilla/5.0 Jorgee"
83.247.31.129 - - [09/Jul/2017:13:54:28 +0000] "HEAD http://52.14.87.117:80/mysql/mysqlmanager/ HTTP/1.1" 301 - "-" "Mozilla/5.0 Jorgee"
83.247.31.129 - - [09/Jul/2017:13:54:29 +0000] "HEAD http://52.14.87.117:80/phpmyadmin/ HTTP/1.1" 301 - "-" "Mozilla/5.0 Jorgee"
83.247.31.129 - - [09/Jul/2017:13:54:29 +0000] "HEAD http://52.14.87.117:80/phpMyadmin/ HTTP/1.1" 301 - "-" "Mozilla/5.0 Jorgee"
83.247.31.129 - - [09/Jul/2017:13:54:30 +0000] "HEAD http://52.14.87.117:80/phpMyAdmin/ HTTP/1.1" 301 - "-" "Mozilla/5.0 Jorgee"
83.247.31.129 - - [09/Jul/2017:13:54:30 +0000] "HEAD http://52.14.87.117:80/phpmyAdmin/ HTTP/1.1" 301 - "-" "Mozilla/5.0 Jorgee"
83.247.31.129 - - [09/Jul/2017:13:54:31 +0000] "HEAD http://52.14.87.117:80/phpmyadmin2/ HTTP/1.1" 301 - "-" "Mozilla/5.0 Jorgee"
83.247.31.129 - - [09/Jul/2017:13:54:31 +0000] "HEAD http://52.14.87.117:80/phpmyadmin3/ HTTP/1.1" 301 - "-" "Mozilla/5.0 Jorgee"
83.247.31.129 - - [09/Jul/2017:13:54:32 +0000] "HEAD http://52.14.87.117:80/phpmyadmin4/ HTTP/1.1" 301 - "-" "Mozilla/5.0 Jorgee"

Check this ip address from Who is service

netnum:        83.247.27.0 - 83.247.31.255
netname:        SOLCON
descr:          Solcon DSL BabyXL
remarks:        INFRA-AW
country:        NL
admin-c:        SOLT1-RIPE
tech-c:         SOLT1-RIPE
status:         ASSIGNED PA
mnt-by:         SOLCON-NL-MNT
created:        2005-01-06T12:51:44Z
last-modified:  2009-04-11T12:17:42Z
source:         RIPE # Filtered

role:           Solcon Technical Role Account
address:        Dutch
address:        Solcon Internetdiensten NV
address:        P.O. Box 127
address:        8250 AC Dronten
address:        The Netherlands
phone:          +31 88 0032222

If you have website for USA users only you can create rule and deny access for this ip in the .htaccess file. Need to check ip addresses in the USA and add these rows at the top of your .htaccess file located in your website root directory. We are not welcome ip addresses who tried to hack our website.

order deny,allow
deny from 83.0.0.0/8
allow from all

restart your httpd service:

service httpd restart

We hope these rules help you to create robust and stable website