Setting Up a Web Application on a System Container: A Guide to Using LXD and Nginx

Setting Up a Web Application on a System Container: A Guide to Using LXD and Nginx

Exploring Efficient Application Isolation and Management with LXD Containers and Nginx Reverse Proxy

In this tutorial, we'll walk you through the process of deploying a web application using LXD and Nginx. By the end, you'll know how to set up a system container, install a web server, and configure a reverse proxy.

Introduction to LXD and Nginx

LXD is a next-generation system container manager, offering a user experience similar to virtual machines but using Linux containers instead.

Nginx is a powerful, high-performance web server, capable of serving static content and acting as a reverse proxy, directing incoming requests to other services.

Together, they can create an efficient, isolated environment for your web applications.

Step 1: Installing LXD

The first step in this process is to install LXD. For Ubuntu users, this is as simple as running:

sudo apt update
sudo apt install snapd
sudo snap install lxd

Once the installation is complete, you should initialize LXD by running:

sudo lxd init

This will ask a series of questions to set up LXD. In most cases, you can accept the default options by pressing Enter.

Step 2: Creating a Container

Next, we'll create a new container:

lxc launch ubuntu:18.04 mywebserver

This will create a new container named 'mywebserver' using the Ubuntu 18.04 image.

Step 3: Installing Nginx

With your container created, the next step is to install Nginx. Enter your container with the following command:

lxc exec mywebserver -- /bin/bash

Then, update your package list and install Nginx:

apt update
apt install nginx

Step 4: Configuring Nginx

Nginx uses server blocks to host multiple domains or interfaces. In this guide, we'll set it up to serve as a reverse proxy, directing traffic to a web application running on a different port.

Edit the default server block configuration file using nano (or your preferred text editor):

nano /etc/nginx/sites-available/default

Modify the file to include a location block like this:

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://localhost:8080;
    }
}

After saving your changes and exiting the text editor, check that your configuration is valid and restart Nginx:

nginx -t
systemctl restart nginx

Step 5: Testing Your Setup

With Nginx configured, it's time to test your setup. The method of access will depend on whether you're testing locally or remotely.

  1. Local Testing: If you're on the same machine where Nginx is running, you can open a web browser and navigate to localhost. You should see your application running.

  2. Remote Testing: If you're on a different machine, you'll need the IP address of your server.

    On your server, use the ip command to find your IP address:

    • For wired connections: ip addr show eth0

    • For wireless connections: ip addr show wlan0

In the output, look for the inet line. For example, you might see output like this:

    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.123/24 brd 192.168.1.255 scope global dynamic eth0
           valid_lft 86153sec preferred_lft 86153sec
        inet6 fe80::5054:ff:fe12:3456/64 scope link 
           valid_lft forever preferred_lft forever

In this case, the IP address is 192.168.1.123.

The IP address is listed there. With this IP address, you can access your application from a different machine by navigating to http://<your-server-ip> in a web browser.

Remember to replace <your-server-ip> with the actual IP address of your server. Also, ensure that port 80 is open in your firewall settings to allow traffic to reach your Nginx server.

Congratulations! You should now be able to access your web application served by Nginx on your LXD container.

Conclusion

In this tutorial, we showed you how to set up a system container using LXD, install Nginx, and configure it to serve as a reverse proxy for a web application. With this setup, you can efficiently isolate your web applications and manage them with ease.

You can Buy Me a Coffee if you want to and please don't forget to follow me on YouTube, Twitter, and LinkedIn also.

Happy coding!