How to start apache2 in a mininet host, and access it from another host?

180 views Asked by At

Using Mininet, I need to setup an emulated network to test web browsing performance, where one host of it running an apache2 server, and then access it from another host. I already have a configured website on the physical machine and have verified to be working properly.

To start the emulation, I use the following toy mininet python script simpleTest.py with the simplest topology where h1 and h2 can ping each other. Before the emulation, the apache2 is stopped on the hosting machine.

#!/usr/bin/python                                                                            
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI

class SingleSwitchTopo(Topo):
    "Single switch connected to n hosts."
    def build(self, n=2):
        switch = self.addSwitch('s1')
        for h in range(n):
            host = self.addHost('h%s' % (h + 1))
            self.addLink(host, switch)

def simpleTest():
    "Create and test a simple network"
    topo = SingleSwitchTopo(n=2)
    net = Mininet(topo)
    net.start()
    CLI(net)
    net.stop()

if __name__ == '__main__':
    simpleTest()

After running the script using sudo python3 simpleTest.py, I start two xterms on h1 and h2, respectively with

mininet>xterm h1 h2

Later, apache2 is started in h1's xterm window using

[Node: h1's xterm]# systemctl start apache2

However, I cannot access the website from h2's xterm window, while h2 can ping h1's IP (10.0.0.1) successfully. Using telnet 10.0.0.1 80 returns "Connection refused". In fact, even telnet 127.0.0.1 80 from h1's xterm window returns "Connection refused". On the other hand, accessing the website from the physical machine's browser or terminal has no problem.

I guess the access to apache2 from within Mininet is unsuccessful is because h1 and h2 are in network namespaces, while the apache2 started using systemctl (even though invoked from h1's xterm) is still on the physical machine?

So, is that possible to make apache2 run in h1's space and be accessible from h2 via h1's IP address 10.0.0.1? Any guidance is highly appreciated.

1

There are 1 answers

0
leeyee On

Inspired by @larsks's comment, I have figured out a solution.

Basically, the apache2 command (/usr/sbin/apache2) needs to be executed directly from h1's xterm window. Before executing it, some environment variables have to be set, which would be automatically set from /etc/apache2/envvars if apache2 were started via systemctl or /etc/init.d/apache2. Therefore, I made a copy of /etc/apache2/envvars and renamed it as a separate script, say $(pwd)/start_apache2_manually.sh. At the end of start_apache2_manually.sh, add a line apache2 -D FOREGROUND. Running the script ./start_apache2_manually.sh from h1's xterm window can then make the website accessible from h2 via the emulated network.