Notes on installing a Play Framework + MongoDB application on a Linux server at A2 Hosting

These are some notes on how I installed a Play Framework + MongoDB application on a Linux server at A2 Hosting. These notes probably won’t make sense to other people, they’re just here for me. (Sorry about that, I don’t have a great way to post things here just for myself.)

Here are the notes:

A2 Hosting
----------

A2 URL: https://my.a2hosting.com/index.php

ssh username@10.1.1.1 -p 22
ssh rootuser@10.1.1.1 -p 22

---++ Enable Firewall, Secure ssh

# vi /etc/ssh/sshd_config
    # ----------------------------------------------------------
    # AJA: see http://wiki.centos.org/HowTos/Network/SecuringSSH
    # AJA: see http://centoshelp.org/security/securing-sshd/
    # ----------------------------------------------------------
    Port 22
    AllowUsers username1
    LoginGraceTime 1m
    PermitRootLogin no
    MaxAuthTries 6
    MaxStartups 4

# service sshd restart

---++ Add Non-root User

# adduser USERNAME
# passwd USERNAME

---++ Add Software

* install java
  yum install java-1.7.0-openjdk-demo.x86_64

* install mongo
  http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat-centos-or-fedora-linux/

* install nginx
  followed these notes:
  http://www.cyberciti.biz/faq/install-nginx-centos-rhel-6-server-rpm-using-yum-command/
  (1)
  wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
  rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
  (2)
  yum install nginx

* Play Framework and MongoDB
  http://blog.knoldus.com/2012/08/02/working-with-play-framework-using-scala-and-mongodb/
  http://mongodb.github.io/casbah/tutorial.html

* install mysql
  yum install mysql-server
  chkconfig mysqld on
  service mysqld start
  /usr/bin/mysql_secure_installation
  - give it a password

* install php
  yum install php-mysql php-devel php-gd php-xmlrpc

* install apache/httpd
  yum install httpd
  chkconfig httpd on
  chkconfig --list httpd
  
  later: need to install 'rpaf' module to get IP addresses working
  (http://stderr.net/apache/rpaf/)

Next: Configure software


---++ Nginx

Play Config Info: http://www.playframework.com/documentation/2.1.x/HTTPServer

Enable nginx service:

  # chkconfig nginx on

Commands:

  # service nginx start
  # service nginx stop
  # service nginx restart
  # service nginx status
  # service nginx reload

Edit:

  /etc/nginx/nginx.conf

More config:

  # mount the mfoo client, which is served up by apache
  location /mfoo {
      proxy_pass        http://mfooclient:8888/;
      proxy_set_header  X-Real-IP $remote_addr;
  }

  # mount the mfoo server, which is served up by the play framework console
  location /server/notes {
      proxy_pass        http://mfooserver:9000/notes;
      proxy_set_header  X-Real-IP  $remote_addr;
  }


---++ MongoDB

Good URLs:

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat-centos-or-fedora-linux/
http://docs.mongodb.org/manual/faq/fundamentals/
http://docs.mongodb.org/manual/faq/diagnostics/

* "Uses .rpm packages as the basis of the installation. 10gen publishes packages of the MongoDB releases as .rpm packages for easy installation and management for users of CentOS..."

# Step 1:
# vi /etc/yum.repos.d/10gen.repo
  [10gen]
  name=10gen Repository
  baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
  gpgcheck=0
  enabled=1

# Step 2:
# yum install mongo-10gen mongo-10gen-server

# Configuration:

   * configuration file:  /etc/mongod.conf
   * control script:  /etc/rc.d/init.d/mongod
   * This MongoDB instance will store its data files in the /var/lib/mongo and its log files in /var/log/mongo, and run using the mongod user account.

* Commands

   * To start on reboot: chkconfig mongod on
   * service mongod start
   * service mongod restart
   * service mongod stop

* Using/Testing

   * mongo (client command)
   * (starting and stopping is very slow on A2)

   WARNING: You are running in OpenVZ. This is known to be broken!!!

* On the Production server

  ...
  $ mongo
  mongo> use mfoo
  mongo> db.users.save({"email":"EMAIL", "name”:"YOUR "NAME, "password":"PASSWORD"})

  # simple test
  db.test.save( { a: 1 } )
  db.test.find()
  ...

* Debug information on MongoDB

   * tail -f /var/log/mongo/mongod.log
   * sudo grep mongod /var/log/messages
   * sudo grep score /var/log/messages

* Ports

   * Admin web console waiting for connections on port 29000
   * waiting for connections on port 29001


---++ Play Framework - Production Notes

http://www.playframework.com/documentation/2.1.x/Production
http://www.playframework.com/documentation/2.1.x/ProductionConfiguration

* To deploy the app, first update =appVersion= in _project/Build.scala_. Then:

  ...
  $ play clean compile dist
  ...

  That creates _target/scala-2.10/kfoo_2.10-1.0-SNAPSHOT.jar_. =scp= that file to the server.

* Script on the server:

  ...
  # Copy this file to the KFOO folder, then use it to run the app.

  # default Xmx is 64M (http://docs.oracle.com/javase/1.4.2/docs/tooldocs/linux/java.html).
  # 'port 9000' is the default, but i put this here in case i need to change it.
  # start -Dhttp.port=8080 -Xms512M -Xmx1G

  chmod +x start
  nohup ./start -server -Dconfig.resource=application-prod.conf -Dhttp.port=9000 &
  ...

  To use that script:
  
    * Copy this file to the KFOO folder (/opt/kfoo), then use it to run the app.
    * Unzip it in /opt/kfoo
    * cd to its directory
    * cp my startup script to that dir
    * shut down the old instance, start this new one

* More options to use when starting the Play server:

  # port and address
  $ start -Dhttp.port=1234 -Dhttp.address=127.0.0.1

  # additional jvm args
  $ start -Xms128M -Xmx512m -server

  $ start -Dconfig.resource=application-prod.conf

  # look for config file on filesystem
  $ start -Dconfig.file=/opt/conf/prod.conf