Thursday, March 29, 2012

Ubuntu on Lion under VirtualBox (2)

In the last post we installed Ubuntu on OS X Lion as the host using VirtualBox, and downloaded and installed the Apache server (version 2.2, apache2).

The basic commands are:

sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 restart
sudo /etc/init.d/apache2 stop

although after the install, I found it was already running. Another useful command to get basic information is /usr/sbin/apache2 (run with sudo), for example:

telliott@U32:~$ sudo /usr/sbin/apache2 -l
Compiled in modules:
  core.c
  mod_log_config.c
  mod_logio.c
  worker.c
  http_core.c
  mod_so.c

telliott@U32:~$ sudo /usr/sbin/apache2 -v
Server version: Apache/2.2.20 (Ubuntu)
Server built:   Feb 14 2012 17:55:17

Files for apache are scattered in various places, governed partly by Unix tradition. For the moment, to see pages from Ubuntu using Firefox, we don't need to change anything about the configuration. Just point Firefox at "localhost":

http://en.wikipedia.org/wiki/Localhost


Or, do sudo apt-get install curl and then:

telliott@U32:~$ curl localhost
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>

This page comes from the "document root" directory, which is /var/www. Since we didn't specify a filename, we got index.html

telliott@U32:~$ cat /var/www/index.html
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>

This directory is set in /etc/apache2/sites-available. Another useful directory is the default directory (or directories) for scripts, also set in the same file as:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
 <Directory "/usr/lib/cgi-bin">
  AllowOverride None
  Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  Order allow,deny
  Allow from all
 </Directory>

So, let's write a script. The example in the docs uses PHP, but we're Python folks. We improve our Unix foo by doing:

cd Desktop
cat > test.py


and then pasting this text after, followed by CTL-D:

#!/usr/bin/python
import os

s = '''Content-type: text/html

<head></head>'''

print s
D = os.environ
for k in D:
    print '<p>', k, D[k], '</p>'

print '</body></html>'

Now, make the script executable, and copy it to /usr/lib/cgi-bin:

telliott@U32:~/Desktop$ sudo cp test.py /usr/lib/cgi-bin/test.py
telliott@U32:/usr/lib/cgi-bin$ sudo chmod 755 test.py


And paste this into Firefox:

http://localhost/cgi-bin/test.py

We obtain:

SERVER_SOFTWARE Apache/2.2.20 (Ubuntu)
SCRIPT_NAME /cgi-bin/test.py
SERVER_SIGNATURE
Apache/2.2.20 (Ubuntu) Server at localhost Port 80
REQUEST_METHOD GET
SERVER_PROTOCOL HTTP/1.1
QUERY_STRING
PATH /usr/local/bin:/usr/bin:/bin
HTTP_USER_AGENT Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0) Gecko/20100101 Firefox/11.0
HTTP_CONNECTION keep-alive
SERVER_NAME localhost
REMOTE_ADDR 127.0.0.1
SERVER_PORT 80
SERVER_ADDR 127.0.0.1
DOCUMENT_ROOT /var/www
SCRIPT_FILENAME /usr/lib/cgi-bin/test.py
SERVER_ADMIN webmaster@localhost
HTTP_HOST localhost
HTTP_CACHE_CONTROL max-age=0
REQUEST_URI /cgi-bin/test.py
HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
GATEWAY_INTERFACE CGI/1.1
REMOTE_PORT 58815
HTTP_ACCEPT_LANGUAGE en-us,en;q=0.5
HTTP_ACCEPT_ENCODING gzip, deflate