Hosting WordPress Site in LXC
5 March, 2021 by
Hosting WordPress Site in LXC
Administrator
| No comments yet


 

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,

apt-get update

To install LXC ,

apt-get install lxc -y

To create a container name application,

lxc-create -n application -t ubuntu

-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,

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

To start a container,

lxc-start -n application -d

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,

apt-get install mysql-server php5-mysql nginx php5-fpm php5-gd libssh2-php -y

Create a Database,

mysql -u root -p
CREATE DATABASE wordpress;
CREATE USER user@localhost IDENTIFIED BY 'user123';
GRANT ALL PRIVILEGES ON wordpress.* TO user@localhost;

Download and untar wordpress

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

Configure wordpress

cd /tmp/wordpress
mv wp-config-sample.php wp-config.php

Make below changes in wp-config.php

// ** 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');

copy files to document root

sudo rsync -avP /tmp/wordpress/ /usr/share/nginx/html

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

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to 10.0.3.245:80

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

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

start container on your destination host.

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

lxc-stop -n application

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

lxc-destroy -n application




Sign in to leave a comment