在此记录一个从 0 开始的配置过程,其中所有源的配置基本都是因为在国内,如果服务器在国外,所有源相关的配置都不再需要。
掘金的 Markdown 编辑器有问题,列表层级多了 + 多个代码块就出现错误,所以有些层级错误,懒得改了,将就看吧。
步骤
配置 yum 源
- 默认环境没有 wget,所以要使用 xftp 将源配置文件传输到服务器;
- 在传输文件之前,先将服务器原本的源配置文件备份:
cd /etc/yum.repos.dmkdir bakmv *.repo bak
- 在本地下载:
wget -O Centos-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo,然后将其传输到服务器; - 更新 yum 缓存:
yum clean all && yum makecache - 安装 wget:
yum install -y wget - 安装 epel 源:
yum install -y epel-release - 下载阿里开源镜像的 epel 源文件:
wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo - 再次执行上面的更新 yum 缓存命令。
- 更新已安装的包:
yum update,在更新完后要把/etc/yum/repos.d/CentOS-Base.repo删除(保留Centos-Base.repo)
安装 vim
yum install -y vim*- 更改一些配置:
vim /etc/vimrc
set nu # 显示行号
set showmode # 在命令行界面最下面显示当前模式
set ruler # 显示光标所在的行数等
set autoindent # 回车后,光标移动到下一行且与上一行的起始字符对齐
syntax on # 语法检测
安装 nginx
- 配置 nginx 源:
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm /etc/yum.repos.d下应该会多出一个 nginx.repo 文件yum install -y nginxsystemctl start nginx && systemctl enable nginx
安装 node v20.10.0
- 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash,如果遇到网络问题,那么要配置/etc/hosts,能不能解决问题看天- 查看可安装的 node 版本:
NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/ nvm ls-remote- 在这里必须指定 nodejs.org 的镜像,否则会遇到网络问题
- 安装 node:
NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node/ nvm install 20.10.0
- 使用
node -v可能会输出错误,因为 node 的版本较高,需要的一些基础库也要升级版本,错误类似下面:
node: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by node)
node: /lib64/libc.so.6: version `GLIBC_2.25' not found (required by node)
node: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by node)
node: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by node)
- 为了解决以上问题:
- 先解决一些 yum 包的问题,先把这条命令
yum list installed | grep "scl"输出的包删除:yum remove centos-release-scl.noarch centos-release-scl-rh.noarch - 重新安装,目的是让其自动生成
CentOS-SCLo-scl.repo和CentOS-SCLo-scl-rh.repo文件:yum install -y centos-release-scl centos-release-scl-rh - 分别更改这两个文件的配置如下(CentOS-SCLo-scl.repo 和 CentOS-SCLo-scl-rh.repo):
- 先解决一些 yum 包的问题,先把这条命令
[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
baseurl=https://mirrors.aliyun.com/centos/7/sclo/$basearch/sclo/ # 这里
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
和
[centos-sclo-rh]
name=CentOS-7 - SCLo rh
baseurl=https://mirrors.aliyun.com/centos/7/sclo/$basearch/rh/ # 这里
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
- 重新生成缓存:
yum clean all && yum makecache - 然后解决 glibc 问题
cd /home
wget http://ftp.gnu.org/gnu/glibc/glibc-2.28.tar.gz
tar xf glibc-2.28.tar.gz
cd glibc-2.28/ && mkdir build && cd build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
- 这一步也会出现问题,报错:
These critical programs are missing or too old: make bison compiler,需要升级这些包
# 升级 gcc
yum install -y devtoolset-8-gcc*
mv /usr/bin/gcc /usr/bin/gcc-4.8.5
ln -s /opt/rh/devtoolset-8/root/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++-4.8.5
ln -s /opt/rh/devtoolset-8/root/bin/g++ /usr/bin/g++
scl enable devtoolset-8 bash
# 升级 make
cd /home
wget http://ftp.gnu.org/gnu/make/make-4.3.tar.gz
tar -xzvf make-4.3.tar.gz && cd make-4.3/
./configure --prefix=/usr/local/make
make && make install
cd /usr/bin/ && mv make make.bak
ln -sv /usr/local/make/bin/make /usr/bin/make
# 如果没有安装 bison
yum install -y bison
- 然后继续之前的一步:
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin,现在可能会报如下的错误:
- Did you change the gcc specs file (necessary after upgrading from
Linux libc5)?
- Are there any symbolic links of the form libXXX.so to old libraries?
Links like libm.so -> libm.so.5 (where libm.so.5 is an old library) are wrong,
libm.so should point to the newly installed glibc file - and there should be
only one such link (check e.g. /lib and /usr/lib)
You should restart this script from your build directory after you've
fixed all problems!
Btw. the script doesn't work if you're installing GNU libc not as your
primary library!
该错误可忽略。
make && make install- 使用
node -v,可能会继续报错,如下:
node: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by node)
- 使用
strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX查看输出,应该是没有 GLIBCXX_3.4.20 的,继续修复
cd /home/glibc-2.28/build
make all
yum whatprovides libstdc++.so.6
yum update -y libstdc++.x86_64
wget http://www.vuln.cn/wp-content/uploads/2019/08/libstdc.so_.6.0.26.zip
unzip libstdc.so_.6.0.26.zip
cp libstdc++.so.6.0.26 /lib64/
cd /lib64
cp libstdc++.so.6 libstdc++.so.6.bak
rm -f libstdc++.so.6
ln -s libstdc++.so.6.0.26 libstdc++.so.6
# 再次输出应该有了 GLIBCXX_3.4.20
strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
- 安装 node 完成
- 设置 npm 源:
npm config set registry https://registry.npmmirror.com
安装 Docker & Docker Compose
Docker
参考官方文档:CentOS | Docker Docs
Docker Compose
参考官方文档:Install Compose standalone | Docker Docs
配置虚拟内存
根据实际情况,我自己购买的服务器内存都较小,有些程序容易爆内存,导致程序挂掉。所以我一般都会配置虚拟内存,避免程序经常挂。
swapon --show
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
swapon --show
此时使用 glances 可以看到 swap 有了。接下来设置系统重启时 swap 仍然生效:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo sysctl vm.swappiness=60
echo 'vm.swappiness=60' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p