centos7 服务器初始化设置

Centos7 服务器部署:

配置 IP 地址网关

#配置IP地址网关
cd /etc/sysconfig/network-scripts/

vi ifcfg-ens33

#修改以下内容
BOOTPROTO=static          #启用静态IP地址
ONBOOT=yes                #开启自动启用网络连接
# 新增以下内容
IPADDR=192.168.2.2
NETMASK=255.255.255.0
GATEWAY=192.168.2.1
DNS1=114.114.114.114
DNS2=8.8.8.8

systemctl restart network #重启网络服务
ip addr                   #查看是否获取到IP地址,
ping www.baidu.com

硬盘挂载

#查看服务器磁盘
df -h
fdisk -l 查看当前磁盘的分区情况;

#进行磁盘分区
fdisk /dev/vda
n #新建分区
p #主分区
w #保存
#输入partprobe 让系统识别新增的分区
#创建物理卷,使用
pvcreate /dev/vda3
#使用vgextend命令进行动态扩展
vgextend centos /dev/vda3    #centos卷组是通过display命令查找出来的
#查看扩展的磁盘
vgdisplay
#将空闲的磁盘扩展到/分区
lvextend -l +100%FREE /dev/mapper/centos-root
xfs_growfs /dev/mapper/centos-root

备份官方的原 yum 源的配置

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 下载 Centos-7.repo 文件,或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#取决于你是否有 wget 的命令
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 清除 yum 缓存
yum clean all
# 缓存本地 yum 源
yum makecache

安装必备软件

yum -y install gcc gcc-c++ autoconf automake libtool make cmake zlib zlib-devel openssl openssl-devel pcre-devel vim traceroute httpd-tools wget net-tools zip unzip

vim 配置修改


vim /etc/vimrc
hi comment ctermfg=6

if has("autocmd")
  filetype indent on
  autocmd FileType \* setlocal ts=4 sts=4 sw=4 ai et nu
  autocmd FileType make setlocal ts=4 sts=4 sw=4 noet nu
endif

添加用户

userdel -r test #删除用户和用户主目录下所有文件,不加-r删除用户文件不删除
groupdel testgroup	#删除用户组

adduser test
passwd test
su test
mkdir apps softwares newapps

linux 下普通用户添加 sudo 免密码

sudo vim /etc/sudoers
#在代码的最后加上:
test   ALL=(ALL)NOPASSWD:ALL

安装 jdk

cd softwares
wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz
tar -zxvf jdk-17_linux-x64_bin.tar.gz

配置 jdk

cd ~
vim .bashrc
export JAVA_HOME=/home/test/apps/jdk1.8.0_121
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

防火墙配置

su
cd /etc/firewalld/zones/
vim public.xml
#添加 2 行
<service name="mysql"/>
<service name="test"/>
#然后 cd ../services/
vim test.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>test</short>
  <description>test</description>
  <port protocol="tcp" port="8080"/>
  <port protocol="tcp" port="8161"/>
  <port protocol="tcp" port="7070"/>
</service>

安装 nginx

cd softwares
yum -y install gcc gcc-c++ autoconf automake libtool make cmake zlib zlib-devel openssl openssl-devel pcre-devel net-tools zip unzip

wget https://nginx.org/download/nginx-1.26.1.tar.gz

./configure --with-http_ssl_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_preread_module --with-http_gzip_static_module --with-http_realip_module

make & make install

nginx 配置


vim /etc/profile
export NGINX_HOME=/usr/local/nginx
export PATH=$NGINX_HOME/sbin:$PATH
source /etc/profile

生成一个 RSA 密钥

openssl genrsa -des3 -out server.key 4096
#拷贝一个不需要输入密码的密钥文件
openssl rsa -in server.key -out server.key
#生成一个证书请求
openssl req -new -key server.key -out server.csr
#自己签发证书
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
#另外一个比较简单的方法就是用下面的命令,一次生成 key 和证书
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout server.key -out server.crt

centos7 系统配置

#主机名
hostnamectl  --static set-hostname  $1
hostnamectl  set-hostname  $1
# 安装必要支持工具及软件工具
yum update -y
yum install -y nmap unzip wget vim lsof xz net-tools iptables-services ntpdate ntp-doc psmisc
yum install -y gcc gcc-c++ autoconf automake libtool make cmake  zlib zlib-devel openssl openssl-devel pcre-devel vim


# 修改文件打开数 set the file limit
cp /etc/security/limits.conf /etc/security/limits.conf.bak
cat > /etc/security/limits.conf << EOF
* soft nofile 1024000
* hard nofile 1024000
* soft nproc  1024000
* hard nproc  1024000
hive   - nofile 1024000
hive   - nproc  1024000
EOF

cp /etc/security/limits.d/20-nproc.conf /etc/security/limits.d/20-nproc.conf.bak
cat > /etc/security/limits.d/20-nproc.conf << EOF
*          soft    nproc     409600
root       soft    nproc     unlimited
EOF

cp /etc/sysctl.conf /etc/sysctl.conf.bak

cat > /etc/sysctl.conf << EOF
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_retries1 = 3
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_max_tw_buckets = 60000
net.ipv4.tcp_max_orphans = 32768
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_wmem = 4096 16384 13107200
net.ipv4.tcp_rmem = 4096 87380 17476000
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.ip_forward = 1
net.ipv4.route.gc_timeout = 100
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 32768
net.nf_conntrack_max = 6553500
net.netfilter.nf_conntrack_max = 6553500
net.netfilter.nf_conntrack_tcp_timeout_established = 180
vm.overcommit_memory = 1
vm.swappiness = 1
fs.file-max = 1024000
EOF

/sbin/sysctl -p

# 设置UTF-8   LANG="zh_CN.UTF-8"
echo "LANG=\"en_US.UTF-8\"">/etc/locale.conf
source  /etc/locale.conf

#关闭SELINUX disable selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

mysql:

#修改root密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'g#4Ih^4F';

CREATE DATABASE IF NOT EXISTS test_boot_prod DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;
source /home/test/apps/test_boot_dev.sql

FLUSH PRIVILEGES; #刷新权限
create user 'test'@'%' identified by 'jkk3NzZIwYRry5q8';
grant all privileges on test_boot_prod._ to "test"@'%';

grant all privileges on test_boot_prod.\* to "test"@'%';

Linux centos7 安装字体文件,解决 Probable fatal error:No fonts found.

sudo yum -y install fontconfig freetype freetype-devel
#安装完后,就可以使用 fc-cache 命令了,最后执行以下命令就 ok 了。

fc-cache -f -v
fc-list | grep "Arial"
# UTF8 字体
yum install google-noto-cjk-fonts.noarch
yum install google-noto-sans-cjk-fonts.noarch

开机启动设置:

vim /etc/rc.d/rc.local

/usr/local/nginx/sbin/nginx
su - test -c "sh /home/test/autostart.sh"

chmod  +x /etc/rc.d/rc.local

vim /home/test/autostart.sh
#/bin/sh
/home/test/apps/mysql-8.0.26/startup.sh
/home/test/apps/redis-5.0.3/startup.sh
sleep 30s
/home/test/newtest/apps/startup.sh test-test-boot

1. 查询系统时间和 bios 时间

  • 查看系统时间
[root@DBCENTER etc]# date
2021年 03月 05日 星期五 02:34:15 CST
  • 查看 bios 时间
[root@DBCENTER etc]# hwclock --show
2021年03月05日 星期五 02时35分21秒  -1.026306 秒
  • 通过 date 命令查询时区
   [root@FJ050 ~]# date +"%Z %z"
   CST +0800
   #或者
   [root@FJ050 ~]# date -R
   Thu, 04 Mar 2021 23:35:06 +0800
  • 修改时区
   [root@FJ050 ~]# timedatectl set-timezone Asia/Shanghai
   [root@FJ050 ~]# date -R
   Thu, 04 Mar 2021 23:37:53 +0800
  • 安装 NTP
   [root@FJ050 ~]# sudo yum -y install ntp
  • 使用 ntpdate 校准时间
   [root@FJ050 ~]# ntpdate pool.ntp.org
   [root@FJ050 ~]# date
   2021 年 03 月 04 日 星期四 15:41:30 CST
: "
中国-cn.pool.ntp.org
最好使用 pool.ntp.org 查找 NTP 服务器
服务器 0.cn.pool.ntp.org
服务器 1.cn.pool.ntp.org
服务器 2.cn.pool.ntp.org
服务器 3.cn.pool.ntp.org
"
  • 若想持续校准时间启动 ntpd daemon
[root@FJ050 ~]# systemctl start ntpd
# 查看 ntpd daemon 是否启动
[root@FJ050 ~]# systemctl status ntpd
  • 将 Linux 系统同步给 BIOS 时间
#因为每次开机的时候系统会重新读取 BIOS 时间,所以当我们进行完 DATE 时间的校准后,还需要更新 BIOS 的时间

date -R #显示系统时间

# 显示 bios 时间
hwclock --show
#或
hwclock -r

# 将系统时间写入 bios(根据需要执行)
hwclock --systohc
#或
hwclock -w

# 将 bios 时间写入系统(根据需要执行)
hwclock --hctosys
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值