Pages

Showing posts with label erlang. Show all posts
Showing posts with label erlang. Show all posts

Monday, July 20, 2015

Yaws - Install and Configure route

I have been looking to build a wiki based on things in my head. I then figured it would be fun to write some Erlang, and I'm a fan of Yaws - Yet Another Web Server - so I decided to install yaws, designate a subdomain to it, and have apache proxy requests to port 8080, where Yaws is listening.

Step One - Install Yaws


sudo apt-get install yaws

well that was easy.

Then you can check the status of the yaws service



To view the default yaws page, navigate to the url: 192.168.2.101:8080 (localhost:8080 if on the same machine - I'm installing on a raspberry pi over ssh, so I'm useing the static IP) as seen in the status, you can also navigate to https://<URL>:8443 and get the same


Step Two - Access Yaws by Domain Name

I'm running this on a chromebook and haven't yet found a way to change how chrome resolves hostnames. To get around this, I use dnsmasq on the pi to allow computers on my network to access the local site. I put

yaws.bitzel.net 192.168.2.101 

in /etc/hosts and reload with sudo service dnsmasq force-reload to put the new domain into effect. This allows me to access the page through yaws.bitzel.net:8080 (or 8443 if using https). Before going any further, I've added the apache configurations from this post to a github repository github.com/sbitz/apache

Step Three - Eliminate the Port

In order to eliminate the need for the port, I'd like to use apache to proxy and send any traffic destined for yaws.bitzel.net over port 80 (the default http port) to port 8080. I do this by setting up a virtual host in my apache configuration.

I found this article very helpful when configuring virtual hosts on my server. It explains that apache looks for and reads the configuration in conf.d/virtual.conf, as well as explaining how apache treats the sites-available and sites-enabled directories. It is clear enough from their names, but I like to know why programs do certain things. Instead of being explicitly configured in a file, anything in these directories is loaded on startup by apache.

The configurations for each <VirtualHost> is saved in sites-available, and a link is placed in sites-enabled using the a2ensite command (a2dissite will clean up the link).

And the debugging begins... 


At first, I had NameVirtualHost * defined in my virtual.conf file. This caused apache to complain about mixing http and https ports. I got around this by adding a second line, and changing the * in the two lines to *:80 and *:443.

I fumbled around for a while trying to reload via: service apache2 and contstantly getting the message:
[FAILED] Reloading web server config: apache2 failed! 

This stack exchange post addressed the issue, or rather pointed me to the command: `apachectl configtest` which told me that I had a syntax error in line 5 of my virtual host configuration - ProxyPass needed a source path, and I reloaded after inserting a '/ ' between ProxyPass and my forwarded URL. This enabled me to reload apache without issue.

One more test showed yaws.bitzel.net displaying the main apache page, but with one last command:
a2dissite default 

disabled the default virtual host, leaving my custom virtualhost for the yaws app to handle the request.

Saturday, July 6, 2013

Erlang Web Application: Getting Started

Lately I've been getting back into building my own web application. It started with some MongoDB which led to Python and Bottle through this video to Apache to Yaws and Erlang.  I've finally settled on working through some of the examples included in the O'Reily book Building Web Applications with Erlang.

It's been a lot of installation and figuring out what goes where, but I'm starting to see some cool stuff. I really like the idea of running a web application through Yaws. Erlang has always made a lot of sense to me with it's light-weight application threads and message passing.

More Details

I didn't do anything special from the standard way of installing packages in Ubuntu. I don't think there's anything special other than
steve@ubuntu$ sudo apt-get install yaws
This should install all the dependencies, including Erlang which you'd need to get started. The yaws service is started, using 
/usr/share/yaws
  as a home directory for the server. Starting/stopping/restarting the service is as easy as:

steve@ubuntu$ sudo service yaws <command>

 First Examples

  I just have a couple very simple examples right now. Hopefully more to come shortly. One thing I've found is that if you modify index.html in the server's home directory, changes can be seen by visiting localhost:8080/index.html but not simply localhost:8080 until you perform a server restart (or maybe reload). Most likely due to file handles of the server. Fortunately, creating examples and running <erl> tags within new files works because you specify the URL for the files explicitly, so you don't have to restart the server between every step.

First output

The first example was straight from the book. Creating method.yaws in the server home directory:
<erl>
method(Arg) ->
  Rec = Arg#arg.req,
  Rec#http_request.method.

out(Arg) ->
  {ehtml, f("Method: ~s" , [method(Arg)])}.

</erl>
Then  browsing to localhost:8080/method.yaws shows that you used a get request.

 Simple Redirect

The next simple example is to redirect to another page:
<erl>
method(Arg) ->
  Rec = Arg#arg.req,
  Rec#http_request.method.

out(Arg) ->
  {ehtml, f("Method: ~s" , [method(Arg)])}.

</erl>
This simply redirects to the method.yaws example above.