服务器搭建:CentOS上安装配置nginx

本文详细介绍了如何在CentOS 6.3 mini版虚拟机环境中安装并配置Nginx和Tomcat。包括安装必要软件、下载并配置Nginx、防火墙配置、启动与停止Nginx的方法,以及如何配置自定义命令以实现开机启动。同时,还提供了一个用于启动和停止Nginx的脚本实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我这里是内网搭建的一个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

[root@node1 ~]# chkconfig --add nginx
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.


From: http://my.oschina.net/biezhi/blog/287614
参考:http://linux.chinaunix.net/techdoc/develop/2008/04/24/995704.shtml
修改html目录另参考:http://www.cnblogs.com/jenry/archive/2013/06/13/3134414.html

nginx(发音同engine x)是一款由俄罗斯程序员Igor Sysoev所开发轻量级的网页服务器、反向代理服务器以及电子邮件(IMAP/POP3)代理服务器。起初是供俄国大型的门户网站及搜索引擎Rambler(俄语:Рамблер)使用。此软件BSD-like协议下发行,可以在UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及Microsoft Windows等操作系统中运行。 nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态、静态页面的分离,可以按照轮询、ip哈希、URL哈希、权重等多种方式对后端服务器做负载均衡,同时还支持后端服务器的健康检查。 Nginx负载均衡一些基础知识: nginx 的 upstream目前支持 4 种方式的分配 1)、轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 2)、weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 2)、ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 3)、fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配。 4)、url_hash(第三方) 按访问的url的hash结果分配,使每个url定向到同一个后端服务器,后端为缓存服务器比较有效。 Nginx 截图: Nginx 更新日志: Nginx 1.6.0 稳定版发布,该版本包含很多来自 1.5.x 的新特性,包括各种 SSL 的提升、SPDY 3.1 支持、根据请求条件对缓存重新验证、认证请求模块等等。 SEnginx主线版本1.6.0发布,主要变更如下: Changes with senginx 1.6.0 14 May 2014 *) Feature: 升级到nginx 1.6.0 *) Feature: dynamic resolve功能增强,增加了设置DNS查询失败时的动作等功能 *) Feature: ngx_http_statistics模块,支持对流量和攻击的统计 *) Feature: http://demo.senginx.org ,用于演示ngx_http_statistics模块 *) Feature: 升级ModSecurity到2.8.0版本 *) Bugfix: 修改了cookie防篡改模块的若干bug
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值