Extra Exercises!

LAMP Tutorial

This tutorial takes you through installing LAMP (Linux, Apache, MySQL, and PHP) on Ubuntu.

Many people have a local enviroment when developing web apps so that they don't have to deploy files to a server every time they want to test a new feature. This is an essential process in developing web apps and is one that I've had to do out in industry. So doing this now will give you valuable exposure! Finishing this lab will get you set up with a local web development environment so that you can implement and test your code locally before making it live.

What is LAMP?

LAMP stands for Linux, Apache, MySQL, and PHP.  The LAMP stack is what typical websites use to serve pages.  Linux is the operating system; Apache is the server (which responds to web requests); MySQL is the database; PHP is the web scripting language used to create web pages.
 
Each piece is interchangeable. Instead of Linux, you can use Mac (MAMP).  On a side note, some people prefer nginx to Apache.  Some low traffic sites use SQLite (a database in one flat file), PostgreSQL, or NoSQL instead of MySQL.  Many sites include Perl, Python, and Ruby, in addition to PHP.  

Exercises

  1. First, get LAMP or MAMP installed.  
    1. Since this is something that you'll do infrequently and that might change depending on the version of your operating system, you won't remember how to do it, but you might remember how to find how to do it.  Thus, look for a guide online.
    2. On Ubuntu, you should just be able to use your package manager (apt-get). Here's a recent guide I found that works: https://www.digitalocean.com/community/articles/how-to-install-linux-apa...
      • Follow the steps through Step 3 and up to, but not including PHP Modules
      • When it installs MySQL in Step 2, you should set a root password!)
    3. On a mac, if you don't have a package manager like macports installed, you'll need to just download a mac dmg.  Go to this site here: http://www.mamp.info/en/index.html
  2. Make sure that it's working
    1. localhost is a web address that refers to the current machine that you're on.  Thus, http://localhost will point you to the document root of the web server.  
    2. When LAMP is working, going to http://localhost should display a page that says that it works.
    3. If that doesn't work, it might be because of ports.  Every TCP or UDP web request that a server gets will have (among other things) an IP address and a port.  The IP address identifies the computer.  "localhost" means "use my own IP address."  If you don't provide a port, that means "use port 80, the default HTTP port."  LAMP, thus, will serve pages over port 80 by default.  However, macs aren't always quite so user friendly, and MAMP serves pages over port 8888 by default.  You can change this to port 80, or you can go to, for instance, http://localhost:8888.  
  3. Find the default document root
    1. This will be different depending on whether you're on Linux or on a BSD distribution like a mac, and it might even vary within your distribution of Linux.  It might help to include your distribution (like Ubuntu) when you Google to find the default document root.
    2. To test that you have found the right place, add an HTML file called "hello_world.html" to it (make sure that this file contains some text).  You will probably need root permissions (sudo) in order to move a file into the document root directory.  
    3. Now, visit  http://localhost/hello_world.html.  If everything worked correctly, you should see the webpage that you just created.
    4. Now, you know enough to use your own computer as a local environment for web development!
  4. Serve files from different places
    1. One apache server can serve multiple domains.  For instance, at Stanford, there is one apache server that serves out of a different document root for each student.  That way, the pages at stanford.edu/~samking are at /afs/ir/users/s/a/samking/WWW/ on the filesystem, and no student needs administrator privileges to edit their own web pages.
    2. Find the apache configuration directory.
    3. Change the default site so that it serves out of your home directory.  
    4. Verify that this works.  Note that you might need to restart apache or your http daemon (a daemon is a service that runs in the background).
    5. You might want to change it back afterwards.
    6. Add a new directory to serve from.  That way, you can serve from both the default location and from your home directory.  This will be a little bit more involved.  Try googling this first, but you might want to try looking into virtual hosts, aliases, or directories. 

 

Console Web Browsing

w3m is a console based web browser. It's a little step up from cURL. It isn't installed on the Myth machines, so you'll need to use your own computer and install it there. Alternately, you could use lynx, which does come installed.

You can briefly check out the man page, but the online guide at http://w3m.sourceforge.net/MANUAL or SHIFT+H when inside w3m are both much more helpful.

w3m can open any html file on your local computer. Just use w3m filename to open an html file using w3m.
Use wget to download http://www.google.com/
Use w3m to see what that file looks like
quit out using q or SHIFT+Q
Using the same syntax, w3m can also look at webpages. Here is a guide on how to do some basic stuff
run w3m google.com
use your mouse to click on the search bar. Alternately, get the cursor there using the arrow keys, hjkl, or tab.
hit enter. type whatever you want to search for. hit enter to enter the text.
double click on the "google search" link. Alternately, get the cursor to the link using the the keyboard, and hit enter.
Navigate to one of the webpages in the same way.
Hmm, that wasn't the page you wanted. Hit SHIFT+B to go back.
Nothing at the top of the Google page is interesting. Go down a few pages using spacebar.
Go back up using lowercase b.
Actually, I just want to see all of the pages that I could go to from here. Hit M-m (meta + m. If you have an alt key, use alt+m. If you don't, then hit escape, release it, and hit m). Select a page.
I'm bored with searching. Let's go to a new page. Hit SHIFT+U, and type in wikipedia.org.
Wait, I want to see my page history (like when you hold-click on the back button in a normal browser)! Press s.
Hmm, right, that's just Google, which I just said I was bored with. How do I get rid of this w3m popup? Press ctrl+c.
A few years ago, there was a browser that introduced tabbed browsing, and I hear that's all the rage. I want a tab too! Press SHIFT+T to open a new tab. Use U to move that tab to a new URL.
Use { or } to switch between your tabs.
Use Ctrl + q to close the old tab.
A lot of things are similar to vim. For instance, if you want to search, you can use / (or ? to search backwards), and you can use n to get the next result (or N to get the backwards next result).
Did something weird happen? Look at the help page by typing SHIFT+H. If your mouse isn't working in w3m, you might have accidentally hit m. Just hit m again to reenable it.
Now, use what you know to check your email (or facebook, as the case may be)! Note that Gmail uses too much Javascript for w3m to handle the actual login.