redmine搭建心得(apache+fcgi)

有了效率工具,感覺在家辦公都充滿了激情了:)
首先聲明我不會Ruby,但是參考網絡資料搭建起來花時也就一天,不是什麼難事。

主要的資料包括兩篇官方文檔,和一篇博客:
[[http://www.redmine.org/projects/redmine/wiki/RedmineInstall]]
[[http://www.redmine.org/projects/redmine/wiki/HowTo_configure_Apache_to_run_Redmine]]
[[https://www.keopx.net/blog/howto-install-redmine-242-ubuntu-1204-lts]]

RedmineInstall

這篇講到webrick測試 [[http://localhost:3000/]] 就沒有下文了。
小字部分說得很清楚,開發版不能代替發佈版,存在安全隱患,應該更多地去了解 Passenger (aka mod_rails), FCGI or a Rack serve 之類。

這裏面踩到幾個坑:

1. 執行 bundle install 時進程Killed掉了。

# bundle install
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine.
Fetching gem metadata from https://rubygems.org/...........Killed

這個原因是內存不足導致的。需要通過swap增加虛擬內存。
查看swap用指令

$grep Swap /proc/memninfo
$free -h

如果swap都是0,就可以通過創建swap來解決。
假如 df -h 查看文件系統,發現根目錄 / 還有剩餘空間,就可以把swap文件放到根目錄 / 下。

$df -h
Filesystem      Size  Used Avail Use% Mounted on
...
/dev/xvda1       20G  5.9G   14G  31% /
...
$cd /
$sudo dd if=/dev/zero of=Swap.disk bs=1M count=2k           # 單位1M,創建2k,即總計2G的swap空間
$sudo mkswap -f Swap.disk   
$sudo swapon Swap.disk  

如果已經有swap文件,則需要先刪除,再創建

$swapon -s                                                  # 查看swap文件在哪
Filename                                Type            Size    Used    Priority
/Swap.disk                              file            2097148 424448  -2
$swapoff /Swap.disk
$rm -rf /Swap.disk

如果要實現開機的自動掛載,還得修改 /etc/fstab 文件。

2. bundle install --without development test 安裝依賴項產生的各種問題

比如 nokogiri rmagick mysqlclient
bundle安裝不了,卻可以通過 gem install 或者 apt install 見招拆招。
只要能從LOG中看出是哪個依賴項,就不成問題。

3. 理解mysql配置

數據庫是這樣建立的。

CREATE DATABASE redmine CHARACTER SET utf8mb4;
CREATE USER 'redmine'@'localhost' IDENTIFIED BY 'my_password';
GRANT ALL PRIVILEGES ON redmine.* TO 'redmine'@'localhost';

這三句的意思是:

  1. 創建一個名爲redmine的database
  2. 創建用戶redmine,指定密碼my_password
  3. 登錄用戶redmine後,擁有對數據庫redmine操作的所有權限。

控制檯登錄都應該是這樣
$mysql -uredmine -hlocalhost -pmy_password

config/database.yml 中的 database username password 應該與上面的操作相對應。

production:
  adapter: mysql2
  database: redmine
  host: localhost
  username: redmine
  password: "my_password" 

數據庫登錄不了會反應到 log/production.log 上。(這裏是把 username 寫成了 root)

app/models/setting.rb:235:in `check_cache'
app/controllers/application_controller.rb:90:in `user_setup'
Started GET "/" for 119.32.89.27 at 2019-08-13 17:18:14 +0000
Processing by WelcomeController#index as HTML
Completed 500 Internal Server Error in 7ms
  
Mysql2::Error (Access denied for user 'root'@'localhost'):

4. 對於 production, 附加依賴項 Gemfile.local 很重要。

因爲它涉及到 fcgi 的安裝。
這個 Gemfile.local 文件是在源文件根目錄下手動創建的。加上如下內容後,執行 bundle install 會自動安裝上。

gem ‘fcgi’

但是 bundle install 還不一定成功,最後我用 apt 解決

apt install libfcgi-dev

5. 文件系統權限問題

Step8 提到這個問題,是哪個賬戶來運行的 redmine 程序,源文件文件的用戶和組就應該與之對應。
我們將 redmine 的 production 版本交給 Apache + fcgi 服務來運行,就要看 Apache 服務是哪個用戶和組來運行。

$ ps aux | grep apache
ron      18453  0.0  0.2  14856  1064 pts/1    S+   03:06   0:00 grep --color=auto apache
root     25754  0.0  0.3  98220  1856 ?        Ss   Aug13   0:00 /usr/sbin/apache2 -k start
www-data 25756  0.0  0.2 100252  1416 ?        S    Aug13   0:00 /usr/sbin/apache2 -k start
www-data 25778  0.0  1.2 846804  5936 ?        Sl   Aug13   0:00 /usr/sbin/apache2 -k start
www-data 25779  0.0  1.1 846748  5632 ?        Sl   Aug13   0:00 /usr/sbin/apache2 -k start

所以我把源文件的用戶和組全都換成了 www-data 。(即把這裏的 redmine:redmine 替換成 www-data:www-data )

howto-install-redmine-242-ubuntu-1204-lts

完成 webrick 測試,接着就是向着 production 進軍了。這篇博文從測試到發佈做了比較全面的總結,給了我一些很好的提示:

1. redmine 源文件解壓後應放到 /var/www/ 目錄下

2. redmine 已經支持 fcgi,兩個關鍵文件:public/dispatch.fcgi.example,public/htaccess.fcgi.example

3. 頁面配置應放到 /etc/apache2/sites-available/ 目錄下,如何編寫配置文件,如何通過 apache 啓用頁面。

Download and install redmine 2.4.2 on Ubuntu 12.04 LTS:

$ sudo cd /var/www
$ sudo wget http://www.redmine.org/releases/redmine-2.4.2.tar.gz

$ sudo tar zxvf redmine-2.4.2.tar.gz
$ sudo mv redmine-2.4.2 project.example.com
$ sudo cd project.example.com/config/
$ sudo cp database.yml.example database.yml
$ sudo nano database.yml
$ sudo cd /var/www/project.example.com
Installing dependencies:

Ruby 1.9.3:

$ sudo apt-get install ruby1.9.3 
Install redmine

$ sudo gem install bundle
$ sudo bundle install --without development test
Some errors during installation:

Error mysql2:

An error occurred while installing mysql2 (0.3.15), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.15'` succeeds before bundling.
Solution, install MySQL libraries:

$ sudo apt-get install mysql-client libmysqlclient-dev
$ sudo gem install mysql2 -v '0.3.15'
Error rmagick:

An error occurred while installing rmagick (2.13.2), and Bundler cannot continue.
Make sure that `gem install rmagick -v '2.13.2'` succeeds before bundling.
Solution, install magick libraries:

$ sudo apt-get install libmagickcore-dev libmagickwand-dev
$ sudo gem install rmagick -v '2.13.2'

We continue with installation:

$ sudo bundle install --without development test
$ sudo rake generate_secret_token
$ sudo RAILS_ENV=production rake db:migrate
$ sudo RAILS_ENV=production rake redmine:load_default_data
$ sudo mkdir -p tmp tmp/pdf public/plugin_assets
$ sudo chown -R 777 files log tmp public/plugin_assets
$ sudo chmod -R 755 files log tmp public/plugin_assets

Webrick server:

$ sudo ruby script/rails server webrick -e production

Apache con fcgid:

$ sudo aptitude install libapache2-mod-fcgid
$ sudo cd /var/www/project.example.com/public/
$ sudo mv dispatch.fcgi.example dispatch.fcgi
$ sudo chmod 755 dispatch.fcgi
$ sudo mv htaccess.fcgi.example .htaccess
$ sudo nano /etc/apache2/sites-available/project.example.com

##$ sudo Proyectos
<VirtualHost *:80>
        ServerAdmin support_mail
        ServerName project.example.com
        DocumentRoot /var/www/project.example.com/public
        <Directory /var/www/project.example.com/public>
                Options Indexes ExecCGI FollowSymLinks
                Order allow,deny
                Allow from all
                AllowOverride all
        </Directory>
        ErrorLog /var/log/apache2/proyectos_isarea_error.log
        $ sudo Possible values include: debug, info, notice, warn, error, crit,
        $ sudo alert, emerg.
        LogLevel warn
        CustomLog /var/log/apache2/proyectos_isarea_access.log combined
        ServerSignature On
</VirtualHost>

$ sudo a2ensite project.example.com
$ sudo service apache2 restart

可以發現這篇博文也對上訴提到的 bundle install 不成功的依賴項,提出了簡潔的解決方案。

HowTo_configure_Apache_to_run_Redmine

將博文結合 HowTo_configure_Apache_to_run_Redmine 就能搭建好 production 版redmine。
這裏要指出幾個問題

1. 頁面配置應有後綴名 *.conf

博文中的 project.example.com 這個文件,Apache其實訪問不到。
可以參考默認頁面 /etc/apache2/sites-available/000-default.conf

2. 監聽端口 3000

配置文件中應改爲 <VirtualHost *:3000>,保險起見,在第一行加入 Listen 3000

3. 不要聽官網的說法,將 redmine 源文件解壓至 /opt/,再到 /var/www/ 做public目錄的軟鏈接。

這個說法出現在 Ubuntu Server (Version ?) (This not don’t work for 8.04 LTS) 一欄。
並沒有什麼卵用,而且會造成文件權限錯誤(End of script output before headers)

[Tue Aug 13 16:24:06.843878 2019] [fcgid:warn] [pid 21346:tid 139802452125440] (104)Connection reset by peer: [client x.x.x.x:20090] mod_fcgid: error reading data from FastCGI server
[Tue Aug 13 16:24:06.844040 2019] [core:error] [pid 21346:tid 139802452125440] [client x.x.x.x:20090] End of script output before headers: dispatch.fcgi

4. 頁面配置文件不要用複雜的 ServerName,用IP訪問就行了

當然原因是我沒有買域名。這裏可參考000-default.conf。

5. 完整 001-redmine.conf 如下

Listen 3000

<VirtualHost *:3000>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/redmine/public

    <Directory /var/www/redmine/public>
        Options Indexes ExecCGI FollowSymLinks
        Order allow,deny
        Allow from all
        AllowOverride all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/redmine_error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/redmine_access.log combined
    ServerSignature On
</VirtualHost>

6. 官方文檔說的 fcgi 配置文件換了目錄。

見 fcgid from China Coremail service 一欄。

/etc/httpd/conf.d/mod_fcgid.conf 變更爲 /etc/apache2/mods-available/fcgid.conf

且安裝 mod_fcgid 時不用修改 Makefile 中的 top_dir,只要能找到 mod_fcgid.so 這個文件就好。
mod_fcgid 是 mod_fastcgi 的改良版,目前 mod_fastcgi 已棄用。


不得不說,我還是習慣可以拆單、寫wiki的生活。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章