Eric Guo's blog.cloud-mes.com

Hoping writing JS, Ruby & Rails and Go article, but fallback to DevOps note

Deploy Another Rails 6 App to Existing CentOS 7 Server

Permalink

Prepare the execution files & account

Prepare the execution files & account
adduser sccsa_web
sudo su - sccsa_web
mkdir .ssh
chmod 700 .ssh
vi .ssh/authorized_keys
chmod 600 .ssh/authorized_keys

Install rbenv and ruby-build

Install rbenv and ruby-build
cd # as a sccsa_web
git clone https://github.com/sstephenson/rbenv.git .rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
~/.rbenv/bin/rbenv init
# As an rbenv plugin
mkdir -p "$(rbenv root)"/plugins
# git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
git clone https://gitee.com/Eric-Guo/ruby-build.git "$(rbenv root)"/plugins/ruby-build
# git clone https://github.com/andorchen/rbenv-china-mirror.git "$(rbenv root)"/plugins/rbenv-china-mirror
git clone https://gitee.com/Eric-Guo/rbenv-china-mirror.git "$(rbenv root)"/plugins/rbenv-china-mirror

Install Ruby 3.0.1

Install Ruby 3.0.1
rbenv install -l
rbenv install 3.0.1
rbenv global 3.0.1
rbenv shell 3.0.1
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
echo "gem: --no-document" > ~/.gemrc
# gem install bundler --default -v "1.17.3"
gem install bundler
bundle config build.pg --with-pg-config=/usr/pgsql-13/bin/pg_config

Fix permission for CentOS

Fix permission for CentOS
sudo mkdir /var/www
cd /var/www
sudo mkdir sccsa_web
sudo chown sccsa_web:sccsa_web sccsa_web/

Fix Rake 11.3 and Jekyll 0.12.1 Warning Under Ruby 2.7.1

Permalink

After upgrade ruby to 2.7.1, I found my old octopress give below two warning:

/usr/local/lib/ruby/gems/2.7.0/gems/rake-11.3.0/lib/rake/application.rb:378: warning: deprecated Object#=~ is called on Proc; it always returns nil

Just need change application.rb:378 as below to fix it.

Guard Rake option parsing against non-string values
- opt.select { |o| o =~ /^-/ }.map(&:downcase).sort.reverse
+ opt.select { |o| o.is_a?(String) && o =~ /^-/ }.map(&:downcase).sort.reverse

Another warning is:

/usr/local/lib/ruby/gems/2.7.0/gems/jekyll-0.12.1/lib/jekyll/post.rb:140: warning: URI.escape is obsolete

Just change as below:

Replace deprecated URI.escape in category links
- "categories" => categories.map { |c| URI.escape(c) }.join('/'),
+ "categories" => categories.map { |c| URI.encode_www_form_component(c) }.join('/'),

Fix Puma Failed to Restart in Ruby 2.7.1

Permalink

After install ruby 2.7.1, found capistrano deploy via puma having below error when restart:

Bundler LoadError when restarting Puma on Ruby 2.7.1
/home/web_site/.rbenv/versions/2.7.1/bin/bundle:23:in `load': cannot load such file -- /home/web_site/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/libexec/bundle (LoadError)

After checking long time found it's just not install the bundler 1.17.3 as pumactl relay on it.

Fix puma failed to restart in ruby 2.7.1
rbenv uninstall 2.6.5
rbenv global 2.7.1
rbenv shell 2.7.1
gem install bundler --default -v "1.17.3"
gem install bundler

Notes, still need after capistrano-bundler release 2.0.

Install Snell-server in CentOS 7

Permalink

Prepare the execution files & account

Prepare the execution files & account
adduser snell
sudo su - snell
mkdir .ssh
chmod 700 .ssh
vi .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
# see other version at https://kb.nssurge.com/surge-knowledge-base/release-notes/snell
wget https://dl.nssurge.com/snell/snell-server-v5.0.1-linux-amd64.zip
unzip snell-server-v5.0.1-linux-amd64.zip
mkdir snell
mv snell-server snell
rm snell-server-v5.0.1-linux-amd64.zip
cd snell
./snell-server # generate conf file

Configure the system auto start

Open the Snell systemd service file
vi /etc/systemd/system/snell.service
Snell systemd service definition
[Unit]
Description=Snell Proxy Service
After=network.target
[Service]
Type=simple
User=snell
Group=snell
LimitNOFILE=32768
ExecStart=/home/snell/snell/snell-server -c /home/snell/snell/snell-server.conf
[Install]
WantedBy=multi-user.target

Add port

Add port
sudo firewall-cmd --zone=public --add-port=11666/tcp
sudo firewall-cmd --zone=public --add-port=11666/tcp --permanent
sudo firewall-cmd --zone=public --add-port=11666/udp
sudo firewall-cmd --zone=public --add-port=11666/udp --permanent
sudo firewall-cmd --reload

Restart or run below

Reload systemd and start Snell
systemctl daemon-reload
systemctl start snell
systemctl status snell
systemctl restart snell
systemctl enable snell
cat /home/snell/snell/snell-server.conf

Ubuntu 18.04 Ruby on Rails Installation Log

Permalink

Enable the Firewall

Enable the Firewall
ufw app list
ufw allow ssh
ufw enable
ufw status

Install node.js

Install node.js
apt install nodejs
apt install npm
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
bash nodesource_setup.sh
apt-get install -y nodejs
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
apt update
apt install yarn

Install rbenv

Install rbenv
apt update
apt install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev
apt install git
logout # as ubuntu
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
type rbenv
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
git clone https://github.com/andorchen/rbenv-china-mirror.git "$(rbenv root)"/plugins/rbenv-china-mirror
rbenv install -l

Install ruby 2.6.5

Install ruby 2.6.5
rbenv install 2.6.5
rbenv global 2.6.5
echo "gem: --no-document" > ~/.gemrc
gem install bundler
gem install rails
rbenv rehash

Install postgresql

Install postgresql
apt-get install postgresql
apt-get install postgresql-server-dev-all
sudo su - postgres
createuser ubuntu --pwprompt
psql
ALTER ROLE ubuntu LOGIN;
CREATE DATABASE cam_price_prod WITH ENCODING='UTF8' OWNER=ubuntu;

Install nginx

Install nginx
apt-get install nginx

Generate ssh key

Generate ssh key
ssh-keygen
sudo mkdir /var/www
cd /var/www
sudo mkdir cam_price
sudo chown ubuntu:ubuntu cam_price/

Install HTTPS

Largely following certbot guide

Install HTTPS
apt-get update
apt-get install software-properties-common
add-apt-repository universe
add-apt-repository ppa:certbot/certbot
apt-get update
apt-get install certbot python-certbot-nginx

Running Rails 6 App With Sqlite3 on CentOS 7

Permalink

Running CentOS 7 as operation system for Rails is very stable, but such stability comes with stagnation, for example, the sqlite3 version is 3.7 instead of 3.8, so cause Rails 6 refused to run in CentOS 7 withouth install a third party sqlite3 version.

So a quickly fix is install atomic sqlite and setting the correct build options.

Configure the sqlite3 gem to use Atomic SQLite
$HOME/.rbenv/bin/rbenv exec bundle config build.sqlite3 "--with-sqlite3-include=/opt/atomic/atomic-sqlite/root/usr/include --with-sqlite3-lib=/opt/atomic/atomic-sqlite/root/usr/lib64 --with-sqlite3-dir=/opt/atomic/atomic-sqlite/root/usr"

Maybe also need to do a patch but any way, I successfully upgrade to Rails 6.

My Maintain Mysql Commands

Permalink

My maintain mysql commands
mysql> SHOW FULL PROCESSLIST;
mysql> SELECT * FROM information_schema.innodb_trx;
mysql> kill xxx;
mysql> SHOW GRANTS FOR cad_reader;

List all table size.

List all table size.
SELECT
TABLE_NAME AS `Table`,
ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024),2) AS `Size (MB)`
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = "cybros_dev"
ORDER BY
(DATA_LENGTH + INDEX_LENGTH)
DESC;

Find table name.

Find table name.
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_name LIKE '%contract%';

IE 11 Iframe Weird X-Frame-Options but Working Setting

Permalink

After research 1 hours, I answered in the SO

After research 1 hours, I answered in the SO
def cors_set_access_control_headers
headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Methods"] = "GET"
headers["Access-Control-Request-Method"] = "*"
headers["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Accept, Authorization"
headers["X-Frame-Options"] = "ALLOW-FROM http://172.16.1.159"
headers["X-XSS-Protection"] = "0"
end

Prepare the 16 kHz Monaural MP3 for Wechat API Speach to Text Interface

Permalink

Prepare the 16 kHz Monaural MP3 for Wechat API speach to text interface
ffmpeg -i test_voice.amr -acodec mp3 -ac 1 -ar 16000 test_voice.mp3

微信AI开放接口, require that interface.

How to install in CentOS 7 from source code

Build the LAME MP3 encoder on CentOS 7
yum install nasm
cd /usr/local/src
wget http://downloads.sourceforge.net/lame/lame-3.100.tar.gz
tar -zxvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix=/usr/local
make && make install
ln -s /usr/local/lib/libmp3lame.so.0.0.0 /usr/lib64/libmp3lame.so.0
wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
tar -jxvf ffmpeg-snapshot.tar.bz2
cd ffmpeg
./configure --prefix=/usr/local --enable-libmp3lame
make && make install
ffmpeg

Original post