Optimizing DigitalOcean’s WordPress 1-click app droplet

My $6/month DigitalOcean WordPress droplet (Premium AMD CPU, 1GB RAM, 25GB NVMe SSD) is suffering high memory usage and sometimes would return “Error establishing a database connection” message. I would also get “Error 521: Web server is down” from CloudFlare.


Check memory usage

htop
Indeed there isn’t a lot memory left, with MySQL and Apache consuming most of it.

Optimize MySQL with MySQLTuner

Download and run

wget http://mysqltuner.pl/ -O mysqltuner.pl
perl mysqltuner.pl

I would get an error below:

[!!] Attempted to use login credentials from Debian maintenance account, but they failed.

Fix

perl mysqltuner.pl --defaults-file /etc/mysql/my.cnf

Source: https://github.com/major/MySQLTuner-perl/issues/463

Here are the recommendations I got:

-------- Recommendations ---------------------------------------------------------------------------
 General recommendations:
     Control warning line(s) into /var/log/mysql/error.log file
     MySQL was started within the last 24 hours - recommendations may be inaccurate
     Reduce your overall MySQL memory footprint for system stability
     Dedicate this server to your database for highest performance.
     Reduce or eliminate unclosed connections and network issues
     Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
     Before changing innodb_log_file_size and/or innodb_log_files_in_group read this: https://bit.ly/2TcGgtU
 Variables to adjust:
   *** MySQL's maximum memory usage is dangerously high ***
   *** Add RAM before increasing MySQL buffer variables ***
     innodb_log_file_size should be (=16M) if possible, so InnoDB total log files size equals to 25% of buffer pool size.

So I edited my config file:

Create new MySQL config file
sudo nano /etc/mysql/conf.d/customize.cnf

Add customizations:

[mysqld]
innodb_log_file_size=16M
innodb_stats_on_metadata=0

restart MySQL:

sudo service mysql restart

Check:
mysql
SHOW VARIABLES;

Add Swap Space

Check if there is configured/active swap:

sudo swapon --show
free -h

Check available space on disk

df -h

Create and enable swap

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Make swap permanent

sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Tune swap settings

sudo nano /etc/sysctl.conf

Add at bottom: 
vm.swappiness=10
vm.vfs_cache_pressure=50

reboot

Source: https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-20-04


Fixing CloudFlare’s Error 521: Web server is down

iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
for ip in $(curl https://www.cloudflare.com/ips-v4); do iptables -I INPUT -p tcp -m multiport --dports http,https -s "$ip" -j ACCEPT; done
for ip in $(curl https://www.cloudflare.com/ips-v6); do ip6tables -I INPUT -p tcp -m multiport --dports http,https -s "$ip" -j ACCEPT; done

Source: https://hooshmand.net/how-to-use-cloudflare/

Leave a comment