Tag Archives: server

Start Unicorn on reboot in a Rails application

If you need to run some cron jobs on server, check out the ruby gem whenever. It lets you write cron jobs in ruby way. You can do lots of useful thing using whenever, create database dumps/backups, run scripts/commands on reboot etc.

Using whenever to start unicorn on reboot

1. Add whenever to your Gemfile and run bundle command.

gem 'whenever', :require => false

2.Writing a simple cron job
Move to the project directory and run

wheneverize .

Now, open the file config -> schedule.rb and remove the default contents. Add these lines instead. (you need to replace unicorn_app with whatever you’ve defined in your nginx config)

If you use capistrano for deployment, you should add this to config/deploy.rb.

require "whenever/capistrano"

Next time, you push the code to the server, it will create the required cron jobs on the server. Make sure, it’s working as expected and let me know (via comments) if you’ve any issue.

Deploying Ruby on Rails Application to OpenShift PaaS

OpenShift is a Platform as a Service (PaaS) from RedHat. It’s great for deploying web applications as you can setup/scale/manage apps quickly without any hassle, just like Heroku, but the OpenShift platform is available as a free and open source software, so you’re not locked in (you can install OpenShift on your server and create your own private PaaS if you want).

Previously, I’ve already written on various deployment options available for a rails application, also on deploying rails app to vps. In this article, I’ll guide you in deploying your Rails application to OpenShift.

Step 1. OpenShift Setup

First of all, Create a free Account. You get 3 small gears (resource container/unit : one small gear is equivalent to 512 MB RAM and 1GB storage) for free. If you need more, you can upgrade to premium plans.

Install rhc tool

gem install rhc

Note : If you use rbenv for managing ruby then you also need to run `rbenv rehash`.


create token and upload public keys

rhc setup

And follow the instructions. Once the setup is completed, you can easily create/manage your apps using this client utility (rhc).

Just type :


to see available options. If you want to see all the cartridges (application environment e.g ruby, php, python etc) available, just type :

rhc cartridges

Create a Ruby on Rails App [openshift]
Run this command from the parent directory of your project (~/parent_directory/project) or it will create a directory inside your app.

Note : joblee is the name of the Rails app we’re going to deploy. So, replace it with the name of your app. And I assume you’re using Postgresql for database.

rhc app create ruby-1.9 -a joblee

Add database cartridge

rhc cartridge add postgresql-9.2 -a joblee

Add ‘pg’ to your Gemfile. And run bundle install. Although, you’ll receive database details in the end (above command) but it’s a better to use openshift environment variables in database.yml. So, update the database.yml accordingly. Something like this one : production config for database.yml (openshift)

Step 2. Preparing the application for deployment

Once the openshift is setup, move to your project directory and setup git push deployment for your app. rhc show app rails

cd joblee
rhc show-app joblee

And get the value of Git URL from the above command. Now, add the remote url for deployment. (replace GIT_REMOTE_URL with the value you got above)

git remote add openshift GIT_REMOTE_URL

Now, merge the remote repo with your development repo.

git pull openshift master

Then, you need to manually fix conflict in config.ru. Keep the default content of config.ru file.

 git add .
 git commit -am "fixed merge and now getting ready for deployment"

Step 3. Deploy

git push openshift master

That’s all :-). Now, your app should be live at : http://your_app_name-domain.rhcloud.com.

Note : If anything goes wrong, you can always ssh into your app server and fix things there. To ssh into your server, type :

rhc ssh joblee

Managing apps
ssh into the app server and type


to see lots of options available. You can also get rails console, just by typing :

cd app-root/repo
bundle exec rails console RAILS_ENV=production

Postgresql Database Setup

cd app-root/repo
RAILS_ENV=production rake db:setup

Assets compilation problem
I had to run few other commands on server due to some gems/assets related problems. But you can add it to openshift deployment hooks like this example rails app, so you won’t have to manually execute it every time you deploy the app.

RAILS_ENV=production bundle
RAILS_ENV=production bundle exec rake assets:precompile

Adding Custom Domain
First, add openshift app url (app-domain.rhcloud.com) as a cname record for www and the IP Address (using the free redirection service) for root record. So, it will redirect root domain to www version.

Now, register that domain with rhc server.

rhc alias-add joblee www.joblee.in

If you don’t want to use www domain, then you should type this instead : (and you also need to add cname record for root domain, at your domain registrar)

rhc alias-add joblee joblee.in

Note : I’ve setup naked domain so, www version of the domain is getting redirected(301) to the root domain. Not all DNS providers supports this (I’m using namecheap and I can specify openshift app url as a CNAME record for the root record) and sometimes it can cause some weird behaviours, especially if you’re using email with the domain. (read more on how it can break MX records)

ajenti screenshot

Ajenti – Awesome and easy server administration frontend!

Ajenti is a server admin panel program for web administrators. It’s available for Debian (or its derivatives such as Ubuntu), Arch, CentOS, FreeBSD, Gentoo etc. Most of the server administrators prefer command line, that’s usually fast and efficient for setup and deployment, but for monitoring servers, it would be lot easier to use an admin panel like Ajenti, especially if you’re not very good at commands.

ajenti screenshot


Ajenti provides a very nice and easy to use Dashboard for monitoring and controlling servers, you can check resource usages (RAM, CPU graphs and reports), configure firewall, DNS, check logs, install/update packages, analyse network status, create/remove cron jobs, recovery/backup settings and a lot more things (just install the plugins you want to use). It basically provides an elegant GUI for managing servers, in a very easy way. You would probably feel like – using a desktop environment, specifically designed for server administration.

Setting up Ajenti on Debian/Ubuntu

Checkout the official website (for more info) : http://ajenti.org/

speed up wordpress

How to setup wordpress on Ubuntu 12.04 Server (VPS)

It’s a step by step guide for setting up wordpress on a vanila server (e.g Digital Ocean, don’t know about it? then read Digital Ocean VPS Review) running Ubuntu 12.04 LTS (latest LTS release, recommended for server). With, most of the VPS providers, you can deploy GNU/Linux distribution from their control panel itself, after that, everything will done over the ssh connection.

setting up wordpress

Step 1. Setting up the server

ssh into the server and create a user (as it’s not a good idea to use root account).

ssh root@vps_ip_address
apt-get -y update
adduser user_name --ingroup sudo

Now, logout from the current session and login again with the user newly created above.

ssh user_name@vps_ip_address

Install the web server – Apache (you could also choose nginx)

sudo apt-get install apache2

Now, create a virtual host –
In this tutorial I’ll be using nano editor (a light-weight command line editor) So, if it’s not already installed – then install it using sudo apt-get install nano.

sudo nano /etc/apache2/sites-available/example.com

and add this contents (change few things according to your need such as domain name, user_name)

If you selected nginx instead of Apache, here is a sample configuration for nginx/wordpress blog.

Then enable the virtual host,

sudo a2ensite example.com
sudo service apache2 reload

Step 2. Installing Dependencies (PHP, MySQL)

During installation process, you will be asked to provide password for MySQL root account.

sudo apt-get install mysql-server
sudo apt-get install php5 libapache2-mod-php5 php5-mysql

For image manipulation, wordpress uses php-gd library, so you need to install that as well,

sudo apt-get install php5-gd

You may have to enable rewrite mod for apache2, to do that simply type :

sudo a2enmod rewrite
sudo service apache2 restart

Step 3. Creating Database

Login with MySQL root account and create the required users and databases (you need to remember these details for wp-config.php)

mysql -u root -p
create database DBNAME;
grant all on DBNAME.* to DB_USER@localhost identified by 'password';

Step 4. Installing WordPress

Download the latest version of wordpress and extract its content.

cd ~
wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz

For simplicity, you can rename the wordpress directory to something like – your domain name e.g example.com.

mv wordpress example.com

Now, you can manually edit the wp-config.php file or go through the automated installation process (just visit the URL, I assume you have setup DNS correctly, or use IP address instead).

cd example.com
nano wp-config-sample.php

Then add the database details and random strings for session tokens etc and save the file (Ctrl+x, y). Now, rename the file.

mv wp-config-sample.php wp-config.php

That’s set, you’ve successfully installed wordpress, visit the URL/IP_ADDRESS and create the admin user.

Moving your Blog ?

If you’re moving your old wordpress blog to a new server, then you also need to copy wp-contents directory (from the old one to new server, it’s the directory – that holds your images, themes and plugins). Besides that you will also have to export/import database contents. Use your server control panel or a tool like PHPMyAdmin (which is usually installed on all shared hosting providers) to export your database contents in *.sql format.

I assume you already have an exported database file (*.sql), So you can easily import it, on new server using the single command –

mysql -h localhost -u DB_USER -p DB_NAME < db_backup.sql

Note : If you have any problem during installation, then leave a comment here, we’ll try to figure it out!


VNC server for ubuntu 10.10

What is VNC ?

VNC(Virtual Network Computing) is a protocol that uses Remote Frame Buffering(RFB) to share/control the desktop(graphically) remotely over the internet. VNC is  composed of three things –

  • VNC Client : The Program Running on the computer who will access the desktop remotely
  • VNC Server : The Program Running on the computer which will allow the other computer to share/control the desktop.
  • VNC Protocol(RFB) : simple communication protocol which transfer the events(mouse and keyboard events) between the client and server.

For Ubuntu 10.10(Maverick Meerkat), a lot of free and open source vnc servers are available,some of them includes –

#1 : Vino : it is the default vnc server of Ubuntu 10.10. You can start using it on ubuntu by going to System->Preferences->Remote Desktop. Then configure it according to your need. Use strong password for security reasons.


#2 : x11vnc : it is independent of Linux Desktop (GNOME/KDE) and specially useful for xubuntu users. Using x11vnc requires some command line skills so, it isn’t much easy for beginner linux users.

Other vnc servers are tightvncserver,krfb etc.

Recommended Third party VNC server/client

Teamviewer is the best vnc server for beginner users, it is available for Linux based OS,Windows and Mac. it is free for personal uses. The desktop sharing can be easily done using teamviewer. First it creates a session then you can send the unique session id/password(generated by teamviewer) to other client end ; all with a simple graphical interface(No commands knowledge is required).

teamviewer vnc server

See More Tutorials on installing TeamViewer on Ubuntu 10.10 if you want to try teamviewer.