centos 7 上安装洁账小程序后端
洁账小程序后台部署:
- 注意事项,数据库版本需要mysql 5.7 避免走弯路。
洁账小程序后端:https://github.com/yigger/jiezhang/release
1. 环境配置
cp config/environments/production.rb.example config/environments/production.rb
2. 自定义配置
cp config/settings/production.yml.exmaple config/settings/production.yml
cp config/secrets.yml.example config/secrets.yml
3. 安装依赖
可能需要先执行:bundle update
bundle install
4. 创建数据库
bundle exec rake db:create RAILS_ENV=production
5. 迁移数据表
bundle exec rake db:migrate RAILS_ENV=production
第3步安装报错:
current directory: /home/ruby/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/rmagick-2.13.4/ext/RMagick
/home/ruby/.rbenv/versions/2.7.3/bin/ruby -I /home/ruby/.rbenv/versions/2.7.3/lib/ruby/2.7.0 -r ./siteconf20210420-3380-161xjvn.rb extconf.rb
checking for Ruby version >= 1.8.5... yes
checking for gcc... yes
checking for Magick-config... no
checking for pkg-config... yes
checking for ImageMagick version >= 6.4.9... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/home/ruby/.rbenv/versions/2.7.3/bin/$(RUBY_BASE_NAME)
extconf.rb:154:in ``': No such file or directory - convert (Errno::ENOENT)
from extconf.rb:154:in `block in <main>'
from /home/ruby/.rbenv/versions/2.7.3/lib/ruby/2.7.0/mkmf.rb:971:in `block in checking_for'
from /home/ruby/.rbenv/versions/2.7.3/lib/ruby/2.7.0/mkmf.rb:361:in `block (2 levels) in postpone'
from /home/ruby/.rbenv/versions/2.7.3/lib/ruby/2.7.0/mkmf.rb:331:in `open'
from /home/ruby/.rbenv/versions/2.7.3/lib/ruby/2.7.0/mkmf.rb:361:in `block in postpone'
from /home/ruby/.rbenv/versions/2.7.3/lib/ruby/2.7.0/mkmf.rb:331:in `open'
from /home/ruby/.rbenv/versions/2.7.3/lib/ruby/2.7.0/mkmf.rb:357:in `postpone'
from /home/ruby/.rbenv/versions/2.7.3/lib/ruby/2.7.0/mkmf.rb:970:in `checking_for'
from extconf.rb:151:in `<main>'
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/home/ruby/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/rmagick-2.13.4/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /home/ruby/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/rmagick-2.13.4 for inspection.
Results logged to /home/ruby/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/rmagick-2.13.4/gem_make.out
An error occurred while installing rmagick (2.13.4), and Bundler cannot continue.
Make sure that `gem install rmagick -v '2.13.4' --source 'https://gems.ruby-china.com/'` succeeds before bundling. //这行给了解决方案
In Gemfile:
rmagick
- 原因是要需要依赖这个:ImageMagick,及其库文件,ImageMagick-devel 解决:先执行以下命令安装依赖后再重新执行第3步
yum install ImageMagick
yum install ImageMagick-devel
gem install rmagick -v '2.13.4' 解决方案中的命令依赖前面两个包
第4步创建数据库报错
/home/ruby/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/mime-types-3.2.2/lib/mime/types/logger.rb:30: warning: `_1' is reserved for numbered parameter; consider another name
/home/ruby/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/mime-types-3.2.2/lib/mime/types/logger.rb:30: warning: `_2' is reserved for numbered parameter; consider another name
/home/ruby/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/mime-types-3.2.2/lib/mime/types/logger.rb:30: warning: `_3' is reserved for numbered parameter; consider another name
rake aborted!
ExecJS::RuntimeUnavailable: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
/www/wwwroot/bookkeeping-api/config/application.rb:7:in `<top (required)>'
/www/wwwroot/bookkeeping-api/Rakefile:4:in `require_relative'
/www/wwwroot/bookkeeping-api/Rakefile:4:in `<top (required)>'
/home/ruby/.rbenv/versions/2.7.3/bin/bundle:23:in `load'
/home/ruby/.rbenv/versions/2.7.3/bin/bundle:23:in `<main>'
(See full trace by running task with --trace)
- 原因:缺少JavaScript runtime,解决方案如下:
1. gem execjs
2. 下载安装Nodejs
如果安装完还是报错,注意环境变量,安装用户是否为当前用户?
第5步报错
问题1. Index column size too large. The maximum column size is 767 bytes 数据库用的是MariaDB
原因: INNODB 引擎,UTF-8,主键字符串 默认最大 767,需要修改
解决方案:
- 对数据库进行设置 set global innodb_file_format = BARRACUDA set global innodb_large_prefix = ON
- 对脚本进行修改,添加ROW_FORMAT=DYNAMIC
问题2. ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘json, expend_compare
json, day_avg
json, week_avg
json, month_surplus
js’ at line 1: CREATE TABLE month_charts
(id
bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, user_id
int, year
int, month
int, dashboard
json, expend_compare
json, day_avg
json, week_avg
json, month_surplus
json, budget_used
json, asset_total
json, month_last_10
json, begin_text
text, end_text
text, cover
text, created_at
datetime NOT NULL, updated_at
datetime NOT NULL) ENGINE=InnoDB
原因:MariaDB可能是不支持json格式 解决:由于ruby不熟悉,不知道该怎么把ROW_FORMAT=DYNAMIC这个添加到建表语句中,加上json问题,于是决定将数据库改为mysql5.7
本人的服务器安装了宝塔,还有其他应用在跑,切换版本时提示需要备份并删除所有数据库才能切换。于是需要如下操作:
- 备份所有数据库。
- 注意把所有数据库的用户名、库名、密码也保存一下,保存之后再删除,root密码也建议保存一下。
- 做完上述操作开始切换,等了十多分钟好像突然卡死了,于是赶紧把nginx先停了,还好过了会有反应了,建议删除数据库之前把nginx服务及网站都停止,避免应用重试连接数据库造成资源占用。
- 这个切换版本的过程很慢,建议在宝塔面板软件商店手动操作卸载再安装,安装时选择极速安装,快很多。
- 安装完赶紧恢复其他应用的数据库,操作步骤是添加数据库,按之前保存的数据库名、用户名、密码添加,添加之后导入之前的备份。
- 数据恢复完成之后继续执行步骤5,出现以下错误:
/home/ruby/.rbenv/versions/2.7.3/bin/rake db:migrate RAILS_ENV=production: relocation error: /home/ruby/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/mysql2-0.5.3/lib/mysql2/mysql2.so: symbol mysql_options4, version libmysqlclient_18 not defined in file libmysqlclient.so.18 with link time reference
可能原因是我之前的数据库是MariaDB,在安装ruby环境时,mysql的驱动会根据当前数据库下载对应的版本,于是重新安装驱动,执行以下命令:
gem install mysql2
等待安装完成,再次执行步骤5,又出现权限问题,这个我是在ruby用户下操作的,修改下权限重新执行即可,报错如下:
rake aborted!
Errno::EACCES: Permission denied @ rb_sysopen - /www/wwwroot/api/db/schema.rb
至此,数据库导入成功。
启动应用, 若有权限问题自行调整。
bundle exec unicorn_rails -l 0.0.0.0:3000 -D -E production -c config/unicorn.rb
nginx配置:
- 添加网站直接在宝塔面板操作即可,设置为静态类型。
upstream myapp {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
server
{
#省略若干自动生成的配置
#添加如下配置:
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
try_files $uri @ruby;
}
location @ruby {
proxy_pass http://myapp;
}
}
部署完遇到的问题
访问后台出现静态资源文件404
实际生成的静态资源文件名称不带self
这个问题困扰了好几天,尝试了各种方法。最终按以下方式解决:
删除根目录下的:
- Gemfile.lock 及 yarn.lock
- 删除lock文件后执行命令:bundle exec rake assets:precompile
- 重新执行:bundle install
- 删除项目根目录下tmp文件夹中所有内容
- 重启rails服务
- 修改nginx配置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log /dev/null;
access_log /dev/null;
root /xxx/wwwroot/xxxx/public/;# 静态资源指向项目public目录
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log /dev/null;
access_log /dev/null;
root /xxx/wwwroot/xxx/public/;# 静态资源指向项目public目录
}
若还是未成功部署尝试以下解决方案
- 修改.ruby-version文件 将.ruby-version文件写的ruby版本修改为2.7.3
- 修改rmagic的gem包版本限制,或者装6.x版imagemagick 查看所安装的imagemagic的库的版本:convert -v
如果版本是6.x的,那么可以略过;如果版本是7的,那么需要修改Gem的依赖。修改Gemfile文件:
gem “rmagick”, “~> 2.13.1” 改成: gem “rmagick”#, “~> 2.13.1”
然后执行:bundle update 更新一下依赖
- 启用environments目录下对应的配置
config/environments 这个文件夹下的配置文件需要重命名,要用哪个环境,就改相应的文件。比如产线的,就把production.rb.example改成production.rb。但不建议用产线的配置,看最后的说明
- 注释掉 exception_notification.rb 报错的代码
config/initializers/exception_notification.rb
config.add_notifier :email, { :email_prefix => “[ERROR] “, :sender_address => “”Notifier” <#{Settings.exception_email.first}>”, :exception_recipients => Settings.exception_email }
整段注释掉(段落前面加#)
最后,上面的步骤是按产线的方式跑的,可能会有问题(比如涉及secret、nginx反向代理、预编译资源),那可以考虑用开发模式。 把development.rb.example改名为development.rb,做相应的配置。完成后:
rails db:create
rails db:migrate
rails s
来启动