1.Nginx介绍
Nginx是一个非常轻量级的HTTP服务器,Nginx,它的发音为“engine X”,
是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器。
安装nginx
$ sudo apt-get install nginx
在nginx配置目录
$ cd /usr/local/nginx/conf
在这个目录下新建一个用户可执行的vhost文件夹
$ mkdir vhost
$ chmod 755 vhost
$ cd vhost
$ touch xx.conf
$ sudo vi xx.conf(如果有多个域名,多个项目,只需创建对应的.conf文件即可)
upstream APP_Name(填写你的APP Name) {
# server unix:/tmp/unicorn_project-knowledge-sharing.sock fail_timeout=0;
server unix:/home/用户(正在使用的计算机用户名)/APP_Name(填写你的APP Name)/unicorn.sock fail_timeout=0;
}
server {
listen 80;
charset utf-8;
server_name xx.xx.com;
# Application root, as defined previously
root /home/用户(正在使用的计算机用户名)/APP_Name(填写你的APP Name)/public;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_pass http://APP_Name(填写你的APP Name);
}
}
在/usr/local/nginx/conf下的nginx.conf中添加一行
include vhost/*.conf;
配置unicorn
Gemfile中添加 gem 'unicorn'
在config文件夹下新建unicorn.rb
module Rails
class <<self
def root
File.expand_path("../..", __FILE__)
end
end
end
puts Rails.root
rails_env = ENV["RAILS_ENV"] || "production"
preload_app true
working_directory Rails.root
pid "#{Rails.root}/tmp/pids/unicorn.pid"
stderr_path "#{Rails.root}/log/unicorn.log"
stdout_path "#{Rails.root}/log/unicorn.log"
listen 5006(注意此处端口号不要和别的重复), :tcp_nopush => false
listen "#{Rails.root}/unicorn.sock"
worker_processes 1
timeout 60
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
before_exec do |server|
ENV["BUNDLE_GEMFILE"] = "#{Rails.root}/Gemfile"
end
before_fork do |server, worker|
old_pid = "#{Rails.root}/tmp/pids/unicorn.pid.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
puts "Send 'QUIT' signal to unicorn error!"
end
end
end
在根目录下创建start.sh和restart.sh(可执行文件)
start.sh
bundle exec unicorn_rails -c ./config/unicorn.rb -D
restart.sh
kill -s USR2 `cat ./tmp/pids/unicorn.pid`
chmod 777 start.sh
chmod 777 restart.sh
启动:
$ cd railsapp
$ ./start