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.
How to Create Linux Containers in Ubuntu
For the purpose of this tutorial, I am going to create three LXCs named com-budhajeewa
, lk-sanmark
, and org-rahulacollege
.
Assign IPs to LXCs
Once that’s done, assign static IPs to all of them following the instructions in following article.
Assign Static Internal IP Addresses to LXC
I’ll assign IPs as per the following table.
LXC | IP |
---|---|
com-budhajeewa | 10.0.3.2 |
lk-sanmark | 10.0.3.3 |
org-rahulacollege | 10.0.3.4 |
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:
How to Point a Domain to LXC using NGINX in Host
I’ll assign domains as per the following table.
LXC | Domain |
---|---|
com-budhajeewa | budhajeewa.com |
lk-sanmark | sanmark.lk |
org-rahulacollege | rahulacollege.org |
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.
How to Auto Start LXCs when Host Starts
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.