18 Jan 2018
Just a workout from the Project “Build Wordpress Server with CentOS7“.
[An old way]
In the project of building Wordpress server, I config the network in VM with the following steps. There are two adapters for network config in total.
NAT mode:
With this mode, the VM can access outbound websites via local PC.
Virtual Machine → Host → Outbound (not serve as a server)
And we can check the IP address of NAT.
So far, there is no way for localhost PC to communicate the VM because NAT is not working in two ways. In this case, we need to config another mode “Host-only mode”.Host-only mode:
This mode ensures localhost PC and VM can communicate with each other. Here we set the IP address as 192.168.56.*. Then check theifconfig
in VM again and get the IP in VM which is192.168.56.101
.
Finally, the configuration of two network adapters has finished. Then we can use ssh root@192.168.56.101
to talk to VM from local PC. And the VM can access to the websides outside as well.
[The new way]
Later, a better way has been found. In this new way, we just only use one NAT adapter to fulfill requirements above.
- NAT config: the same as above.
- Add “Port Forwarding” rules in NAT setting.
- The first rule is for SSH and the second one is for web access. The IP address
10.0.2.15
is IP for VM under NAT mode.
In this case, local PC can SSH to VM using the commandssh -p 2222 root@10.2.209.56
. Here, the IP address10.2.209.56
is the IP address of local PC.(For example, the IP in Internal network of an office).
Therefore, every requests for accessing port 2222 in 10.2.209.56 will be forwarded to port 22 in 10.0.2.15(the VM itself). - As for the second rule, it’s for web service. As I have mentioned, the VM will serve as a web server of Wordpress, so we need a port-forward for port 80 in VM. In this case, the requests from outside can access to the VM port 80.
We randomly choose a port 8888 for local PC. Therefore, every requests for port 8888 in 10.2.209.56 will be forwarded to port 80 in 10.0.2.15 so that VM can provide the web service. (just tryhttp://10.2.209.56:8888
in browser)
Actually the ideal case is that we can open port 80 in 10.2.209.56 so that we don’t need to type the port number anymore(just typing http://10.2.209.56) because port 80 is the default port for web service.
However, ports lower than 1024 are the privilege ports, which need root to open. In this case, we need to open VirtualBox as root. Some other methods are listed in this blog as well.
- The first rule is for SSH and the second one is for web access. The IP address
Ref:
- Config port forwarding to access to websites inside VM
- Install a CentOS 7 Minimal Virtual Machine with VirtualBox
Notes:
In CentOS7, maybe SELinux somehow will affect port forwarding. So I disable it here. Use getenforce
to check the status of SELinux.
[root@localhost ~]# getenforce
Enforcing
To disable SELinux, just edit config, then restart VM:
/etc/selinux/config
#change SELINUX=disabled
Another factor will be the Firewall. So if it doesn’t work as intended, just check the Firewall as well.