简体   繁体   English

Centos 7x64 - 使用 Nginx 和 Unicorn 部署 Rails

[英]Centos 7x64 - Deploy Rails using Nginx and Unicorn

I've followed this tutorial How to deploy rails apps using unicorn and nginx on CentOS 6.5 to deploy in Centos 7x64.我已经按照本教程如何在 CentOS 6.5 上使用 unicorn 和 nginx 部署 rails 应用程序以在 Centos 7x64 中部署。

The first time I made it work accessing to http://[my-ip]:8080/tasks , but now after repeating the exercise many times I only see a 'can not access' in the browser.我第一次成功访问http://[my-ip]:8080/tasks ,但现在在多次重复练习后,我只在浏览器中看到“无法访问”。 But I can see Nginx start page in http://[my-ip] .但是我可以在http://[my-ip]看到 Nginx 起始页。

浏览器在这里

Questions:问题:

  1. What am I doing wrong?我究竟做错了什么?
  2. Are these settings okay considering I want to store multiple Rails apps in same Droplet?考虑到我想在同一个 Droplet 中存储多个 Rails 应用程序,这些设置是否可以?
  3. In other Nginx tutorials, inside nginx folder I've seen a folder called 'sites-enabled', but in my config it isn't.在其他 Nginx 教程中,在 nginx 文件夹中,我看到了一个名为“启用站点”的文件夹,但在我的配置中却没有。 How is that important?这有多重要?

These are my CentOS settings:这些是我的 CentOS 设置:

General settings通用设置

echo "export LC_ALL=en_US.UTF-8" >> /etc/profile
export LC_ALL=en_US.UTF-8
yum -y update
yum groupinstall -y 'development tools'
rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
yum repolist
yum --disablerepo="*" --enablerepo="epel" list available
yum --enablerepo=epel info htop
yum --enablerepo=epel install -y htop  
yum install -y curl-devel nano sqlite-devel libyaml-devel   

Install Ruby and RoR安装 Ruby 和 RoR

sudo yum install -y ruby
sudo yum install -y gcc g++ make automake autoconf curl-devel openssl-devel zlib-devel httpd-devel apr-devel apr-util-devel sqlite-devel
sudo yum install -y ruby-rdoc ruby-devel
sudo yum install -y rubygems
sudo gem update
sudo gem update --system
sudo gem install rails --no-rdoc --no-ri  

Install Unicorn安装独角兽

gem install unicorn

Install Nginx安装 Nginx

sudo yum install -y nginx
ifconfig eth0 | grep inet | awk '{ print $2 }'

Install NodeJS安装 Node.js

sudo yum install -y gcc gcc-c++ 
wget http://nodejs.org/dist/v0.10.36/node-v0.10.36.tar.gz  
tar xzvf node-v* && cd node-v*   
./configure  
make  
sudo make install  
node --version

Generate Rails app生成 Rails 应用

# Create a sample Rails application
cd  /var
mkdir www
cd www
rails new my_app

# Enter the application directory
cd my_app

# Create a sample resource
rails generate scaffold Task title:string note:text

# Create a sample database
RAILS_ENV=development rake db:migrate
RAILS_ENV=production  rake db:migrate

# Create a directory to hold the PID files
mkdir pids   

These are my configuration files:这些是我的配置文件:

my_app/config/unicorn.rb my_app/config/unicorn.rb

# Set the working application directory
# working_directory "/path/to/your/app"
working_directory "/var/www/my_app"

# Unicorn PID file location
# pid "/path/to/pids/unicorn.pid"
pid "/var/www/my_app/pids/unicorn.pid"

# Path to logs
# stderr_path "/path/to/log/unicorn.log"
# stdout_path "/path/to/log/unicorn.log"
stderr_path "/var/www/my_app/log/unicorn.log"
stdout_path "/var/www/my_app/log/unicorn.log"

# Unicorn socket
listen "/tmp/unicorn.[app name].sock"
listen "/tmp/unicorn.myapp.sock"

# Number of processes
# worker_processes 4
worker_processes 2

# Time-out
timeout 30  

/etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf

upstream app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/tmp/unicorn.myapp.sock fail_timeout=0;
}

server {


    listen 80;
    server_name localhost;

    # Application root, as defined previously
    root /root/my_app/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}  

Run inside the my_app在 my_app 内运行

unicorn_rails -c config/unicorn.rb -D

Start nginx启动nginx

systemctl start nginx.service
systemctl status nginx.service 



nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled)
   Active: active (running) since Thu 2015-01-29 13:37:08 EST; 1min 57s ago
  Process: 642 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 604 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
 Main PID: 650 (nginx)
   CGroup: /system.slice/nginx.service
           ├─650 nginx: master process /usr/sbin/nginx
           └─654 nginx: worker process

demo nginx[604]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
demo nginx[604]: nginx: configuration file /etc/nginx/nginx.conf test is successful
demo systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
demo systemd[1]: Started The nginx HTTP and reverse proxy server.
demo systemd[1]: Started The nginx HTTP and reverse proxy server.

Logs日志

log/unicorn.log日志/独角兽.log

I, [2015-01-29T14:45:58.886225 #11502]  INFO -- : listening on addr=/tmp/unicorn.[app name].sock fd=10
I, [2015-01-29T14:45:58.886616 #11502]  INFO -- : listening on addr=/tmp/unicorn.myapp.sock fd=11
I, [2015-01-29T14:45:58.886891 #11502]  INFO -- : worker=0 spawning...
I, [2015-01-29T14:45:58.887657 #11502]  INFO -- : worker=1 spawning...
I, [2015-01-29T14:45:58.888673 #11505]  INFO -- : worker=0 spawned pid=11505
I, [2015-01-29T14:45:58.889029 #11505]  INFO -- : Refreshing Gem list
I, [2015-01-29T14:45:58.891061 #11502]  INFO -- : master process ready
I, [2015-01-29T14:45:58.918331 #11508]  INFO -- : worker=1 spawned pid=11508
I, [2015-01-29T14:45:58.918732 #11508]  INFO -- : Refreshing Gem list
I, [2015-01-29T14:46:03.608903 #11508]  INFO -- : worker=1 ready
I, [2015-01-29T14:46:03.609694 #11505]  INFO -- : worker=0 ready

config/production.log配置/生产.log

D, [2015-01-28T22:22:34.225698 #1864] DEBUG -- :    (3.6ms)  CREATE TABLE "schema_migrations" ("version" varchar NOT NULL)
D, [2015-01-28T22:22:34.226055 #1864] DEBUG -- :    (0.1ms)  select sqlite_version(*)
D, [2015-01-28T22:22:34.229402 #1864] DEBUG -- :    (2.8ms)  CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
D, [2015-01-28T22:22:34.230780 #1864] DEBUG -- :   ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
I, [2015-01-28T22:22:34.234599 #1864]  INFO -- : Migrating to CreateTasks (20150129032219)
D, [2015-01-28T22:22:34.235363 #1864] DEBUG -- :    (0.1ms)  begin transaction
D, [2015-01-28T22:22:34.237769 #1864] DEBUG -- :    (0.5ms)  CREATE TABLE "tasks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar, "note" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
D, [2015-01-28T22:22:34.245241 #1864] DEBUG -- :   SQL (0.2ms)  INSERT INTO "schema_migrations" ("version") VALUES (?)  [["version", "20150129032219"]]
D, [2015-01-28T22:22:34.248567 #1864] DEBUG -- :    (3.0ms)  commit transaction

/var/log/nginx/error.log /var/log/nginx/error.log

Empty

Start Rails log启动 Rails 日志

[root@demo my_app]# rails s
Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring.
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-01-29 14:59:12] INFO  WEBrick 1.3.1
[2015-01-29 14:59:12] INFO  ruby 2.0.0 (2013-11-22) [x86_64-linux]
[2015-01-29 14:59:12] INFO  WEBrick::HTTPServer#start: pid=11560 port=3000
  • First of all, I suggest you to install Ruby with rbenv or rvm even when this has nothing to do with your problem.首先,我建议您使用rbenvrvm安装 Ruby,即使这与您的问题无关。
  • The next thing I notice is, that you get an error when you start nginx: Failed to read PID from file /run/nginx.pid: Invalid argument .我注意到的下一件事是,当您启动 nginx 时出现错误: Failed to read PID from file /run/nginx.pid: Invalid argument Maybe your config doesn't gets loaded?也许你的配置没有被加载?
  • Please also check the nginx, Rails and Unicorn logs for further information.另请查看 nginx、Rails 和 Unicorn 日志以获取更多信息。 I think you will find a more useful problem description there.我想你会在那里找到更有用的问题描述。 Your Unicorn log file is under "/var/www/my_app/log/unicorn.log" regarding to your config and the Rails log file is "/var/www/my_app/log/production.log" .你的 Unicorn 日志文件在"/var/www/my_app/log/unicorn.log"关于你的配置,Rails 日志文件是"/var/www/my_app/log/production.log" The log file of nginx is somewhere under "/var/log/nginx/" , but I am not sure about that. nginx 的日志文件在"/var/log/nginx/"下的某个地方,但我不确定。

Please share your log files with us when you haven't find the problem then.当您还没有发现问题时,请与我们分享您的日志文件。

Regarding to your questions:关于你的问题:

  1. See above.看上面。
  2. I don't know what a Droplet is, but you can run multiple Rails apps at once.我不知道 Droplet 是什么,但你可以同时运行多个 Rails 应用程序。 Just create additional nginx hosts.只需创建额外的 nginx 主机。
  3. No it's not essential (see this question + answers ).不,这不是必需的(请参阅此问题 + 答案)。 It is more a best practice for managing several virtual hosts.它更像是管理多个虚拟主机的最佳实践。 You can also cram all your hosts into the default.conf file.您还可以将所有主机塞入default.conf文件中。

In dir rails:在目录导轨中:

 rm -rf public/assets
 rake assets:clean RAILS_ENV=development

At the end, please apply the below permissions最后,请应用以下权限

chown -R nginx:nginx /www/[APLICACCION]/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM