常用软件安装与 YUM 源配置

本篇汇总 CentOS 7 环境下常用软件(Nginx、搜狗输入法、Docker-CE、MySQL 5.7 等)的安装方法,以及国内 YUM 源的配置;对安装过程中典型的依赖坑(container-selinux 版本不足、libaio.so.1 缺失)按"现象 → 原因 → 解决"结构整理。

一、配置国内 YUM 源

新装系统后,默认的官方源在国内访问较慢,建议替换为阿里云或 163 源,并补充 EPEL 源。

1. 阿里云 Base 源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

2. 163 源

wget -O /etc/yum.repos.d/CentOS-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo

3. EPEL 源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

4. 重建缓存

yum clean all
yum makecache

提示:wget -O 用于将下载的 repo 文件直接写入指定路径,若本机没有 wget,可用 curl -o 替代。


二、Nginx 安装与常用配置

Nginx 通常有 YUM 安装和源码编译两种方式。

1. YUM 源安装(需要 EPEL 源)

yum install -y epel-release
yum install -y nginx

主配置文件位置:

/etc/nginx/nginx.conf

2. 源码编译安装

从官网 http://nginx.org/ 下载源码包,示例版本 1.15.11:

wget http://nginx.org/download/nginx-1.15.11.tar.gz
tar -xvf ./nginx-1.15.11.tar.gz -C /usr/src
cd /usr/src/nginx-1.15.11
yum install -y pcre pcre-devel openssl zlib zlib-devel gcc gcc-c++
./configure
make && make install

源码编译方式的主配置文件位置:

/usr/local/nginx/conf/nginx.conf

3. 开启目录浏览(类似 Apache 的目录列表)

在 server 或 location 段中加入:

autoindex on;               # 是否开启目录浏览
autoindex_exact_size off;   # 关闭精确大小显示(以 KB/MB 等友好单位显示)
autoindex_localtime on;     # 使用服务器本地时区显示文件修改时间

4. 修改网站根目录

在配置文件中将 root 指令改为目标绝对路径:

root  /path/to/your/webroot;

5. Nginx 的典型用途

  • 反向代理
  • 缓存加速
  • 负载均衡(Load Balance)

6. 反向代理

使用 location 下的 proxy_pass:

location / {
    proxy_pass http://被代理服务器IP/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}

proxy_set_header Host $host; 与 X-Forwarded-For 是外网转内网的双网段架构中必须加的两行,用于回传原始 Host 和客户端真实 IP。

7. 网站切分(按路径分发到不同后端)

location /nba/ {
    proxy_pass http://web1IP/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}
location /cba/ {
    proxy_pass http://web2IP/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}

验证(3.3.3.5 为双网卡 Nginx 的外网 IP):

curl http://3.3.3.5/nba/
curl http://3.3.3.5/cba/

8. 动静分离

location ~ \.(html|htm|gif|jpeg|jpg|css|js|png|swf)$ {
    proxy_pass http://192.168.224.11;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}
location ~ \.(php|cgi|txt)$ {
    proxy_pass http://192.168.224.12;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}

验证:

curl http://nginxIP/1.txt
curl http://nginxIP/1.html

观察两次返回内容的差异。

9. 负载均衡

使用 upstream + location。将 upstream 段放在 server 段之前:

upstream backendweb {
    server 192.168.224.11 weight=1 max_fails=2 fail_timeout=1s;
    server 192.168.224.12 weight=1 max_fails=2 fail_timeout=1s;
}

location / {
    proxy_pass http://backendweb;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}

验证:

curl http://3.3.3.5

负载均衡四要素:

  1. 调度算法
  2. 健康检查
  3. 会话保持
  4. 数据一致性(rsync / 共享存储 / 分布式存储)

10. 会话保持(ip_hash)

在 upstream 中加入 ip_hash;:

upstream backendweb {
    ip_hash;
    server 192.168.224.11 weight=1 max_fails=2 fail_timeout=1s;
    server 192.168.224.12 weight=1 max_fails=2 fail_timeout=1s;
}

11. Nginx 调度算法

算法 说明
round robin 轮询,平均分配(默认)
weight 权重,人为分配,适合后端服务器性能不均
fair 按后端响应时间分配,需要第三方模块;后端均在内网时意义不大
url_hash 按访问 URL 的 hash 结果分配,同一 URL 定向到同一后端,多台缓存时可提高命中率
ip_hash 在负载均衡基础上实现会话保持;配置方便但无法完全均衡

12. 配合 Squid 缓存服务器

upstream squid {
    server 3.3.3.4;   # Squid 缓存服务器 IP
}
upstream web {
    server 3.3.3.3;   # LNMP 服务器 IP
}

location ~ .*\.php$ {
    proxy_pass   http://web;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}
location ~ .*\.(html|htm|gif|jpeg|jpg|css|js|png|swf)$ {
    proxy_pass   http://squid;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
    proxy_pass   http://squid;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}

Squid 端需将监听配置改为支持反向代理模式:

http_port 80 accel vhost vport

端口与 Nginx 对应即可(也可使用 3128,但 Nginx 与后续所有对 Squid 的操作端口都要随之改成 3128)。


三、安装搜狗输入法

1. 查找并安装 EPEL 源

yum list *epel*
sudo yum install epel-release.noarch

2. 添加第三方源

sudo yum-config-manager --add-repo=https://copr.fedoraproject.org/coprs/mosquito/myrepo/repo/epel-7/mosquito-myrepo-epel-7.repo

3. 安装搜狗输入法 RPM 包

sudo rpm -ivh sougoupinyin-1.2.0.0056-1.fc22.x86_64.rpm

首次执行会提示缺少依赖,按提示安装 fcitx 相关组件:

sudo yum install fcitx
sudo yum install fcitx-configtool.x86_64

依赖安装完成后,再次执行安装:

sudo rpm -ivh sougoupinyin-1.2.0.0056-1.fc22.x86_64.rpm

4. 切换输入法框架

gsettings set org.gnome.settings-daemon.plugins.keyboard active false
sudo imsettings-switch fcitx

第一条命令若出现错误可忽略,直接执行第二条。

5. 重启生效

init 6
# 或
reboot

重启后打开终端进入输入模式,使用 Shift 即可调出搜狗输入法。


四、安装 Docker-CE(container-selinux 版本坑)

1. 现象(Docker-CE 依赖报错)

按官方步骤安装最新版 docker-ce(旧版称为 docker 或 docker-engine):

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli

出现报错:

错误:软件包:3:docker-ce-18.09.5-3.el7.x86_64 (docker-ce-stable)
          需要:container-selinux >= 2.9

2. 原因(container-selinux 版本过低)

系统自带的 container-selinux 版本低于 Docker-CE 要求的 2.9,源中没有可用的更高版本。

3. 解决(Docker-CE)

手动下载并安装满足要求的 container-selinux RPM 包:

wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.95-2.el7_6.noarch.rpm
rpm -ivh container-selinux-2.95-2.el7_6.noarch.rpm

再重新安装 Docker-CE:

yum install -y docker-ce docker-ce-cli

五、安装 MySQL 5.7(缺 libaio.so.1)

1. 现象(MySQL 5.7 缺 libaio)

在 CentOS 7.0 上使用官方通用二进制包 mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz 安装 MySQL 5.7.19,解压后执行初始化:

/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/www/mysql/data

报错:

error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

原文命令写作 --initailize,正确参数应为 --initialize。

2. 原因(libaio.so.1 未安装)

MySQL 的 mysqld 依赖 Linux 异步 I/O 库 libaio,系统为 minimal 安装时未提供该共享库。

3. 解决(MySQL 5.7)

先核查是否已安装:

rpm -qa | grep libaio

若无输出,安装 libaio-devel(会带上 libaio 主包):

yum install -y libaio-devel

安装完成后重新执行数据库初始化命令,即可成功。

最后修改:2026 年 09 月 20 日
如果觉得我的文章对你有用,请随意赞赏