-
1 案例1:搭建Nginx服务器
-
在IP地址为192.168.4.5的主机上安装部署Nginx服务,并可以将Nginx服务器,要求编译时启用如下功能:
-
步骤一:构建Nginx服务器
-
1)使用源码包安装nginx软件包
[root@svr5 ~]# yum –y install gcc pcre-devel openssl-devel #安装常见依赖包
[root@svr5 ~]# useradd –s /sbin/nologin nginx
[root@svr5 ~]# tar -xf nginx-1.8.0.tar.gz
[root@svr5 ~]# cd nginx-1.8.0
[root@svr5 nginx-1.8.0]# ./configure \
> --prefix=/usr/local/nginx \ #指定安装路径
> --user=nginx \ #指定用户
> --group=nginx \ #指定组
> --with-http_ssl_module #开启SSL加密功能
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
[root@svr5 nginx-1.7.10]# make && make install #编译并安装
- 2)nginx命令的用法
[root@svr5 ~]# /usr/local/nginx/sbin/nginx #启动服务
[root@svr5 ~]# /usr/local/nginx/sbin/nginx -s stop #关闭服务
[root@svr5 ~]# /usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –V #查看软件信息
- nginx服务默认通过TCP 80端口监听客户端请求:
- [root@svr5 ~]# netstat -anptu | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10441/squid
root@psvr5~]# kill 10441 #关闭非nginx 80端口
[root@svr5 ~]# /usr/local/nginx/sbin/nginx #重启启动服务
[root@svr5 ~]# netstat -anptu | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10441/nginx
-
3)为Nginx Web服务器建立测试首页文件
-
Nginx Web服务默认首页文档存储目录为/usr/local/nginx/html/,在此目录下建立一个名为index.html的文件:
[root@svr5 ~]# cat /usr/local/nginx/html/index.html
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
-
步骤二:升级Nginx服务器
-
1)编译新版本nginx软件
[root@svr5 ~]# tar -zxvf nginx-1.9.0.tar.gz
[root@svr5 ~]# cd nginx-1.9.0
[root@svr5 nginx-1.9.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module
[root@svr5 nginx-1.9.0]# make
- 备份老的nginx主程序,并使用编译好的新版本nginx替换老版本
[root@svr5 nginx-1.9.0]# mv /usr/local/nginx/sbin/nginx \
>/usr/local/nginx/sbin/nginxold
[root@svr5 nginx-1.9.0]# cp objs/nginx /usr/local/nginx/sbin/ //拷贝新版本
[root@svr5 nginx-1.9.0]# 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`
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –v //查看版本
骤三:客户端访问测试
1)分别使用浏览器和命令行工具curl测试服务器页面
[root@client ~]# firefox http://192.168.4.5
[root@client ~]# curl http://192.168.4.5