Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxivak/1c4034f118aa26b7daa9 to your computer and use it in GitHub Desktop.
Save maxivak/1c4034f118aa26b7daa9 to your computer and use it in GitHub Desktop.
New Rails app in Nginx+Passenger

New Rails app in Nginx with Passenger

This gist shows how to setup and configure Rails application in Nginx on Centos.

Our application will be located in '/var/www/apps/mysite'.

Passenger

After installing Passenger Nginx will be located in '/opt/nginx' directory.

Read this to install Passenger and Nginx on Centos.

Server in Nginx

Create configuration file '/opt/nginx/sites-available/mysite.conf' for server (virtual host) in Nginx:

server {
  passenger_enabled on;
  passenger_ruby /home/uadmin/.rvm/wrappers/ruby-2.1.5/ruby;  

  server_name mysite.com ;
  root /var/www/apps/mysite/current/public;
  rails_env production;

  access_log /var/www/logs/mysite/access.log;
  error_log /var/www/logs/mysite/error.log;

  if (-f $document_root/system/maintenance.html) {
    rewrite ^(.*)$ /system/maintenance.html break;
  }

}

It assumes that Rails app will be running with Ruby 2.1.5. If you have different version of Ruby change the line passenger_ruby /home/uadmin/.rvm/wrappers/ruby-2.1.5/ruby; to point to your Ruby binary. It is useful if you have multiple ruby versions. If you specify the same Ruby version in Nginx main config file then you don't need to have this line passenger_ruby ... Find Nginx main config file in this gist below.

Create necessary directories:

mkdir /var/www/logs

cd /var/www/logs
mkdir mysite

Make the server enabled in Nginx:

sudo ln -s /opt/nginx/sites-available/mysite.conf /opt/nginx/sites-enabled/mysite.conf

Restart Nginx:

sudo service nginx restart
user nginx dev;
worker_processes 1;
error_log /var/www/logs/nginx/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 /home/myrvmuser/.rvm/gems/ruby-2.1.5/gems/passenger-5.0.13;
passenger_ruby /home/myrvmuser/.rvm/gems/ruby-2.1.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 /var/www/logs/nginx/access.log main;
sendfile on;
client_max_body_size 8m;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml image/png image/gif image/jpg;
server {
listen 80;
server_name myhost.myserver.com;
root /var/www/html;
index index.php index.html index.htm;
access_log /var/www/logs/nginx/main.access.log main;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# redis admin
#location /phpredisadmin {
# auth_basic "Login";
# auth_basic_user_file /var/www/html/phpredisadmin/.htpasswd;
#}
location ~ /\. { deny all; }
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
include /opt/nginx/conf.d/*.conf;
include /opt/nginx/sites-enabled/*;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment