WordPress / MySQL keeps crashing on Ubuntu / Digital Ocean

I host a couple of WordPress sites on Ubuntu hosted on Digital Ocean droplets. I use their standard WordPress application (pre built WordPress on Ubuntu) with the smallest size Droplet possible. It has just 512MB Ram and 20GB SSD Disk and 1 logical core – i.e. it isn’t very quick.

Anyhow, I started to have a few problems with MySQL crash. I manifested itself by WordPress complaining that it could not connect to the database. I could fix the problem by restarting MySQL – ‘sudo service mysql restart’.

The longterm fix is to give MySQL more memory but I didn’t really want to do that. So, I opted to provide the server with more swap space. Given that Digital Ocean server are SSD’s this would be quite quick.

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

Here is how you add swap to an Ubuntu server:

1 – Check you what swap you have.

sudo swapon -s

If you get blank response then you don’t have any swap. Digital Ocean doesn’t seem to provide swap by default.

2 – Check how much disk space you have.

df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            234M     0  234M   0% /dev
tmpfs            49M  528K   49M   2% /run
/dev/vda1        20G  2.3G   17G  12% /
tmpfs           245M     0  245M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           245M     0  245M   0% /sys/fs/cgroup
tmpfs            49M     0   49M   0% /run/user/1000

I had 17G and so could easily give the server 1g swap.

3 – Use fallocate to reserve space on the file system. Fallocate is used because it doesn’t bother to write anything to the file system. It just allocates the space on the disk.

sudo fallocate -l 4G /swapfile

4 – Set file permissions

sudo chmod 600 /swapfile

5 – Convert it to a swap file

sudo mkswap /swapfile

6 – Add the swap

sudo swapon /swapfile

7 – Check that the swap is there

sudo swapon -s

8 – Add it to the fstab
so that it is mounted when the server reboots

sudo nano /etc/fstab

and put this line at the bottom:

/swapfile   none    swap    sw    0   0

Finally, you can check it has all worked…

sudo swapon -s
Filename				Type		Size	Used	Priority
/swapfile                              	file    	1194300	103160	-1

How to increase the max image upload size in WordPress

There are two variables which restrict the maximum media upload size to wordpress.  There are PHP variables.

upload_max_filesize
post_max_size

Assuming that you are using Ubuntu and Apache then you should create a .ini file in the conf.d directory. Don’t edit the default php.ini file because this is sometimes updated by the package maintainer and there is a chance that any changed you make to might be lost. So create a file called:

/etc/php5/apache2/conf.d/wordpress.ini

I wanted up be able to upload 30 megabyte files and so put:

upload_max_filesize = 30M
post_max_size = 30M

Then restart Apache:

apache2ctl restart

I hope this helps someone.

How to change the font size of SyntaxHighlighter

I’ve just installed SyntaxHighlighter on this blog to style the code snippets. The default font-size was too large for the website. They don’t seem to have any documention or setting to adjust the font-size. The solution is to add some custom css to your theme style:

body .syntaxhighlighter code, body .syntaxhighlighter .gutter {
    font-size: 12px!important;
}