王筝的博客
ruby学习

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-debian-7

https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-nginx-on-ubuntu-14-04

1) Update Apt-Get

The apt-get update command is used to re-synchronize the package index files from their sources. If used in combination with the apt-get upgrade command, they install the newest versions of all packages currently available.

At the moment, we only need to do a thorough update:

sudo apt-get update

2) Install MySQL on your VPS

MySQL is a powerful database management system used for organizing and retrieving data

To install MySQL, open terminal and type in these commands:

sudo apt-get install mysql-server

During the installation, MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell.

Password

Once you have installed MySQL, we should activate it with this command:

3) Install and Configure Nginx on your VPS

Installation

Initial installation is simple with the apt-get command.

sudo apt-get install nginx

nginx needs a command to begin running:

sudo service nginx start

Configuration

vim /etc/nginx/nginx.conf, 在http中加入以下代码,

      server {
        listen   80;


        root /opt/app/blog/wz/wordpress/;
        index index.php index.html index.htm;

        server_name iwangzheng.com;

        location / {
                try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
                location = /50x.html {
                root /usr/share/nginx/www;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }

     }

nginx.conf的第一行是user www-data;

把这个项目目录的权限修改下

sudo chown -R www-data:www-data /opt/app/blog/wz/wordpress/

4) Install and Configure PHP

Installation

You probably guessed it! We will use the apt-get command to install PHP-FPM:

sudo apt-get install php5-fpm php5-mysql
sudo service nginx restart
sudo service php5-fpm restart

 

老项目换unicorn的大致步骤:

(1)修改config/application.rb
删除
29     # assign log4r’s logger as rails’ logger.
30     log4r_config= YAML.load_file(File.join(File.dirname(__FILE__),”log4r.yml”))
31     YamlConfigurator.decode_yaml( log4r_config[‘log4r_config’] )
32     config.logger = Log4r::Logger[Rails.env]
新增memcached的配置
29     config.cache_store = :dalli_store, *Settings.memcached.servers,
30       { :namespace => Settings.memcached.namespace, :compress => true }

(2)新建config/deploy/文件夹,
新增production.rb
staging.rb

(3) 新增config/environments/
development.rb
production.rb
staging.rb
test.rb

(4) 修改Gemfile,删除thin,新增unicorn
37 gem ‘dalli’
38 gem ‘unicorn’
39
40 group :test, :development do
41   gem “rspec-rails”, “>= 2.5.0″
42   gem ‘factory_girl’, ‘2.6.4’
43   gem ‘factory_girl_rails’, ‘1.7.0’
44   gem ‘faker’, ‘1.1.2’
45   gem ‘forgery’, ‘0.5.0’
46   gem ‘magic_encoding’, ‘0.0.2’
47   gem ‘email_spec’, ‘1.4.0’
48
49   gem ‘net-ssh’, ‘~> 2.0′
50   gem ‘capistrano’, ‘~> 2.15.7′
51   gem ‘capistrano-rbenv’, ‘1.0.5’
52   gem ‘rb-readline’,’0.4.2′
53 end

(5)修改配置config/settings.yml的配置
181 memcached:
182   namespace: xxx-xxxxx-cms-staging
183   servers:
184     – 127.0.0.1:11215

(6)配置config/deploy.rb文件

(7)bundle exec cap deploy:setup
bundle exec cap deploy:cold

部署可能出现的问题
(1)Could not parse object(由于写错了config/deploy.rb 里的set :repository导致的)

(2)不能自动识别config/application.rb的配置uninitialized constant Cms::Application::Settings
修改Gemfile, gem “rails_config”, ‘0.3.4’

(3)/home/wz/.rbenv/versions/1.9.3-p545/lib/ruby/gems/1.9.1/gems /activesupport-3.0.8/lib/active_support/cache.rb:65:in `rescue in lookup_store': Could not find cache store adapter for dalli_store (cannot load such file — active_support/cache/dalli_store) (RuntimeError)
需要加

gem 'dalli'

最后一步,把项目文件夹目录下的nginx配置做一个软链接到
$ nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful

$ ln -s /opt/app/ruby/tudou-tv-cms-unicorn/current/config/nginx.staging.conf /opt/nginx/conf/tudou_tv_cms.conf
12:33 [root@a02]$ nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
tty:[0] jobs:[0] cwd:[~]
12:33 [root@a02]$ nginx -s reload
tty:[0] jobs:[0] cwd:[~]
12:33 [root@a02]$ netstat -tlnp | grep 52
tcp        0      0 0.0.0.0:5200                0.0.0.0:*                   LISTEN      20263/nginx         
tcp        0      0 10.100.14.83:5308           0.0.0.0:*                   LISTEN      1526/cfservd        
tcp        0      0 0.0.0.0:5218                0.0.0.0:*                   LISTEN      1391/thin server (0 
12:33 [root@a02.test.stat.3g.vm.m6.youku]$ vim /opt/nginx/conf/nginx.conf
这里把新的链接include进来
 include tudou_tv_cms.conf;
tty:[0] jobs:[0] cwd:[~]
12:34 [root@a02]$ nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
tty:[0] jobs:[0] cwd:[~]
12:34 [root@a02]$ nginx -s reload
tty:[0] jobs:[0] cwd:[~]
12:34 [root@a02]$ netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:6500                0.0.0.0:*                   LISTEN      2347/thin server (0 
tcp        0      0 0.0.0.0:6565                0.0.0.0:*                   LISTEN      20307/nginx         
tcp        0      0 0.0.0.0:5030                0.0.0.0:*                   LISTEN      20307/nginx         
tcp        0      0 0.0.0.0:22022               0.0.0.0:*                   LISTEN      26901/sshd          
tcp        0      0 127.0.0.1:199               0.0.0.0:*                   LISTEN      26927/snmpd         
tcp        0      0 127.0.0.1:6600              0.0.0.0:*                   LISTEN      11834/uwsgi

 

Capistrano 2 首次部署流程

  1. 修改 config/deploy.rb 和 config/deploy/production.rb
  2. bundle exec cap production deploy:setup
  3. bundle exec cap production deploy:check
  4. bundle exec cap production deploy:cold

配置 Nginx

  1. ln -s /opt/app/ruby/aaa-cms/current/config/nginx.conf /etc/nginx/conf.d/ott_tv_cms.conf
  2. 添加 “include conf.d/aaa_cms.conf;” 到 /etc/nginx/nginx.conf
  3. 运行 nginx -t 来检查 nginx 配置没有问题
  4. 运行 nginx -s reload 来重启 nginx

初始话数据库数据

RAILS_ENV=production bundle exec bin/rake db:seed

Capistrano 2 首次部署完成后再部署

bundle exec cap production deploy

 

 

# 创建项目相关目录
/opt/logs/tv-cibn-cms

#ruby 用户下安装 rbenv 及 ruby 1.9.3
# 安装编译相关包
yum install -y gcc openssl-devel libyaml-devel readline-devel zlib-devel

su – ruby  #/home/ruby

# 安装 rbenv
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo ‘export PATH=”$HOME/.rbenv/bin:$PATH”‘ >> ~/.bash_profile
echo ‘eval “$(rbenv init -)”‘ >> ~/.bash_profile
. .bash_profile

# 安装 rbenv-install 及 ruby 1.9.3-p545
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
mkdir ~/.rbenv/cache
cd ~/.rbenv/cache && wget http://ruby.taobao.org/mirrors/ruby/1.9/ruby-1.9.3-p545.tar.gz
cd ~ && rbenv install 1.9.3-p545

# 更换 rubygems 源
gem sources –remove http://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l

# 安装 bundler
gem install bundler

#安装 mysql2 依赖库
# yum install mysql-devel
yum install mysql-devel-ms