I recently installed a web server on a Virtual Machine. I use VirtualBox which came along with my Ubuntu 22.04 system and I decided to install the Virtual Machine with Ubuntu 22.04 server installed. It doesn’t matter if you simply install Nginx or Apache, or if you want to run a full LAMP (Linux, Apache, MySQL, PHP) package on which you can run a WordPress site. The problem really deals with how you can access the web server on the virtual machine from your browser window on the host running the virtual machine.
I read quite a lot of articles and tried several options. I have got to admit, I was close several times, but a few mistakes made me spend almost an hour trying to solve the problem. Luckily, you can just follow these instructions and it should, hopefully, go much faster for you.
I assume that you already have Apache or Nginx operating on your virtual machine (Ubuntu server) so I will not deal with how that works in this article.
Find out if the website is working (on the virtual machine).
If you have installed Ubuntu 22.04 on your virtual machine, then you can open the browser on your virtual machine and type localhost in the address bar. This will immediately show you if the website is working or not. If it is, you should see the standard page produced by either Apache or Nginx.
If you have Ubuntu server installed on the virtual machine, you can run the following command to see if it is working or not.
curl -i localhost
Below you can see two outcomes as I run the command on two different servers.
So we know that the web server is running on localhost. But, how can we access this website in the browser on the host machine actually running the virtual machine through VirtualBox?
How to access a web server running on a virtual machine with VirtualBox on the host machine?
It isn’t complicated at all, you just need to do it correctly.
Before you shut down your virtual machine, type the following command in the command line. This will show you the internal IP address of the server.
ip a
Here you will see quite a bit of information, but you need to be looking for an address like this: 10.0.2.15 (this is a default address, but it might look otherwise for you). If this is the IP address you see, you don’t have much to do. If it is something else, write it down so that you do not forget it.
Close the active virtual machine running the web server. Close it completely. Then you go to the settings of the given virtual machine in VirtualBox.
Now you select settings for the virtual machine on which the web server is running. Then you select network and keep it with the standard NAT option. Make sure that Cable Connected is enabled. The real trick comes as you go to port forwarding.
When you come to port forwarding, you need to add the following data:
- Name: Whatever you want.
- Protocol: TCP
- Host IP: 127.0.0.1
- Port: 8080
- Guest IP: 10.0.2.15 (if the IP address of your virtual machine is something else, type that instead).
- Port: 80
Click OK to apply these settings to your virtual machine.
If you wait a few seconds, you can now enter the following address in the address bar on the host computer:
http://localhost:8080
Since I have a virtual machine with a WordPress installed, I see the following result:
I have another virtual machine with Apache installed (and nothing else). As I try to look at it at the host machine, I see the following.
It wasn’t that hard, was it?
What if I want to watch content on a different server port?
I have a virtual machine running VirtualMin. This is a fantastic package ideal if you run a web server with multiple websites. To log in to the server interface I have to use port 10000 on localhost. But, how can I access the same page from the host computer hosting the virtual machine running VirtualMin?
The procedure is the same, you just add a new row to port forwarding.
- Name: VirtualMin server
- Protocol: TCP
- Host IP: 127.0.0.1
- Port: 15000
- Guest IP: 10.0.2.15 (if the IP address of your virtual machine is something else, type that instead).
- Port: 10000
What will happen now if I type the following address in the address bar on the host computer?
http://localhost:15000
As you can see, I am now able to log in to VirtualMin/WebMin on the virtual machine. I can also see the default website by visiting the localhost:80 in my browser.
That’s it! You should now be able to have fun with your web server running on the virtual machine powered by VirtualBox, and also visit the content on the website from the browser on your host machine.
I hope you are satisfied and happy. If you still have any comments or questions, please use the comment field below.
Have I been able to help you? I would love to hear from you, so please leave a comment below and let me hear from you!