Play with Apache or nginx forwarding on Ubuntu 12.10

Posted on Thursday, March 21, 2013


I am still playing around with scala play so I may be mistaken in this idea… so take that into mind as I describe what I am doing…

I have a need to run multiple scala applications on the same server for different domain names.   I think I can do this if I have apache or nginx as the front end HTTP server sending the correct address to the correct scala play app.  I also need to run all of this on an ubuntu  12.10 server.

At any rate here is what I did to accomplish that.





The Play guys put up some good documentation to get you started at http://www.playframework.com/documentation/2.1.0/HTTPServer [1]
I used some of their notes here to help out.



Scala program


First get two hello world scala play programs running on your system.  I am going to assume you do not have scala, java, or scala play installed on your Ubuntu 12.10 system.

Installing Scala on Ubuntu 12.10


From the command line you could run the following


> sudo apt-get install scala


If you do this on Ubuntu 12.10 you get Scala 2.9.2 using OpenJDK




That is all well and good but I would rather use Java 1.7 from Sun and the latest scala build 2.10.0 so to do that install you need to first install java 1.7 to do that run the following commands


>  sudo apt-get purge openjdk*
>  sudo apt-get install python-software-properties
>  sudo add-apt-repository ppa:webupd8team/java
>  sudo apt-get update
>  sudo apt-get install oracle-java7-installer
>  java -version




 
Run the following command to download the .tgz file from scala and untar it


  > wget http://www.scala-lang.org/downloads/distrib/files/scala-2.10.0.tgz
  >  tar xf scala-2.10.0.tgz


Now place the scala program in /usr/bin/scala
And make a ln to it

    > sudo mkdir /usr/lib/scala
    > sudo mv scala-2.10.0 /usr/lib/scala/
    > sudo touch /usr/bin/scala
    > sudo ln -fs /usr/lib/scala/scala-2.10.0/bin/scala /usr/bin/scala
    >  sudo chmod a+x /usr/bin/scala



Testing Ubuntu


Open a new terminal and run scala


  > scala




Here you can see scala is using Java 1.7_0_17 from the JAVA_HOME variable.




Install the Play Framework on Ubuntu


Download Scala Play and unzip it


> cd
> mkdir play
> cd play
> wget http://downloads.typesafe.com/play/2.1.0/play-2.1.0.zip
> unzip play-2.1.0.zip



Create a location to put the play software, put it in /usr/lib/play
And make a ln to it


    > sudo mkdir /usr/lib/play
    > sudo mv play-2.1.0 /usr/lib/play
    > sudo touch /usr/bin/play
    > sudo ln -fs /usr/lib/play/play-2.1.0/play /usr/bin/play
    >  sudo chmod a+x /usr/bin/play



Now a quick test.   Run this command


  > play




Play will attempt to start, but it has no project to run so it just quits.  It does display the version of Play, Java, and Scala you are using.


This is the bare bones set up. 



Creat the 1st  scala program that will run on port 9000


Run the following command to create a Scala Play Application


> cd
> play new HelloWorld




Click enter then when presented with the option of scala or java use scala by entering 1


This creates a HelloWorld folder in the current directory.





Test It

Now that the web application has been created run it.


> cd HelloWorld
> play ~run



Open a web browser (assuming your server is running locally open it at this address)







Update it


As a quick update go into the program and have it say

"Hello World this is the port 9000 Play server"



> cd
> vi HelloWorld/app/controllers/Application.scala




Update this portion of the code with the message and save it.

Play auto recompiles it,  now refresh your web page.





Now this message appears in the header





Creat the 2nd  scala program that will run on port 8000


Run the following command to create the second scala Play application


> cd
> play new HelloWorld2





Click enter then when presented with the option of scala or java use scala by entering 1

This creates a HelloWorld2 folder in the current directory.



Test It


Now that the web application has been created run it on port 8000


> cd HelloWorld2
> play -Dhttp.port=8000  ~run


Open a web browser (assuming your server is running locally open it at this address)







Update it


As a quick update go into the program and have it say

"Hello World this is the port 8000 Play server"


> cd
> vi HelloWorld2/app/controllers/Application.scala




Update this portion of the code with the message and save it.
Play auto recompiles it,  now refresh your web page.





Now this message appears in the header



Install Apache


Now that there are 2 Play servers running one using port 9000 and the other port 8000.   I am going to try to get Apache to serve as a proxy server for them.



>  sudo apt-get install apache2


I found this site to help with the apache configurations http://kkpradeeban.blogspot.com/2012/03/configuring-apache-modproxy-load.html [2]

Set up the proxy, run these commands


>  sudo a2enmod proxy_http


Now edit /etc/apache2/sites-available/default


>  sudo vi /etc/apache2/sites-available/default






Configure for proxy


For my first test I am only going to set up the apache server to forward to the play server running on port 9000


<VirtualHost *:80>
  ProxyPreserveHost On
  ServerName www.example.com
  ProxyPass  /excluded !
  ProxyPass / http://127.0.0.1:9000/
  ProxyPassReverse / http://127.0.0.1:9000/
</VirtualHost>




Now restart apache and start up play using port 9000



>  sudo /etc/init.d/apache2 restart
> play ~run


Opening my site I did get scala play up

Now open up the address you put in for this site.  In my case I did a little test locally on my network and called it  hometest.10x13.com

So I opened up

and saw this





Success!!  It is forwarding to the play server running on port 9000



update /etc/apache2/sites-available/default for 2 sites


>  sudo vi /etc/apache2/sites-available/default



<VirtualHost *:80>
  ProxyPreserveHost On
  ServerName www.example.com
  ProxyPass  /excluded !
  ProxyPass / http://127.0.0.1:9000/
  ProxyPassReverse / http://127.0.0.1:9000/
</VirtualHost>
<VirtualHost *:80>
  ProxyPreserveHost On
  ServerName www.example2.com
  ProxyPass  /excluded !
  ProxyPass / http://127.0.0.1:8000/
  ProxyPassReverse / http://127.0.0.1:8000/
</VirtualHost>




Save it and restart apache



>  sudo /etc/init.d/apache2 restart


Open up the address you put in for this site.  In my case I did a little test locally on my network and called it  hometest8000.10x13.com

So I opened up

and saw this




Success!!  It is forwarding to the play server running on port 8000
  
Here is an image of both web sites open at the same time





Now since I want to try doing this with nginx I am going to remove apache2.  Here are the commands I used to remove apache2 from my ubuntu server.


>  sudo apt-get autoremove
> sudo apt-get remove apache2*





Install nginx


Now that there are 2 Play servers running one using port 9000 and the other port 8000.   I am going to try to get nginx to serve as a proxy server for them.



>  sudo apt-get update
> sudo apt-get upgrade
> sudo apt-get install nginx


Start the nginx server


>  sudo /etc/init.d/nginx start


Open the address of the server in a web browser and confirm nginx is running

 

Success!!





Edit the config file


Edit the /etc/nginx/nginx.conf file.


>  sudo vi sudo vi /etc/nginx/nginx.conf


Edit the file to the following  (just to test one single site)


user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

  proxy_buffering    off;
  proxy_set_header   X-Real-IP $remote_addr;
  proxy_set_header   X-Scheme $scheme;
  proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header   Host $http_host;

  upstream my-backend {
    server 127.0.0.1:9000;
  }

  server {
    server_name www.example.com;
  }

  server {
    keepalive_timeout    70;
    server_name www.example.com;
    location / {
      proxy_pass  http://my-backend;
    }
  }
}



Then restart


>  sudo /etc/init.d/nginx restart





Success!! It forwarded correctly.





Now update /etc/nginx/nginx.conf file. To handle 2 play servers


>  sudo vi sudo vi /etc/nginx/nginx.conf


Edit the file to the following  (just to test one single site)


user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

  proxy_buffering    off;
  proxy_set_header   X-Real-IP $remote_addr;
  proxy_set_header   X-Scheme $scheme;
  proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header   Host $http_host;

  upstream my-backend {
    server 127.0.0.1:9000;
  }

  server {
    server_name www.example.com;
  }

  server {
    keepalive_timeout    70;
    server_name www.example.com;
    location / {
      proxy_pass  http://my-backend;
    }

  upstream my-backend-8000 {
    server 127.0.0.1:8000;
  }

  server {
    server_name www.example2.com;
  }

  server {
    keepalive_timeout    70;
    server_name www.example2.com;
    location / {
      proxy_pass  http://my-backend-8000;
    }
  }
}


Then restart



>  sudo /etc/init.d/nginx restart






Success!! It forwarded correctly.



And both open at the same time




This is a very simple set up and does not account for https for the servers.  The nginx example at http://www.playframework.com/documentation/2.1.0/HTTPServer [1] shows the details about SSL.





References
[1]        Set-up a front-end HTTP server           http://www.playframework.com/documentation/2.1.0/HTTPServer
                Accessed 03/2013
[2]        Configuring Apache mod_proxy load balancer
                Accessed 03/2013

No comments:

Post a Comment