LINUX 搭建Nginx服务器

问题
在IP地址为192.168.4.5的主机上安装部署Nginx服务,并可以将Nginx服务器,要求编译时启用如下功能:
支持SSL加密功能
设置Nginx账户及组名称均为nginx
Nginx服务器升级到更高版本。
然后客户端访问页面验证Nginx Web服务器:
使用火狐浏览器访问
使用curl访问
1.2 方案
提前准备运维课程所需的所有虚拟机,为后续所有实验做准备,克隆4台RHEL7虚拟机,实验环境所需要的主机及对应的IP设置列表如表-1所示,正确配置IP地址、主机名称,并且为每台主机配置YUM源。不需要配置网关与DNS。
表-1 主机列表
LINUX 搭建Nginx服务器_第1张图片
第一天课程需要使用2台RHEL7虚拟机,其中一台作为Nginx服务器(192.168.4.5)、另外一台作为测试用的Linux客户机(192.168.4.10),如图-1所示。

图-1
安装nginx-1.10.3版本时,需要使用如下参数:
–with-http_ssl_module:提供SSL加密功能
–user:指定账户 --group:指定组

1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:构建Nginx服务器
1)使用源码包安装nginx软件包

1.[root@proxy ~]# yum -y install gcc pcre-devel openssl-devel        //安装依赖包
2.[root@proxy ~]# useradd -s /sbin/nologin nginx
3.[root@proxy ~]# tar  -xf   nginx-1.10.3.tar.gz
4.[root@proxy ~]# cd  nginx-1.10.3
5.[root@proxy nginx-1.10.3]# ./configure   \
6.> --prefix=/usr/local/nginx   \                //指定安装路径
7.> --user=nginx   \                            //指定用户
8.> --group=nginx  \                            //指定组
9.> --with-http_ssl_module                        //开启SSL加密功能
10.  .. ..
11.nginx path prefix: "/usr/local/nginx"
12.  nginx binary file: "/usr/local/nginx/sbin/nginx"
13.  nginx configuration prefix: "/usr/local/nginx/conf"
14.  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
15.  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
16.  nginx error log file: "/usr/local/nginx/logs/error.log"
17.  nginx http access log file: "/usr/local/nginx/logs/access.log"
18.  nginx http client request body temporary files: "client_body_temp"
19.  nginx http proxy temporary files: "proxy_temp"
20.  nginx http fastcgi temporary files: "fastcgi_temp"
21.  nginx http uwsgi temporary files: "uwsgi_temp"
22.  nginx http scgi temporary files: "scgi_temp"
23.[root@proxy nginx-1.10.3]# make && make install    //编译并安装

2)nginx命令的用法

1.[root@proxy ~]# /usr/local/nginx/sbin/nginx                    //启动服务
2.[root@proxy ~]# /usr/local/nginx/sbin/nginx -s stop            //关闭服务
3.[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload        //重新加载配置文件
4.[root@proxy ~]# /usr/local/nginx/sbin/nginx –V                //查看软件信息
5.[root@proxy ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/        //方便后期使用

netstat命令可以查看系统中启动的端口信息,该命令常用选项如下:
-a显示所有端口的信息
-n以数字格式显示端口号
-t显示TCP连接的端口
-u显示UDP连接的端口
-l显示服务正在监听的端口信息,如httpd启动后,会一直监听80端口
-p显示监听端口的服务名称是什么(也就是程序名称)
nginx服务默认通过TCP 80端口监听客户端请求:

1.root@proxy ~]# netstat  -anptu  |  grep nginx
2.tcp        0        0 0.0.0.0:80        0.0.0.0:*        LISTEN        10441/nginx

3)设置防火墙与SELinux

1.[root@proxy ~]# firewall-cmd --set-default-zone=trusted
2.[root@proxy ~]# setenforce 0

4)测试首页文件
Nginx Web服务默认首页文档存储目录为/usr/local/nginx/html/,在此目录下默认有一个名为index.html的文件,使用客户端访问测试页面:

1.[root@client ~]# curl http://192.168.4.5
2.
3.
4.Welcome to nginx!
5.
6.
7.

Welcome to nginx!

8. 9.

步骤二:升级Nginx服务器
1)编译新版本nginx软件

1.[root@proxy ~]# tar  -zxvf   nginx-1.12.2.tar.gz
2.[root@proxy ~]# cd nginx-1.12.2
3.[root@proxy nginx-1.12.2]# ./configure   \
4.> --prefix=/usr/local/nginx   \ 
5.> --user=nginx   \ 
6.> --group=nginx  \ 
7.> --with-http_ssl_module
8.[root@proxy nginx-1.12.2]# make            
  1. 备份老的nginx主程序,并使用编译好的新版本nginx替换老版本

    1.[root@proxy nginx-1.12.2]# mv /usr/local/nginx/sbin/nginx
    2.>/usr/local/nginx/sbin/nginxold
    3.[root@proxy nginx-1.12.2]# cp objs/nginx /usr/local/nginx/sbin/ //拷贝新版本
    4.[root@proxy nginx-1.12.2]# make upgrade //升级
    5./usr/local/nginx/sbin/nginx -t
    6.nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    7.nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    8.kill -USR2 cat /usr/local/nginx/logs/nginx.pid
    9.sleep 1
    10.test -f /usr/local/nginx/logs/nginx.pid.oldbin
    11.kill -QUIT cat /usr/local/nginx/logs/nginx.pid.oldbin
    12.[root@proxy ~]# /usr/local/nginx/sbin/nginx –v //查看版本

步骤三:客户端访问测试
1)分别使用浏览器和命令行工具curl测试服务器页面
如果使用firefox火狐浏览器,注意在ssh远程的时候一定要加-X选项。

1.[root@client ~]# firefox http://192.168.4.5
[root@client ~]# curl http://192.168.4.5

你可能感兴趣的:(学习)