Moving my nginx+mysql WordPress VPS native install to Docker containers on a KVM VPS

My WordPress blog that you’re reading right now is running on nginx and MySQL installed on a cheap OpenVZ VPS. I’ve been running on a $2.50 VPS from Virmach for the past 6 months or so and been very happy with the service. I spent a bunch of time tweaking the nginx and MySQL config params to run in < 512MB, which it does comfortably, but nginx and MySQL are both installed directly on the Ubuntu VM instance and it would be great of I could make this setup more easily movable between cloud providers (or even to have a local copy of the setup for testing, vs the live site).

I’ve been spending a lot of time playing with Docker and Kubernetes, so it seems logical that I should move the site into containers and then this will allow me to explore other deployment options.

Migration Steps – find a KVM VPS

As far as I know you can’t install Docker in an OpenVZ virtualized VPS container, so first step I need to move to a KVM based VPS so I can install Docker (and possibly Kubernetes). I’ve been shopping the deals on lowendbox.com and there’s plenty of reasonably deals for around $5/month for various combinations of 2 to 4GB RAM and 2 to 4 vCPU.

Dockerize nginx, MySQL and WordPress

I’ve been playing with this already. I’ve picked up my own combo of favorite/useful WordPress plugins, so I’ll probably share a generic set of Dockerfiles and then leave it up to anyone if they want to use them to customize your own WordPress install in the container.

Configure a local dev/test environment Docker setup vs production environment Docker setup on my VPS

This makes a lot of sense and is a benefit of using containers. This will allow me to test my config locally, and then push to my production node. I’ve been looking at using Rancher to help with this, but still got lots to learn.

More updates to come as my project progresses.

WordPress migration complete! (From OpenShift Online to a VPS)

If you’re reading this then I’ve successfully migrated this WordPress site from Red Hat OpenShift Online to hosting in a Virtual Private Server (VPS). I had a rather long list of tasks for the migration, including:

  • Exporting content from the old site and importing to the new
  • Re-issuing my SSL certificate and installing on the new server
  • Updating my DNS config
  • Unassociating my deployed app on Openshift with my domain name / alias
  • Installing my WordPress plugins, such as reCAPTCHA, view counter, the importer etc

I’m probably still missing some minor config items, but at this point I think I’m far enough to make the switch, so the site is now live on my new VPS hosting.

Given that I’m only running on a 2 core, 512MB RAM VPS, the new site is surprisingly snappy, and dare I say it, noticeably quicker than when it was running before on OpenShift Online? I’m sure I’ve still got plenty to tweak and configure, but so far so good, and I’m pleased with the transition!

Reducing memory usage by Apache and MySql in a 512MB VPS

Installing with the default Apache and MySQL configurations, you end up with far too many idle Apache threads, and MySQL is the largest memory user by far. The default config of these two is maxing out my available 512MB in my VPS server:

/etc/apache2$ ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r       

%MEM %CPU   RSS    VSZ COMMAND
 9.5  0.0 49904 720256 /usr/sbin/mysqld
 6.7  0.0 35620 286860 /usr/sbin/apache2 -k start
 6.5  0.0 34452 283524 /usr/sbin/apache2 -k start
 6.2  0.0 32692 283012 /usr/sbin/apache2 -k start
 5.9  0.0 31276 283116 /usr/sbin/apache2 -k start
 5.8  0.0 30896 282652 /usr/sbin/apache2 -k start
 5.0  0.0 26724 282588 /usr/sbin/apache2 -k start
 4.8  0.0 25204 279552 /usr/sbin/apache2 -k start
 4.8  0.0 25200 279552 /usr/sbin/apache2 -k start
 4.7  0.0 25156 279508 /usr/sbin/apache2 -k start
 4.4  0.0 23216 279540 /usr/sbin/apache2 -k start
 2.8  0.0 15136 278400 /usr/sbin/apache2 -k start
 0.7  0.0  3968  90908 sshd: myuser [priv] 
 0.4  0.0  2564  61312 /usr/sbin/sshd -D
 0.4  0.0  2264  33188 init
 0.3  0.0  2060  18128 -bash
...

/etc/apache2$ free
             total       used       free     shared    buffers     cached
Mem:        524288     476496      47792      68220          0     260764
-/+ buffers/cache:     215732     308556
Swap:            0          0          0

 

From tips in this article, I reduced down the Apache threads and now we’re at:

/etc/apache2/mods-enabled$ ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r
%MEM %CPU   RSS    VSZ COMMAND
 9.5  0.0 49904 720256 /usr/sbin/mysqld
 2.8  0.0 15132 278400 /usr/sbin/apache2 -k start
 1.1  0.0  6004 278424 /usr/sbin/apache2 -k start
 0.7  0.0  3968  90908 sshd: myuser [priv] 
 0.4  0.0  2564  61312 /usr/sbin/sshd -D
 0.4  0.0  2264  33188 init
 0.3  0.0  2076  18136 -bash

/etc/apache2/mods-enabled$ free
             total       used       free     shared    buffers     cached
Mem:        524288     400244     124044      68220          0     262480
-/+ buffers/cache:     137764     386524
Swap:            0          0          0

Now we’ve got some free space, and definitely not maxing out our available ram. Next up, I’ll take a look at MySQL.

Setting up WordPress on Apache and MySql on a Linux VPS host

I’ve run this blog for the past couple of years on OpenShift Online. I’ve been excited that the new Online v3 is moving to a container based service, but the cost per month is now much more than I wanted to pay. I was about to setup WordPress on AWS in an EC2 instance (if for nothing more than to get some experience playing with EC2), but after posting on Twitter for suggestions, there was a suggestion to check out cheap Virtual Private Server (VPS) offerings. The lowendbox.com site covers many offers from hosting companies offering VPS based services, so I took a look and picked up a 2 CPU core, 512MB, 200GB disk VPS for $2.50 a month. That’s a manageable cost and looks like a comparably price for a low end VPS server.

To get started after provisioning my VPS , I created a new user with sudo access instead of using the default root user created on initial setup:

  • adduser newuser
  • usermod -aG sudo newuser

On my Ubuntu 14.04 minimal server install, apparently even sudo is not yet installed, so per steps here, su’d to root, and then installed sudo:

  • su -
  • apt-get update
  • apt-get install sudo

Installed mysql-server. There’s many guides for installing MySQL, but here’s one as a reference.

  • sudo apt-get update
  • sudo apt-get install mysql-server

During installation I got this error, and the server failed to start during installation:

/var/lib/dpkg/info/mysql-server-5.5.postinst: line 150: logger: command not found
ATTENTION: An error has occured. More info is in the syslog!

From this post here, the fix is to:

  • apt-get --reinstall install bsdutils

As this ran, the mysql install started showing additional input and the server started up.

Next, setup:

  • sudo mysql_secure_installation

Followed the prompts and remove anonymous user access, test database etc.

Created a new MySQL database with user/password that WordPress will use to access the database, following steps here.

Installed apache:

sudo apt-get install apache2

Installed php modules for apache:

sudo apt-get install php5 libapache2-mod-php5

Hitting the WordPress setup url, got this error:

Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

This is fixed by installing the php mysql package (discussed here):

apt-get install php5-mysql

Installing WordPress from scratch is covered in detail here. The only additional step I needed to do was to tell Apache to server index.php so you’d see the WordPress site when hitting the site root url.

This can be done by editing /etc/apache2/apache2.conf and adding a DirectoryIndex:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        DirectoryIndex index.php
        Require all granted
</Directory>

Restart apache and you should be up and running:

sudo service apache2 restart

As  start this is pretty good. It looks like the default apache and mysql settings are maxing out my 512MB so I’ve got some tuning to do, and then I need to migrate my WordPress database across, but so far so good!