ubuntu+rails+passenger+apache+nginx常见问题
很多问题的标题就是报错之后日志中记录的错误信息。apache的日志在/var/log/apache2/目录中,error.log。当然,也有可能在其他位置。如果是下载httpd,编译安装的话,有可能在/usr/local/apache目录中。
nginx的日志在/opt/nginx/log/中,也是error.log。也是有可能在其他位置的,例如安装目录或者是/var/log/nginx/中。
1.rails 3.1.0 ActionView::Template::Errror (application.css isn't precompiled)
解答:
这个错误是在生产环境中发生的,生产环境要求assets预先编译,或者是设置运行时编译。
设置生产环境的配置信息。
[*]# config/environments/production.rb
[*]...
[*]config.assets.compile = true
[*]...
或者是执行
[*]bundle exec rake assets:precompile
然后重启rails server,或者是重启apache和nginx。
参考链接
http://stackoverflow.com/questions/7275636/rails-3-1-0-actionviewtemplateerrror-application-css-isnt-precompiled
http://ruby-china.org/topics/1415
2.安装passenger
gem install passenger
passenger-install-apache2-module
passenger-install-nginx-module
安装的过程中会有提示,提示你没有安装的组件,等你都安装好之后,passenger在apache和nginx的模块就可以顺利安装了。安装之后,还会提示你如何设置apache和nginx。
3.Railsapplication unable to connect to mysql, Can't connect to local MySQLserver through socket '/tmp/mysql.sock' (2) (Mysql2::Error)
/tmp/mysql.sock是你在config/database.yml文件中的
[*]production:
[*]adapter: mysql2
[*]encoding: utf8
[*]host: localhost
[*]database: blog
[*]pool: 5
[*]username: root
[*]password: 123.com
[*]socket: /tmp/mysql.sock
[*]timeout: 5000
就是说这个/tmp/mysql.sock文件不存在,其实就是位置不对。
在安装了mysql之后,通过下面的方法找到这个文件。
mysqladmin -u root -p variables | grep socket
在输入密码之后,会出现面的内容
[*]root@web:/home/github/blog# mysqladmin -u root -p variables |grep sock
[*]Enter password:
[*]| socket | /var/run/mysqld/mysqld.sock |
[*]root@web:/home/github/blog#
上面的红色内容填写到database.yml文件中就可以了,或者建立一个符号链接。
[*]ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
也可以解决这个问题。
参考链接
http://stackoverflow.com/questions/5499035/ruby-on-rails-3-cant-connect-to-local-mysql-server-through-socket-tmp-mysql-s
http://stackoverflow.com/questions/11350929/rails-application-unable-to-connect-to-mysql-cant-connect-to-local-mysql-serve
4.部署到生产环境的apache中,如何在apache的配置文件中设置RAILS_ENV
在rails server命令中可以添加参数,指定服务启动使用什么配置,生产环境还是开发环境。
rails server --environment=production
在进行数据migration的时候,也可以指定使用database.yml中的哪种环境,生产环境还是开发环境。
rake db:create RAILS_ENV=production
[*]
[*]ServerName www.yourdomain.com
[*]DocumentRoot /var/www/yourfolder/public
[*]
[*] AllowOverride all
[*] Options -MultiViews
[*] RailsEnv development
[*]
[*]
[*]ErrorLog /var/log/apache2/yourdomain.error.log
[*]LogLevel warn
[*]CustomLog /var/log/apache2/yourdomain.access.log combined
[*]
请注意上面红色的文字。
参考链接
http://www.hoenick.com/blog/2012/01/02/ruby-on-rails-rails_env-im-apache-definiere/
5.部署到生产环境的nginx,如何在nginx的配置文件中设置RAILS_ENV
server {
listen 80;
server_name www.yourhost.com;
root /somewhere/public; #
[*]config.cache_class = true;
就是缓存了代码,在多次请求也不会重新加载代码,所有没有看见更新之后的效果。当然了,发布到生产环境的代码都是经过严格测试,稳定的代码,不会是修改来修改去的东西,修改也不会要求马上见效,需要严格测试才行。
如果有这个需要的话,可以将设置改为false。如果不想该,也可以在tmp下建立一个restart.txt文件。
[*]touch tmp/restart.txt
需要重新加载的时候,就修改一下文件的访问时间。
[*]echo "hello" >> restart
也可以实现重新加载,而且还不破坏生产环境的配置文件。
参考链接
Rails Cache Clearing
10.rails ActionView::Template::Error (undefined method `post_comments_path'
功能是在一个博客的浏览页面添加评论。
在app/views/posts/show.html.erb中添加了add comment部分的form,但是没有在config/route.rb中添加
[*]resources posts do
[*]
[*]resources comments
[*]
[*]end
11.NoMethodError: undefined method `has_attached_file'
You will also need to add the following to your Gemfile
gem "paperclip", :git => "http://github.com/thoughtbot/paperclip.git"
参考文献
1.Rails之道---> 摘录(1)Rails环境与配置
页:
[1]