HOME      Other material for programmers   Pi? Arduino? Other?
(Parent page for this and other small getting started with Raspberry Pi tutorials)

I have notes for translators, if you would add a translation.


An Apache Webserver Created
With a Raspberry Pi

filename: pt0FirstWebserver1.htm

This is part 1 of what will eventually have at least two parts. This part discusses establishing a simple webserver with a simple Pi.

We will add php capabilities in a later tutorial... when it is written!

Creating the webserver is a simple matter. When I'd been using a Pi for less than a week I created one in about 25 minutes, including writing the process up as I went along.

I am indebted to the official RaspberryPi.Org page about setting up a webpage. It was helpful and clear.


Ah... be careful what you wish for?

As I say, setting up the webserver in the Pi was easy. (I'll describe it in a moment.

Just stop and ask yourself, though... do I want to manage a webserver?

You can have a webserver that is only accessible to other computers on the LAN the webserver is on. Even that limited access creates the possibility of security breaches.

I don't flatter myself that my files are all that interesting. But I don't want my computer co-opted into helping a malicious bot-net. And such compromises today are not always evident.

So... that had to be said. Assuming that you, like me, are willing to "take a chance" (having done what we can to make things difficult for the Bad Guys), I'll press on. The software I have installed on my own Pi, and am recommending to you, has a long history. It is "probably" "mostly safe".

Installing the webserver isn't the hard part

I will get to "how we do it" shortly, but I have to warn you: Installing the webserver is only the first part of getting a web site accessible to Internetland. (The other steps are many and tedious, but I will take you through them.)

These instructions were prepared 12 Aug 2018, in the following context: I was using a Pi 3 B, with Raspbian. I had given the Pi it's own monitor, keyboard, mouse, and connection to my LAN, through which the Pi could access the internet.


Let's do it....

You have a Pi, running nicely.

Go into the CLI. Do the usual sudo apt-get update/ sudo apt-get upgrade.

Then do...

sudo apt-get install apache2 -y

... which will "do" all sorts of stuff. Don't be alarmed. It is actually quite wonderful.

... and that should leave you with a working webserver! (Although you might not know yet that you have it!)

Working webserver?

Don't get too excited... it may not quite be all you mean by "working webserver" yet! But you probably do have something that can reasonably be described thus. You just might not fully appreciate all you took on when you decided you wanted to start down this path!!

This would be a good time to reboot your Pi. You've made major changes. Let's be sure all loose ends are tied off. Remember what a pain a reboot ("restart",in Windows-speak) is on a "big" computer? It took me more time just now to check that "restart" really is the Windows term than it took my Raspberry Pi to DO one!

So... you've rebooted your Pi.

Start up an internet browser. The Raspbian-standard "Chromium" ("Chrome") will do just fine.

Where you might ordinarily enter, say, "http://Snopes.com", enter, without the quotes, "localhost". And press enter.

The browser will probably fill with a page of "stuff" headed "Apache2 Debian Default Page". Don't worry about trying to read it. Leave the browser open for now.

"localhost", when typed on the machine with the server in it, says "give me the default page from the server". (There are ways to change what is served up as the default page. At the moment, it will be "index.html".

By the way... some files end .html, others end .htm. For the file you want, you have to enter the l or not, depending what was used when the file was created. Presence or absence of the l doesn't mean anything.

Try "localhost/index.html". You should get the same page as before.

Go into the CLI.

Issue the following command. It will start a CLI text editor ("nano"), and open (or create, if it doesn't already exist, which it doesn't at the moment) a file called MyFirstPiWebpage.htm, in the folder called html, which is in the folder www, which is in the folder var, which is in the root of the filesystem. Why there? Because that's The Right Place. (Sorry!).

sudo nano /var/www/html/MyFirstPiWebpage.htm

(And press enter.) Beware the "sudo"... I am always mistyping that. Maybe it's a common mistake?

That will leave you in the CLI, but the screen will have changed. Enter "I did it" onto the page. Jot a great "webpage". Not a paragon of correct syntax. But it Will Work.

Press ctrl-O. (That's "oh" for Out. You are going to write your text out to a file.) The CLI will (across the bottom of the window) ask you to confirm that you want to write to the file we said. Press enter, to make it happen. You will still be in nano... don't worry. Leave that open. (When I began computing, everything was like doing it in the CLI and you NEVER had more than one thing open, running at a time. You may send sympathy.

Go back to the browser. Change what's in the address bar to...

localhost/MyFirstPiWebpage.htm

Press enter. I hope you are saying "wow!"?

Not saying "Wow"? Have you got the name EXACTLY right? It needs to be whatever you put at the end of "sudo nano..."

Did you, like me, take the "l" off of "html" (Doesn't matter if you didn't... just make what you put in the browser match what you said to nano.) Did you use the same capitalization? MyFirstWebPage is different from MyFirstWebpage. (Look at the "P"/"p".) If you messed up the...

/var/www/html/

.. you'll have to do the sudo nano again.

NOW you are saying "Wow!"? Hurrah!

You did it! You have a working webserver, just as I said you would.

Optional extra... if you are feeling ambitious, add...

"<h1>My first heading</h2>"

... to what's there, do ctrl-O again. Reload the file in the browser. (Do ctrl-X when you are ready to finish editing the file.)

Writing good html (we've been writing bad html!) is a story for another time. The point here is that if you put a file in /var/www/html, and give it .htm or .html as it's extension, then that page will be available from the webserver.

I've written a page that addresses a number of issues, but a good chunk of it is an introduction to writing html... the "stuff" that goes into the files "behind" web pages, to make Nice Things appear on people's screens when they fetch those files/ pages. My Intro to Writing HTML. (Parts of that page address other matters.)

But our little page will suffice for now.

So far, we've only looked at our webpage from the keyboard and screen of our Pi. (If that's all we wanted to do, we could have just navigated to it with the file manager and double clicked on the entry for it. It would have opened in the browser for us. (If you try it, not that the line in the address bar starts "file://" instead of "http://")

I hope your Pi is on a LAN, with another computer also on the LAN?

If you go to that other computer, open a web browser, and try "localhost", it will only look inside itself for a webserver.

Remember we gave our Pi a "hostname"? (If you've forgotten, go into the CLI, and look at the prompt. If it is pi@yyy, they you are using this Pi as user "pi" (which would be normal and fine), and the Pi's hostname is "yyy". (A slightly odd choice, but you could have assigned that.)

So... you are on a machine sharing a LAN with a Pi that has a hostname. Let's say the hostname is pi5aug18. That is case-sensitive, by the way, as is often the case in Linux.

You have a web browser open on the "other" machine.

Put "http://pi5aug18/MyFirstPiWebpage.htm" (without the quotation marks) into the browser's address bar. You should get your webpage! (If you know about Samba, file sharing, etc, etc, but haven't done anything about those things yet... don't worry. You don't NEED to have done them for the above to work!) (I would expect just "pi5aug18/MyFirstPiWebpage.htm", but that didn't seem enough... or I had a typo, or prefixed some unhelpful character... a moment ago. If not bothering with the http:// works for you, go for it!)

Hurrah! Go celebrate! You have a webserver! It will deliver anything you have in the /var/www/html/ folder. (Or in subfolders created there... if you create /var/www/html/MySubFolder/sf.htm, you can access it with...

http://ich07p/MySubFolder/sf.htm

... oops. I digressed.

As I was saying... You have a webserver. You can see html files in your browser on the Pi that is the webserver. You can see them in browsers on other computers on your LAN. THIS IS VERY COOL. Enjoy your triumph.

What's next....

I'm afraid there are stormy seas ahead... In Webserver2 I will show you how to go from where you are to what you probably really wanted... pages that anyone, anywhere on the internet can read. (You can, by the way, make access to your pages password protected. But get them accessible by anyone, first. And get them accessible across you LAN (instructions above) before that!)

Go to Webserver2, the next part of this guide.



-->

Hope that helped?

I hope that was helpful. Getting started is always so tedious. This page was just "a sidebar" off of my main "Getting started with Raspberry Pi" page. Feel free to contact me (see below) with comments, suggestions, questions... save the next reader being confused by something? Please cite this page's URI, if you do: pt0FirstWebserver2.htm.




Footer

Please remember that this material is copyright. (TK Boyd, 2018) There are further notes in this page's parent page.




   Search this site or the web      powered by FreeFind

Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.

This page is a "sidebar" to my main discussion of Getting Started with the extraordinary Raspberry Pi.




To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcome!



Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes. There were two "unknown attributes" in Google+ button code. Sigh.

Why does this page cause a script to run? Because of the Google panels, the code for the search button, etc. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....