Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
nginx的模块需要第三方库的支持,检查是否安装下列库:zlib、zlib-devel(nginx扩展,gzip压缩)、openssl、openssl-devel(nginx扩展)、prce、prce-devel(重写rewrite、支持nginx伪静态);Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个。
1、安装编译环境
1
|
[root@localhost ~]
# yum -y install gcc gcc-c++ automake autoconf libtool make
|
2、安装PCRE库
1
2
3
4
5
6
7
|
[root@localhost src]
# cd /usr/local/src/
[root@localhost src]
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
[root@localhost src]
# tar zxvf pcre-8.36.tar.gz
[root@localhost pcre-8.36]
# cd pcre-8.36
[root@localhost pcre-8.36]
# ./configure --prefix=/usr/local/pcre
[root@localhost pcre-8.36]
# make
[root@localhost pcre-8.36]
# make install
|
3、安装zlib库
1
2
3
4
5
6
7
|
[root@localhost pcre-8.36]
# cd /usr/local/src/
[root@localhost src]
# wget http://zlib.net/zlib-1.2.8.tar.gz
[root@localhost src]
# tar zxvf zlib-1.2.8.tar.gz
[root@localhost src]
# cd zlib-1.2.8
[root@localhost zlib-1.2.8]
# ./configure --prefix=/usr/local/zlib
[root@localhost zlib-1.2.8]
# make
[root@localhost zlib-1.2.8]
# make install
|
4、安装ssl
1
2
3
4
5
6
7
8
9
10
|
[root@localhost zlib-1.2.8]
# cd /usr/local/src/
[root@localhost src]
# wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
[root@localhost src]
# tar zxvf openssl-1.0.1j.tar.gz
[root@localhost src]
# cd openssl-1.0.1j
[root@localhost openssl-1.0.1j]
# ./config --prefix=/usr/local/openssl
[root@localhost openssl-1.0.1j]
# make
[root@localhost openssl-1.0.1j]
# make install
[root@localhost openssl-1.0.1j]
# vim /etc/profile
export
PATH=$PATH:
/usr/local/openssl/bin
[root@localhost openssl-1.0.1j]
# source /etc/profile
|
5、安装nginx
1
2
3
4
5
6
7
8
9
|
[root@localhost openssl-1.0.1j]
# cd ..
[root@localhost src]
# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[root@localhost src]
# tar zxvf nginx-1.6.2.tar.gz
[root@localhost src]
# cd nginx-1.6.2
[root@localhost nginx-1.6.2]
# groupadd www
[root@localhost nginx-1.6.2]
# useradd -g www www -s /sbin/nologin
[root@localhost nginx-1.6.2]
# ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=wwww --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1j --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.36
[root@localhost nginx-1.6.2]
# make
[root@localhost nginx-1.6.2]
# make install
|
--with-openssl=/usr/local/src/openssl-1.0.1j --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.36指向的是源码包解压的路径,而不是安装的路径,否则会报错
检查是否安装成功
1
2
3
4
5
6
|
[root@localhost nginx-1.6.2]
# cd /usr/local/nginx/sbin/
[root@localhost sbin]
# ./nginx -t
nginx: the configuration
file
/usr/local/nginx/conf/nginx
.conf syntax is ok
nginx: [emerg] getgrnam(
"wwww"
) failed
nginx: configuration
file
/usr/local/nginx/conf/nginx
.conf
test
failed
[root@localhost sbin]
#
|
编译时候指定组成wwww,重新修改下即可
1
2
3
4
5
6
7
8
|
[root@localhost nginx]
# usermod -g wwww www
[root@localhost nginx]
# id www
uid=500(www) gid=501(wwww)
groups
=501(wwww)
[root@localhost nginx]
# cd /usr/local/nginx/sbin/
[root@localhost sbin]
# ./nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration
file
/usr/local/nginx/conf/nginx
.conf syntax is ok
nginx: configuration
file
/usr/local/nginx/conf/nginx
.conf
test
is successful
[root@localhost sbin]
#
|
参数"-c"指定了配置文件的路径,如果不加'-c"参数, nginx,会默认加载其安装目录中conf子目录中的nginx.conf文件。
-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误
查看nginx版本:
-v:显示 nginx 版本号。
-V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。
1
2
3
4
5
6
7
8
|
[root@localhost sbin]
# ./nginx -V
nginx version: nginx
/1
.6.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=
/usr/local/nginx
--user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=
/usr/local/src/openssl-1
.0.1j --with-zlib=
/usr/local/src/zlib-1
.2.8 --with-pcre=
/usr/local/src/pcre-8
.36
[root@localhost sbin]
# ./nginx -v
nginx version: nginx
/1
.6.2
[root@localhost sbin]
#
|
启动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
|
[root@localhost sbin]
# ./nginx
[root@localhost sbin]
# /usr/local/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already
in
use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already
in
use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already
in
use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already
in
use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already
in
use)
nginx: [emerg] still could not bind()
[root@localhost sbin]
# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE
/OFF
NODE NAME
clock-app 7093 root 21u IPv4 53701 0t0 TCP 10.15.24.112:52763->63.238.2.211:http (ESTABLISHED)
nginx 10145 root 6u IPv4 53797 0t0 TCP *:http (LISTEN)
nginx 10146 www 6u IPv4 53797 0t0 TCP *:http (LISTEN)
[root@localhost sbin]
# kill -9 7093
[root@localhost sbin]
# kill -9 10145
[root@localhost sbin]
# kill -9 10146
[root@localhost sbin]
# /usr/local/nginx/sbin/nginx
[root@localhost sbin]
# netstat -ano|grep -i ":80"
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN off (0.00
/0/0
)
[root@localhost sbin]
# ps -ef|grep "nginx"
root 10167 1 0 05:17 ? 00:00:00 nginx: master process
/usr/local/nginx/sbin/nginx
www 10168 10167 0 05:17 ? 00:00:00 nginx: worker process
root 10174 1882 0 05:18 pts
/0
00:00:00
grep
nginx
[root@localhost sbin]
#
|
执行/usr/local/nginx/sbin/nginx时提示端口被占用,因为前面我已经启动过了./nginx导致端口被占用,
重启nginx
1
2
3
|
[root@localhost sbin]
# /usr/local/nginx/sbin/nginx -s reload
或者
[root@localhost sbin]
# kill -HUP `cat /usr/local/nginx/logs/nginx.pid `
|
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
更改默认站点位置:
默认站点在/usr/local/nginx/html/,现在更换到/var/www/html中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@localhost conf]
# vim nginx.conf
43 location / {
44
# root html;
45 root
/var/www/html
;
46 index index.html index.htm;
47 }
[root@localhost conf]
# cd /var/www/html/
[root@localhost html]
# vim index.html
Hello Justin!
[root@localhost conf]
# service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@localhost conf]
#
|
如果按照上面在location / {部分修改了root目录后还不能访问,类似报File not found错误大部分应该是/etc/nginx/conf.d/default.conf里面的php解析部分配置不对,解决的话就是把root定义,在server下加上,这样root的作用域就扩大了。把location ~ \.php${里面的root删除。fastcgi_param部分改为$document_root$fastcgi_script_name;(本次环境nginx是通过yum安装的)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@Zabbix zabbix]
# vim /etc/nginx/conf.d/default.conf
5 listen 80;
6 root
/usr/share/nginx/html/zabbix
;
#添加此行
7 server_name _;
13 location / {
14 root
/usr/share/nginx/html/zabbix
;
#添加此行
15
#root /usr/share/nginx/html;
16 index index.php index.html index.htm;
17
# example
42 location ~ \.php$ {
43
#root html; #注释此行
44 fastcgi_pass 127.0.0.1:9000;
45 fastcgi_index index.php;
46 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#修改此行
47 include fastcgi_params;
48 }
[root@Zabbix zabbix]
# service nginx restart
|
更改默认端口:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@ProxyServer conf]
# vim /opt/nginx/conf/nginx.conf
35 server {
36 listen 80; ;修改为需要的端口号
37 server_name localhost;
38
39
#charset koi8-r;
40
41
#access_log logs/host.access.log main;
42
43 location / {
44 root html;
45 index index.html index.htm;
46 }
|
禁止空主机头访问:
即直接输入IP地址后无法访问,设置返回404
1
|
[root@ProxyServer conf]
# cp nginx.conf nginx.conf20151106
|
找到server,在上面一行添加如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
34 server {
35 listen 80 default;
36 server_name _;
37 location / {
38 root html;
39
return
404;
40 }
41 location ~ /.ht {
42 deny all;
43 }
44 }
#添加如上内容:
45 server {
46 listen 80;
|
或者重定向到其他地方,如www.sina.com.cn
在文末添加以下内容
1
2
3
4
5
|
server {
server_name 192.168.100.198 ;
rewrite ^(.*) http:
//www
.sina.com$1 permanent;
}
}
|
切换到普通用户nginx下时候无法启动报:Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
以为是端口被占用,通过netstar发现80端口并未被占用,切换到root下面发现可以启动,查找资料:在Linux中1024以下的端口号都需要root权限才能使用,所以普通用户启动程序绑定会报出权限问题。于是我把80修改成了8089,结果可以正常启动
设置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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
[root@localhost ~]
# vim /etc/rc.d/init.d/nginx
[root@localhost ~]
# cat /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
.
/etc/rc
.d
/init
.d
/functions
# Source networking configuration.
.
/etc/sysconfig/network
|