Ubuntu-24.04-Desktop で Redmine + postgreSQL + nginx をサブディレクトリで起動するメモ

## Redmine + nginx の情報が意外と少なく、あっても情報が古かったりで苦労したのでメモ

![test.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/139073/4f65ce99-8408-4d5c-bbb9-735527173363.png)


### 必要なものをインストール
```shell
sudo apt install -y language-pack-ja
sudo locale-gen ja_JP.UTF-8
sudo apt update
sudo apt install -y build-essential zlib1g-dev libssl-dev libreadline-dev libyaml-dev libcurl4-openssl-dev libffi-dev
sudo apt install -y postgresql libpq-dev
sudo apt install -y imagemagick fonts-takao-pgothic
sudo apt install -y subversion git curl vim
```
### Rubyをインストール
```shell
curl -O https://cache.ruby-lang.org/pub/ruby/3.3/ruby-3.3.6.tar.gz
tar xvf ruby-3.3.6.tar.gz
cd ruby-3.3.6
./configure --disable-install-doc
make
sudo make install
cd ..
```
### postgreSQLのRedmineユーザーのパスワード設定。とりあえずpassに設定
```shell
sudo -i -u postgres createuser -P redmine
Enter password for new role: (PostgreSQLの redmine用ユーザーに設定するパスワードを入力)
Enter it again: (パスワード再入力)
```
### postgreSQLにRedmineのデータベースを作成
```shell
sudo -i -u postgres createdb -E UTF-8 -l ja_JP.UTF-8 -O redmine -T template0 redmine
```
### Redmineの設定
```shell
sudo mkdir /var/lib/redmine
sudo chown www-data /var/lib/redmine
sudo -u www-data svn co https://svn.redmine.org/redmine/branches/6.0-stable /var/lib/redmine

sudo vim /var/lib/redmine/config/database.yml
production:
  adapter: postgresql
  database: redmine
  host: localhost
  username: redmine
  password: "pass"
  encoding: utf8
  
sudo vim /var/lib/redmine/config/configuration.yml
# ※domainはとりあえずPCにしております
production:
  email_delivery:
    delivery_method: :smtp
    smtp_settings:
      address: "localhost"
      port: 25
      domain: "pc"
  rmagick_font_path: /usr/share/fonts/truetype/takao-gothic/TakaoPGothic.ttf

cd /var/lib/redmine
sudo bundle config set --local without 'development test'
sudo bundle install
sudo -u www-data bin/rake generate_secret_token
sudo -u www-data bin/rake db:migrate RAILS_ENV="production"
sudo gem install passenger -N
```
### Nginxをインストール、設定
```shell
cd ~
sudo apt-get install nginx -y
sudo apt-get install -y dirmngr gnupg apt-transport-https ca-certificates curl

# ※　passengerは依存関係がシビアみたいで、以下のやり方では、おそらくUbuntu-24.04*以外では入らないと思われる
curl https://oss-binaries.phusionpassenger.com/auto-software-signing-gpg-key.txt | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/phusion.gpg >/dev/null
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger noble main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
sudo apt-get install -y libnginx-mod-http-passenger
if [ ! -f /etc/nginx/modules-enabled/50-mod-http-passenger.conf ]; then sudo ln -s /usr/share/nginx/modules-available/mod-http-passenger.load /etc/nginx/modules-enabled/50-mod-http-passenger.conf ; fi
sudo ls /etc/nginx/conf.d/mod-http-passenger.conf
sudo service nginx restart
sudo /usr/bin/passenger-config validate-install
# Passenger it selfを選択する
sudo /usr/sbin/passenger-memory-stats
sudo apt-get update
sudo apt-get upgrade
sudo vim /etc/nginx/nginx.conf
# 60行目の include /etc/nginx/sites-enabled/*;を以下に変更する
include /etc/nginx/sites-available/*;
cd /etc/nginx/sites-available
# defaultを削除する
sudo rm -r default

sudo vim /etc/nginx/sites-available/redmine
# ※server_nameはとりあえずPCにしております
server {
  listen 80;
  server_name pc;
  passenger_enabled on;
  passenger_ruby /usr/local/bin/ruby;
  root /var/lib/redmine/public;
}

sudo systemctl restart nginx
sudo systemctl reload nginx
# http://pcにアクセスしてredmineが表示されるか確認する
```

### サブディレクトリで動くようにするメモ(http//pc/redmine)
```shell
sudo vim /var/lib/redmine/config/environment.rb
# 一番上に追加
ENV['RAILS_RELATIVE_URL_ROOT'] = '/redmine'
# Rails.application.initialize!の上に以下を追記
RedmineApp::Application.routes.default_scope = { :path => '/redmine', :shallow_path => '/redmine' }

sudo vim /var/lib/redmine/config/application.rb
# endの上に以下を追記
config.assets.prefix = '/redmine/assets'

sudo vim /var/lib/redmine/config/configuration.yml
# production:の下にrelative_url_root: /redmineを追加
production:
  relative_url_root: /redmine

sudo vim /etc/nginx/sites-available/redmine
server {
        listen 80;
        server_name pc;
        location /redmine/ {
                proxy_pass http://localhost:3000/redmine/;
                passenger_enabled on;
                passenger_ruby /usr/local/bin/ruby;
                root /var/lib/redmine/public;
        }
}

sudo systemctl reload nginx
# http://pc/redmineにアクセスする
```


## 参考にさせていただいたサイト
https://blog.redmine.jp/articles/6_0/install/ubuntu24/

https://www.phusionpassenger.com/docs/advanced_guides/install_and_upgrade/nginx/install/oss/noble.html

https://qiita.com/yteraoka/items/bf47a893c0007489dda7