93

By default,

 rails s #running on 3000 port

Now I want to run it on port 80. So I tried:

 sudo rails -s -p80

But it threw an error:

mlzboy@mlzboy-MacBook ~/my/b2c2 $ sudo rails s -p80
sudo: rails: command not found

I used rvm to install ruby & rails. It seems rvm is user specified. Is it not able to find rails in root?

I also tried below code:

mlzboy@mlzboy-MacBook ~/my/b2c2 $ which rails
/home/mlzboy/.rvm/gems/ruby-1.9.2-p0/bin/rails
mlzboy@mlzboy-MacBook ~/my/b2c2 $ sudo /home/mlzboy/.rvm/gems/ruby-1.9.2-p0/bin/rails s -p80
Ravindra S
  • 6,302
  • 12
  • 70
  • 108
mlzboy
  • 14,343
  • 23
  • 76
  • 97

5 Answers5

215
rvmsudo rails server -p 80
iain
  • 16,204
  • 4
  • 37
  • 41
  • @pinouchon it works because `rvmsudo` does sudo plus loading rvm as sudo. – iain Mar 19 '13 at 15:55
  • 2
    If like me you're using rbenv rather than rvm, this rbenv plugin will do the equivalent: https://github.com/dcarley/rbenv-sudo – micapam May 16 '13 at 02:40
  • Would this be more of a risk if there’s a security hole in RVM, given that it’s running as root? – Kevin Chen Nov 03 '13 at 05:58
  • 2
    @Kevin Chen: Not in RVM, but I would be worried about running Rails as root. In production you would use Apache or Nginx, which needs root permissions to claim the port, but runs as a special user. My solution is just for testing things out in development. – iain Nov 04 '13 at 12:37
  • I tired this but getting the `socket.rb:206:in `bind': Address already in use - bind(2) for 0.0.0.0:80 (Errno::EADDRINUSE)` error – r15 Mar 20 '14 at 10:36
  • @r15 that means that you already have something else running on port 80. – iain Mar 21 '14 at 07:19
  • thanks I resolved the error. my apache is running on 80 – r15 Mar 21 '14 at 07:59
  • @micapam You should post that as an answer (about rbenv). – Tyler Collier Aug 27 '15 at 05:23
24

Just forward the request from port 80 to 3000 using below command:

sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000

Another option is:

rvmsudo rails server -p 80

However please remember to free this port from Apache or other services which consume this port normally. Also, I m not sure giving sudo permission to RVM may have any security issue or not?

Dinesh Saini
  • 2,917
  • 2
  • 30
  • 48
  • This will consume two ports, and it will not allow to run another ruby instance on port 3000. – Konstantin Sep 08 '15 at 10:47
  • How do you reverse this? It works perfectly for me, which is great, but what if I want to undo the redirect? Thanks. – robins35 Mar 22 '17 at 21:55
  • need to reset the IP route table to default – Dinesh Saini Mar 27 '17 at 08:13
  • 1
    You can reverse this command by replacing the `-I` with `-D`, so `sudo iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000`. The answers here go into more detail: https://serverfault.com/questions/159544/undoing-port-forwarding . – bentrevor May 22 '18 at 03:36
9

Was going to suggest

rails=`which rails` ; sudo $rails server -p 80

but that still tries to use the global gemset and not the project gemset from RVM. So...

  1. Make sure sshd is running on your Mac. (System Prefs => Sharing => Remote Login checked)
  2. Make sure rails s is running on port 3000 as your non-root user
  3. Open a new terminal and...

    me=``whoami``; sudo ssh -L 80:127.0.0.1:3000 -l $me -N localhost

(BTW reduce the duplicate `'s to singular ones in the line above, I cannot figure out how escape it properly here.)

The first Password: is your root user, the second is the password for whomever whoami returns.

Though you probably want to install Phusion Passenger and set it up under your local Apache. Unless you are just trying to demo something real quick and this is not a permanent solution of course.

cfeduke
  • 23,100
  • 10
  • 61
  • 65
2

If you are using RVM, and you did the default setup, then you shouldn't use sudo.

Just:

mlzboy@mlzboy-MacBook ~/my/b2c2 $ rails server -p 80

However 80 is a privileged port, so you need to run as root, and you will have follow the instructions for Multi-User installation of RVM.

Swanand
  • 12,317
  • 7
  • 45
  • 62
-1

you can start server on port 80

rails s -p 80

If port 80 does not bind(other processes is not using to port 80).

uma
  • 2,932
  • 26
  • 20
  • 1
    Port below 1024 needs root access and you can't run application until you have not setup rails via root URL. I have implemented and test it. – Dinesh Saini Dec 18 '14 at 14:26