我已经按照本教程How to deploy rails apps using unicorn and nginx on CentOS 6.5在Centos 7x64中进行了部署。
我第一次访问http://[my-ip]:8080/tasks
时就成功了,但现在在重复了多次之后,我在浏览器中只看到了一个“无法访问”。但我可以在http://[my-ip]
中看到Nginx的起始页。
问题:
我做错什么了?
考虑到我想将多个Rails应用程序存储在同一个液滴中,这些设置可以吗?
在其他Nginx教程中,在Nginx文件夹中,我看到了一个名为“sites enabled”的文件夹,但在我的配置中没有。这有什么重要的?
以下是我的CentOS设置:
常规设置
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
安装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
安装独角兽
gem install unicorn
安装Nginx
sudo yum install -y nginx
ifconfig eth0 | grep inet | awk '{ print $2 }'
安装NodeJS
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
生成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
以下是我的配置文件:
我的应用程序/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文件
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;
}
在my_应用程序中运行
unicorn_rails -c config/unicorn.rb -D
启动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.
原木
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
配置/生产日志
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文件
Empty
开始轨道日志
[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
最佳答案
首先,我建议您使用rbenv或rvm安装Ruby,即使这与您的问题无关。
接下来我注意到的是,当您启动nginx时会出现一个错误:Failed to read PID from file /run/nginx.pid: Invalid argument
。也许你的配置没有加载?
请查看nginx、Rails和Unicorn日志以获取更多信息。我想你会在那里找到一个更有用的问题描述。您的Unicorn日志文件位于与您的配置相关的"/var/www/my_app/log/unicorn.log"
下,Rails日志文件位于"/var/www/my_app/log/production.log"
下。nginx的日志文件在"/var/log/nginx/"
下面,但我不确定。
如果您还没有发现问题,请与我们共享您的日志文件。
关于你的问题:
见上文。
我不知道一滴是什么,但你可以同时运行多个Rails应用程序。只需创建其他nginx主机。
不,这不是必要的(请看这个question + answers)。这是管理多个虚拟主机的最佳实践。您还可以将所有主机塞入default.conf
文件。