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的生活。

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