此篇文章以后随缘更新,这个系统的相关问题网上一般都有答案,浪子个人已不再使用 CentOS 系统。

一、CentOS7最小安装

1、配置网卡

先进行网络的连接,编辑网络配置文件(vi /etc/sysconfig/network-scripts/ifcfg-ens32),不同的机器最后的文件名称可能不同,一般都是 ifcfg- 开头。

1
2
3
4
5
6
7
# 空着的部分自定义即可
BOOTPROTO=static
ONBOOT=yes
IPADDR=
NETMASK=
GATEWAY=
DNS1=

设置完成后,保存退出,使用命令 systemctl restart network 重启网卡。

2、关闭防火墙以及 Linux 的一些安全策略

1
2
3
4
5
6
systemctl stop firewalld
systemctl disable firewalld

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

setenforce 0

3、配置本地 yum 源

1
2
3
4
5
6
7
8
# 进入目录
cd /etc/yum.repos.d/
# 创建 备份 文件夹
mkdir bak
# 移动该目录下的所有文件到备份文件夹
mv * bak
# 拷贝一份文件进行编辑
cp bak/CentOS-Media.repo /etc/yum.repos.d/CentOS-Media.repo

编辑刚才我们拷贝的文件:vi CentOS-Media.repo,这就是安装软件时读取的安装源配置,加入以下内容,先使用本地镜像安装。

1
2
3
4
5
[linux]
name=linux
baseurl=file:///media/
gpgcheck=0
enabled=1

清除yum缓存:yum -y clean all
重建yum缓存:yum makecache

4、安装常用工具

yum -y install curl telnet vim wget lrzsz net-tools

修改vim配置(可以不修改,按照默认的即可,这里仅仅是偏好)

1
2
3
4
5
6
7
8
9
10
vim ~/.vimrc
set encoding=utf-8 " 文件编码
set number " 显示行号
set tabstop=4 " tab宽度为4
set softtabstop=4 " 设置一次可以删除4个空格
set expandtab " tab转换为空格
set nowrap " 不自动换行
set showmatch " 显示括号配对情

syntax on " 开启语法高亮

5、安装依赖关系

yum -y install gcc gcc-c++ make autoconf wget lrzsz libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel libxslt-devel libevent-devel libtool libtool-ltdl bison gd gd-devel vim-enhanced pcre-devel zip unzip ntpdate sysstat patch bc expect rsync

6、修改yum源

1
2
3
4
5
6
7
8
9
10
# 复制文件
cp /etc/yum.repos.d/bak/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
# 进入目录
cd /etc/yum.repos.d/
# 添加 网易 的下载源
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
# 重建缓存
yum makecache
# 更新源
yum -y update

如果不想使用 wget http://mirrors.163.com/.help/CentOS6-Base-163.repo 的话,可以自己编辑:/etc/yum.repos.d/CentOS-Base.repo 这个文件(以下是用的清华源)。编辑完成之后更新库,这时需要网络。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

7. rpm命令

1
2
3
4
5
6
7
8
# 安装
rpm -ivh xxx.rpm
# 卸载
rpm -evh xxx.rpm
# 更新
rpm -Uvh xxx.rpm
# 显示所有已安装软件
rpm -qa

8. lrzsz:文件上传下载

1
2
3
4
5
6
# 安装
yum install -y lrzsz
# 下载
sz
# 上传
rz

二、部署服务

1. Redis

1
2
3
4
5
6
# 安装编译 redis 需要的工具
sudo yum -y install gcc automake autoconf libtool make
# 进入解压目录,进行编译,直到编译完成
make MALLOC=libc
# 安装到指定路径
make install PREFIX=/usr/local/redis

Redis 的默认的配置文件在源码解压后的目录中

2. RabbitMQ

RabbitMQ 是使用 Erlang 语言编写的中间件,联想一下 Java,我们可以猜到它需要先搭建 Erlang 环境。主要是这个环境需要编译源码,RabbitMQ 本身官网提供了二进制压缩包。

一、Erlang环境搭建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 安装编译 Erlang 的相关依赖
yum install gcc glibc-devel make ncurses-devel openssl-devel xmlto
# 解压源码包
tar -zxvf otp_src_24.1.7.tar.gz

cd otp_src_24.1.7/
# 指定安装目录
./configure --prefix=/usr/local/erlang
# 编译安装
make && make install
# 测试安装是否成功:
cd /usr/local/erlang/bin/
./erl

# 安装成功后配置环境变量 vim /etc/profile
export PATH=$PATH:/usr/local/erlang/bin

# 添加完成保存退出,刷新使其生效
source /etc/profile

二、安装Rabbitmq

通过第一步,我们就搭建好了 Erlang 环境,接下来就是安装 RabbitMQ 了,这个还是比较简单的,因为它已经编译好了,我们可以下载直接配置,无需编译。

1
2
3
4
5
6
7
8
9
# 解压
tar -Jxvf rabbitmq-server-generic-unix-3.9.11.tar.xz
# 移动
mv rabbitmq_server-3.9.11 /usr/local/rabbitmq

# 添加环境变量:vim /etc/profile
export PATH=$PATH:/usr/local/rabbitmq/sbin
# 刷新变量
source /etc/profile

3. Python

Linux 下基本不需要配置 Python 的环境,有个别的 ISO 镜像版本比较老,比如 CentOS7 的 mini ISO 镜像是2.x的,我们可以通过各大系统的包管理工具进行安装,也可以自己通过源码编译安装。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 安装相关工具和依赖
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel

# 解压压缩包
tar -zxvf Python-3.10.2.tgz
# 进入文件夹
cd Python-3.10.2

# 配置安装位置
./configure prefix=/usr/local/python3

# 编译并安装
make && make install

# 使用 python3 验证是否安装成功
python3 -V

#添加python3的软链接
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3

#添加 pip3 的软链接
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3

4. Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
# 解压
tar -zvxf nginx-1.20.2.tar.gz
# 配置安装路径
./configure --prefix=/usr/local/nginx
# 编译安装
make && make install
# 进入安装目录查看是否安装成功
cd /usr/local/nginx

# 启动 停止 重启
./nginx start
./nginx -s stop
./nginx -s reload

5. Btop++

Btop++ 是一个 Linux 资源监视器,显示处理器、内存、磁盘、网络和进程的使用情况和统计资料,界面美观,使用简单。这里使用了源码编译安装,但是浪子推荐下载 Github 仓库的二进制包解压运行 install.sh 脚本安装。

1
2
3
4
5
6
7
8
9
10
11
# 安装、升级相关依赖工具
yum install coreutils sed build-essential -y
yum install centos-release-scl -y
yum install devtoolset-10 -y
scl enable devtoolset-10 bash
echo "source /opt/rh/devtoolset-10/enable" >> /etc/profile

# 克隆源码编译安装
git clone https://gitee.com/mirrors/btop.git
cd btop
make && make install

6. FFmpeg

1
2
3
4
5
6
7
8
9
10
11
12
13
# 克隆源码 也可以下载 https://github.com/FFmpeg/FFmpeg/releases 相应的包上传
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg

# 安装相关依赖
yum install yasm.x86_64 -y

cd ffmpeg
./configure --enable-ffplay --enable-ffserver --prefix=/usr/local/ffmpeg
# 编译安装
make && make install

# 查看是否安装成功
cd /usr/local/ffmpeg

7. MySQL(解压版)

yum 方式安装:https://zhuanlan.zhihu.com/p/87069388

使用通用的 MySQL8.x 版本的二进制压缩包进行安装。至于卸载,就把有关 MySQL 创建的几个文件夹删掉就行了,/etc/my.cnf 是默认自带的,卸载的时候删不删都没有问题,如果默认没有这个文件也不必担心,可以手动添加。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 检查mysql用户组和用户是否存在,如果没有,则创建
cat /etc/group | grep mysql
cat /etc/passwd | grep mysql
# 创建 mysql 组
groupadd mysql
# 新建 mysql 用户并加入 mysql 群组
useradd -r -g mysql mysql

# 安装所需依赖(需要安装 libaio-devel.x86_64 numactl 这两个依赖)
yum -y install libaio-devel.x86_64 numactl

# 解压二进制文件包到 /usr/local/mysql 目录
tar xxx -C /usr/local/mysql
cd /usr/local/mysql/bin
# 初始化数据,成功初始化后需要记录最后 root@localhost: 后的字符串(初始化失败则不显示),它是后面进入 bash 环境的初始密码
./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql

# vim /etc/my.cnf(没有该文件手动创建) 修改内容
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306

# 启动MySQL服务 启动成功会有 Starting MySQL.. SUCCESS! 提示;否则就是启动失败,根据提示查看日志记录定位问题
cd /usr/local/mysql/support-files/
mysql.server start

# 添加软链接并重启服务
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
service mysql restart
# systemctl restart mysql

# 添加开机自启
# 1、将服务文件拷贝到 init.d 下,并重命名为 mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 2、赋予可执行权限
chmod +x /etc/init.d/mysqld
# 3、添加服务
chkconfig --add mysqld
# 4、显示服务列表
chkconfig --list

至此,安装任务基本完成,下面需要添加用户并分配权限,进入 MySQL 的 bash 环境需要之前进行初始化时生成的密码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 登录 MySQL,密码使用初始化成功时 root@localhost: 后的字符串
mysql -u root -p

# 修改密码 毕竟那么不好记
# 而且如果不修改 它会报 ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. 的错误
alter user root@'localhost' identified by 'newpassword';
flush privileges;

# 开放远程连接
use mysql;
# ① MySQL 8.0 之前的版本,password 为自己设置的密码
grant all privileges on *.* to 'root'@'%' identified by 'password';

# ② 适用于 MySQL 8.0 之后的版本,推荐先创建一个用户 dev,再进行授权
# 直接授权给 root
# grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;
create user dev@'%' identified by '123456';
grant select,update,delete,insert on *.* to dev@'%' with grant option;

# 刷新权限,这一句很重要,使修改生效,如果没有写,则还是不能进行远程连接。这句表示从mysql数据库的grant表中重新加载权限数据,因为MySQL把权限都放在了cache中,所以,做完修改后需要重新加载。
flush privileges;

初始化成功截图:
初始化成功截图

记录日志最末尾位置 root@localhost: 后的字符串,此字符串为mysql管理员临时登录密码。

问题一:密码正确但是进不去 bash 环境

1
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES/NO) 

解决方法:

  1. 使用 kill 命令停止 mysqld 相关服务
  2. cd /usr/local/mysql/bin/,运行命令: mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
  3. 使用密码登录数据库 mysql -u root -p 并切换到 mysql 数据库
  4. 执行命令:update user set host='%' where user='root';

问题二:预读处理

1
2
3
mysql> use dbname;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

这个问题是之前使用的 apt-get 管理工具安装的 MySQL 出现的,原因是因爲數據庫採用了預讀處理。解决办法就是在我们进入MySQL的bash环境时,需要加入 -A 参数,不让其预读数据库信息,mysql -u root -p -A。如果覺得每次进入 bash 环境都要添加参数比较麻烦,也可以在 my.cnf 文件里加上如下內容:

1
2
[mysql]
no-auto-rehash

问题三:Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘ (2)

这是 my.cnf 的配置问题,下面的三块内容必须都要设置,不然就会使用默认的 socket,位于 /tmp/mysql.sock 目录,因此我们最好配置一下:

/etc/my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
port=3306

[client]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock

[mysql]
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock

或者我们打一个软链接:

1
ln -s /usr/local/mysql/mysql.sock /tmp/mysql.sock

三、附

1. 关于源码编译安装失败

如果源码编译失败,先确认所需依赖是否全部成功安装,然后清除上一次编译的缓存,之后再次编译,不然会一直失败。参考网址

1
2
3
4
# 清除上一次编译失败的缓存
make distclean
# 再次编译
make

2. 防火墙

CentOS 版本不同采用的防火墙管理也不同(当然,我们部署后可以安装)。CentOS 6 使用的 iptables,CentOS 7 使用的 firewall

CentOS 7 firewall 基础使用

1
2
3
4
5
6
7
8
9
10
# 查询开放端口
firewall-cmd --list-port
# 开放端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent
# 禁用端口
firewall-cmd --zone=public --remove-port=8083/tcp --permanent
# 重新加载
firewall-cmd --reload
# 再次查询开放端口
firewall-cmd --list-port

CentOS 6 iptables 基础使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 启动
service iptables start

# 关闭
service iptables stop

# 查看状态
service iptables status

# 开机禁用
chkconfig iptables off

# 开机启用
chkconfig iptables on
# 允许本地回环接口(即运行本机访问本机) -A和-I参数分别为添加到规则末尾和规则最前面。
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的或相关联的通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许所有本机向外的访问
iptables -P INPUT ACCEPTiptables -A OUTPUT -j ACCEPT

# 允许访问22端口 -s后可以跟 IP 段或指定 IP 地址,如果有其他端口的话,规则也类似。
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -s 10.159.1.0/24 --dport 22 -j ACCEPT

# 允许ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# 禁止其他未允许的规则访问
iptables -A INPUT -j REJECT
iptables -A FORWARD -j REJECT

# 屏蔽单个IP
iptables -I INPUT -s 123.45.6.7 -j DROP
# 封整个段即从123.0.0.1到123.255.255.254
iptables -I INPUT -s 123.0.0.0/8 -j DROP
# 封IP段即从123.45.0.1到123.45.255.254
iptables -I INPUT -s 124.45.0.0/16 -j DROP
# 封IP段即从123.45.6.1到123.45.6.254
iptables -I INPUT -s 123.45.6.0/24 -j DROP

# 查看已有规则 -n:只显示IP地址和端口号,不将 IP 解析为域名
iptables -L -n

# 将所有 iptables 以序号标记显示
iptables -L -n --line-numbers

# 添加规则
# 添加的规则是添加在最后面。如针对 INPUT 链增加一条规则,接收从 eth0 口进入且源地址为192.168.0.0/16网段发往本机的数据
iptables -A INPUT -i eth0 -s 192.168.0.0/16 -j ACCEPT

# 删除规则
iptables -D
# 删除所有规则
iptables -F

# 备份规则
iptables-save > /etc/sysconfig/iptables.save
# 使用规则
iptables-restore < /etc/sysconfig/iptables.save

# 重启生效
service iptables save
service iptables restart

开放端口

方法一:通过命令行

1
2
3
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart

方法二:编辑配置文件

编辑配置文件:vi /etc/sysconfig/iptables,添加:

1
2
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
service iptables restart

本站由 江湖浪子 使用 Stellar 1.29.1 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。