安装依赖

yum install -y gcc gcc-c++ automake autoconf libtool make pcre pcre-devel zlib zlib-devel openssl openssl-devel

CentOS系统配置

安装相关工具

  1. yum -y install net-tools wget nscd lsof

DNS缓存

编辑/etc/resolv.conf配置DNS缓存服务器,打开NSCD服务,缓存DNS,提高域名解析响应速度。

  1. systemctl start nscd.service #启动NSCD服务
  2. systemctl enable nscd.service

修改文件打开数限制

操作系统默认单进程最大文件打开数为1024,要想实现高并发,可以把单进程的文件打开数调整为65536

  1. # 查看现在的最大文件打开数
  2. ulimit -n
  3. # 返回 1024
  4. # 修改最大文件打开数为65536
  5. # *表示所有用户
  6. echo "* soft nofile 65536" >> /etc/security/limits.conf
  7. echo "* hard nofile 65536" >> /etc/security/limits.conf
  8. # 重启服务器即可生效
  9. reboot
  10. ulimit -n
  11. # 返回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,数字越大,优先级越低

文件内容:

  1. #! /bin/sh
  2. # chkconfig: 2345 55 25
  3. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  4. NAME=nginx
  5. NGINX_BIN=/usr/local/nginx/$NAME
  6. CONFIGFILE=/usr/local/nginx/$NAME.conf
  7. PIDFILE=/usr/local/nginx/$NAME.pid
  8. ulimit -n 8192
  9. case "$1" in
  10. start)
  11. echo -n "Starting $NAME... "
  12. if [ -f $PIDFILE ];then
  13. mPID=`cat $PIDFILE`
  14. isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
  15. if [ "$isStart" != '' ];then
  16. echo "$NAME (pid `pidof $NAME`) already running. "
  17. exit 1
  18. fi
  19. fi
  20. $NGINX_BIN -c $CONFIGFILE
  21. if [ "$?" != 0 ] ; then
  22. echo " failed "
  23. exit 1
  24. else
  25. echo " success "
  26. fi
  27. ;;
  28. stop)
  29. echo -n "Stoping $NAME... "
  30. if [ -f $PIDFILE ];then
  31. mPID=`cat $PIDFILE`
  32. isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
  33. if [ "$isStart" = '' ];then
  34. echo "$NAME is not running."
  35. exit 1
  36. fi
  37. fi
  38. $NGINX_BIN -s stop
  39. if [ "$?" != 0 ] ; then
  40. echo " failed. Use force-quit"
  41. exit 1
  42. else
  43. echo " done"
  44. fi
  45. ;;
  46. status)
  47. if [ -f $PIDFILE ];then
  48. mPID=`cat $PIDFILE`
  49. isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
  50. if [ "$isStart" != '' ];then
  51. echo "$NAME (pid `pidof $NAME`) already running."
  52. exit 1
  53. else
  54. echo "$NAME is stopped"
  55. exit 0
  56. fi
  57. else
  58. echo "$NAME is stopped"
  59. exit 0
  60. fi
  61. ;;
  62. restart)
  63. $0 stop
  64. sleep 1
  65. $0 start
  66. ;;
  67. reload)
  68. echo -n "Reload service $NAME... "
  69. if [ -f $PIDFILE ];then
  70. mPID=`cat $PIDFILE`
  71. isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
  72. if [ "$isStart" != '' ];then
  73. $NGINX_BIN -s reload
  74. echo " done"
  75. else
  76. echo "$NAME is not running, can 't reload."
  77. exit 1
  78. fi
  79. else
  80. echo "$NAME is not running, can 't reload."
  81. exit 1
  82. fi
  83. ;;
  84. configtest)
  85. echo -n "Test $NAME configure files... "
  86. $NGINX_BIN -t
  87. ;;
  88. *)
  89. echo "Usage: $0 {start|stop|restart|reload|status|configtest}"
  90. exit 1
  91. ;;
  92. esac