Saturday September 10th, 2016


How to tune Wordpress running on AWS t2.micro

In this tutorial I will walk you through on how to tune your Wordpress running on AWS t2.micro. An EC2 instance with a t2.micro is part of the amazon Free Tier which includes services with a free tier for 12 months from the date you've signup on AWS by creating your account. Those Free Tier services can be used within certain usage limits without your credit card being charged. This makes the t2.micro a good choice for a simple Wordpress blog.

Problem

After creating my Wordpress blog on AWS t2.micro instance, I started running into some issues. Sometime, I have to restart my running services and mysql service was sometime stopped for some reasons. After some investigations, I figured out that it was some low memory issues since the t2.micro only provides approximately 1 GB of memory. On top of not having too much memory, the t2.micro does not have the swap partition. The investigation has shown that 90% of the memory was constantly in use which is obviously not good.


Tweak the following

Apache

All the apache tweaking will be done in the apache httpd.conf which is the main configuration file located under /etc/httpd/conf/httpd.conf . The following setting values or called directives will be changed.

  • TIMEOUT defines in second the total amount time the server will wait for I/O. The default value is 300 and since I am running a simple blog if the page load time exceeds 25 seconds, I will assume something is going terrible wrong. So set this directive to the following: TimeOut 30
  • KEEPALIVE by default is set to off . Enable KeepAlive which will provide long-lived HTTP session which in turn will allow multiple requests per connection (the same TCP connection). So enable this directive KeepAlive on
  • KEEPALIVETIMEOUT defines in seconds the amount of time the server will wait for a subsequent request before it closes the TCP connection. Since we've enabled the KeepAlive, therefore it's a good idea to set KeepAliveTimeout low due to the fact that when the server is becoming busy it can quick spawn the maximum number of child processes which will dramatically slow down the server. So set this directive to the following value: KeepAliveTimeout 10
  • MAXKEEPALIVEREQUESTS defines the number of requests per TCP connection. So set to the following value: MaxKeepAliveRequests 50

The following tweaking is where the magic really happen, we will tell apache to generate a limited number of processes.

  • StartServers , an integer presenting the number of processes apache should create at startup
  • MinSpareServers and MaxSpareServers will tell Apache server to dynamically adapt to the load and maintain the number of spare processes based on the number of incoming requests received. For example, the server will check the number of servers (processes) waiting for a request and will terminate some if the number of servers (processes) are more than the MaxSpareServers and create some if the number is less than MinSpareRequests.
  • MaxClients represents the total number of simultaneous requests to be served, and more than that number, any incoming request will be queued.
  • MaxRequestsPerChild represents the number of request each child server process can handle and after that number the child server process will die.

See below the configuration for all those directives mentioned above. And add or modify this in your httpd.conf file under /etc/httpd/conf/httpd.conf.

Timeout 30
KeepAlive On
MaxKeepAliveRequests 50
KeepAliveTimeout 10

<IfModule prefork.c>
    StartServers          3
    MinSpareServers       2
    MaxSpareServers       5
    MaxClients            10
    MaxRequestsPerChild   1000
</IfModule>

And now restart your httpd service $ sudo systemctl restart httpd


MySQL

For a small WordPress blog just use the small and simple my.cnf configuration file shipped with the MariaDB RPM because in this specific case, my blog doesn't really require to perform extensive analysis.

$ sudo mv /etc/my.cnf /etct/my.cnf.backup
$ sudo cp /usr/share/mysql/my-small.cnf /etc/my.cnf

Add or modify bind-address=127.0.0.1 in the [mysqld] section which will limit the connections to the localhost address. And now restart MariaDB $ sudo systemctl restart mariadb

Swap Partition

Swapping can lead the system to run extremely slow but nevertheless, it is still much more better than a panic and a system reboot only caused by the database error [ERROR] mysqld: Out of memory

$ sudo dd if=/dev/zero of=/swap bs=`expr 1024 \* 1024` count=1024
$ sudo mkswap /swap
$ sudo swapon /swap
$ echo “/swap none swap sw 0 0>> /etc/fstab

Another solution

Since you are running your WordPress on AWS infrastructure, use the AWS RDS to load off your initial server which will be running mainly apache.

© 2020 Revolight AB