Linux虽说是开箱即用的,但是如果想有更好的性能,则需要经过一些调教。

服务器时区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
echo -e "\033[36m检查服务器时区\033[0m"
date_sh=`grep "Asia/Shanghai" /etc/sysconfig/clock|wc -l`
if [ $date_sh -eq 1 ]
then
echo -e "\033[36m 上海时区\033[0m"
else
echo -e "\033[36m 修改时区\033[0m"
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
timedatectl set-timezone Asia/Shanghai
cp /etc/sysconfig/clock /tmp/clockbak
echo 'ZONE="Asia/Shanghai"' >/etc/sysconfig/clock
echo 'UTC=false' >>/etc/sysconfig/clock

hwclock -w
systemctl restart rsyslog
systemctl restart crond
fi

同步时间

1
2
ntpdate ntp3.aliyun.com
hwclock -w

服务器内核

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
echo -e "\033[36m检查服务器内核参数\033[0m"
sysctl_conf=`grep -v "#" /etc/sysctl.conf|wc -l`
if [ $sysctl_conf -eq 0 ]
then
echo -e "\033[36m 优化内核参数\033[0m"
cp /etc/sysctl.conf /etc/sysctl.confbak

cat << EOF >> /etc/sysctl.conf
fs.file-max = 12553500
fs.nr_open = 12453500
kernel.shmall = 4294967296
kernel.shmmax = 68719476736
kernel.msgmax = 65536
kernel.sysrq = 0
net.core.netdev_max_backlog = 2000000
net.core.rmem_default = 699040
net.core.rmem_max = 50331648
net.core.wmem_default = 131072
net.core.wmem_max = 33554432
net.core.somaxconn = 65535
net.ipv4.ip_local_port_range = 15000 65000
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.tcp_fin_timeout = 7
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 655360
net.ipv4.tcp_max_tw_buckets = 6000000
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_rmem = 32768 699040 50331648
net.ipv4.tcp_wmem = 32768 131072 33554432
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_window_scaling = 1
vm.swappiness = 0
EOF

sysctl -p
else
sysctl_confs=`cat /etc/sysctl.conf |grep -v "#"`
echo -e "\033[36m 内核配置已优化\033[0m"
fi

打开文件限制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
echo -e "\033[36m检查服务器打开文件限制\033[0m"
ulimit_num=`ulimit -a |grep "open files" |awk -F " " '{print $4}'`
if [ $ulimit_num -ge 65535 ]
then
echo -e "\033[36m 打开文件数限制已调整"
else
cp /etc/security/limits.conf /etc/security/limits.confbak
cat <<EOF >>/etc/security/limits.conf
root soft nproc 65535
root hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF
echo -e "\033[36m 打开文件数已调整 \033[0m"
fi

常用软件

1
2
3
yum update -y
yum install -y epel-release yum-utils
yum install -y gcc gcc-c++ wget make automake autoconf openssl openssl-devel xz zip unzip vim libtool libtool-ltdl-devel zlib1g-dev zlib zlib-devel pcre pcre-devel curl curl-devel ntp ntpdate rsync sqlite-devel libX11* git parted screen htop lrzsz

Mysql

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
# 卸载已存在的
rpm -qa|grep mysql | xargs rpm -uvh
yum clean all
# CentOS8
wget https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm -O mysql.rpm
# CentOS7
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -O mysql.rpm
# CentOS6
wget https://dev.mysql.com/get/mysql80-community-release-el6-3.noarch.rpm -O mysql.rpm

# 安装
rpm -ivh mysql.rpm
yum install -y mysql mysql-server mysql-devel

# 启动/开机自启
# CentOS7
systemctl start mysqld
systemctl enable mysqld

# CentOS6
service mysqld start
chkconfig mysqld on

# 修改密码
grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
use mysql;
ALTER USER 'root'@'%' IDENTIFIED BY '123#Aa!456';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
exit;

Redis

1
2
3
yum install -y redis
systemctl start redis
systemctl enable redis

Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
# CentOS7
wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.1-1.el7.ngx.x86_64.rpm -O nginx-1.20.1.rpm
rpm -ivh nginx-1.20.1.rpm
yum install -y nginx

systemctl start nginx
systemctl enable nginx

# Vue rewrite
try_files $uri $uri/ @router;
location @router {
rewrite ^.*$ /index.html last;
}