我这里是内网搭建的一个centos6.3mini版的虚拟机环境。
-
nginx版本:1.6
-
tomcat版本:7.0.54
1. 安装nginx
在安装nginx前,需要确保系统安装了g++、gcc、openssl-devel、pcre-devel和zlib-devel软件。
安装必须软件:这里我使用的是yum安装,刚装好的操作系统是纯净的什么都没有,
1
2
|
[root@unique ~]
# yum install gcc-c++
[root@unique ~]
# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
|
检查系统安装的Nginx:
1
|
[root@unique ~]
# find -name nginx
|
如果有的话卸载掉他即可,我这里还没有装。我用wget命令下载一个1.6.0版本的,移动在/usr/local下
1
2
3
4
5
6
7
|
[root@unique ~]
# wget http://nginx.org/download/nginx-1.6.0.tar.gz
[root@unique ~]
# mv nginx-1.6.0.tar.gz /usr/local
[root@unique
local
]
# tar -zxv -f nginx-1.6.0.tar.gz
[root@unique
local
]
# mv nginx-1.6.0 nginx
[root@unique
local
]
# cd nginx
[root@unique nginx]
# ./configure --prefix=/usr/local/nginx/
[root@unique nginx]
# make && make install
|
在make install的时候遇到
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@unique nginx]
# make install
make
-f objs
/Makefile
install
make
[1]: Entering directory `
/usr/local/nginx
'
test
-d
'/usr/local/nginx/'
||
mkdir
-p
'/usr/local/nginx/'
test
-d
'/usr/local/nginx//sbin'
||
mkdir
-p
'/usr/local/nginx//sbin'
test
! -f
'/usr/local/nginx//sbin/nginx'
||
mv
'/usr/local/nginx//sbin/nginx'
'/usr/local/nginx//sbin/nginx.old'
cp
objs
/nginx
'/usr/local/nginx//sbin/nginx'
test
-d
'/usr/local/nginx//conf'
||
mkdir
-p
'/usr/local/nginx//conf'
cp
conf
/koi-win
'/usr/local/nginx//conf'
cp
:
"conf/koi-win"
与
"/usr/local/nginx//conf/koi-win"
为同一文件
make
[1]: *** [
install
] 错误 1
make
[1]: Leaving directory `
/usr/local/nginx
'
make
: *** [
install
] 错误 2
|
这里估计不少人已经出错了,很多编译安装的说明都没有设置conf-path,但是我没有设置的话,在make install 阶段,会出现cp: `conf/koi-win’ and `/usr/local/nginx/conf/koi-win’ are the same file错误。所以我们在这里设置一下,那我们就指定好nginx的conf重新来配置一遍
1
2
|
[root@unique nginx]
# ./configure --prefix=/usr/local/nginx/ --conf-path=/usr/local/nginx/nginx.conf
[root@unique nginx]
# make && make install
|
O了,nginx就已经成功安装在我的系统上了。
2. 配置防火墙
1
2
3
4
5
6
7
8
|
#修改防火墙配置:
[root@unique nginx]
# vi + /etc/sysconfig/iptables
#添加配置项
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
#重启防火墙和网络配置
[root@unique nginx]
# service iptables restart
[root@unique nginx]
# /etc/init.d/network restart
|
这样nginx的web服务就可以通过80端口访问了
3. 启动nginx
1
2
3
4
5
|
#方法1
[root@unique nginx]
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#方法2
[root@unique nginx]
# cd /usr/local/nginx/sbin
[root@unique sbin]
# ./nginx
|
4. 停止nginx
1
2
3
4
5
6
7
8
|
#查询nginx主进程号
ps
-ef |
grep
nginx
#停止进程
kill
-QUIT 主进程号
#快速停止
kill
-TERM 主进程号
#强制停止
pkill -9 nginx
|
5. 重启nginx
1
|
[root@unique sbin]
# /usr/local/nginx/sbin/nginx -s reload
|
6. 测试nginx
1
2
3
4
|
#测试端口
netstat
–na|
grep
80
#浏览器中测试
http:
//ip
:80
|
酷炫的nginx就配置完成了~
7. 扩展:配置自定义命令
这里就做一个自定义的nginx启动停止脚本
1
|
[root@unique sbin]
# vi /etc/init.d/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
|
#! /bin/sh
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
PATH=
/usr/local/sbin
:
/usr/local/bin
:
/sbin
:
/bin
:
/usr/sbin
:
/usr/bin
DESC=
"nginx daemon"
NAME=nginx
DAEMON=
/usr/local/nginx/sbin/
$NAME
CONFIGFILE=
/usr/local/nginx/conf/
$NAME.conf
PIDFILE=
/usr/local/nginx/logs/
$NAME.pid
SCRIPTNAME=
/etc/init
.d/$NAME
set
-e
[ -x
"$DAEMON"
] ||
exit
0
do_start() {
$DAEMON -c $CONFIGFILE ||
echo
-n
"nginx already running"
}
do_stop() {
kill
-INT `
cat
$PIDFILE` ||
echo
-n
"nginx not running"
}
do_reload() {
kill
-HUP `
cat
$PIDFILE` ||
echo
-n
"nginx can't reload"
}
case
"$1"
in
start)
echo
-n
"Starting $DESC: $NAME"
do_start
echo
"."
;;
stop)
echo
-n
"Stopping $DESC: $NAME"
do_stop
echo
"."
;;
reload|graceful)
echo
-n
"Reloading $DESC configuration..."
do_reload
echo
"."
;;
restart)
echo
-n
"Restarting $DESC: $NAME"
do_stop
do_start
echo
"."
;;
*)
echo
"Usage: $SCRIPTNAME {start|stop|reload|restart}"
>&2
exit
3
;;
esac
exit
0
|
给文件添加执行权限
1
2
3
4
5
6
7
8
9
|
[root@unique sbin]
# chmod +x /etc/init.d/nginx
#然后可以通过
#/etc/init.d/nginx start 命令启动nginx
#/etc/init.d/nginx stop 命令停止nginx
#/etc/init.d/nginx restart 命令重启nginx
#重启nginx
[root@unique init.d]
# /etc/init.d/nginx restart
Restarting nginx daemon: nginx.
|
8. 扩展:配置开机启动
如果需要开机启动服务,保存好 /etc/init.d/nginx文件后,执行以下命令:
1
2
|
[root@unique init.d]
#chkconfig --add ningx
[root@unique init.d]
#chkconfig --level nginx 2345 on
|
service nginx does not support chkconfig
很是奇怪,后经过查找资料,发现如果想添加脚本用service启动,必须要脚本里面包含这2行:
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve html files and CGI.