标签归档:centos

CentOS 离线安装 docker

一、引言

生产环境中,很多时候都无法访问互联网,如何安装 docker 服务,官方推荐使用编译好的二进制包方案。本文以 CentOS 7 为例

二、先决条件

  • 64 位安装
  • 版本 3.10 或更高版本的 Linux 内核, 建议使用适用于您的平台的最新版本的内核
  • iptables 1.4 或更高版本
  • git 版本 1.7 或更高版本
  • ps 可执行文件,通常由 procps 或类似包提供
  • XZ Utils 4.9 或更高版本
  • 正确安装的 cgroupfs 层次结构

三、让我们开始吧

1.下载(官方安装包在这里
$ curl -LO https://download.docker.com/linux/static/stable/x86_64/docker-20.10.17.tgz
2.解压
$ tar -zxvf docker-20.10.17.tgz
3.移动二进制可执行文件至 /usr/bin 【官方推荐的 executable path, 一定要在这里,否则可能在 systemd 执行中有些问题】
$ sudo cp docker/* /usr/bin
4.编辑守护进程启动文件 /etc/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
  
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/docker/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
  
[Install]
WantedBy=multi-user.target
5.重载守护进程文件
$ systemctl daemon-reload
6.加入开机自启
$ systemctl enable docker.service

Linux 编译安装 Nginx

引言

在生产环境中,Nginx 通常以源码编译的方式来安装,一则生产环境网络要求苛刻,二则可以自选模块,比如在 复用 443 端口时候,需要用到 stream_ssl_module 模块。

本文以 CentOS 7 和 Debian 11 操作系统为例,文中第四部分是如何新增模块,重新编译已经安装好的 Nginx 服务。

安装

安装依赖包

以下适用于 CentOS 7 | RadHat 7

yum install -y libtool autoconf cmake curl vim \
gcc gcc-c++ \
openssl openssl-devel \
pcre pcre-devel \
zlib zlib-devel \
libxml2 libxml2-devel \
libxslt libxslt-devel \
gd gd-devel \
GeoIP GeoIP-devel GeoIP-data \
gperftools 

以下适用于 Debian 11

apt-get install gcc build-essential manpages-dev \
libpcre3 libpcre3-dev libxslt-dev \
libgeoip-dev google-perftools \
libgoogle-perftools4 libgoogle-perftools-dev \
libtcmalloc-minimal4 libgdchart-gd2-noxpm \
libgdchart-gd2-noxpm-dev -y

下载 Nginx | openssl 包

curl -LO https://nginx.org/download/nginx-1.23.3.tar.gz
curl -LO https://ftp.openssl.org/source/old/1.1.1/openssl-1.1.1k.tar.gz

解压

tar -zxvf nginx-1.23.3.tar.gz -C /usr/local/src
tar -zxvf openssl-1.1.1k.tar.gz -C /usr/local/src

cd /usr/local/src/nginx-1.23.3

useradd www

生成侦测文件

./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-mail --with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_ssl_preread_module \
--with-google_perftools_module \
--with-pcre \
--with-openssl=/usr/local/src/openssl-1.1.1k \
--with-openssl-opt=enable-weak-ssl-ciphers

编译并安装

make && make install

守护进程脚本

vim /etc/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /usr/local/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /usr/local/nginx/logs/nginx.pid)"

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start nginx.service

重新编译已存在的 nginx

下载 Nginx

解压

生成侦测文件

在这里添加你想要添加的模块,比如 –with-stream_ssl_module,根据需求来

编译这里千万不要 make install

make

备份并复制

# 备份原有的 nginx 可执行文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.default.bak

# 复制全新编译的 nginx
cp /usr/local/src/nginx-1.20.2/objs/nginx /usr/local/nginx/sbin/nginx

重新启动 Nginx

systemctl restart nginx

CentOS 安装 python3

CentOS 安装 python3 是一篇基础环境架设的日志记录

一、安装记录

# 安装依赖包
sudo yum install gcc gcc-c++ cmake pcre pcre-devel \
autoconf libtool zlib zlib-devel bzip2 bzip2-devel \
ncurses ncurses-devel readline readline-devel \
openssl openssl-devel xz lzma xz-devel \
sqlite sqlite-devel tk tk-devel \
libffi libffi-devel openssl-static -y

# 1.从官网下载Python 3.10.5 - 06-Jun-2022 12:08 [stable version]
sudo curl -LO https://www.python.org/ftp/python/3.10.5/Python-3.10.5.tgz

# 2.解压至/usr/local
sudo tar -xvf Python-3.10.5.tgz -C /usr/local

# 3.生成Makefile
cd /usr/local/Python-3.10.5
sudo ./configure

# 4.编译解压
sudo make -j 8 && sudo make install

# 5.验证 python3 和 pip 安装成功
python3 --version
pip3 --version

二、特别说明

  • 在安装 python3 的时候,已经将 pip 功能安装好了
  • pip install 的时候,如果在中国,建议指定中国清华大学源,e.g
pip install example -i https://pypi.tuna.tsinghua.edu.cn/simple

CentOS新装系统的后续工作

对于新装 CentOS 系统,我个人会通常做以下工作

1.换源

我一般换 中国科学技术大学镜像源 ,因为阿里源、华为源都放过我鸽子XDDD

2.基础环境

sudo yum install gcc gcc-c++ pcre openssh openssh-devel openssl openssl-devel libtools cmake autoconf tcl ntpdate -y

# 升级内核和所有软体
sudo yum update -y

3.同步时间服务器(server time.ustc.edu.cn)

[root@basic-platform ~]# cat /etc/ntp.conf | grep -v "^#" | grep -v "^$"
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server time.ustc.edu.cn
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

[root@basic-platform ~]# systemctl start ntpdate.service

CentOS安裝squid服務并配置http和yum代理

安裝squid服務

找一台可以訪問外網的服務器

# 安裝squid
yum install squid -y

修改配置文件 /etc/squid/squid.conf (重點是 http_access

[root@linux-host1 local]#  cat /etc/squid/squid.conf | grep -v "^$" | grep -v "^#"
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost manager
http_access deny manager
http_access allow localnet
http_access allow localhost
http_access allow all
http_port 3128
coredump_dir /var/spool/squid
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern .               0       20%     4320

# 啓動squid服務
systemctl start squid

# 加入開機自啓
systemctl enable squid

yum代理配置

# 在/etc/yum.conf配置文件[main]節點下追增以下配置

# squid代理服務
proxy=http://172.50.1.119:3128 

# 服務器賬號
proxy_name=root 

# 服務器密碼
proxy_password=2am#ue9P&.aqb*14 

http代理配置

# 創建配置文件
touch /etc/profile.d/proxy.sh

# 在 /etc/profile.d/proxy.sh 添加以下配置
# 格式: export http_proxy="http://user_name:password@IP:PORT" username是squid服務器的賬號密碼,這裏沒有給squid服務配置賬號密碼認證
export http_proxy="http://root:2am#ue9P&.aqb*14@172.50.1.119:3128" 
export https_proxy="http://root:2am#ue9P&.aqb*14@172.50.1.119:3128"

# 刷新配置生效
source /etc/profile.d/proxy.sh

CentOS7根目录磁盘扩容(/dev/mapper/centos-root 空间不足)

0.查看根分区大小

[root@master ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
devtmpfs                         63G     0   63G   0% /dev
tmpfs                            63G     0   63G   0% /dev/shm
tmpfs                            63G  1.3G   62G   2% /run
tmpfs                            63G     0   63G   0% /sys/fs/cgroup
/dev/mapper/cl_geekthings-root   50G   38G   13G  76% /
/dev/sda1                      1014M  311M  704M  31% /boot
/dev/mapper/cl_geekthings-home  918G   12G  906G   2% /home
tmpfs                            13G   12K   13G   1% /run/user/42
tmpfs                            13G     0   13G   0% /run/user/0
overlay                          50G   38G   13G  76% /var/lib/docker/overlay2/70dffe831895bb55d79b4a8f483364a4f736637ccb2f6694f1cf82ba7ff1743d/merged

1.查看磁盘编号

[root@master ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2

2.这里在vMware vCenter中新增一块硬盘(编辑->新增设备) 再次查看磁盘

[root@master ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb

3.对新增硬盘进行分区, 这里新增的磁盘名称叫 /dev/sdb

[root@localhost ~]# fdisk /dev/sdb

# 输入 m, 得到帮助,可以按照提示操作进行分区了
# 因为这块硬盘只有 100G,所以我只打算分一个区,这里是需要输入 n,然后接下来都按 Enter 键,默认即可,最后输入 w 保存退出
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

4.将分区后的磁盘格式化成xfs类型

[root@localhost ~]# mkfs.xfs /dev/sdb1

5.创建pv

[root@master ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

6.查看vg组

[root@master ~]# vgs
  VG            #PV #LV #SN Attr   VSize    VFree
  cl_geekthings   1   3   0 wz--n- <999.00g 4.00m

7.扩展vg

[root@master ~]# vgextend cl_geekthings /dev/sdb1
  Volume group "cl_geekthings" successfully extended

[root@master ~]# vgs
  VG            #PV #LV #SN Attr   VSize VFree  
  cl_geekthings   2   3   0 wz--n- 1.17t 200.00g

8.扩展lv

[root@master ~]# lvextend -L +200G /dev/mapper/cl_geekthings-root
  Size of logical volume cl_geekthings/root changed from 50.00 GiB (12800 extents) to 250.00 GiB (64000 extents).
  Logical volume cl_geekthings/root successfully resized.

9.系统重新读取大小

[root@master ~]# xfs_growfs /dev/mapper/cl_geekthings-root
meta-data=/dev/mapper/cl_geekthings-root isize=512    agcount=4, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=13107200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13107200 to 65536000

10.查看扩容是否成功

[root@master ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
devtmpfs                         63G     0   63G   0% /dev
tmpfs                            63G     0   63G   0% /dev/shm
tmpfs                            63G  1.3G   62G   2% /run
tmpfs                            63G     0   63G   0% /sys/fs/cgroup
/dev/mapper/cl_geekthings-root  250G   38G  213G  16% /
/dev/sda1                      1014M  311M  704M  31% /boot
/dev/mapper/cl_geekthings-home  918G   12G  906G   2% /home
tmpfs                            13G   12K   13G   1% /run/user/42
tmpfs                            13G     0   13G   0% /run/user/0
overlay                         250G   38G  213G  16% /var/lib/docker/overlay2/70dffe831895bb55d79b4a8f483364a4f736637ccb2f6694f1cf82ba7ff1743d/merged
[root@master ~]# lsblk
NAME                   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                      8:0    0  1000G  0 disk 
├─sda1                   8:1    0     1G  0 part /boot
└─sda2                   8:2    0   999G  0 part 
  ├─cl_geekthings-root 253:0    0   250G  0 lvm  /
  ├─cl_geekthings-swap 253:1    0  31.5G  0 lvm  
  └─cl_geekthings-home 253:2    0 917.5G  0 lvm  /home
sdb                      8:16   0   200G  0 disk 
└─cl_geekthings-root   253:0    0   250G  0 lvm  /
sr0                     11:0    1   4.1G  0 rom

CentOS記錄所有用戶登陸操作詳細日志

setp1 : 在 /etc/profile 末尾追增以下代碼

############################ 用戶登陸操作歷史脚本 ############################

USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
HISTDIR=/tmp/log/history
DT=`date +%Y-%m-%d`

if [ -z $USER_IP ]; then
    USER_IP=`hostname`
fi

pdf="."
if [[ ! $USER_IP == *${pdf}* ]]; then
    USER_IP=`hostname`
fi

if [ ! -d $HISTDIR ]; then
    mkdir -p $HISTDIR
    chmod 300 $HISTDIR
fi

if [ ! -d $HISTDIR/${DT} ]; then
    mkdir -p $HISTDIR/${DT}
    chmod 300 $HISTDIR/${DT}
fi

export HISTFILESIZE=10000 # 定義文件中最多的只有HISTFILESIZE行
export HISTSIZE=10000 # 定義history命令輸出的記錄數
DT2=`date +%Y-%m-%d_%H:%M:%S`
export HISTFILE="$HISTDIR/${DT}/${LOGNAME}@${USER_IP}_$DT2"
export HISTTIMEFORMAT="%Y-%m-%-d_%H:%M:%S # "
chmod 600 $HISTDIR/${DT}/*_* 2>/dev/null

##############################################################################

step2: 刷新生效

[root@localhost ~]# source /etc/profile

CentOS两种方式安装7z命令

一、安装(以CentOS为例)

1.yum安装(能访问互联网的,用此方式)

建议更换国内源 :中国科学技术大学镜像源

  • 安装epel源
    [root@linux-host1 opt]# sudo yum install epel-release -y
  • yum安装
    [root@linux-host1 opt]# sudo yum install p7zip -y
2.二进制安装

7z 最新版本链接
7z 官网

  • 下载到/usr/local
    [root@linux-host1 ~]# cd /usr/local && wget https://nchc.dl.sourceforge.net/project/p7zip/p7zip/16.02/p7zip_16.02_src_all.tar.bz2
  • 解压
    [root@linux-host1 local]# sudo tar xjvf p7zip_16.02_src_all.tar.bz2
  • 安装
    [root@linux-host1 local]# cd p7zip_16.02 && sh install.sh

二、命令

[root@linux-host1 opt]# 7za x frontend_7_06_JsTest_0303.7z  -r -o./
# 参数含义:
# x  代表解压缩文件,并且是按原始目录树解压(还有个参数 e 也是解压缩文件,但其会将所有文件都解压到根下,而不是自己原有的文件夹下)
# frontend_7_06_JsTest_0303.7z是压缩文件,这里我用phpadmin做测试。这里默认使用当前目录下的phpMyAdmin-3.3.8.1-all-languages.7z
# -r 表示递归解压缩所有的子文件夹
# -o 是指定解压到的目录,-o后是没有空格的,直接接目录。

[root@linux-host1 opt]# 7za a -t7z -r frontend_7_06_JsTest_0303.7z /opt/frontend_7_06_JsTest_0303/*
# 参数含义:
# a  代表添加文件/文件夹到压缩包
# -t 是指定压缩类型,这里定为7z,可不指定,因为7za默认压缩类型就是7z
# -r 表示递归所有的子文件夹
# frontend_7_06_JsTest_0303.7z 是压缩好后的压缩包名
# /opt/frontend_7_06_JsTest_0303/*   :是压缩目标。
# 7za不仅仅支持.7z压缩格式,还支持.tar.bz2等压缩类型的。如上所述,用-t指定即可。