Aliyun Linux 2上部署环境,遇到了几个问题。记录如下。

安装Postgresql 11 client

官方Guide

在RHEL 7系列上安装方法为:

1
2
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install postgresql11

Aliyun Linux 2安装问题

但该方法在Alinux Linux 2上没法成功安装,因为官方源依赖于yum的变量$releasever,$basearch。在Aliyun Linux 2中,这些变量和RHEL和CentOS上不相同。导致没法直接用官方rpm yum包来安装postgresql11。

解决方法

在安装完pgdg-redhat-all.repo过后,可以手动来修改yum源,Aliyun Linux 2的官方描述中说Aliyun Linux 2兼容CentOS 7。使用下列命令修改/etc/yum.repos.d/pgdg-redhat-all.repo中的baseurl为具体的RHEL 7的地址即可。

1
sed -i.bak 's/$releasever/7/g' /etc/yum.repos.d/pgdg-redhat-all.repo

然后再yum install postgresql11即可安装。

安装高版本Node.js

安装高版本的原因

Aliyun Linux 2中的Node.js版本和CentOS 7的版本一样,都是6.x版本。但现在很多npm包都需要8.x以上版本。 因此需要手动安装高版本Node.js。

安装问题

安装nodejs多种方法,可以源码编译,也可以使用nvm。不过我比较偏爱的方式是从NodeSource Repository中安装。
以nodejs 10.x为例,正常安装方法是

1
2
curl –sL https://rpm.nodesource.com/setup_10.x | sudo bash -
sudo yum install –y nodejs

这个步骤在CentOS 7.x和Amazon Linux上都可以work, 但是在Aliyun Linux 2上会提示系统版本不兼容。

1
## You don't appear to be running a supported version of Enterprise Linux. Please contact NodeSource at https://github.com/nodesource/distributions/issues if you think this is incorrect or would like your architecture to be considered for support. Include your 'distribution package' name: alinux-release-2.1903-4.al7.x86_64.

原因调查

下载了setup_10.x的源码来进行查看,发现有这么一段检查OS的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
DISTRO_PKG=$(rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release)
...
if [[ $DISTRO_PKG =~ ^(redhat|centos|cloudlinux|sl)- ]]; then
DIST_TYPE=el
elif [[ $DISTRO_PKG =~ ^(enterprise|system)-release- ]]; then # Oracle Linux & Amazon Linux
DIST_TYPE=el
elif [[ $DISTRO_PKG =~ ^(fedora|korora)- ]]; then
DIST_TYPE=fc
else
print_status "\
You don't appear to be running a supported version of Enterprise Linux. \
Please contact NodeSource at \
https://github.com/nodesource/distributions/issues if you think this is \
incorrect or would like your architecture to be considered for support. \
Include your 'distribution package' name: ${DISTRO_PKG}. \
"
exit 1
fi

哈哈,从逻辑上看,setup_10.x的脚本能识别Oracle Linux和Amazon Linux,但是貌似并不支持Aliyun Linux。

解决办法

简单修改了下,让脚本将Aliyun Linux识别为el 7。

1
2
sed -i.bak 's/\^(enterprise|system)-release-/\^(enterprise|system|alinux)-release-/g' setup_10.x
sed -i 's/\$DISTRO_PKG =~ \^system-release/\$DISTRO_PKG =~ \^(system|alinux)-release/g' setup_10.x

再进行安装,就能够work了。

1
2
sudo bash setup_10.x
yum install nodejs

额外延伸

获取yum变量的小命令

可以使用如下python命令来获取yum的变量

1
python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)'

下面是该命令在Aliyun Linux 2,Amazon Linux 2CentOS 7.6上的输出。

Aliyun Linux 2的结果

1
2
3
4
5
6
7
$ python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)'
Loaded plugins: fastestmirror
{'arch': 'ia32e',
'basearch': 'x86_64',
'releasever': '2.1903',
'uuid': '33f759f5-349a-4c14-bb77-3ccd32b7a675'}
$

Amazon Linux 2上的运行结果

1
2
3
4
5
6
7
8
9
10
11
$ python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)'
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
{'arch': 'ia32e',
'awsdomain': 'amazonaws.com',
'awsregion': 'us-west-2',
'basearch': 'x86_64',
'product': 'core',
'releasever': '2',
'target': 'latest',
'uuid': '3f6395c1-b6c3-45e1-b7e4-aeaa76bf67a6'}
$

CentOS 7.6上的运行结果

1
2
3
4
5
6
7
8
9
$ python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)'
Loaded plugins: fastestmirror
{'arch': 'ia32e',
'basearch': 'x86_64',
'contentdir': 'centos',
'infra': 'stock',
'releasever': '7',
'uuid': '679cafd0-f0f5-4bc4-9055-eddca793d302'}
$

各系统redhat-release的信息

命令rpm -q --whatprovides redhat-release在各个系统Aliyun Linux 2,Amazon Linux 2CentOS 7.6上的输出。

Aliyun Linux 2的结果

1
2
3
$ rpm -q --whatprovides redhat-release
alinux-release-2.1903-4.al7.x86_64
$

Amazon Linux 2上的运行结果

1
2
3
$ rpm -q --whatprovides redhat-release
system-release-2-11.amzn2.x86_64
$

CentOS 7.6上的运行结果

1
2
3
$ rpm -q --whatprovides redhat-release
centos-release-7-6.1810.2.el7.centos.x86_64
$

安装Postgresql 11 Server

按照上文所述,添加pgdg的repository。
安装Server包

1
yum install -y postgresql11-server

初始化数据库和开启开机启动。

1
2
3
/usr/pgsql-11/bin/postgresql-11-setup initdb
systemctl enable postgresql-11.service
systemctl start postgresql-11.service

根据实际需要,修改绑定地址
打开/var/lib/pgsql/11/data/postgresql.conf, 找到listen_address, 进行修改。

1
listen_address = '*' # defaults to 'localhost'; use '*' for all

修改postgres账号默认密码

1
2
3
4
$ su - postgres
postgresql=$ psql -c "alter user postgres with password 'password'"
ALTER ROLE
postgresql=$

修改/var/lib/pgsql/11/data/pg_hba.conf, 添加如下允许任意地址访问或修改为实际允许访问的地址。

1
2
# Accept from anywhere
host all all 0.0.0.0/0 md5

感悟

写一个或者定制一个系统,已经是一件不容易的事情了。需要构建一个生态,就更加地不容易。
查了下nodesource最新的setup_14.x脚本, 发现还是没支持Aliyun自定制的Linux。
为了方便以后可能还要用到,每次手动改脚本比较麻烦。就fork了一份nodesource/distributions到自己的Github上jibing57/distributions, 研究了一下,添加了Aliyun Linux 2的支持。commit - Add Aliyun Linux 2 support
以后要安装时,可以直接从自己的Github上拿setup脚本安装。

1
2
curl -sL 'https://raw.githubusercontent.com/jibing57/distributions/master/rpm/setup_10.x' | sudo bash -
sudo yum install –y nodejs

添加完后,顺带也给nodesource/distributions提了个pull request, 不知道会不会被采纳。

Reference

留言