#部署前准备工作

#配置阿里云的yum源
yum -y install wget

cd /etc/yum.repos.d/ wget http://mirrors.aliyun.com/repo/Centos-7.repo mv CentOS-Base.repo CentOS-Base.repo.bak mv Centos-7.repo CentOS-Base.repo yum clean all yum makecache yum update

 

yum -y install wget vim lrzsz make cmake gcc gcc-c++

yum install -y libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed

useradd nginx -s /sbin/nologin -M

#下载安装包、部署服务

cd /usr/local/src/

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

tar zxvf nginx-1.16.1.tar.gz

cd nginx-1.16.1/

./configure --prefix=/usr/local/nginx-1.16.1 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module

make && make install

ln -s /usr/local/nginx-1.16.1/ /usr/local/nginx

chown -R nginx.nginx /usr/local/nginx-1.16.1

#常用模块参数
./configure --prefix=/usr/local/nginx-1.24.0/ \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx-1.16.1"
  nginx binary file: "/usr/local/nginx-1.16.1/sbin/nginx"
  nginx modules path: "/usr/local/nginx-1.16.1/modules"
  nginx configuration prefix: "/usr/local/nginx-1.16.1/conf"
  nginx configuration file: "/usr/local/nginx-1.16.1/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx-1.16.1/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx-1.16.1/logs/error.log"
  nginx http access log file: "/usr/local/nginx-1.16.1/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

#启动服务

/usr/local/nginx/sbin/nginx

[root@localhost conf]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4087/nginx: master  

#优化配置文件

#原配置文件
user nginx; worker_processes
1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

 #优化Nginx服务的worker进程个数

#查看服务cpu核数
grep
'processor' /proc/cpuinfo |wc -l 2
#修改nginx.con中的worker_processes
worker_processes 2;
#修改前默认一个master一个worker process; [root@localhost conf]#
ps aux | grep nginx root 4087 0.0 0.1 46056 1932 ? Ss Apr15 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 42506 0.0 0.2 46496 2028 ? S 01:05 0:00 nginx: worker process #检查配置文件、重载配置文件 [root@localhost conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.16.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.16.1/conf/nginx.conf test is successful [root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload #worker process数量已经变为两个 [root@localhost conf]# ps aux | grep nginx | grep -v "grep" root 4087 0.0 0.1 45960 1972 ? Ss Apr15 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 42582 0.0 0.1 46396 1940 ? S 01:31 0:00 nginx: worker process nginx 42583 0.0 0.1 46396 1940 ? S 01:31 0:00 nginx: worker process

#配置Nginx worker进程最大打开文件数

#如果系统没优化过默认很低(我的是1024)
[root@localhost conf]# ulimit -n 1024
[root@localhost ~]# vim /etc/security/limits.conf # End of file
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
#重新登录或重启系统后查看
[root@localhost ~]# ulimit -n
65535
#接下来修改nginx配置文件,在核心配置区中加入以下配置,保存后重载nginx
worker_rlimit_nofile 65535;

 #调整Nginx单个进程允许的客户端最大连接数(在核心配置区中加入以下配置,保存后重载nginx

 worker_connections  20480;

#根据系统的最大打开文件数来调整,worker_connections进程连接数量要小于等于系统的最大打开文件数

 #隐藏版本号(在http标签中加入以下配置并重载配置文件)

server_tokens off;

#关闭前
[root@localhost conf]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx1.16
Date: Thu, 16 Apr 2020 02:57:50 GMT
Content-Type: text/html
#关闭后
[root@localhost conf]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 16 Apr 2020 03:13:55 GMT
Content-Type: text/html

 #设置nginx连接超时时间(在http标签中加入以下配置并重载配置文件)

keepalive_timeout  60;

posted on 2024-04-17 14:07  Mr_星火  阅读(1)  评论(0编辑  收藏  举报