红联Linux门户
Linux帮助

CentOS 6.5 Nginx搭建Web服务器

发布时间:2015-04-06 21:50:50来源:linux网站作者:jinjianjun

CentOS 6.5 Nginx搭建Web服务器,实现平滑升级,虚拟主机及访问控制。


一.搭建Nginx服务器

1.释放80端口

# netstat -tulnp | grep :80

# service httpd stop

# chkconfig --level 35 httpd off

2.安装依赖软件包

# yum -y groupinstall“开发工具”“开发库”

# yum -y install gcc gcc-c++ make

# yum-y install pcre-devel

# yum -y install openssl-devel

3.安装Nginx软件

# useradd -M -s /sbin/nologin nginx    //创建nginx程序用户

#tar -zxvf nginx-0.8.55.tar.gz

#cd nginx-0.8.55

./configure  \

> --prefix=/usr/local/nginx    \

> --user=nginx  \

> --group=nginx  \

> --with-http_stub_status_module  \

> --with-http_ssl_module

# make && make install

4.开启服务

# cd /usr/local/nginx/sbin

# ./nginx

# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local

//设置为开机启动

# netstat -tulnp | grep :80

tcp0      0 0.0.0.0:80      0.0.0.0:*  LISTEN      6924/nginx


二.平滑升级(在不停止服务的情况下,升级Nginx软件版本)

#tar -zxvf nginx-1.0.5.tar.gz  //解压高版本Nginx

#cd nginx-1.0.5

./configure  \

> --prefix=/usr/local/nginx    \

> --user=nginx  \

> --group=nginx  \

> --with-http_stub_status_module  \

> --with-http_ssl_module

# make

# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

# cp objs/nginx /usr/local/nginx/sbin/

# make upgrade

/usr/local/nginx/sbin/nginx -t

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

kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

sleep 1

test -f /usr/local/nginx/logs/nginx.pid.oldbin

kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

# /usr/local/nginx/sbin/nginx -v  //查看nginx版本

nginx: nginx version: nginx/1.0.5


三.配置Nginx虚拟主机

1.基于域名虚拟主机

1.1 编辑主配置文件

# vim /usr/local/nginx/conf/nginx.conf

……

17 http {

18    include      mime.types;

19    default_type  application/octet-stream;

……

34    server  {

35listen  80;

36server_name  www.linux.com;

37  location  /  {

38root  /www;

39index  index.html;

40  }

41    }

42    server  {

43listen  80;

44server_name  bbs.linux.com;

45location  /  {

46root  /bbs;

47index  index.html;

48  }

1.2 制作测试网页

# mkdir /www

# mkdir /bbs

# echo www.linux.com > /www/index.html      

# echo bbs.linux.com > /bbs/index.html

1.3 检查配置文件

# ./nginx -t    

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

1.4 重启服务

# /usr/local/nginx/sbin/nginx -s stop

# /usr/local/nginx/sbin/nginx

1.5 客户端测试

# vim /etc/hosts

192.168.100.1  www.linux.com  www

192.168.100.1  bbs.linux.com  bbs

# elinks -dump http://www.linux.com

www.linux.com

# elinks -dump http://bbs.linux.com

bbs.linux.com

2.基于端口的虚拟主机

2.1 修改主配置文件

server  {

listen  8080;

server_name  www.linux.com;

location  /  {

root  /www;

index  index.html;

}

}

server  {

listen  8090;

server_name  www.linux.com;

location  /  {

root  /bbs;

index  index.html;

}

}

2.2 重启服务

# /usr/local/nginx/sbin/nginx -s stop

# /usr/local/nginx/sbin/nginx

2.3 客户端测试

# elinks -dump http://www.linux.com:8080

www.linux.com

# elinks -dump http://www.linux.com:8090

bbs.linux.com

3.基于ip的虚拟主机

3.1 添加IP

# ifconfig eth0:0 192.168.100.2

3.2 修改主配置文件

server  {

listen  192.168.100.1:80;

#server_name  www.linux.com;

location  /  {

root  /www;

index  index.html;

}

}

server  {

listen  192.168.100.2:80;

#server_name  www.linux.com;

location  /  {

root  /bbs;

index  index.html;

}

}

3.3 重启服务

# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

3.4 客户端测试

# elinks -dump 192.168.100.1

www.linux.com

# elinks -dump 192.168.100.2

bbs.linux.com


四、客户端访问控制

实验需求:配置基于域名的虚拟主机

www.linux.com

bbs.linux.com

只允许从ip 192.168.100.254主机访问  bbs.tarena.com的8080端口,访问时要提交正确的验证用户admin密码123456方可访问

4.1 修改主配置文件

# vim /usr/local/nginx/conf/nginx.conf

……

server  {

listen  192.168.100.1:80;

server_name  www.linux.com;

location  /  {

root  /www;

index  index.html;

}

}

server  {

listen  192.168.100.1:8080;

server_name  bbs.linux.com;

location  /  {

root  /bbs;

index  index.html;

allow 192.168.100.254;

deny all;

auth_basic  "check your name";

auth_basic_user_file    /usr/local/nginx/conf/authuser.txt;

}

……

4.2 创建验证用户

# htpasswd -c /usr/local/nginx/conf/authuser.txt admin

New password:

Re-type new password:

Adding password for user admin

# cat /usr/local/nginx/conf/authuser.txt

admin:m37ojgyep3fls

4.3 重启服务

# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

4.4 客户端测试
CentOS 6.5 Nginx搭建Web服务器

注:以上图片上传到红联Linux系统教程频道中。