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

5 Answers 5

215
rvmsudo rails server -p 80
8
  • @pinouchon it works because rvmsudo does sudo plus loading rvm as sudo.
    – iain
    Mar 19, 2013 at 15:55
  • 2
    If like me you're using rbenv rather than rvm, this rbenv plugin will do the equivalent: github.com/dcarley/rbenv-sudo
    – micapam
    May 16, 2013 at 2: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 3, 2013 at 5: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 4, 2013 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, 2014 at 10:36
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?

4
  • This will consume two ports, and it will not allow to run another ruby instance on port 3000.
    – Konstantin
    Sep 8, 2015 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, 2017 at 21:55
  • need to reset the IP route table to default Mar 27, 2017 at 8: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: serverfault.com/questions/159544/undoing-port-forwarding .
    – bentrevor
    May 22, 2018 at 3: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.

1
  • make sure you use /usr/bin/which rails in case rails is aliased Aug 20, 2015 at 15:34
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.

4
  • root is required for privileged ports.
    – cfeduke
    Dec 4, 2010 at 4:27
  • @cfeduke - Oh! Right you are.
    – Swanand
    Dec 4, 2010 at 4:35
  • @marimaf - The documentation has matured over the time. Here is the new link: rvm.io/rvm/install
    – Swanand
    Dec 14, 2012 at 4:47
  • Thank you, this helps with accessing a MAC VM from a pc host to test a rails app i was working on. In that case though i did have to use the -b (bind) option with the vm's ip address.
    – Tom T
    Oct 12, 2017 at 13:26
-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).

1
  • 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. Dec 18, 2014 at 14:26

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.