一、操作系统相关准备
1、系统服务安全
安装扩展工具包yum源
# yum -y install epel-release
安装扩展包
# yum install net-tools wget nscd lsof
2、 DNS缓存
编辑 /etc/resolv.conf 配置DNS服务器,打开NSCD服务,缓存DNS,提高域名解析响应速度
启动NSCD服务
# systemctl start nscd.service # systemctl enable nscd.service
3、修改文件打开数限制
想要实现高并发,调整单进程的文件 65536
echo "* soft nofile 65536" >>/etc/security/limits.conf echo "* hard nofile 65536" >>/etc/security/limits.conf
二、Nginx源码编译
[root@centos7 ~]# mkdir -p /opt/data/source [root@centos7 ~]# cd /opt/data/source/ [root@centos7 source]# wget http://nginx.org/download/nginx-1.17.4.tar.gz [root@centos7 source]# tar zxmf nginx-1.17.4.tar.gz
三、编译配置参数
1、首先进入到Nginx目录下
[root@centos7 source]# cd nginx-1.17.4/
[root@centos7 nginx-1.17.4]# pwd /opt/data/source/nginx-1.17.4
[root@centos7 nginx-1.17.4]# ll total 760 drwxr-xr-x. 6 user1 user1 4096 Sep 13 05:05 auto -rw-r--r--. 1 user1 user1 299659 Sep 13 05:05 CHANGES -rw-r--r--. 1 user1 user1 457336 Sep 13 05:05 CHANGES.ru drwxr-xr-x. 2 user1 user1 168 Sep 13 05:05 conf -rwxr-xr-x. 1 user1 user1 2502 Sep 13 05:05 configure drwxr-xr-x. 4 user1 user1 72 Sep 13 05:05 contrib drwxr-xr-x. 2 user1 user1 40 Sep 13 05:05 html -rw-r--r--. 1 user1 user1 1397 Sep 13 05:05 LICENSE drwxr-xr-x. 2 user1 user1 21 Sep 13 05:05 man -rw-r--r--. 1 user1 user1 49 Sep 13 05:05 README drwxr-xr-x. 9 user1 user1 91 Sep 13 05:05 src
2、安装编译工具及依赖库
# yum -y install gcc pcre-devel zlib-devel openssl-devel libxm12-devel \ libxslt-devel gd-devel GeoIP-devel jemalloc-devel libatomic_ops-devel \ perl-devel perl-ExtUtils-Embed
3、编译模块功能
# ./configure \ --with-threads \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gzip_static_module \ --with-compat \ --with-pcre-jit
4、安装
# make && make install
5、关闭防火墙
[root@centos7 sbin]# systemctl stop firewalld.service
6、运行Nginx服务
[root@centos7 nginx-1.17.4]# cd /usr/local/nginx/
[root@centos7 nginx]# ll total 4 drwxr-xr-x. 2 root root 4096 Sep 13 05:16 conf drwxr-xr-x. 2 root root 40 Sep 13 05:16 html drwxr-xr-x. 2 root root 6 Sep 13 05:16 logs drwxr-xr-x. 2 root root 19 Sep 13 05:16 sbin
[root@centos7 nginx]# cd sbin/ [root@centos7 sbin]# ll total 6680 -rwxr-xr-x. 1 root root 6838656 Sep 13 05:16 nginx [root@centos7 sbin]# ./nginx
如果需要继续添加模块,可以通过添加第三方模块 (待验证)
添加第三方静态模块的方法
# ./configure --prefix=/usr/local/nginx --add-module=--with-compat
添加第三方动态模块的方法
# ./configure --prefix=/usr/local/nginx --add-dynamic-module=../ngx_http_proxy_connect_module \
--with-compat
7、配置环境变量
# cat >/etc/profile.d/nginx.sh << EOF PATH=$PATH:/usr/local/nginx/sbin EOF # source /etc/profile
8、将配置Nginx配置文件放在/etc目录下
# ln -s /usr/local/nginx/conf /etc/nginx
9、操作Nginx基本命令
# nginx 启动nginx # nginx -t 执行配置文件检查 # nginx -t -q 执行配置文件检查,且只输出错误信息 # nginx -s stop 快速停止nginx # nginx -s quit 正常关闭nginx # nginx -s reopen 重新打开日志文件 # nginx -p /usr/local/newnginx 指定nginx的执行目录 # nginx -c /etc/nginx/nginx.conf 指定nginx.conf文件的位置 外部指定pid和worker_processes配置指令参数 nginx -g "pid /var/run/nginx.pid; worker_processes 'sysctl -n hw.ncpu';"