ホーム > カテゴリ > Ruby・Ruby on Rails >

NginxでRuby on RailsとPHPを共存させる [CentOS]

CentOSで各種サーバー構築(目次)

共存させるにはNginxの「*.conf」ファイルとRailsプロジェクトの「config/routes.rb」「config.ru」を設定するだけです。

やること

http://IPアドレス/全域でPHPを使用可能
実際のパス:/home/ユーザー名/html
http://IPアドレス/bbb/Ruby on Railsプロジェクト
実際のパス:/home/ユーザー名/myapp

※Railsはサブディレクトリを切る感じで動作させます。

前提条件

次の記事を先に済ませて下さい。

1Nginx/Unicorn/Ruby on Railsの本番環境構築/デプロイ
2NginxでPHP7(PHP-FPM)を動作させる

1. myapp/config/routes.rbの編集

Rails.application.routes.draw do
  get 'tasks/index'
  get 'tasks/show'
  get 'tasks/new'
  get 'tasks/edit'

  # root(/)をtasks#indexにする
  root to: 'tasks#index'
end

2. myapp/config.ruの編集

RAILS_RELATIVE_URL_ROOT='/bbb'

require_relative 'config/environment'

if RAILS_RELATIVE_URL_ROOT then
  map RAILS_RELATIVE_URL_ROOT do
    run Rails.application
  end
else
  run Rails.application
end

# 元のコード
# require_relative 'config/environment'
# run Rails.application

3. myapp/public/index.htmlの削除

初回(1)の記事で作成したので削除しておく。

4. /etc/nginx/conf.d/myapp.confの編集

sudo vi /etc/nginx/conf.d/myapp.conf

[myapp.conf]

upstream unicorn {
    server  unix:/home/ユーザー名/myapp/tmp/unicorn.sock;
}

server {
    listen       80;
    server_name  IPアドレス; 
  
    client_max_body_size 100m;
    error_page   404              /404.html;
    error_page   500 502 503 504  /50x.html;          
    
    root /home/ユーザー名/html;
    
    # /home/ユーザー名/myapp/publicの各ファイルにアクセス可能にする
    location ~ ^/bbb/assets/(.*) {
        alias /home/ユーザー名/myapp/public/assets/$1;
    }    
    
    # Railsアプリケーション 
    location /bbb/ {
        alias /home/ユーザー名/myapp/public;  
        try_files $uri @unicorn;    
    }
    
    # ユニコーン
    location @unicorn {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_pass http://unicorn;
    }
    
    # URLの末尾が/の場合
    location / {   
        index index.php index.html index.htm;
    }
    
    # 拡張子がphpの場合
    location ~ \.php$ {
        # 存在しない場合は404
        try_files $uri /404.html;  
            
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }    
}

5. Nginx/ユニコーンの再起動

ユニコーンはUnicornの自動起動を参照。

// ユニコーンの再起動 その1(CentOS7以降)
sudo systemctl restart unicorn_sample_app

// ユニコーンの再起動 その2(CentOS6以下)
cd /home/ユーザー名/myapp
bundle exec rake unicorn:stop
bundle exec rake unicorn:start

// Nginxのリスタート
sudo systemctl restart nginx

6. 確認用ファイルの作成

/home/ユーザー名/html/index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello!</title>
</head>
<body> 
  あいうえお
</body>
</html>

/home/ユーザー名/html/test.php

<?php

phpinfo();

?>

/home/ユーザー名/html/aaa/test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test!</title>
</head>
<body> 
  テスト
</body>
</html>

/home/ユーザー名/html/aaa/index.php

<?php
echo "/aaa版";

phpinfo();

?>

7. ブラウザで動作確認

http://IPアドレス/
http://IPアドレス/test.php
http://IPアドレス/aaa/
http://IPアドレス/aaa/test.html
http://IPアドレス/bbb/
http://IPアドレス/bbb/tasks/index
http://IPアドレス/bbb/tasks/show
http://IPアドレス/bbb/404.html  
http://IPアドレス/bbb/500.html  

404/500.htmlは/home/ユーザー名/myapp/publicにあるエラーページを意図的に表示させています。既存のHTMLを表示させているだけで「エラーではない」のでご注意ください。

参考URL

nginx連載5回目: nginxの設定、その3 - locationディレクティブ





関連記事



公開日:2019年09月16日 最終更新日:2019年11月30日
記事NO:02768


この記事を書いた人

💻 ITスキル・経験
サーバー構築からWebアプリケーション開発。IoTをはじめとする電子工作、ロボット、人工知能やスマホ/OSアプリまで分野問わず経験。

画像処理/音声処理/アニメーション、3Dゲーム、会計ソフト、PDF作成/編集、逆アセンブラ、EXE/DLLファイルの書き換えなどのアプリを公開。詳しくは自己紹介へ
プチモンテ代表、アーティスト名:プチモンテ
🎵 音楽制作
BGMは楽器(音源)さえあれば、何でも制作可能。歌モノは主にロック、バラード、ポップスを制作。歌詞は叙情詩、叙情的な楽曲が多い。楽曲制作は2023年12月中旬 ~

オリジナル曲を始めました✨

YouTubeで各楽曲を公開しています🌈
https://www.youtube.com/@petitmonte

【男性ボーカル】DA・KA・RA | 新たな明日が風と共に訪れる

【男性、女性ボーカル】時空を超越する先に | 時空と風の交響曲

【女性、男性ボーカル】絆 | 穏やかな心に奏でる旋律