0%

Nginx的安装

Nginx的安装

mac下安装

首先看一下nginx的信息

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#执行命令
brew info nginx
#下面来解释一下下面的意思
--------------------------------------------------------
#最新已经到了1.19.6版本
nginx: stable 1.19.6 (bottled), HEAD

HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server

https://nginx.org/
#已经安装过了 安装的位置

/usr/local/Cellar/nginx/1.19.4 (25 files, 2.2MB) *
# 以及安装的时间

Poured from bottle on 2020-11-13 at 11:31:37
# 从哪个网站下载的

From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/nginx.rb

License: BSD-2-Clause
# 依赖

==> **Dependencies**
# openssl是可以没有的 pcre必须要有

Required: **openssl@1.1** **✘**, **pcre** **✔**

==> **Options**

--HEAD

​ Install HEAD version

==> **Caveats**
# Docroot网站根目录位置

Docroot is: /usr/local/var/www


# 默认配置位置
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that

nginx can run without sudo.


#nginx加载servers下的所有文件
nginx will load all files in /usr/local/etc/nginx/servers/.



To have launchd start nginx now and restart at login:

brew services start nginx

Or, if you don't want/need a background service you can just run:

nginx

==> **Analytics**

install: 35,316 (30 days), 124,077 (90 days), 453,743 (365 days)

install-on-request: 35,265 (30 days), 123,459 (90 days), 445,150 (365 days)

build-error: 0 (30 days)

既然说需要依赖pcre组件,那么pcre是干什么的呢

PCRE库支持正则表达式。如果我们在配置文件nginx.conf中使用了正则表达式,那么在编译Nginx时就必须把PCRE库编译进Nginx,因为Nginx的HTTP模块需要靠它来解析正则表达式。

进行安装nginx

1
brew install nginx

linux下安装源码包

使用源码包进行安装,需要自己去下载nginx需要的依赖

  • 需要gcc
  • 需要pcre,yum install pcre
  • 需要zlib,yum install zlib zlib-devel
  • 如果需要支持ssl的话,安装OpenSSL,yum install openssl openssl-devel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 创建nginx所需用户和组
groupadd www
useradd -r -g www www -s /sbin/nologin
# 如果提示缺少什么,就加上相应的选项,比如缺少pcre的话,就加上 --with-pcre=/usr/common/temp/pcre-8.34
./configure \
"--prefix=/usr/local/nginx" \
"--sbin-path=/usr/local/nginx/sbin/nginx" \
"--conf-path=/usr/local/nginx/conf/nginx.conf" \
"--error-log-path=/usr/local/nginx/log/error.log" \
"--http-log-path=/usr/local/nginx/log/access.log" \
"--pid-path=/usr/local/nginx/var/nginx.pid" \
"--lockpath=/usr/local/nginx/var/nginx.lock" \
"--user=www" \
"--group=www" \
"--with-http_stub_status_module" \
"--with-http_ssl_module"


# 第二步:配置后就依次
make
make install

编译选项

通用配置选项

常见的选项
–prefix= 安装路径,如果没有指定,默认为/usr/local/nginx
–sbin-path= nginx可执行命令的文件路径,如果没有指定,默认为/sbin/nginx
–conf-path= 在没有使用-c参数指定的情况下nginx.conf的默认位置,如果没有指定,默认
/conf/nginx.conf
–pid-path= nginx.pid的路径,如果没有在nginx.conf中通过“pid”指令指定,默认为

/logs/nginx.pid
–lock-path= nginx.lock文件路径,如果没有指定,默认为/logs/nginx.lock。
–error-log-path= 当没有在nginx.conf中使用“error_log”指令指定时的错误日志位置,
如果没有指定,默认为/logs/error.log
–user= work进程运行的用户,当没有在nginx.conf中使用“user”指令指定时nginx运行的用户,如果没有指定,
默认为nobody
–group= worker进程运行的组,当没有在nginx.conf中使用“user”指令指定时nginx运行的组,如果没有指定,
默认为nobody
–with-file-aio 为Linux2.6.22+系统启用异步IO
–with-debug 启用debug记录
–builddir=DIR 设置构建目录
–with-rtsig_module 启用rtsig模块
–with-select_module –without-select_module 如果在configure的时候没有发现kqueue, epoll,
rtsig或/dev/poll其中之一,select模块始终为启用状态
–with-poll_module –without-poll_module 如果在configure的时候没有发现kqueue, epoll,
rtsig或/dev/poll其中之一,poll模块始终为启用状态
–with-pcre=DIR 设置PCRE库源文件路径
–with-pcre-opt=OPTIONS 在编译时为PCRE设置附加参数
–with-md5=DIR 设置md5库源文件路径
–with-md5-opt=OPTIONS 在编译时为md5设置附加参数
–with-md5-asm 使用md5汇编源
–with-sha1=DIR 设置sha1库源文件路径
–with-sha1-opt=OPTIONS 在编译时为sha1设置附加参数
–with-sha1-asm 使用sha1汇编源
–with-zlib=DIR 设置zlib库源文件路径
–with-zlib-opt=OPTIONS 在编译时为zlib设置附加参数
–with-zlib-asm=CPU 为指定的CPU使用zlib汇编源进行优化,可用值为: pentium, pentiumpro
–with-openssl=DIR 设置openssl库源文件路径
–with-openssl-opt=OPTIONS 在编译时为openssl设置附加参数
–add-module=PATH 增加一个在PATH中的第三方模块
–without-pcre 禁用PCRE库文件,同时将禁用HTTP rewrite模块,如果要在location指令中使用正则表达式,同样需要PCRE库

优化编译配置选项

–with-cc=PATH 设置C编译器路径
–with-cpp=PATH 设置C预处理器路径
–with-cc-opt=OPTIONS 变量CFLAGS中附加的参数,用于FreeBSD中的PCRE库,同样需要指定–with-cc-opt=”-I /usr/local/include”,如果我们使用select()函数则需要同时增加文件描述符数量,可以通过–with-cc-opt=”-D FD_SETSIZE=2048”指定。
–with-ld-opt=OPTIONS 通过连接器的附加参数,用于FreeBSD中的PCRE库,同样需要指定–with-ld-opt=”-L /usr/local/lib”
–with-cpu-opt=CPU 指定编译的CPU,可用的值为: pentium, pentiumpro, pentium3, pentium4,
athlon, opteron, amd64, sparc32, sparc64, ppc64

配置web选项

–http-log-path= 当没有在nginx.conf中使用“access_log”指令指定时的访问日志位置,
如果没有指定,默认为/logs/access.log
–without-http-cache 使用upstream模块时,nginx能够配置本地缓存,该选项可以禁用缓存
–with-http_perl_module 启用ngx_http_perl_module,扩展使用perl代码
–with-perl_modules_path=PATH 为perl模块设置路径
–with-perl=PATH 为perl库设置路径
–http-client-body-temp-path=PATH 为http连接的请求实体临时文件设置路径,如果没有指定,默认为/client_body_temp
–http-proxy-temp-path=PATH 为http代理临时文件设置路径,如果没有指定,默认为

/proxy_temp
–http-fastcgi-temp-path=PATH 为http fastcgi临时文件设置路径,如果没有指定,默认为

/fastcgi_temp
–http-uwsgi-temp-path=PATH 为http uwsgi临时文件设置路径,如果没有指定,默认为

/uwsgi_temp
–http-scgi-temp-path=PATH 为http scgi临时文件设置路径,如果没有指定,默认为

/scgi_temp
–without-http 禁用HTTP服务

配置邮件选项

–with-mail 启用IMAP4/POP3/SMTP代理模块,默认没有启用
–with-mail_ssl_module 启用ngx_mail_ssl_module,代理任何一种类型的使用SSL/TLS的mail
–without-mail_pop3_module 在启用mail模块后,禁用pop3模块
–without-mail_imap_module 在启用mail模块后,禁用IMAP模块
–without-mail_smtp_module 在启用mail模块后,禁用SMTP模块

使用模块选项

–with-http_ssl_module 启用ngx_http_ssl_module,启用SSL支持并且能够处理HTTPS请求。需要
OpenSSL,在Debian系统中,对应的包为libssl-dev
–with-http_realip_module 启用ngx_http_realip_module,如果nginx在七层负载均衡器之后,将http头中的客户端ip地址传递,需要启动该模块
–with-http_addition_module 启用ngx_http_addition_module,该模块作为一个输出过滤器,使得在请求经过一个location前或后时在该location本身增加内容
–with-http_xslt_module 启用ngx_http_xslt_module,用于处理xml响应转换,基于一个或多个XSLT格式(需要libxml2和libxslt库)
–with-http_image_filter_module 启用ngx_http_image_filter_module,该模块可以用来作为图像过滤器使用,在将图像投递到客户端之前进行处理(需要libgd库)
–with-http_geoip_module 启用ngx_http_geoip_module,该模块可以设置各种变量以便在配置文件中的区段使用,基于地理位置查找客户端ip地址(需要MaxMfind GeoIP库)
–with-http_sub_module 启用ngx_http_sub_module,该模块实现了替代过滤,在响应中用一个字符串替代另一个字符串
–with-http_dav_module 启用ngx_http_dav_module,该模块将激活WebDAV配置指令
–with-http_flv_module 启用ngx_http_flv_module,该模块可提供伪流媒体
–with-http_mp4_module 启用ngx_http_mp4_module,该模块可支持H.264/AAC文件伪流媒体
–with-http_gzip_static_module 启用ngx_http_gzip_static_module,当调用的资源没有.gz结尾格式的文件时,如果支持发送预压缩版本的静态文件,可以使用该模块
–with-http_gunzip_module 启用ngx_http_gunzip_module,对于不支持gzip编码的客户端,该模块可以用于为客户端解压缩预压缩内容
–with-http_random_index_module 启用ngx_http_random_index_module,如果想提供从一个目录中随机选择文件的索引文件,需要使用该模块
–with-http_secure_link_module 启用ngx_http_secure_link_module,该模块会将一个哈希值链接到一个url中,只有使用正确的密码能够计算链接
–with-http_stub_status_module 启用server status(服务状态)页,会收集nginx自身的状态信息,输出的状态信息可以使用RRDtool来绘制成图

禁用模块选项

–without-http_charset_module 禁用ngx_http_charset_module,该模块负责设置Content-Type响应头,以及从一个字符集转换到另一个字符集
–without-http_gzip_module 禁用ngx_http_gzip_module,该模块可以将返回给客户端的内容进行压缩,如果启用,需要zlib包。
–without-http_ssi_module 禁用ngx_http_ssi_module,该模块用于处理SSI
–without-http_userid_module 禁用ngx_http_userid_module,该模块使得nginx可以设置cookie,用于用户标识,变量$uid_set和$uid_got可以记录用户跟踪
–without-http_access_module 禁用ngx_http_access_module,该模块基于IP控制访问location
–without-http_auth_basic_module 禁用ngx_http_auth_basic_module,该模块通过http基本身份验证限制访问
–without-http_autoindex_module 禁用ngx_http_autoindex_module,该模块可以手机目录列出文件
–without-http_geo_module 禁用ngx_http_geo_module,该模块可以基于客户端IP地址设置配置变量,然后根据这些变量的值采取行动
–without-http_map_module 禁用ngx_http_map_module,该模块可以映射一个变量到另一个变量
–without-http_split_clients_module 禁用ngx_http_split_clients_module,该模块用于创建A/B测试的变量
–without-http_referer_module 禁用ngx_http_referer_module,该模块用于阻止基于referer的Http头的请求
–without-http_rewrite_module 禁用ngx_http_rewrite_module。该模块可以将请求传递到其他服务器或者服务器组,如果启用,需要PCRE包
–without-http_proxy_module 禁用ngx_http_proxy_module
–without-http_fastcgi_module 禁用ngx_http_fastcgi_module,该模块可以将请求传递到FastCGI服务器
–without-http_uwsgi_module 禁用ngx_http_uwsgi_module,该模块可以将请求传递到uWSGI服务器
–without-http_scgi_module 禁用ngx_http_scgi_module,该模块可以将请求传递到SCGI服务器
–without-http_memcached_module 禁用ngx_http_memcached_module,该模块可以与一个memcached服务器进行交互,将响应放置到变量查询中
–without-http_limit_conn_module 禁用ngx_http_limit_conn_module,该模块可以设置连接限制
–without-http_limit_req_module 禁用ngx_http_limit_req_module,该模块可以限制每个用户的请求率
–without-http_empty_gif_module 禁用ngx_http_empty_gif_module,该模块在内存中产生一个1*1像素的透明GIF图像
–without-http_browser_module 禁用ngx_http_browser_module,该模块可以基于User-Agent请求头配置
–without-http_upstream_ip_hash_module 禁用ngx_http_upstream_ip_hash_module,该模块定义了一组可以与不同代理模块结合使用的服务器

防火墙开放80端口

在 /etc/sysconfig/iptables文件中添加

1
-A INPUT -m state --state NEW -m multiport -p tcp -dport 22,80 -j ACCEPT

重启防火墙

1
service iptables restart

启动和停止nginx

这里提供两种方式来启动和停止nginx

一种是不作为一个后台服务来启动

1
2
3
4
5
6
#启动
nginx
# 指定配置文件启动
nginx -c /usr/local/nginx/sbin/conf/nginx.conf
# 停止
nginx -s stop

一种是作为后台服务来启动(开机自启动)

1
2
3
brew services start nginx

brew services stop nginx

欢迎关注我的其它发布渠道