Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the port that thin binds to when starting a Rails server?

I inherited a Rails application, and I'm trying to understand it. However, when I run:

rails s

I receive this log:

=> Booting Thin
=> Rails 3.2.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop

However, this seems problematic to me, as both servers are trying to listen on 3000. What makes rails launch thin when I run rails s?

like image 856
Geo Avatar asked Dec 12 '22 04:12

Geo


2 Answers

When the thin gem is installed rails will use that as a server by default.

You can change the port with the -p option, for example -p 3001. There are also some more options available to set environment, bind address and similar. There is more info on those in the Rails guide.

like image 200
Johan Avatar answered Dec 15 '22 00:12

Johan


Example, a Padrinorb application with nginx and thin server:

Thin

# config/thin.yml

port: 3000
user: padrino
group: padrino
pid: tmp/pids/thin.pid
timeout: 30
wait: 30
log: log/thin.log
max_conns: 1024
require: []
environment: production
max_persistent_conns: 512
servers: 4
threaded: true
no-epoll: true
daemonize: true
socket: tmp/sockets/thin.sock
chdir: /home/padrino/my-padrino-app
tag: my-padrino-app

Nginx

# /etc/nginx/sites-enabled/my-padrino-app

server {
    listen 80 default_server;
    server_name my-padrino-app.com;
    location / {
        proxy_pass http://padrino;
    }
}

upstream padrino {
    server unix:/home/padrino/my-padrino-app/tmp/sockets/thin.0.sock;
    server unix:/home/padrino/my-padrino-app/tmp/sockets/thin.1.sock;
    server unix:/home/padrino/my-padrino-app/tmp/sockets/thin.2.sock;
    server unix:/home/padrino/my-padrino-app/tmp/sockets/thin.3.sock;
}

Script to start, stop, restart, status

#!/usr/bin/env bash
# bin/my-padrino-app-service.sh

APPDIR="/home/padrino/my-padrino-app"
CURDIR=$(pwd)

if [[ $# -lt 1 ]]
then
    echo
    echo "Usage:"
    echo "  $0 <start|stop|restart|status>"
    echo
    exit 1
fi

case $1 in
    "status")
        cat $APPDIR/tmp/pids/thin.* &> /dev/null
        if [[ $? -ne 0 ]]
        then
            echo "Service stopped"
        else
            for i in $(ls -C1 $APPDIR/tmp/pids/thin.*)
            do
                echo "Running: $(cat $i)"
            done
        fi
    ;;
    "start")
        echo "Making thin dirs..."
        mkdir -p $APPDIR/tmp/thin
        mkdir -p $APPDIR/tmp/pids
        mkdir -p $APPDIR/tmp/sockets

        echo "Starting thin..."
        cd $APPDIR
        # Production
        thin start -e production -C $APPDIR/config/thin.yml
        cd $CURDIR
        sleep 2
        $0 status
    ;;
    "stop")
        cat $APPDIR/tmp/pids/thin.* &> /dev/null
        if [[ $? -eq 0 ]]
        then
            for i in $(ls -C1 $APPDIR/tmp/pids/thin.*)
            do
                PID=$(cat $i)
                echo -n "Stopping thin ${PID}..."
                kill $PID
                if [[ $? -eq 0 ]]
                then
                    echo "OK"
                else
                    echo "FAIL"
                fi
            done
        fi
        $0 status
    ;;
    "restart")
        $0 stop
        $0 start
        $0 status
    ;;
esac
like image 35
queru Avatar answered Dec 15 '22 00:12

queru