三.配置Nginx
先是修改 hosts 文件,意思是创建一个本地域名以便我们访问,比如:
$ sudo subl /etc/hosts 127.0.0.1 rails_project.local
但是这里可以跳过,因为hosts文件中本身默认包含一个localhost本地域,所以我们如果只通过localhost访问页面,这里不需要做修改.
这里我们测试一下hosts设置的是否正确,简单的ping一下就可以了:
ping localhost PING localhost (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.056 ms 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.150 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.137 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.213 ms
然后是Nginx配置文件,如果你不打算使用SSL连接的方式,你只需要如下一段:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { passenger_root /Users/apple/.rvm/gems/ruby-2.2.5/gems/passenger-5.1.1; passenger_ruby /Users/apple/.rvm/gems/ruby-2.2.5/wrappers/ruby; include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; root /Users/apple/src/ruby_src/rails/sample_app/public; passenger_enabled on; rails_env production; } }
里面唯一需要注意的地方是server块中的server_name和root的路径,你需要替换为你自己的东东,另外要注意的是passenger_enbled和rails_env两行配置.
最后你在编辑完了nginx的config文件之后,可以首先用sudo nginx -t命令检查一下基本的conf语法没有错误:
sudo nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
如果此时提示错误,请按照对应出错行号检查附近的代码语法.
四.Nginx进程的管理下面是几个管理nginx服务进程的命令:
//启动nginx服务 sudo nginx //停止nginx服务 sudo nginx -s stop //重新载入nginx服务的配置 sudo nginx -s reload
你主要只需要上面3条命令,更多的你可以通过man nginx查看. 在运行nginx服务后你可以用如下命令查看nginx和passenger进程的状态是否正常:
rvmsudo passenger-memory-stats -------- Apache processes -------- ----------- Nginx processes ------------ PID PPID VMSize Resident Name ---------------------------------------- 54096 1 2408.4 MB 2.2 MB nginx: master process nginx 54111 54096 2408.7 MB 2.1 MB nginx: worker process ------ Passenger processes ------ PID VMSize Resident Name --------------------------------- 54104 2406.3 MB 6.8 MB Passenger watchdog 54107 2465.2 MB 12.1 MB Passenger core 54109 2399.1 MB 6.8 MB Passenger ust-router 54188 2476.9 MB 3.1 MB Passenger RubyApp: /Users/apple/src/ruby_src/rails/sample_app/public (production)
如果此时看不见上述进程,那么请检查之前的操作是否有错误的地方.
如果一切都没问题,这时你在浏览器打开localhost地址应该可以显示你在开头建立的rails_project网站.