服务器搭建:手把手教你搭建Tomcat7+Nginx1.6之配置nginx服务
我这里是内网搭建的一个centos6.3mini版的虚拟机环境。
-
nginx版本:1.6
-
tomcat版本:7.0.54
1. 安装nginx
在安装nginx前,需要确保系统安装了g++、gcc、openssl-devel、pcre-devel和zlib-devel软件。
安装必须软件:这里我使用的是yum安装,刚装好的操作系统是纯净的什么都没有,
1 | [root@unique ~] # yum install gcc-c++ |
2 | [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 | [root@unique ~] # wget http://nginx.org/download/nginx-1.6.0.tar.gz |
2 | [root@unique ~] # mv nginx-1.6.0.tar.gz /usr/local |
3 | [root@unique local ] # tar -zxv -f nginx-1.6.0.tar.gz |
4 | [root@unique local ] # mv nginx-1.6.0 nginx |
5 | [root@unique local ] # cd nginx |
6 | [root@unique nginx] # ./configure --prefix=/usr/local/nginx/ |
7 | [root@unique nginx] # make |
8 | [root@unique nginx] # make install |
在make install的时候遇到
01 | [root@unique nginx] # make install |
02 | make -f objs/Makefile install |
03 | make [1]: Entering directory `/usr/ local /nginx' |
04 | test -d '/usr/local/nginx/' || mkdir -p '/usr/local/nginx/' |
05 | test -d '/usr/local/nginx//sbin' || mkdir -p '/usr/local/nginx//sbin' |
06 | test ! -f '/usr/local/nginx//sbin/nginx' || mv '/usr/local/nginx//sbin/nginx' '/usr/local/nginx//sbin/nginx.old' |
07 | cp objs/nginx '/usr/local/nginx//sbin/nginx' |
08 | test -d '/usr/local/nginx//conf' || mkdir -p '/usr/local/nginx//conf' |
09 | cp conf/koi-win '/usr/local/nginx//conf' |
10 | cp : "conf/koi-win" 与 "/usr/local/nginx//conf/koi-win" 为同一文件 |
11 | make [1]: *** [ install ] 错误 1 |
12 | make [1]: Leaving directory `/usr/ local /nginx' |
13 | 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 | [root@unique nginx] # ./configure --prefix=/usr/local/nginx/ --conf-path=/usr/local/nginx/nginx.conf |
2 | [root@unique nginx] # make |
3 | [root@unique nginx] # make install |
O了,nginx就已经成功安装在我的系统上了。
2. 配置防火墙
1 | #修改防火墙配置: |
2 | [root@unique nginx] # vi + /etc/sysconfig/iptables |
3 | #添加配置项 |
4 | -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT |
5 |
6 | #重启防火墙和网络配置 |
7 | [root@unique nginx] # service iptables restart |
8 | [root@unique nginx] # /etc/init.d/network restart |
这样nginx的web服务就可以通过80端口访问了
3. 启动nginx
1 | #方法1 |
2 | [root@unique nginx] # /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf |
3 | #方法2 |
4 | [root@unique nginx] # cd /usr/local/nginx/sbin |
5 | [root@unique sbin] # ./nginx |
4. 停止nginx
1 | #查询nginx主进程号 |
2 | ps -ef | grep nginx |
3 | #停止进程 |
4 | kill -QUIT 主进程号 |
5 | #快速停止 |
6 | kill -TERM 主进程号 |
7 | #强制停止 |
8 | pkill -9 nginx |
5. 重启nginx
1 | [root@unique sbin] # /usr/local/nginx/sbin/nginx -s reload |
6. 测试nginx
1 | #测试端口 |
2 | netstat –na| grep 80 |
3 | #浏览器中测试 |
4 | http://ip:80 |
酷炫的nginx就配置完成了~
7. 扩展:配置自定义命令
这里就做一个自定义的nginx启动停止脚本
1 | [root@unique sbin] # vi /etc/init.d/nginx |
把下面的脚本复制进去然后保存
01 | #! /bin/sh |
02 | # Default-Start: 2 3 4 5 |
03 | # Default-Stop: 0 1 6 |
04 | # Short-Description: starts the nginx web server |
05 |
06 | PATH=/usr/ local /sbin:/usr/ local /bin:/sbin:/bin:/usr/sbin:/usr/bin |
07 | DESC= "nginx daemon" |
08 | NAME=nginx |
09 | DAEMON=/usr/ local /nginx/sbin/$NAME |
10 | CONFIGFILE=/usr/ local /nginx/conf/$NAME.conf |
11 | PIDFILE=/usr/ local /nginx/logs/$NAME.pid |
12 | SCRIPTNAME=/etc/init.d/$NAME |
13 |
14 | set -e |
15 | [ -x "$DAEMON" ] || exit 0 |
16 |
17 | do_start() { |
18 | $DAEMON -c $CONFIGFILE || echo -n "nginx already running" |
19 | } |
20 |
21 | do_stop() { |
22 | kill -INT ` cat $PIDFILE` || echo -n "nginx not running" |
23 | } |
24 |
25 | do_reload() { |
26 | kill -HUP ` cat $PIDFILE` || echo -n "nginx can't reload" |
27 | } |
28 |
29 | case "$1" in |
30 | start) |
31 | echo -n "Starting $DESC: $NAME" |
32 | do_start |
33 | echo "." |
34 | ;; |
35 | stop) |
36 | echo -n "Stopping $DESC: $NAME" |
37 | do_stop |
38 | echo "." |
39 | ;; |
40 | reload|graceful) |
41 | echo -n "Reloading $DESC configuration..." |
42 | do_reload |
43 | echo "." |
44 | ;; |
45 | restart) |
46 | echo -n "Restarting $DESC: $NAME" |
47 | do_stop |
48 | do_start |
49 | echo "." |
50 | ;; |
51 | *) |
52 | echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2 |
53 | exit 3 |
54 | ;; |
55 | esac |
56 |
57 | exit 0 |
给文件添加执行权限
1 | [root@unique sbin] # chmod +x /etc/init.d/nginx |
2 | #然后可以通过 |
3 | #/etc/init.d/nginx start 命令启动nginx |
4 | #/etc/init.d/nginx stop 命令停止nginx |
5 | #/etc/init.d/nginx restart 命令重启nginx |
6 |
7 | #重启nginx |
8 | [root@unique init.d] # /etc/init.d/nginx restart |
9 | Restarting nginx daemon: nginx. |
8. 扩展:配置开机启动
如果需要开机启动服务,保存好 /etc/init.d/nginx文件后,执行以下命令:
1 | [root@unique init.d] #chkconfig --add ningx |
2 | [root@unique init.d] #chkconfig --level nginx 2345 on |