基于系统:macOS Sierra 10.12.6

安装RVM

官方网站

在命令行输入:

\curl -sSL https://get.rvm.io | bash -s stable

运行过程中,需要输入电脑账户的密码,类似于安装其他应该,需要输入密码一样。

先运行下面的命令,就可以用rvm的命令了

source ~/.rvm/scripts/rvm

查看Ruby已有版本:

rvm list known

我安装的是2.5.1版本,使用如下命令:

rvm install 2.5

如果只输入版本的前两位,会默认安装2.5版本中最新的版本。同时会安装gem

查看Ruby和Gem的版本:

ruby -v

gem -v

更换gem的国内源(如果可以翻墙或者网好,可忽略)

gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

然后查看一下是否更换成功

gem sources list

如果现实结果如下,就可以了:


*** CURRENT SOURCES ***

https://gems.ruby-china.com

使用gem安装rails

gem install rails

默认安装的是最新版本。

使用rails命令,创建一个rails项目

rails new awesome_project

切换到awesome_project目录,启动rails

rails server

浏览器输入http://localhost:3000来访问项目。成功的话,会显示ROR的页面。

安装Passenger

gem install passenger

使用passenger命令安装Nginx

passenger-install-nginx-module

安装过程中:

Welcome to the Phusion Passenger Nginx module installer, v5.3.3.

This installer will guide you through the entire installation process. It
shouldn't take more than 5 minutes in total.

Here's what you can expect from the installation process:

 1. This installer will compile and install Nginx with Passenger support.
 2. You'll learn how to configure Passenger in Nginx.
 3. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.

选择1

Which languages are you interested in?

Use <space> to select.
If the menu doesn't display correctly, press '!'

 ‣ ⬢  Ruby
   ⬢  Python
   ⬡  Node.js
   ⬡  Meteor

默认就可以,直接回车

Automatically download and install Nginx?

Nginx doesn't support loadable modules such as some other web servers do,
so in order to install Nginx with Passenger support, it must be recompiled.

Do you want this installer to download, compile and install Nginx for you?

 1. Yes: download, compile and install Nginx for me. (recommended)
    The easiest way to get started. A stock Nginx 1.14.0 with Passenger
    support, but with no other additional third party modules, will be
    installed for you to a directory of your choice.

 2. No: I want to customize my Nginx installation. (for advanced users)
    Choose this if you want to compile Nginx with more third party modules
    besides Passenger, or if you need to pass additional options to Nginx's
    'configure' script. This installer will  1) ask you for the location of
    the Nginx source code,  2) run the 'configure' script according to your
    instructions, and  3) run 'make install'.

Whichever you choose, if you already have an existing Nginx configuration file,
then it will be preserved.

Enter your choice (1 or 2) or press Ctrl-C to abort:

此处还是选择1

下面的选择很重要:

Where do you want to install Nginx to?

Please specify a prefix directory [/opt/nginx]:

此处的directory,一定要放在有权限的文件夹下面,我使用的~/nginx,第一次由于没有权限,直接退出安装过程了,555

然后就是漫长的下载、编辑、安装的过程了

安装好之后,passenger会很人性的,向Nginx配置文件中添加

http {
      ...
      passenger_root /Users/sunjarod/.rvm/gems/ruby-2.5.1/gems/passenger-5.3.3;
      passenger_ruby /Users/sunjarod/.rvm/gems/ruby-2.5.1/wrappers/ruby;
      ...
  }

配置文件的目录

~/nginx/conf/nginx.conf

然后,在配置文件中,添加一些内容,我的配置文件如下:

worker_processes  1;

events {
  worker_connections  1024;
}

http {
  passenger_root /Users/sunjarod/.rvm/gems/ruby-2.5.1/gems/passenger-5.3.3;
  passenger_ruby /Users/sunjarod/.rvm/gems/ruby-2.5.1/wrappers/ruby;

  include       mime.types;
  default_type  application/octet-stream;
  sendfile      on;
  keepalive_timeout  65;

  server {
    listen 3500;
    server_name localhost;
    root root /Users/sunjarod/Documents/ruby_works/awesome_project/public;
    passenger_enabled on;
    rails_env development;
  }
}

自带的配置有坑,最好是删除内容之后,把上面的内容复制进去,然后,修改路径为你的项目路径。

由于macOS限制了80端口,我也懒得折腾了,就改成了3500,网上有教程,可以修改成80端口可以使用。

可以通过Nginx访问rails项目了~

按照我的配置,在浏览器中输入http://localhost:3500,就可以看到和刚才使用rails server后一样的页面了。

这个版本的Nginx命令

这个版本的Nginx,无法像Linux系统中那样,使用service来控制,再介绍几个主要命令

启动Nginx

nginx

你没看错,直接在命令行敲,就可以了

停止Nginx

nginx -s stop

测试配置文件

nginx -t

重载配置文件

nginx -s reload