@armink
2014-04-10T15:17:20.000000Z
字数 14531
阅读 2181
参考安装文档:
- http://www.redmine.org/projects/redmine/wiki/RedmineInstall#fn1
- http://blog.csdn.net/csfreebird/article/details/18138869
sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
删除匿名用户
#登陆MySQL数据库
mysql -u root -p
#查看所有用户
mysql> select user,host from mysql.user;
#删除所有空用户(匿名用户)
mysql> delete from mysql.user where user='';
#检查删除结果
mysql> select user,host from mysql.user;
#退出MySQL数据库
mysql> \q
sudo apt-get install -y nginx
sudo apt-get install imagemagick libmagickcore-dev libmagickwand-dev
如果存在旧版本Ruby1.8及1.9请先卸载
sudo apt-get remove ruby1.8 ruby1.9
下载、编译Ruby2.0
mkdir /tmp/ruby && cd /tmp/ruby
curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p353.tar.gz | tar xz
cd ruby-2.0.0-p353
./configure --disable-install-rdoc
make
sudo make install
国内用户建议更换Ruby Gem源为淘宝源,提高Gem安装速度
sudo gem sources --remove https://rubygems.org/
sudo gem sources -a http://ruby.taobao.org/
#更新缓存
sudo gem sources -u
#查看源,请确保只有 ruby.taobao.org
gem sources -l
安装Bundler Gem
sudo gem install bundler --no-ri --no-rdoc
安装ImageMagick Gem
sudo gem install rmagick -v '2.13.2'
#创建Redmine用户
sudo adduser --disabled-login --gecos 'Redmine' redmine
#进入Redmine用户目录
cd /home/redmine
#下载解压源码
curl --progress http://www.redmine.org/releases/redmine-2.5.1.tar.gz | tar xz
#修改文件夹名为redmine
sudo mv redmine-2.5.1 redmine
初始化数据库
#登陆数据库
mysql -u root -p
#创建Redmine数据库
mysql> CREATE DATABASE redmine CHARACTER SET utf8;
#创建Redmine用户,密码'my_password'可以自己设定,后面需要用到
mysql> CREATE USER 'redmine'@'localhost' IDENTIFIED BY 'my_password';
mysql> GRANT ALL PRIVILEGES ON redmine.* TO 'redmine'@'localhost';
数据库连接配置
#进入Redmine目录
cd /home/redmine/redmine
#复制Redmine数据库默认配置
sudo cp config/database.yml.example config/database.yml
修改配置信息
sudo gedit config/database.yml
找到如下内容
production:
adapter: mysql2
database: redmine
host: localhost
username: root
password: ""
encoding: utf8
默认password为空,将其修改为之前设置的MySQL中Redmine用户密码,保存即可。
#进入Redmine目录
cd /home/redmine/redmine
#通过bundle安装Gem依赖
sudo bundle install --without development test
#生成session store
sudo rake generate_secret_token
#创建数据库结构
sudo RAILS_ENV=production rake db:migrate
#插入默认数据,根据提示信息选择指定语言。我这里选择中文,对应选项为zh
sudo RAILS_ENV=production rake redmine:load_default_data
sudo mkdir -p tmp tmp/pdf public/plugin_assets
sudo chown -R redmine:redmine files log tmp public/plugin_assets
sudo chmod -R 755 files log tmp public/plugin_assets
#启动服务用于测试
sudo ruby script/rails server webrick -e production
在浏览器中输入http://localhost:3000
即可看到Redmine的站点了
默认账号:admin
默认密码:admin
nginx 是一个非常高效的 http server, 而 unicorn 是一个非常高效的 app server。
首先编辑Gemfile文件
sudo gedit Gemfile
打开文件后,在增加unicorn配置,将下面代码插入文件中。(我这里是放到openid组的后面)
# Application server add by armink
group :unicorn do
gem "unicorn", '~> 4.6.3'
gem 'unicorn-worker-killer'
end
通过bundle更新Gem依赖,此时会看到unicorn被安装。
sudo bundle install --without development test
增加unicorn配置文件,通过gedit命令打开config/unicorn.rb
sudo gedit config/unicorn.rb
打开空文件后,在里面添加如下内容,我这里端口设置为8082。(里面内容都是参考Gitlab中的unicorn配置修改的)
# Sample verbose configuration file for Unicorn (not Rack)
#
# This configuration file documents many features of Unicorn
# that may not be needed for some applications. See
# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
# for a much simpler configuration file.
#
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# documentation.
# Use at least one worker per core if you're on a dedicated server,
# more will usually help for _short_ waits on databases/caches.
worker_processes 2
# Since Unicorn is never exposed to outside clients, it does not need to
# run on the standard HTTP port (80), there is no reason to start Unicorn
# as root unless it's from system init scripts.
# If running the master process as root and the workers as an unprivileged
# user, do this to switch euid/egid in the workers (also chowns logs):
# user "unprivileged_user", "unprivileged_group"
# Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up.
working_directory "/home/redmine/redmine" # available in 0.94.0+
# listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
listen "/home/redmine/redmine/tmp/sockets/redmine.socket", :backlog => 64
listen "127.0.0.1:8082", :tcp_nopush => true
# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30
# feel free to point this anywhere accessible on the filesystem
pid "/home/redmine/redmine/tmp/pids/unicorn.pid"
# By default, the Unicorn logger will write to stderr.
# Additionally, some applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path "/home/redmine/redmine/log/unicorn.stderr.log"
stdout_path "/home/redmine/redmine/log/unicorn.stdout.log"
# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
# Enable this flag to have unicorn test client connections by writing the
# beginning of the HTTP headers before calling the application. This
# prevents calling the application for connections that have disconnected
# while queued. This is only guaranteed to detect clients on the same
# host unicorn runs on, and unlikely to detect disconnects even on a
# fast LAN.
check_client_connection false
before_fork do |server, worker|
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
# The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house
# twice as many worker_processes as you have configured.
#
# This allows a new master process to incrementally
# phase out the old master process with SIGTTOU to avoid a
# thundering herd (especially in the "preload_app false" case)
# when doing a transparent upgrade. The last worker spawned
# will then kill off the old master process with a SIGQUIT.
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
#
# Throttle the master from forking too quickly by sleeping. Due
# to the implementation of standard Unix signal handlers, this
# helps (but does not completely) prevent identical, repeated signals
# from being lost when the receiving process is busy.
# sleep 1
end
after_fork do |server, worker|
# per-process listener ports for debugging/admin/migrations
# addr = "127.0.0.1:#{9293 + worker.nr}"
# server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
# the following is *required* for Rails + "preload_app true",
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
# if preload_app is true, then you may also want to check and
# restart any other shared sockets/descriptors such as Memcached,
# and Redis. TokyoCabinet file handles are safe to reuse
# between any number of forked children (assuming your kernel
# correctly implements pread()/pwrite() system calls)
end
我这里思路和Gitlab是一样的,首先在script文件夹中增加脚本文件对unicorn实现启动、停止等命令控制,再在/etc/init.d
文件夹中增加开机自启动脚本,注册启动服务即可。详细过程如下。
增加unicorn控制脚本,通过gedit命令打开script/web
sudo gedit script/web
打开空白文件后,再里面添加如下内容。
#!/usr/bin/env bash
cd $(dirname $0)/..
app_root=$(pwd)
unicorn_pidfile="$app_root/tmp/pids/unicorn.pid"
unicorn_config="$app_root/config/unicorn.rb"
function get_unicorn_pid
{
local pid=$(cat $unicorn_pidfile)
if [ -z $pid ] ; then
echo "Could not find a PID in $unicorn_pidfile"
exit 1
fi
unicorn_pid=$pid
}
function start
{
bundle exec unicorn_rails -D -c $unicorn_config -E $RAILS_ENV
}
function stop
{
get_unicorn_pid
kill -QUIT $unicorn_pid
}
function reload
{
get_unicorn_pid
kill -USR2 $unicorn_pid
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
*)
echo "Usage: RAILS_ENV=your_env $0 {start|stop|reload}"
;;
esac
设置该脚本文件属性为可执行文件
sudo chmod 755 script/web
增加开机自启动脚本,实现对Redmine中的unicorn开机自启动,通过gedit命令打开/etc/init.d/redmine
sudo gedit /etc/init.d/redmine
在该空白启动脚本文件中加入以下内容
#! /bin/sh
# REDMINE
# Maintainer: @armink
# Authors: armink.ztl@gmail.com, @armink
### BEGIN INIT INFO
# Provides: redmine
# Required-Start: $local_fs $remote_fs $network $syslog redis-server
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
###
# DO NOT EDIT THIS FILE!
# This file will be overwritten on update.
# Instead add/change your variables in /etc/default/redmine
# An example defaults file can be found in lib/support/init.d/redmine.default.example
###
### Environment variables
RAILS_ENV="production"
# Script variable names should be lower-case not to conflict with
# internal /bin/sh variables such as PATH, EDITOR or SHELL.
app_user="redmine"
app_root="/home/$app_user/redmine"
pid_path="$app_root/tmp/pids"
socket_path="$app_root/tmp/sockets"
web_server_pid_path="$pid_path/unicorn.pid"
# Read configuration variable file if it is present
test -f /etc/default/redmine && . /etc/default/redmine
# Switch to the app_user if it is not he/she who is running the script.
if [ "$USER" != "$app_user" ]; then
sudo -u "$app_user" -H -i $0 "$@"; exit;
fi
# Switch to the redmine path, exit on failure.
if ! cd "$app_root" ; then
echo "Failed to cd into $app_root, exiting!"; exit 1
fi
### Init Script functions
## Gets the pids from the files
check_pids(){
if ! mkdir -p "$pid_path"; then
echo "Could not create the path $pid_path needed to store the pids."
exit 1
fi
# If there exists a file which should hold the value of the Unicorn pid: read it.
if [ -f "$web_server_pid_path" ]; then
wpid=$(cat "$web_server_pid_path")
else
wpid=0
fi
}
## Called when we have started the two processes and are waiting for their pid files.
wait_for_pids(){
i=0;
while [ ! -f $web_server_pid_path ]; do
sleep 0.1;
i=$((i+1))
if [ $((i%10)) = 0 ]; then
echo -n "."
elif [ $((i)) = 301 ]; then
echo "Waited 30s for the processes to write their pids, something probably went wrong."
exit 1;
fi
done
echo
}
# We use the pids in so many parts of the script it makes sense to always check them.
# Only after start() is run should the pids change.
check_pids
## Checks whether the different parts of the service are already running or not.
check_status(){
check_pids
# If the web server is running kill -0 $wpid returns true, or rather 0.
# Checks of *_status should only check for == 0 or != 0, never anything else.
if [ $wpid -ne 0 ]; then
kill -0 "$wpid" 2>/dev/null
web_status="$?"
else
web_status="-1"
fi
if [ $web_status = 0 ]; then
redmine_status=0
else
# http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
# code 3 means 'program is not running'
redmine_status=3
fi
}
## Check for stale pids and remove them if necessary.
check_stale_pids(){
check_status
# If there is a pid it is something else than 0, the service is running if
# *_status is == 0.
if [ "$wpid" != "0" -a "$web_status" != "0" ]; then
echo "Removing stale Unicorn web server pid. This is most likely caused by the web server crashing the last time it ran."
if ! rm "$web_server_pid_path"; then
echo "Unable to remove stale pid, exiting."
exit 1
fi
fi
}
## If no parts of the service is running, bail out.
exit_if_not_running(){
check_stale_pids
if [ "$web_status" != "0" ]; then
echo "Redmine is not running."
exit
fi
}
## Starts Unicorn if it's not running.
start() {
check_stale_pids
if [ "$web_status" != "0" ]; then
echo -n "Starting the Redmine Unicorn."
fi
# Then check if the service is running. If it is: don't start again.
if [ "$web_status" = "0" ]; then
echo "The Unicorn web server already running with pid $wpid, not restarting."
else
# Remove old socket if it exists
rm -f "$socket_path"/redmine.socket 2>/dev/null
# Start the web server
RAILS_ENV=$RAILS_ENV script/web start &
fi
# Wait for the pids to be planted
wait_for_pids
# Finally check the status to tell wether or not Redmine is running
print_status
}
## Asks the Unicorn if it would be so kind as to stop, if not kills it.
stop() {
exit_if_not_running
if [ "$web_status" = "0" ]; then
echo -n "Shutting down the Redmine Unicorn"
fi
# If the Unicorn web server is running, tell it to stop;
if [ "$web_status" = "0" ]; then
RAILS_ENV=$RAILS_ENV script/web stop
fi
# If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script.
while [ "$web_status" = "0" ]; do
sleep 1
check_status
printf "."
if [ "$web_status" != "0" ]; then
printf "\n"
break
fi
done
sleep 1
# Cleaning up unused pids
rm "$web_server_pid_path" 2>/dev/null
print_status
}
## Prints the status of Redmine and it's components.
print_status() {
check_status
if [ "$web_status" = "0" ]; then
echo "The Redmine is \033[32mrunning\033[0m.\nThe Redmine Unicorn web server pid is $wpid."
else
printf "The Redmine is \033[31mnot running\033[0m.\n"
fi
}
## Tells unicorn to reload it's config
reload(){
exit_if_not_running
if [ "$wpid" = "0" ];then
echo "The Redmine Unicorn Web server is not running thus its configuration can't be reloaded."
exit 1
fi
printf "Reloading Redmine Unicorn configuration... "
RAILS_ENV=$RAILS_ENV script/web reload
echo "Done."
wait_for_pids
print_status
}
## Restarts Unicorn.
restart(){
check_status
if [ "$web_status" = "0" ]; then
stop
fi
start
}
### Finally the input handling.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload|force-reload)
reload
;;
status)
print_status
exit $redmine_status
;;
*)
echo "Usage: service redmine {start|stop|restart|reload|status}"
exit 1
;;
esac
exit
设置该脚本文件属性为可执行文件
chmod 755 /etc/init.d/redmine
存放redmine启动脚本默认运行参数到系统目录
新建并打开/etc/default/redmine文件
sudo gedit /etc/default/redmine
将以下内容拷贝至此空白文件
# RAILS_ENV defines the type of installation that is running.
# Normal values are "production", "test" and "development".
RAILS_ENV="production"
# app_user defines the user that Redmine is run as.
# The default is "redmine".
app_user="redmine"
# app_root defines the folder in which redmine and it's components are installed.
# The default is "/home/$app_user/redmine"
app_root="/home/$app_user/redmine"
# pid_path defines a folder in which the redmine and it's components place their pids.
# This variable is also used below to define the relevant pids for the redmine components.
# The default is "$app_root/tmp/pids"
pid_path="$app_root/tmp/pids"
# socket_path defines the folder in which redmine places the sockets
#The default is "$app_root/tmp/sockets"
socket_path="$app_root/tmp/sockets"
# web_server_pid_path defines the path in which to create the pid file fo the web_server
# The default is "$pid_path/unicorn.pid"
web_server_pid_path="$pid_path/unicorn.pid"
设置此文件属性为可执行文件
chmod 755 /etc/default/redmine
设置Redmine开机自启动
sudo update-rc.d redmine defaults 21
在安装Bundler时,可能会报如下错误:
ERROR: Loading command: install (LoadError)
cannot load such file -- openssl
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass
原因是没有安装openssl,如果在之前安装了Gitlab默认依赖包里面会包括openssl,否则需要手动安装openssl,方法如下:
#安装libssl-dev
sudo apt-get install libssl-dev
#进入之前解压Ruby2.0源码的目录
cd /tmp/ruby
#进入openssl目录
cd ext/openssl
#编译安装openssl
ruby extconf.rb
make
sudo make install
在进行到 Step 4 - Dependencies installation时,sudo bundle install --without development test
命令总是会报ImageMagick安装出错,部分出错日志如下:
ERROR: Error installing rmagick:
ERROR: Failed to build gem native extension.
/usr/bin/ruby1.8 extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for gcc... yes
checking for Magick-config... yes
checking for ImageMagick version >= 6.4.9... yes
checking for HDRI disabled version of ImageMagick... yes
checking for stdint.h... yes
checking for sys/types.h... yes
checking for wand/MagickWand.h... no
终于在这里找到答案,前面两个回答对我都没有效果,只有第三个手动安装方式终于尝试安装成功,手动安装ImageMagick步骤如下:
curl --progress ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz | tar xz
#进入ImageMagick源码目录
cd ImageMagick-6.8.8-10
./configure --prefix=/usr/local --with-x=no --disable-static --with-modules --without-perl --without-magick-plus-plus --with-quantum-depth=8 --disable-openmp
make
sudo make install
sudo /sbin/ldconfig /usr/local
sudo ln -f /usr/local/bin/Magick-config /usr/bin/Magick-config
sudo PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/ gem install rmagick
安装完成后,再重新执行sudo bundle install --without development test
即可完成gems的相关依赖安装。