安装依赖
yum install -y gcc gcc-c++ automake autoconf libtool make pcre pcre-devel zlib zlib-devel openssl openssl-devel
CentOS系统配置
安装相关工具
yum -y install net-tools wget nscd lsof
DNS缓存
编辑/etc/resolv.conf配置DNS缓存服务器,打开NSCD服务,缓存DNS,提高域名解析响应速度。
systemctl start nscd.service #启动NSCD服务
systemctl enable nscd.service
修改文件打开数限制
操作系统默认单进程最大文件打开数为1024,要想实现高并发,可以把单进程的文件打开数调整为65536
# 查看现在的最大文件打开数
ulimit -n
# 返回 1024
# 修改最大文件打开数为65536
# *表示所有用户
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
# 重启服务器即可生效
reboot
ulimit -n
# 返回65536
系统级的最大文件打开数限制,是系统内存的10%(以KB来计算),又称系统级限制。
例如:
1GB内存,换算成KB,1GB = 1024000KB
,再乘以0.1,1024000*0.1 = 102400
就是这个系统的最大文件打开数,也就是102400。
可以使用 sysctl -a | grep fs.file-max
命令来查看系统级别的最大打开文件数。
Nginx安装
获取最新稳定安装包:https://nginx.org/en/download.html
命令:
cd /home/data
wget https://nginx.org/download/nginx-1.20.2.tar.gz
tar -zxvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
./configure —sbin-path=/usr/local/nginx/nginx —conf-path=/usr/local/nginx/nginx.conf —pid-path=/usr/local/nginx/nginx.pid —with-http_gzip_static_module —with-http_stub_status_module —with-file-aio —with-http_realip_module —with-http_ssl_module —with-pcre
make -j 4 && make install
测试安装是否成功:
/usr/local/nginx/nginx
Nginx环境配置
Nginx自启动服务文件
创建文件:touch /etc/init.d/nginx
修改权限:chmod 755 /etc/init.d/nginx
注册服务:# 需要先将内容编辑好
chkconfig —add nginx
chkconfig nginx on
文件说明:
#chkconfig: 2345 55 25 # 表示自启动,等级相关信息
2345 表示在2345运行级别上执行
80 表示启动顺序
90 表示关闭顺序
运行级别:0 - 6
0:系统停机状态,系统默认运行级别不能设置为0,否则不能正常启动,机器关闭。
1:单用户工作状态,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式登录。
2:多用户状态,没有NFS支持。
3:完整的多用户模式,有NFS,登陆后进入控制台命令行模式。
4:系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置。
5:X11控制台,登陆后进入图形GUI模式,X Window系统。
6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动。运行init 6机器就会重启。
优先级范围:0 - 100,数字越大,优先级越低
文件内容:
#! /bin/sh
# chkconfig: 2345 55 25
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/$NAME
CONFIGFILE=/usr/local/nginx/$NAME.conf
PIDFILE=/usr/local/nginx/$NAME.pid
ulimit -n 8192
case "$1" in
start)
echo -n "Starting $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
echo "$NAME (pid `pidof $NAME`) already running. "
exit 1
fi
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed "
exit 1
else
echo " success "
fi
;;
stop)
echo -n "Stoping $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" = '' ];then
echo "$NAME is not running."
exit 1
fi
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
else
echo "$NAME is stopped"
exit 0
fi
else
echo "$NAME is stopped"
exit 0
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload service $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can 't reload."
exit 1
fi
else
echo "$NAME is not running, can 't reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest}"
exit 1
;;
esac