[TOC]
远程登陆 Linux
游说老板和 IT 需要一台云主机来部署一个 Redmine,今天终于可以用了!
主机是天翼云的,具体为什么用他家此处不表,目测有华为做技术支持。8 核 16 GB,搭载个 Redmine 应该是绰绰有余……吧?
上来看到有两台主机,立即提交了一个工单把 Windows 改成了 Ubuntu 16.04 Server 64bit
响应速度蛮快,很快就看到一台主机变成了 Ubuntu。
SSH 登陆还是很基础的东西了,之前学习过,按照默认的手册,直接
$ ssh root@host
上来提示:
_____________________________________________________________________
WARNING! Your environment specifies an invalid locale.
The unknown environment variables are:
LC_CTYPE=UTF-8 LC_ALL=
This can affect your user experience significantly, including the
ability to manage packages. You may install the locales by running:
sudo apt-get install language-pack-UTF-8
or
sudo locale-gen UTF-8
To see all available language packs, run:
apt-cache search "^language-pack-[a-z][a-z]$"
To disable this message for all users, run:
sudo touch /var/lib/cloud/instance/locale-check.skip
_____________________________________________________________________
然而并没有管,而是先 apt update && apt upgrade -y
更新走一波。upgrade
期间还是会提示有 locale 相关的错误。update
完成后看了一下 locale
的情况:
root@ecs-XXXX:~# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE=UTF-8
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
搜索一波,有说用
$ sudo locale-gen "en_US.UTF-8"
$ sudo dpkg-reconfigure locales
还有表示应该在 /etc/environment
里面写入:
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
这时客服打来电话,告诉我系统装好了。重新登陆上去发现没有 locale 的警告,可能当时还没配置完系统所以出现了错误警告。先记在这里。
创建用户
之前在虚拟机上试着搭建 Redmine,印象里要两个帐户,一个是我作为管理员,一个 Redmine。不管怎样都不能直接用 root 搭建啊!
root@ecs-XXXX:~# useradd tonylyu
root@ecs-XXXX:~# passwd tonylyu
New password:
Retype new password:
passwd: password updated successfully
两个命令,分别是创建了一个帐户,和(作为 root 用户)为这个用户修改了密码。
重新 SSH 登陆上去,配置一下 tonylyu 的用户环境~
然后又看到了熟悉的警告:
_____________________________________________________________________
WARNING! Your environment specifies an invalid locale.
The unknown environment variables are:
LC_CTYPE=UTF-8 LC_ALL=
This can affect your user experience significantly, including the
ability to manage packages. You may install the locales by running:
sudo apt-get install language-pack-UTF-8
or
sudo locale-gen UTF-8
To see all available language packs, run:
apt-cache search "^language-pack-[a-z][a-z]$"
To disable this message for all users, run:
sudo touch /var/lib/cloud/instance/locale-check.skip
_____________________________________________________________________
微笑面对吧。
先按提示说的
$ sudo apt-get install language-pack-UTF-8
[sudo] password for tonylyu:
tonylyu is not in the sudoers file. This incident will be reported.
嗯,首先我不是 sudoer,就是没有可以执行 sudo
命令的权利。其次我 pwd
发现自己在 /
目录下。不禁怀疑自己是不是没有正确创建用户,连用户的 home directory 都没有。
Google 一下发现果然是两个不同的命令,一个是 useradd
, 一个是的 adduser
。手动微笑。
没关系,删号重练。Linux 下删除用户操作:
root@ecs-XXXX:~# deluser tonylyu
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = "UTF-8",
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
Removing user `tonylyu' ...
Warning: group `tonylyu' has no more members.
Done.
用 adduser
命令,果然更加友好:
root@ecs-XXXX:~# adduser tonylyu
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = "UTF-8",
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
Adding user `tonylyu' ...
Adding new group `tonylyu' (1001) ...
Adding new user `tonylyu' (1001) with group `tonylyu' ...
Creating home directory `/home/tonylyu' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for tonylyu
Enter the new value, or press ENTER for the default
Full Name []: Tony
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y
root@ecs-XXXX:~#
接下来是要给这个帐户变成 sudoer。这里没有研究哪个 usergroup 有 sudo 权限,而是直接用 visudo
给 tonylyu 赋予全部权限。参考了这里。
终于可以登陆 tonylyu 了。提示符太丑,想打 vim .bashrc
修改一下,发现 tab 不仅不会自动补全,还会弹错误:
tonylyu@ecs-XXXX:~$ vim .bash-bash: /dev/null: Permission denied
bash: _upvars: `-a2': invalid number specifier
-bash: /dev/null: Permission denied
bash: _upvars: `-a0': invalid number specifier
可能是一个 Bash 的 bug:可以补全命令,但是不能补全参数。
anyway 在 .bashrc 文件添加一行:
export PS1="\[\e[31m\]\u\[\e[0m\]@\[\e[34m\]\h\[\e[0m\]:\[\e[33m\W \[\e[91m\]\$\[\e[0m\] "
接着解决 tab 补全的问题。
这里有意见认为是没有正确添加用户,也就是我在上面遇到的 useradd
和 adduser
差异的问题。检查 .bashrc
文件后确认 bash-autocompletion
没有问题。
尝试从另一个角度解决问题,检查
-bash: /dev/null: Permission denied
这个报错。
根据这里的解决建议,使用 root
账号执行:rm -f /dev/null; mknod -m 666 /dev/null c 1 3
,重新登陆不再报错,tab-autocompletion
也正常了。
然而这个问题在重启后还会出现,暂且不表。
然后按照上面查过的资料顺手把 /etc/environment
文件改好,不想再看 locale 的报错了。
同样的操作创建 Redmine
账号,进行下一步。
安装 Redmine 的环境
安装 Ruby
这里直接就抄 redmine.org 的 HowTo’s:
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:brightbox/ruby-ng
sudo apt-get update
sudo apt-get -y install ruby ruby-switch ruby-dev ri libruby libssl-dev zlib1g-dev
RVM
第一次安装的时候这里漏掉了,回来补上。
应该是 Ruby 的 Virtual Machine 或者 interpreter 之类的东西,用 redmine 账号执行:
$ gpg --keyserver hkp://pgp.mit.edu --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
$ curl -sSL https://get.rvm.io | bash -s stable
完成后退出 redmine 账号再重登陆一次:
$ rvm list known
$ rvm install ruby-2.4.1
$ rvm list
rvm rubies
=* ruby-2.4.1 [ x86_64 ]
...
$ ruby-switch --list
ruby2.3
ruby2.4
$ ruby --version
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
所以这是两个 Ruby 了……
安装 Redmine
下载 Redmine
官网打不开,只好到 github 上 clone 一个下来。这里的操作使用 redmine 帐户完成。
$ git clone https://github.com/redmine/redmine.git
虽然应该会一直用 3.4-stable 的版本,还是分出来一个文件夹,方便管理。
$ cp --recursive redmine/ redmine_3.4_stable/
$ cd redmine_3.4_stable/
$ git checkout 3.4-stable
$ cd ..
$ mv redmine redmine-master
$ ln -s redmine_3.4_stable/ redmine
结果如下:
lrwxrwxrwx 1 redmine redmine 19 Dec 27 10:30 redmine -> redmine_3.4_stable//
drwxrwxr-x 18 redmine redmine 4096 Dec 27 10:42 redmine_3.4_stable/
drwxrwxr-x 17 redmine redmine 4096 Dec 27 10:29 redmine-master/
配置 MySQL
就是用 MySQL 的 root 帐户创建一个 redmine 的表,然后创建 redmine 用户并把这个用户配置到 redmine 的 configuration 里面去,继续抄袭:
$ sudo mysql -u root -p
> CREATE DATABASE redmine character SET utf8;
> CREATE user 'redmine'@'localhost' IDENTIFIED BY 'my_password';
> GRANT ALL privileges ON redmine.* TO 'redmine'@'localhost';
> exit
$ cd ~/redmine/config/
$ cp database.yml.example database.yml
$ vim database.yml
安装 Redmine
继续抄袭:
$ cd ~/redmine
$ gem install bundler
$ bundle install --without development test postgresql sqlite
值得注意的是,这里要用 redmine 的账号,不能用 root 权限,否则后面会有权限问题。
报错了:
Installing mysql2 0.4.10 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
An error occurred while installing mysql2 (0.4.10), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.10'` succeeds before bundling.
看 log 是一堆 gcc 编译错误,应该是 MySQL 相关的库没有安装完整。
$ sudo apt install libmysqlclient-dev
然后重来,再报错:
Installing rmagick 2.16.0 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /var/lib/gems/2.3.0/gems/rmagick-2.16.0/ext/RMagick
/usr/bin/ruby2.3 -r ./siteconf20171227-28726-vmyl1b.rb extconf.rb
checking for gcc... yes
checking for Magick-config... no
checking for pkg-config... no
Can't install RMagick 2.16.0. Can't find Magick-config or pkg-config in
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
没啥好说的,缺啥装啥。
$ sudo apt install pkg-config imagemagick libmagickcore-dev libmagickwand-dev
再重来,搞定。
不懂 Ruby,不知道这一步是干啥,望文生义大概是初始化 redmine 的 database:
RAILS_ENV=production rake db:migrate
RAILS_ENV=production rake redmine:load_default_data
之前漏掉了 RVM 的安装,重新操作到这一步的时候会遇到 bundle install
path permission 的问题:
There was an error while trying to write to `/home/redmine/.bundle/cache/compact_index/rubygems.org.443.29b0360b937aa4d161703e6160654e47/versions`. It is likely that you need to grant write permissions for that path.
这就是是有的时候不明就里暴力 sudo 导致的问题。
安装 Phusion Passenger
这里完全不懂,抄 HowTo’s。
添加 Phusion Passenger
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
sudo apt-get install apt-transport-https ca-certificates
打开配置文件:sudo vim /etc/apt/sources.list.d/passenger.list
添加源:
deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main
更改文件权限:
sudo chown root: /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list
安装:
sudo apt-get update
sudo apt-get install libapache2-mod-passenger
配置,打开配置文件 sudo vim /etc/apache2/mods-available/passenger.conf
添加这几行
PassengerUserSwitching on
PassengerUser redmine
PassengerGroup redmine
修改 Apache2 的配置文件:/etc/apache2/sites-available/000-default.conf
添加这些:
<Directory /var/www/html/redmine>
RailsBaseURI /redmine
PassengerResolveSymlinksInDocumentRoot on
</Directory>
sudo a2enmod passenger
sudo ln -s /home/redmine/redmine/public/ /var/www/html/redmine
sudo service apache2 restart
生成新的 secret:
sudo su - redmine
cd redmine
rake generate_secret_token
rake db:migrate RAILS_ENV=production
rake redmine:plugins:migrate RAILS_ENV=production
rake tmp:cache:clear
rake tmp:sessions:clear
exit
到这里就算完了,运行 visudo
删掉 redmine 账号的 sudo 权限,重启 Apache:
systemctl restart apache2.service