0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Ubuntu 24.04.1 LTS】Rails7でPassenger+ApacheのWebサーバを立てる(http編)

Posted at

はじめまして。会社の社内SEをしています。初めてQiitaの投稿になります。
仕事でRailsのWebサーバを立てましたが、同じ構成で立てている情報があまりなかったので投稿します。初めての投稿で至らない内容も多々あると思いますが、ご指摘頂ければ修正いたします。

基本情報

IP: 192.168.10.10
Ubuntu 24.04.1 LTS
Ruby on Rails 7.1.5.1
Ruby 3.3.7
rbenv 1.3.2
Gem 3.6.3
MariaDB Ver 15.1
apache2 Apache/2.4.58 (Ubuntu)
Passenger 6.0.27

前提条件

Railsのdevelopment環境では実行できている環境とします。
LAN内でのみ使用するサーバのため、http://IPアドレス で接続しています。

production環境準備

本番環境のプロジェクトを/var/www以下に配置

$ sudo cp -r ~/myrailspath/myproject/ /var/www/myproject/
$ sudo chown -R myusername:myusername /var/www/myproject/

DB環境準備

production環境のDBがない場合は作成しておく。(割愛)
database.ymlにパスワードを記載、migrate。

$ cd /var/www/myproject/
$ sudo vi config/database.yml
    # production:のパスワードを正しく記載

$ rails db:migrate RAILS_ENV=production
	# 各テーブル、VIEWが作られる

コンパイル

$ cd /var/www/myproject/
$ bundle exec rake assets:precompile RAILS_ENV=production

Apacheのセットアップ

Apacheのインストール

$ sudo apt update
$ sudo apt install apache2
$ apachectl -v
Server version: Apache/2.4.58 (Ubuntu)
Server built:   2025-04-03T14:36:49

$ sudo systemctl start apache2

SSLモジュールを有効化

$ sudo a2enmod ssl
$ sudo systemctl restart apache2

Passengerの導入

参考URL:https://blog.redmine.jp/articles/6_0/install/ubuntu24/
Redmineのインストール方法を元にしました

参考URLを元に必要なパッケージのインストール

$ apt list --installed | grep xxx

より各パッケージを調べてないものを入れる。私の場合はapache2-devのみでした。

$ sudo apt update
$ sudo apt install -y apache2-dev

Passengerのインストール

$ cd /var/www/myproject/
$ vi Gemfile
Gemfile
gem "passenger", "~> 6.0", ">= 6.0.27"
$ bundle install

PassengerのApache用モジュールのインストール

インストールコマンドのpathを調べる

$ which passenger-install-apache2-module
/home/myusername/.rbenv/shims/passenger-install-apache2-module

モジュールのインストール

$ sudo /home/myusername/.rbenv/shims/passenger-install-apache2-module --auto --languages ruby
# ・・・長々と実行のログがでて・・・
Detected 0 error(s), 1 warning(s).

Apache用設定内容の確認

$ passenger-install-apache2-module --snippet
LoadModule passenger_module /home/myusername/.rbenv/versions/3.3.7/lib/ruby/gems/3.3.0/gems/passenger-6.0.27/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
	PassengerRoot /home/myusername/.rbenv/versions/3.3.7/lib/ruby/gems/3.3.0/gems/passenger-6.0.27
	PassengerDefaultRuby /home/myusername/.rbenv/versions/3.3.7/bin/ruby
</IfModule>

この結果をconfに記載するため覚えておく

環境変数RAILS_MASTER_KEYの設定

keyの値を確認

$ cd /var/www/myproject/
$ less config/master.key
**************(keyの内容)
$ export RAILS_MASTER_KEY="**************(keyの内容)"

永続化する

$ echo 'RAILS_MASTER_KEY="**************(keyの内容)"' | sudo tee -a /etc/environment

Railsアプリの設定

$ cd /etc/apache2/sites-available/
$ sudo cp 000-default.conf my-default.conf
$ sudo vi my-default.conf

1-6行目の内容は、先ほどの"passenger-install-apache2-module --snippet"の実行結果をそのまま記載する。
あとhttpの場合、HSTS の設定を解除しておく。

my-default.conf
# Passenger
LoadModule passenger_module /home/myusername/.rbenv/versions/3.3.7/lib/ruby/gems/3.3.0/gems/passenger-6.0.27/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
	PassengerRoot /home/myusername/.rbenv/versions/3.3.7/lib/ruby/gems/3.3.0/gems/passenger-6.0.27
	PassengerDefaultRuby /home/myusername/.rbenv/versions/3.3.7/bin/ruby
</IfModule>

PassengerUserSwitching off
PassengerDefaultUser myusername

<VirtualHost *:80>
	DocumentRoot /var/www/myproject/public/

	<Directory /var/www/myproject/public/>
		AllowOverride all
		Require all granted
		Options -MultiViews
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	PassengerEnabled on
	PassengerAppRoot /var/www/myproject

	# HSTS の設定を解除(HTTP の場合のみ有効)
	Header always unset Strict-Transport-Security
</VirtualHost>	

000-default.confを無効化し、my-defaultを適用させる(sites-enabledにリンクを作成する)

$ sudo a2dissite 000-default.conf
$ sudo a2ensite my-default.conf

有効なconfの確認

$ ls -la /etc/apache2/sites-enabled
lrwxrwxrwx 1 root root   30 Jun 10 05:02 my-default.conf -> ../sites-available/my-default.conf

confのHeaderを有効化する。

$ sudo a2enmod headers

confテスト

$ apache2ctl configtest
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 192.168.10.10. Set the 'ServerName' directive globally to suppress this message
Syntax OK

ServerNameを設定しないアラートが出るが問題ないため無視。

passenger-statusが実行できるようにする

$ cd /home/myusername/.rbenv/shims/
$ sudo ./passenger-status
/home/myusername/.rbenv/versions/3.3.7/lib/ruby/gems/3.3.0/gems/passenger-6.0.27/src/ruby_supportlib/phusion_passenger/admin_tools/instance.rb:94:in `initialize': too long unix socket path (116bytes given but 108bytes max) (ArgumentError)
	sock = Net::BufferedIO.new(UNIXSocket.new("#{@path}/#{socket_path}"))

参考URL:https://qiita.com/kensukeyoshida/items/94727adb8581e57930bd
こちらの方と同様にPassengerInstanceRegistryDir /tmpもPASSENGER_TMPDIR=/tmp passenger-statusもうまくいかず、同じように下記

$ sudo systemctl edit apache2
[Service]
PrivateTmp=false

名前を以下の名前で保存。(デフォルトの名前だと保存されなかった)
/etc/systemd/system/apache2.service.d/override.conf

$ sudo systemctl daemon-reload

httpで接続する場合はRailsの本番環境用の設定ファイルproduction.rbでsslをfalseにしておく

$ vi /var/www/myproject/config/environments/production.rb
production.rb
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = false

ファイアウォールの設定

$ sudo ufw allow 80/tcp
$ sudo ufw reload
$ sudo ufw status
To                         Action      From
--                         ------      ----
80/tcp                     ALLOW       Anywhere
80/tcp (v6)                ALLOW       Anywhere (v6)

動作確認

$ sudo systemctl restart apache2
$ sudo systemctl status apache2

ブラウザからアクセス。
http://192.168.10.10
開くはず!

$ sudo ~/.rbenv/shims/passenger-status
Version : 6.0.27
Date    : 2025-06-20 07:15:14 +0000
Instance: GvkMVcnt (Apache/2.4.58 (Ubuntu) OpenSSL/3.0.13 Phusion_Passenger/6.0.27)

----------- General information -----------
Max pool size : 6
App groups    : 1
Processes     : 1
Requests in top-level queue : 0
----------- Application groups -----------
/var/www/myproject (production):
App root: /var/www/myproject
Requests in queue: 0
* PID: 1467    Sessions: 0       Processed: 1       Uptime: 16s
	CPU: 1%      Memory  : 31M     Last used: 16s ago

Application groupsを認識している。

参考文献

本文中にも記載しましたが、下記URLを参考にさせていただきました。ありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?