-1

This may be a very basic question but I can't quite wrap my head around it.

I have some JSON files on a Raspberry Pi that's connected to a network, and I'm wondering if I can serve those files to a website directly from the Pi?

I create the files via a cron job, so they are regularly updated. I suppose I could tweak the cron job to move the files to the site server once they're updated, but would it be possible to use the Pi as a file server directly instead?

I tried just using http://192.my.ip.address/my/folder/location but that doesn't work. Should I make that folder 'public' or is that too much of a security risk? Can I open a port on the Raspberry Pi so that my files can be read by my website? Should I follow some of the tutorials online and install Apache and turn my Pi into a whole web server? Or is that too much just too serve a handful of files (and not, say, a whole web page)?

If anyone can illuminate this I'd appreciate it!

Glorfindel
  • 620
  • 1
  • 8
  • 15
  • 1
    You need to install a web server that can handle the HTTP Request. This can be a lot of various options. If you're comfortable with Apache or Lighttp, those are some options. If you want a bit more packaged options, check out Diet Pi ( https://dietpi.com/ ), their distro is very nice and can save you a lot of time. – Twisty Nov 28 '18 at 22:52
  • 1
    Another option would be to install an FTP Server or another File server type. Your web server could then login and pull the JSON file over. – Twisty Nov 28 '18 at 22:57
  • wow, thanks for the fast response! You've given me lots to think about. I actually have proftp set up on the pi (and can access it via Filezilla / Cyberduck etc.). I think I have enough to get googling but I'll flag this for migration for now. Thanks again! –  Nov 28 '18 at 23:40
  • Yes, if you have ProFTP on the Pi, you can script something on your webserver via CRON or per request to pull the file over to the server. – Twisty Nov 28 '18 at 23:43
  • Would that be an OK way to do things, in your opinion? I was worried that doing it that way was a bit of double handling –  Nov 28 '18 at 23:58
  • "OK" is a relative term. In my opinion it is not the most efficient way to handle it. I would configure a Web Server with PHP and allow PHP to operate as an API that can return the JSON data. I'm assuming that this data is coming from another script or application on the Pi itself. So you can either GET the JSON file itself or perform a POST to PHP that can search or manipulate the JSON data. I would also move the data into a SQL database. This would then give you better search options and recall options. PHP can then build the JSON data dynamically based on the info you send to the API. – Twisty Nov 29 '18 at 00:06
  • That's right, the JSON is coming from a script itself on the Pi (I can provide details but I think that might be extraneous to the question?). I think you've given me a great roadmap to play around with though. I don't know PHP really but I look forward to learning, and I think I can figure it out eventually ( <?php $url = 'path_to_my_file.json'; ?). In the short term I'll use CRON to move the files –  Nov 29 '18 at 00:56
  • PHP is just my prefered server side scripting language. There are others. If you're using Python, for example, there are ways to set that up to work with the web server and respond to requests directly. You should be able to find lots of guides on setting this up. Again, DietPi does a lot of LAMP like setups for you, so it's easy to setup and manage, and it's lightweight. – Twisty Nov 29 '18 at 01:09

1 Answers1

0

It is not difficult to setup a server. I use NGINX which is supposed to use less resources than Apache. See https://www.raspberrypi.org/documentation/remote-access/web-server/README.md which has instructions for both.

EDIT I just looked at the instructions and noted they reference php5 - this is obsolete in Stretch php7 is now default.

If you follow the instructions (edit the files - but ignore changed content) it works.

There are also other options e.g. Python servers.

Whatever you do it will only make files visible on your local network. If you want them to be available outside this you need to configure your router and map to external IP, but that is not a Pi question.

Milliways
  • 59,890
  • 31
  • 101
  • 209