版本信息:
Amazon Linux
Paseenger 4.0.2
Rails 3.2.13
STEP 1 新建一个rails用户,将用户权限改为755,以便于设置Nginx Root /app/public 不会产生permission deny的问题。public文件夹能够被所有用户访问是不够的,需要所有父文件夹都具有可执行权限。
- sudo useradd {wheel} rails_user ----- wheel 是某个用户组,给予rails_user以sodu权限
- optional: [sudo usermod -a -G wheel gollum (如果要使用已有用户,追加已有用户以sudo权限)]
-
sudo passwd --- (create new password)
- sudo su -rails_user 将当前用户改为 rails_user
- sudo chmod -R 755 /home/gollum. change the file permission of the whole directory.
*STEP 2 检查现有系统默认状况。(重要)
- sudo chkconfig --list 查看所有的系统服务的启动项列表
- sudo yum remove nginx 如果有Nginx已经装在系统里面,卸载原有Nginx。(错误的解决: unknown directive "passenger_enabled" in /etc/nginx/nginx.conf)
- sudo chkconfig --del httpd 如果开机自动运行Apache,关闭init.d/httpd服务。
STEP 3 快速安装 Install passenger and nginx with rvm
- 重要!! 做一切之前,首先 sudo yum update ( make sure all the package we download are up to date)
- curl -L get.rvm.io | bash -s stable 下载最新的rvm
- source .bashrc 使系统重新读取配置文件。如你更改了.bashrc,如果不重新登录的话,变动是不会生效的。
- rvm install 1.9.3
- rvm use 1.9.3 --default 默认使用1.9.3
- rvm rubygems current 更新gem源
- gem install passgenger 默认安装在rvm/ruby-1.9.3的目录之下
- rvmsudo passenger-install-nginx-module (rvmsudo 和 sudo 是不一样的。 rvmsodu会加载当前的环境) 按照默认提示,nginx安装目录选择默认 /opt/nginx/
- grep "passenger" /opt/nginx/conf/nginx.conf 检查nginx配置文件中passenger的设定是否正确
STEP 4 Install Rails
- gem install rails
- rails new newapp public path: /home/rails_user/newapp/public
STEP 5 配置Nginx,配置文件
/opt/nginx/conf/nginx.conf
server { listen 80; server_name localhost; location / { root /home/gollum/newapp/public; # <--- 这里是你项目的public目录 index index.html index.htm; } passenger_enabled on; # <--- 启动passenger }
STEP 6 安装nginx启动脚本,开机自动开启服务
- 从nginx官方网站上下载 http://wiki.nginx.org/RedHatNginxInitScript
- 修改路径 nginx="/opt/nginx/sbin/nginx" 和 NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
- sudo chmod +x /etc/init.d/nginx 让文件可执行
- sudo chkconfig --add nginx 将nginx启动脚本添加进入启动config
- sudo /etc/init.d/nginx configtest 测试该脚本是否有语法错误
- sudo service nginx status/start 查看nginx状态,开启nginx服务 [sudo /etc/init.d/nginx start]
STEP 7 访问服务器,可以看到rails默认页面
STEP 8 安装数据库,precompile rails assets
- http://ruby-china.org/topics/701
Referance
- http://ruby-china.org/topics/701 (Unbuntu可参照)
Linux Commands
- sudo chkconfig --list 查看所有的系统服务的启动项列表
- chkconfig --add nginx 添加启动script
- sudo /etc/init.d/nginx configtest 测试nginx启动脚本
- sudo service nginx status 查看服务状态
- sudo service nginx { stop | start } Nginx的系统服务
- sudo service httpd graceful {不暂停服务的重启} Apache的系统服务
- source .bashrc 使系统重新读取配置文件。如你更改了.bashrc,如果不重新登录的话,变动是不会生效的。
- sudo netstat -anp|less 查看Internet connection
- pstree ps aux | grep nginx 查看进程
- history 查看历史命令
- sudo shutdown -r now 重启