【发布时间】:2015-03-29 03:06:12
【问题描述】:
我已按照本教程 How to deploy rails apps using unicorn and nginx on CentOS 6.5 在 Centos 7x64 中进行部署。
我第一次成功访问http://[my-ip]:8080/tasks,但现在在重复多次练习后,我只在浏览器中看到“无法访问”。但我可以在http://[my-ip] 中看到 Nginx 起始页。
问题:
- 我做错了什么?
- 考虑到我想在同一个 Droplet 中存储多个 Rails 应用程序,这些设置是否可行?
- 在其他 Nginx 教程中,在 nginx 文件夹中,我看到了一个名为“启用站点”的文件夹,但在我的配置中它不是。这有多重要?
这些是我的 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
这些是我的配置文件:
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
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_app 中运行
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.
日志
日志/unicorn.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
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
启动 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
【问题讨论】:
标签: ruby-on-rails ruby linux node.js nginx