Friday, February 21, 2014

YAWS Setup

Introduction

YAWS, short for "Yet Another Web Server" is completely written in Erlang. It's prided on being able to handle more traffic than Apache [1]. It's the technology that should have been used by the healthcare website (just kidding). 

This guide will cover from start to finish the steps necessary to create a YAWS based server. This will be thorough as I have no experience with Erlang or web-technologies. If you are new to Erlang or to websites, this will be the guide for you. If you are an advanced user this could be used as a sleep-aid. This guide is setup into different sections. The first is obviously going to be prerequisites.

Prerequisites (What do I need?)

I'm going to set up YAWS on a CrunchBang distro (it's the same for Debian/Ubuntu). It's a 64-bit virtual machine running a Bridged network adapter. The nice thing about running in a Virtual Machine is that anyone can do this! Go grab an ISO for Debian/Ubuntu/LinuxMint/CrunchBang or whatever your flavor of the month is and install it to a virtual machine. I use VirtualBox because it's free and easy to use. 

When setting up your virtual machine make sure to set the network adapter to Bridged. I've heard from colleagues that the NAT adapter has issues. The other reason you want Bridged is so that the network can communicate "more directly" to the virtual machine without having to go through the host machine.

From the linux command line:

> sudo apt-get install erlang

Erlang is needed because it's the base language that YAWS is written in.

Getting YAWS

The first step is to use wget to obtain the source code for yaws. I don't recommend using apt-get to install yaws as the version in the repo seems to have problems. It is recommended to go right to the yaws website to obtain. Open this link in a web browser:

http://yaws.hyber.org/download

This will take you to a list of downloads. Find the most current download; currently it is yaws-1.98.tar.gz. So from the command line do the following:

> wget  http://yaws.hyber.org/download/yaws-1.98.tar.gz

This will download it to the current directory you are in. Now unpack:

> gunzip yaws-1.98.tar.gz
> tar -xf yaws-1.98.tar

All done. Source code is ready to go. Step into the yaws-1.98 directory:

> cd yaws-1.98

Try to configure and build the project:

> ./configure
> make

The first thing I saw was an error two files in stating:

epam.c:2:22: fatal error: pam_appl.h: No such file or directory.

Looks like I'm missing some dependencies! If you get this error, just do the following:

> sudo apt-get install libpam0g-dev

After that my build was successful! The next step is to install yaws:

> sudo make install

This will put yaws into the appropriate place on your system and also it will put together some default configuration files for you. To test out yaws do the following:

> sudo yaws -i 

The -i flag on yaws will put you into interactive mode. If you then point your web browser at 127.0.0.1, you'll see a nice little YAWS page!

First Page

First run the yaws web server: 
 
> sudo yaws -i
 
Pay close attention to the output printed to the console. One of the lines should look like:

=INFO REPORT==== 21-Feb-2014::11:09:21 ===
Yaws: Listening to 0.0.0.0:80 for <2> virtual servers:
 - http://servername under /usr/local/var/yaws/www
 - http://localhost under /tmp

I've highlighted the text of interest. The gold line, 0.0.0.0:80 is stating that the server is listening on address 0.0.0.0 with port 80. When we are listening to address 0.0.0.0, it means any IPv4 address. If you did a netstat -l in Linux, you'll see all of your other servers running and the address they are bound to.

The blue line is what is more interesting. The blue part shows where the web server is running from. Take this value and navigate to that directory:

> sudo su   # Change to root.
> cd /usr/local/var/yaws/www
> ls

You should see a file called index.yaws. This is the file that is first loaded when you point your browser at the server's main site. Try this:

> mv index.yaws index.yaws-backup
> vim index.yaws

Type in the following program to get started:

<html>
    <body>

        <erl>
            out(A) -> { html, "Hello World!" }.
        </erl>

    </body>
</html>

It's that simple! When you run your server:

> sudo yaws -i

And point your web browser at your server, you should see the "Hello World!" message!


No comments:

Post a Comment