Hosting WordPress Site in LXC

31 / Oct / 2014 by Nikhil Kumar Sharma 0 comments

LXC (LinuX Containers) is an operating system–level virtualization method for running multiple isolated Linux systems (containers) on a single control host. In this blog, we will explain, how to host a WordPress site in LXC & migrate it to another server

Installing LXC

To downloads the package lists from the repositories and “updates” them to get information on the newest versions of packages and their dependencies,

[js]
apt-get update
[/js]

To install LXC ,

[js]
apt-get install lxc -y
[/js]

To create a container name application,

[js]
lxc-create -n application -t ubuntu
[/js]

-n : Name of Container
-t : LXC template. In our case, we’re using ubuntu template

It will take a while to create a container, once it has been created, it will give you username and password to login into your container.

To list running status of containers,

[js]
lxc-ls –fancy
[/js]

[js]

root@ip-172-31-34-48:~# lxc-ls –fancy
NAME STATE IPV4 IPV6 AUTOSTART
——————————————-
application STOPPED – – NO
root@ip-172-31-34-48:~#

[/js]

To start a container,

[js]
lxc-start -n application -d
[/js]

It will give you a login prompt, login with the user credentials that you’ve got above.

Installing WordPress Site

Install all the necessary packages,

[js]
apt-get install mysql-server php5-mysql nginx php5-fpm php5-gd libssh2-php -y
[/js]

Create a Database,

[js]
mysql -u root -p
CREATE DATABASE wordpress;
CREATE USER user@localhost IDENTIFIED BY ‘user123’;
GRANT ALL PRIVILEGES ON wordpress.* TO user@localhost;
[/js]

Download and untar wordpress

[js]
cd /tmp/
wget http://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
[/js]

Configure wordpress

[js]
cd /tmp/wordpress
mv wp-config-sample.php wp-config.php
[/js]

Make below changes in wp-config.php

[js]

// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define(‘DB_NAME’, ‘wordpress’);

/** MySQL database username */
define(‘DB_USER’, ‘user’);

/** MySQL database password */
define(‘DB_PASSWORD’, ‘user123’);

[/js]

copy files to document root

[js]
sudo rsync -avP /tmp/wordpress/ /usr/share/nginx/html
[/js]

Add an iptables rule to the prerouting table of host machine, to forward packets on port 80 to our nginx container’s port 80.

[js]
iptables -t nat -A PREROUTING -i eth0 -p tcp –dport 80 -j DNAT –to 10.0.3.245:80
[/js]

Now you can access your site, by
http://your-public-ipaddress ( host machine )

Migrating Your lxc container to another host

Install LXC on destination host and stop LXC on your source host

[js]
rsync -va /var/lib/lxc/container-name destination-host-ip:/var/lib/lxc/
[/js]

start container on your destination host.

Once you’re done with your containers, you can stop it

[js]
lxc-stop -n application
[/js]

or if you want to terminate your container, you can destroy it

[js]
lxc-destroy -n application
[/js]

Thanks,
Nikhil Kumar Sharma

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *