Servers-for-Hackers / the-book

Servers for Hackers eBook Issues
63 stars 5 forks source link

Apache with HTTP #85

Closed ltanase77 closed 6 years ago

ltanase77 commented 6 years ago

Hello,

I cannot make to work the small node (http.js file) application at page 100-102 of the book. When i curl localhost:9000 within the server I receive an error message: curl: (7) Failed to connect to localhost port 9000: Connection refused.

The http.js file is located and is ran on the local machine where I have installed nodejs.

Is this the correct setup or the http.js file should be on the server too, in order for the example to work (which implies that I need to install node as well)? The book is not very explicit about.

Currently I have installed on the server just Apache2, PHP7 and MariaDB. I am also using Vagrant for creating the ubuntu server.

Thanks!

fideloper commented 6 years ago

Hi!

Right, that assumes NodeJS is installed (for the next book update I’ll have to review and make that more clear, and see if NodeJS changed a lot or not, I hope that script still works!).

In any case, if you’re running the node script on your local machine, but doing the curl request from within the VM, that won’t work - localhost in that case refers to the servers localhost which isn’t forwarded or connected to your host machines localhost. If you want to curl from within the server and have it speak to a node server running on your local machine (the host computer) you’ll need to have variant network aware of the host machine (it be already out of the box) and then you can curl the host machines up address - likely that’s something like curl 192.168.16.16:9000”, where the 192.168 address is what we the IP address of the host machine is (it might be a totally different IP address, depending on your computer)

The key point there is understanding that the virtual machine and your host computer have two separate networks that aren’t necessarily connected. (Altho likely are, but using “localhost” as the host name wont be the way to get them to speak to each other - instead it’ll be over an IP address of a network they’re both connected to, which I think is a default setup of vagrant - Altho you may need to assign the vagrant machine a static IP address, which the book covers).

If you want to try out a php web listener instead, you can use that instead with a CLI command you can run from within your vagrant server:

php -S 0.0.0.0:9000 -t /path/to/directory/with/web/files

That will start a PHP dev server that can serve php or static asset files from whatever directory you set it to read from (on port 9000).

I’m not sure if there’s a point there I can expand on. It sounds like mostly a confusion on how the networking is between the VM and the host machine but let me know if I can clarify something more specific.

Hello,

I cannot make to work the small node (http.js file) application at page 100-102 of the book. When i curl localhost:9000 within the server I receive an error message: curl: (7) Failed to connect to localhost port 9000: Connection refused.

The http.js file is located and is ran on the local machine where I have installed nodejs.

Is this the correct setup or the http.js file should be on the server too, in order for the example to work (which implies that I need to install node as well)? The book is not very explicit about.

Currently I have installed on the server just Apache2, PHP7 and MariaDB. I am also using Vagrant for creating the ubuntu server.

Thanks!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Servers-for-Hackers/the-book/issues/85, or mute the thread https://github.com/notifications/unsubscribe-auth/AAch0_mTBAohAcVWPk0IggyFktgb49M0ks5tEQe-gaJpZM4RM0FX .

ltanase77 commented 6 years ago

Hi Chris!

Thank you for your clear explanations! Nice job in writing the book! It is a good resource. I also enjoy the videos on your site! Both, the book and videos, are easing the learning curve regarding this field.

I have installed nodejs (8.9.3) on the server and the example works as expected. It also works with the node file running on the local machine by providing to the curl command the IP address set in the Vagrantfile for the private network, as you suggested.

Moving along with setting the Apache for proxying request to the node application what should be the end result of the example?

Now, if i go in the browser and access app.ipaddress.xip.io i see Hello, World! message printed by node application. If i close the node server i get a service unavailable message from apache.

Before setting the proxy to node application, accessing this address in the browser showed the content of an index.php file located under the document root of the vhost. I can still access the index.php by writing in the browser ipaddress/app.

I hope this is the correct result of running the example!

fideloper commented 6 years ago

Yep, that sounds right - I believe Apache will attempt to fulfill the request itself if it finds a file with its set webroot (document root). Otherwise it will send the request to the proxy.

That being said I don't remember exactly, it's been a while since I used Apache!