手动部署一个ror7 on ubuntu22的服务器+posggresql+puma+nginx


写在前面:
一、bundle install 有点慢, aliyun和ruby china的源似乎都不给力。清华的Rubygem还挺快。
使用以下命令替换 gems 默认源

# 添加 TUNA 源并移除默认源
gem sources --add https://mirrors.tuna.tsinghua.edu.cn/rubygems/ --remove https://rubygems.org/
# 列出已有源
gem sources -l
# 应该只有 TUNA 一个

或者,编辑 ~/.gemrc,将 https://mirrors.tuna.tsinghua.edu.cn/rubygems/ 加到 sources 字段。



bundler
使用以下命令替换 bundler 默认源 bash 
在  bundle install  / bundle update 前运行。

bundle config mirror.https://rubygems.org https://mirrors.tuna.tsinghua.edu.cn/rubygems
或者
 bundle config mirror.https://rubygems.org  "https://mirrors.cloud.tencent.com/rubygems/"



rvm 安装ruby,也可以更换源。
echo "ruby_url=https://cache.ruby-china.com/pub/ruby" > ~/.rvm/user/db


二、服务器用到htmlToPdf
gem wicked_pdf
gem wkhtmltopdf-binary

/data/www/ji/shared/bundle/ruby/3.3.0/gems/wkhtmltopdf-binary-0.12.6.7/bin/wkhtmltopdf_ubuntu_22.04_amd64: 
error while loading shared libraries: libXrender.so.1: cannot open shared object file: No such file or directory
sudo apt-get install libxrender1


正文:
下面是一个大概的流程。

1. adduser hw
2. usermod -aG sudo hw     
sudo usermod -aG sudo username

3.install rvm    
sudo apt update
sudo apt install curl gcc make libssl-dev libreadline-dev zlib1g-dev libsqlite3-dev
sudo apt install gnupg2
gpg2 --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

\curl -sSL https://get.rvm.io | bash -s stable

source ~/.rvm/scripts/rvm


4. rvm install 3.3.4  # 3.3.4是当前最新版本。3.3.3 有个bug, gem install net-pop 失败的问题。

5.config/deploy.rb 里面的一些不相关设置删除比如,rvm版本,puma的pid等,设置用户等信息。

6.cap staging deploy # 尝试运行,发现错误,修复问题,比如 复制master.key 和database.yml到服务器共享目录等。staging的环境文件,database.yml里面的配置文件等。

7.cap staging deploy # 运行到bundle:install ,时候出错。安装 gem install pg 出错。
8. sudo apt install libpq-dev
    这里提示 currently running kernel version is 5.15.0-113-generic is  not the expected kernel version 5.15.0-117-generic.  restarting the system to load the new kernel will not be handled automatically , so you should consider rebooting.

9.  deploy:assets:precomplie  error: ExecJS::RuntimeUnavailable: Could not find a JavsScript runtime.
 install node.js 安装服务器的js运行环境:https://deb.nodesource.com/
curl -fsSL https://deb.nodesource.com/setup_21.x | sudo bash -
sudo apt-get install -y nodejs

10. deploy:migrating
      01 ~/.rvm/bin/rvm default do bundle exec rake db:migrate
      数据库还没有安装,所以那啥,这一步肯定出错了。
11 安装pg
     https://www.postgresql.org/download/linux/ubuntu/

# Import the repository signing key:
sudo apt install curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc

# Create the repository configuration file:
sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Update the package lists:
sudo apt update

# Install the latest version of PostgreSQL:
# If you want a specific version, use 'postgresql-16' or similar instead of 'postgresql'
sudo apt -y install postgresql

12 根据database.yml里面的信息,配置pg的账号和密码。
sudo systemctl start postgresql
这里安装的是pg 16 ,默认的端口已经改成了 5433
需要通过命令加端口号才能进入:  
sudo -u postgres  psql -p 5433

create user deploy with password '888888';
postgres=# alter user deploy with createdb;
ALTER ROLE
postgres=# \du
                             List of roles
 Role name |                         Attributes                         
-----------+------------------------------------------------------------
 deploy    | Create DB
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS

这里还有个问题,因为用cap staging deploy 是不会帮你创建数据库的,因为cap认为,创建数据库是数据库管理员的职责,那么我们就需要手动创建数据库了。

create database lehazi_staging owner deploy;


因为 pg_hba.conf 里面哟个  local all all peer 的配置,也就是说,系统用户和数据库用户名 相同,就可以通过 peer进入系统,不需要验证密码。
这里我系统账号用的是 hw, 然后我可以这样修改以下。
postgres=# create user hw with createdb;
postgres=# grant  all privileges on database  lehazi_staging to hw;
postgres=#alter database lehazi_staging owner hw;

本地测试环境下,也可以通过类似下列这种方式创建数据库, 尤其注意这里的 指定环境放在最后。
~/.rvm/bin/rvm default do bundle exec rake db:create RAILS_ENV=staging


14上面的问题 也是可以修改postgresql的配置文件,修改后要重启服务。

sudo systemctl restart postgresql.service


14  puma:restart
      01 sudo /bin/systemctl restart puma_ijitieca_staging
      01 sudo
      01 :
      01 a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
      01
      01 sudo
      01 :
      01 a password is required

可以尝试使用sudo命令来启动服务,或者将当前用户添加到sudoers文件中。 sudo vim /etc/sudoers 
在文件的末尾,加上下面这一行 ,hw是你的sudo的用户名。
hw ALL=(ALL:ALL) NOPASSWD: ALL

这里都还没有配置puma 服务。。。,下面来配置puma服务。 将 puma.service 放到 /etc/systemed / .

# for puma 6.4.2
cap puma:install                      # Install Puma systemd service


15. 最后安装和配置nginx。
https://nginx.org/en/linux_packages.html#Ubuntu

配置好 你的nginx.conf ,这里是通过sock 通信的,可以通过命令查看你的sock文件放在哪里。
ps auxf|grep puma
hw         44761  0.0  0.2   6480  2388 pts/0    S+   09:02   0:00  |           \_ grep --color=auto puma
hw         44538 40.0  4.0 492364 40064 ?        Rsl  09:02   0:00 puma 6.4.2 (unix:///data/www/ijitieca_staging/shared/tmp/sockets/puma.sock)

到此基本结束
如果发现错误,比如 
curl -i  ip
HTTP/1.1 502 Bad Gateway
Server: nginx/1.26.1
Date: Fri, 26 Jul 2024 09:23:02 GMT
Content-Type: text/html
Content-Length: 157
Connection: keep-alive

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.26.1</center>
</body>
</html>


各个服务都起来之后,如果发现错误,先从 nginx 错误日志-再看puma的错误日志。顺藤莫瓜,找到问题。
阅读量: 323
发布于:
修改于: