①搭建Nginx服务器/②SSL加密网站/③基于域名虚拟主机

1.搭建Nginx服务器

环境如下:
使用2台RHEL7 虚拟机,其中一台作为Nginx服务器(192.168.4.5)、另外一台作为测试用的Linux客户机(192.168.4.100),如图-1所示。
在这里插入图片描述
安装nginx-1.8.0版本时,需要使用如下参数:
with-http_ssl_module:提供SSL加密功能
user:指定账户
group:指定组

步骤一:构建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”
nginx error log file: “/usr/local/nginx/logs/error.log”
nginx http access log file: “/usr/local/nginx/logs/access.log”
nginx http client request body temporary files: “client_body_temp”
nginx http proxy temporary files: “proxy_temp”
nginx http fastcgi temporary files: “fastcgi_temp”
nginx http uwsgi temporary files: “uwsgi_temp”
nginx http scgi temporary files: “scgi_temp”

[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 nginx ##监听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服务器

步骤二:升级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      ##这里不执行make instal

2 ) 备份老的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

2.用户认证

2-1问题
沿用上面的练习一,通过调整Nginx服务端配置,实现以下目标:
1.访问Web页面需要进行用户认证
2.用户名为:tom,密码为:123456

2-2方案
通过Nginx实现Web页面的认证,需要修改Nginx配置文件,在配置文件中添加auth语句实现用户认证。最后使用htpasswd命令创建用户及密码即可。

2-3 步骤
实现此案例需要按照如下步骤进行。

步骤一:修改Nginx配置文件

1)修改/usr/local/nginx/conf/nginx.conf

[root@pc205 ~]# vim /usr/local/nginx/conf/nginx.conf

… …
server {
listen 80;
server_name localhost;
auth_basic “Input Password:”; //认证提示符
auth_basic_user_file “/usr/local/nginx/pass”; //认证密码文件
location / {
root html;
index index.html index.htm;
}
}

2)生成密码文件,创建用户及密码

使用htpasswd命令创建账户文件,需要确保系统中已经安装了httpd-tools。

[root@svr5 ~]# yum -y install  httpd-tools

[root@svr5 ~]# htpasswd -cm /usr/local/nginx/pass  tom 
 //创建密码文件:
New password:******(123456) 
   Re-type new password: 
   Adding password for user tom

3)重启Nginx服务

[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s stop
[root@svr5 ~]# /usr/local/nginx/sbin/nginx

步骤二:客户端测试

1)登录192.168.4.100客户端主机进行测试

root@client ~]# firefox http://192.168.4.5
//输入密码后可以访问

3.基于域名的虚拟主机

3.1 问题
沿用练习二,配置基于域名的虚拟主机,实现以下目标:
1.实现两个基于域名的虚拟主机,域名分别为 www.tarena.combbs.tarena.com
2.对域名为bbs.tarena.com的站点进行用户认证,用户名称为tom,密码为123456

3.2 方案
修改Nginx配置文件,添加server容器实现虚拟主机功能;对于需要进行用户认证的虚拟主机添加auth认证语句。

3.3 步骤
实现此案例需要按照如下步骤进行。

步骤一:修改配置文件

1)修改Nginx服务配置,添加相关虚拟主机配置如下

[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf

… …
server {
listen 80; //端口
server_name bbs.tarena.com;//域名
auth_basic “Input Password:” ; //认证提示符
auth_basic_user_file “/usr/local/nginx/pass”; //认证密码文件
location / {
root html; //指定网站根路径
index index.html index.htm;
}

}
server {
listen 80; //端口
server_name www.tarena.com; //域名
location / {
root www; //指定网站根路径
index index.html index.htm;
}
}

2)创建账户及密码
[root@svr5 ~]# htpasswd –cm /usr/local/nginx/pass tom //创建账户密码文件
New password: ******
Re-type new password:******
Adding password for user tom

3)创建网站根目录及对应首页文件

[root@svr5 ~]# mkdir /usr/local/nginx/www
[root@svr5 ~]# echo "www" > /usr/local/nginx/www/index.html

4)重启nginx服务

[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload

步骤二:客户端测试

1)修改客户端主机192.168.4.100的/etc/hosts文件,进行域名解析

[root@client ~]# vim /etc/hosts
192.168.4.5    www.tarena.com  bbs.tarena.com

2)登录192.168.4.100客户端主机进行测试

[root@client ~]# firefox http://bbs.tarena.com            
//输入密码后可以访问

[root@client ~]# firefox http://www.tarena.com
//直接访问

4.SSL虚拟主机/加密虚拟主机

4.1 问题
沿用练习二,配置基于加密网站的虚拟主机,实现以下目标:
1.域名为 www.test.com
2.该站点通过https访问
3.通过私钥、证书对该站点所有数据加密

4.2 方案
源码安装Nginx时必须使用–with-http_ssl_module参数,启用加密模块,对于需要进行SSL加密处理的站点添加ssl相关指令(设置网站需要的私钥和证书)。

4.3 步骤
实现此案例需要按照如下步骤进行。

步骤一:配置SSL虚拟主机

1)生成私钥与证书

[root@svr5 ~]# cd /usr/local/nginx/conf

[root@svr5 ~]# openssl genrsa -out cert.key                           
 //生成私钥
[root@svr5 ~]# openssl req -new -x509 -key cert.key -out cert.pem      
//生成证书

2)修改Nginx配置文件,设置加密网站的虚拟主机

[root@svr5 ~]# vim  /usr/local/nginx/conf/nginx.conf

… …
server {
listen 443 ssl;
server_name www.test.com;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}

步骤二:客户端验证

1)修改客户端主机192.168.4.100的/etc/hosts文件,进行域名解析
[root@client ~]# vim /etc/hosts
192.168.4.5 www.test.com

2)登录192.168.4.100客户端主机进行测试

[root@client ~]# firefox https://www.test.com           
 //信任证书后可以访问

猜你喜欢

转载自blog.csdn.net/lxh_2018_11_04/article/details/87228890