Using Linux Containers (LXC) is a good way to host multiple applications in a single server with separation of resources and applications.
You may think that running several Linux Containers and multiple instances of same service is very resource heavy. But I currently have 3 LXCs running in a VPS with only 512 MB RAM!
I prefer to run all the dependencies of my applications in same LXC as the application, but you can freely install them in the host server, or install them in a separate LXC.
Here’s how to do it:
Create a Couple of LXCs
Follow the instructions of following article to create a couple of LXCs.
For the purpose of this tutorial, I am going to create three LXCs named
Assign IPs to LXCs
Once that’s done, assign static IPs to all of them following the instructions in following article.
I’ll assign IPs as per the following table.
Point Domains to LXCs
Now that our LXCs have static IP addresses, we can point domains to them. Follow the instructions in following article to do that:
I’ll assign domains as per the following table.
Install Web Servers in each LXC
Log in to each LXC using
lxc-attach command and install the LAMP stack. I usually do this by installing
tasksel, and then choosing LAMP within it.
If all goes well, you’ll be able to see the Apache welcome page of the LAMP stacks you just installed by going to the domains listed above!
Set LXCs to Auto Start when the Host Starts
It’s a pain in thy rear end to start all LXCs each time you restart your host server. You can automate that by following the instructions in the following article.
Now, go ahead and restart the host server by issuing the
reboot command. The LXCs should also restart when the host does.
Setup SSH Access Directly into LXCs
LXCs run inside your host server. As they are set up now, there’s no way to access those LXCs without first accessing the host server. It may not be a big issue, but if we have to regularly access inside of LXCs, it can be a headache. We can do it by setting up a SSH server in the LXC and some port forwarding magic.
Follow the instructions in the following article to set up SSH access into LXCs.
It’s done. It’s over.
We setup LXCs, assigned static IPs to them, pointed domains at them, installed web servers in them, made them auto start with their host, and allowed accessing them from outside through SSH.
Now you can create LXCs at leisure and experiment with it without the fear of messing up your host system.