Linux 搭建Nginx并添加配置 SSL 证书

2. 安装准备
2.1 gcc安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
[root@nginx ~]# yum -y install gcc-c++
2.2 pcre安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。
[root@nginx ~]# yum -y install pcre pcre-devel
2.3 zlib安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
[root@nginx ~]# yum -y install zlib zlib-devel
2.4 OpenSSL安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
[root@nginx ~]# yum -y install openssl openssl-devel
 
3. Nginx安装
3.1 Nginx版本
图片.png
图片.png
选择最新的稳定版 nginx-1.12.2
版本说明:
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
Stable version:最新稳定版,生产环境上建议使用的版本
Legacy versions:遗留的老版本的稳定版
3.2 Nginx下载
使用wget命令下载
1 [root@nginx ~]# wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
如没有wget命令则安装:
[root@nginx ~]# yum -y install wget
3.3 解压
[root@nginx ~]# tar -zxvf nginx-1.12.2.tar.gz
 
3.4.1 新建nginx用户和组
[root@nginx include]# groupadd nginx 
[root@nginx include]# useradd -g nginx -d /home/nginx nginx 
[root@nginx include]# passwd nginx
3.4.2第三方模块安装
本文以安装第三方模块sticky为例,版本为1.,2.5,下载地址: https://pan.baidu.com/s/1Zpv6axGNUJkkGcam7EoLaQ 密码:6jaq
上传解压:
[root@nginx ~]# tar -zxvf nginx-goodies-nginx-sticky-module-ng-08a395c66e42..gz [root@nginx ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-1.2.5
3.4.3 安装
[root@nginx ~]# cd nginx-1.12.2 [root@nginx nginx-1.12.2]# ./configure --add-module=/root/nginx-sticky-1.2.5
指定用户、路径和模块配置(可选):
./configure \ --user=nginx --group=nginx \ #安装的用户组 --prefix=/usr/local/nginx \ #指定安装路径 --with-http_stub_status_module \ #监控nginx状态,需在nginx.conf配置 --with-http_ssl_module \ #支持HTTPS --with-http_sub_module \ #支持URL重定向 --with-http_gzip_static_module #静态压缩 --add-module=/root/nginx-sticky-1.2.5 #安装sticky模块
3.5 编译
[root@nginx nginx-1.12.2]# make &&。、
报错:
/root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c: 在函数‘ngx_http_sticky_misc_sha1’中: /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:15: 错误:‘SHA_DIGEST_LENGTH’未声明(在此函数内第一次使用) u_char hash[SHA_DIGEST_LENGTH]; ^ /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:15: 附注:每个未声明的标识符在其出现的函数内只报告一次 /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:176:10: 错误:未使用的变量‘hash’ [-Werror=unused-variable] u_char hash[SHA_DIGEST_LENGTH]; ^ /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c: 在函数‘ngx_http_sticky_misc_hmac_sha1’中: /root/nginx-sticky-1.2.5//ngx_http_sticky_misc.c:242:15: 错误:‘SHA_DIGEST_LENGTH’未声明(在此函数内第一次使用) u_char hash[SHA_DIGEST_LENGTH];
图片.png
图片.png
解决方法:
修改ngx_http_sticky_misc.c文件,新增#include <openssl/sha.h>和#include <openssl/md5.h>模块
[root@nginx nginx-1.12.2]# sed -i '12a #include <openssl/sha.h>' /root/nginx-sticky-1.2.5/ngx_http_sticky_misc.c 
[root@nginx nginx-1.12.2]# sed -i '12a #include <openssl/md5.h>' /root/nginx-sticky-1.2.5/ngx_http_sticky_misc.c
重新编译:
[root@nginx nginx-1.12.2]# make && make install
3.6 nginx命令全局执行设置
[root@nginx bin]# cd /usr/local/nginx/sbin/ 
[root@nginx sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
 
4. Nginx相关命令
4.1 版本查看
[root@nginx ~]# nginx -v nginx version: nginx/1.12.2
4.2 查看加载的模块
[root@nginx ~]# nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) configure arguments: --add-module=/root/nginx-sticky-1.2.5/
4.3 启停命令
4.3.1 启动
[root@nginx nginx-1.12.2]# nginx
4.3.2 停止
[root@nginx nginx-1.12.2]# nginx -s stop 
[root@nginx nginx-1.12.2]# nginx -s quit
4.3.3 动态加载
[root@nginx nginx-1.12.2]# ngins -s reload
4.3.4 测试配置文件nginx.conf正确性
[root@nginx ~]# 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
nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
nginx -s reload:动态加载,当配置文件nginx.conf有变化时执行该命令动态加载。
4.4 开机自启动
编辑/etc/rc.d/rc.local文件,新增一行/usr/local/nginx/sbin/nginx
1 [root@nginx rc.d]# cd /etc/rc.d 
2 [root@nginx rc.d]# sed -i '13a /usr/local/nginx/sbin/nginx' /etc/rc.d/rc.local 
3 [root@nginx rc.d]# chmod u+x rc.local
 
5. 更改默认端口
编辑配置文件/usr/local/nginx/conf/nginx.conf,将默认端口80修改为81:
1 [root@nginx ~]# view /usr/local/nginx/conf/nginx.conf
图片.png
图片.png
加载配置:
1 [root@nginx ~]# nginx -s reload
 
6. 访问Nginx
6.1 关闭防火墙
1 [root@nginx ~]# firewall-cmd --state running 
2 [root@nginx ~]# systemctl stop firewalld.service 
3 [root@nginx ~]# firewall-cmd --state not running
6.2 访问Nginx
图片.png
图片.png
配置ssl证书之前,先准备SSL证书,至于获取的途径很多(阿里云的服务,第三方服务购买)。这里不详细解释。以下是我的SSL证书
 
 
准备好证书后,找到nginx的安装目录,我的安装位置为:/usr/local/nginx
 
 
进入 config/nginx.conf
如果没有装winscp(一款可视化文件操作工具)的。可以通过命令行的方式,编辑nginx的config文件。
 
开始配置文件的修改
在修改配置文件之前,最好做一个备份,防止修改错误,也能及时回退错误
 
1、找到第一个监听80端口的server:一下是我修改好的server
 
 1 server {
 2     listen 80;
 3     server_name 需要访问的域名;
 4  
 5     rewrite ^(.*) https://$server_name$1 permanent; #这句是代表 把    http的域名请求转成https
 6  
 7     #charset koi8-r;
 8     #access_log logs/host.access.log main;
 9     location / {
10         root html;
11         index index.html index.htm;
12        proxy_pass http://需要访问的域名; #因为这里还是80端口,所以保持http就可以了
13  
14     }
15 } 
 
在实际的配置文件中,最好把我上面的备注删除
 
2、第一个server修改好了之后。那么就需要开始配置第二个server。拉到文件的底部。看到有一个https类型的server。而且已经全部被注释封上了,这是我们需要改的第二个server,如图:
 
 
这里除了HTTPS server这行之外,其他的 # 删除,启动https模块
 
 1 # HTTPS server
 2 #
 3 server {
 4     listen 443 ssl;
 5     server_name 需要访问的域名,这里也不用加https;
 6  
 7     ssl on;
 8  
 9     ssl_certificate /usr/local/nginx/ssl/ssl.pem; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
10     ssl_certificate_key /usr/local/nginx/ssl/ssl.key; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
11  
12  
13     ssl_session_cache shared:SSL:1m;
14     ssl_session_timeout 5m;
15  
16     ssl_ciphers HIGH:!aNULL:!MD5;
17     ssl_prefer_server_ciphers on;
18  
19     location / {
20         proxy_pass http://需要访问的域名:8080/;
21     }
22 }
 
配置好后,nginx的配置就算是完成了。不过这只是配置ssl证书的第一步!
 
接下来就是要让配置文件生效:
 
1、进去nginx的sbin文件夹,我的sbin文件夹在:/usr/local/nginx/sbin
执行以下语句:检验配置文件是否有错误
 
./nginx -t
 
如果nginx曾经安装过SSL模块,那么应该会显示以下界面:(如果已经显示配置成功,那么可以跳过这一步,直接重启nginx就可以了)
 
可是大多数第一次安装https证书,都会报错,说缺少SSL模块,如下:
 
 
2、这时候我们就可以先安装SSL模块:
先确认2个位置:
1)我的nginx是安装在了/usr/local/nginx/下
2)我的nginx的源码包放在了/usr/local/nginx/nginx/nginx-1.8.0下。如果没有的话,重新下载你对应的nginx版本的源码包,找个目录解压
 
3、目录切换到我们的源码包安装位置:
 
cd /usr/local/nginx/nginx/nginx-1.8.0
 
4、执行语句,重新安装ssl模块:
 
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
 
这时候可能又会有点小插曲,启动ssl模块的时候,报错了:
 
看到了error。就知道linux安装失败,停止了。这个错误是因为我们没有安装openssl openssl-devel(如果这一步没有报错的话,可以跳过下面的安装openssl-devel的步骤)
 
5、那么可以先执行安装openssl openssl-devel语句:
 
yum -y install openssl openssl-devel
 
安装上了 openssl-devel后,重新执行:./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
 
6、这时候应该就执行配置成功了。配置成功后,那么就需要编译我们的配置。(注意这里只能用make 而不要用make install,因为执行make install是覆盖安装的意思)
运行:
 
make
 
等待执行完成后,我们需要把新编译的nginx模块替换原来的nginx。
 
7、还是老规矩,先备份旧的nginx,执行语句(这里面复制的文件的路径需要根据你们安装的目录自行修改,如果和我安装的路径是一样的那么可以直接运行该语句):
 
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
 
8、关闭nginx(因为要把新的模块覆盖旧的nginx)
 
先找到nginx端口号,如图,目前我nginx的进程号为:13542
 
ps -ef|grep nginx
 
 
杀死该进程就可以了,执行语句:
 
kill -QUIT 13542
 
9、关闭nginx进程后就可以开始替换了(注意:我当前的位置是在我nginx的源码包中,目录不要搞错了)
 
执行:(复制到我的nginx的目录中)
 
cp ./objs/nginx /usr/local/nginx/sbin/
 
然后就是启动nginx。在启动之前,也可以在测试一次配置文件是否已经生效:
 
#先切换到sbin目录
cd /usr/local/nginx/sbin/
#检测nginx的配置文件是否有错误
./niginx -t
 
看到这样的,就是已经成功了
 
 
最后启动nginx:
 
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
 
因为刚才替换nginx模块的时候是把nginx进程都杀死了,所以要用上面的命令进行启动,而不能使用reload重启。
 
nginx正常启动后,我们在访问我们的网站,发现https就已经配置好了:
 
 
小拓展:
刚才配置nginx的时候是直接在配置文件上做修改,其实我们可以把小的配置抽离出去,尽量保持原配置文件不被修改
 
我们可以在原有配置文件中,加上这句(注意作用域范围,是引入到http的{}之内):
 
include vhost/*.conf;
 
接下来,需要找到对应的配置文件,因为我们这里的路径是直接 vhost。所以在配置文件同级目录,新建一个 vhost 文件夹,而且我们引入的是 *.conf 。意思就是引入vhost中所有后缀是.conf的配置文件:
 
 
在配置文件中,加上我们刚才教程提到的配置:
 
 1 server {
 2 listen 80;
 3 server_name 需要访问的域名(不加http头);
 4  
 5 rewrite ^(.*) https://$server_name$1 permanent;
 6  
 7 #charset koi8-r;
 8 #access_log logs/host.access.log main;
 9 location / {
10   root html;
11   index index.html index.htm;
12   proxy_pass http://需要访问的域名
13  
14 }
15 }
16  
17 # HTTPS server
18 #
19 server {
20 listen 443 ssl;
21 server_name 需要访问的域名;
22  
23 ssl on;
24  
25 ssl_certificate /usr/local/nginx/ssl/ssl.pem;
26 ssl_certificate_key /usr/local/nginx/ssl/ssl.key;
27  
28 ssl_session_cache shared:SSL:1m;
29 ssl_session_timeout 5m;
30  
31 ssl_ciphers HIGH:!aNULL:!MD5;
32 ssl_prefer_server_ciphers on;
33  
34 location / {
35 proxy_pass http://需要访问的域名:8080/;
36 }
37 }
保存后,也是使用nginx -t测试配置文件是否成功,成功后重启nginx即可。
 
如果有多个二级域名,那么就复制多份 xx.conf文件即可,配置文件会自动引入到nginx的配置中,不过每次新增配置文件都需要检测一遍,然后重启nginx。
 
附上nginx配置文件
  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34     server {
 35         listen       80;
 36         server_name  test.feihe.com;
 37         #rewrite ^(.*) https://$server_name$1 permanent; #这句是代表 把http的域名请求转成https
 38 
 39         #charset koi8-r;
 40 
 41         #access_log  logs/host.access.log  main;
 42 
 43         location / {
 44             root   html/;
 45             index  index.html index.htm;
 46         }
 47 
 48 
 49         location /coa/ {
 50             proxy_pass http://localhost:8080;
 51         }
 52 
 53         location /front/ {
 54             proxy_pass http://localhost:8080;
 55         }
 56 
 57         #error_page  404              /404.html;
 58 
 59         # redirect server error pages to the static page /50x.html
 60         #
 61         error_page   500 502 503 504  /50x.html;
 62         location = /50x.html {
 63             root   html;
 64         }
 65 
 66         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 67         #
 68         #location ~ \.php$ {
 69         #    proxy_pass   http://127.0.0.1;
 70         #}
 71 
 72         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 73         #
 74         #location ~ \.php$ {
 75         #    root           html;
 76         #    fastcgi_pass   127.0.0.1:9000;
 77         #    fastcgi_index  index.php;
 78         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 79         #    include        fastcgi_params;
 80         #}
 81 
 82         # deny access to .htaccess files, if Apache's document root
 83         # concurs with nginx's one
 84         #
 85         #location ~ /\.ht {
 86         #    deny  all;
 87         #}
 88     }
 89 
 90 
 91     # another virtual host using mix of IP-, name-, and port-based configuration
 92     #
 93     #server {
 94     #    listen       8000;
 95     #    listen       somename:8080;
 96     #    server_name  somename  alias  another.alias;
 97 
 98     #    location / {
 99     #        root   html;
100     #        index  index.html index.htm;
101     #    }
102     #}
103 
104 
105     # HTTPS server         ssl_protocols  SSLv2 SSLv3 TLSv1;
106     
107     server {
108         listen       443 ssl;
109         server_name  test.feihe.com;
110 
111         ssl                  on;
112         ssl_certificate      /usr/local/nginx/ssl/_.feihe.com_bundle.crt;  #这里是ssl pem文件存放的绝对路径
113         ssl_certificate_key  /usr/local/nginx/ssl/_.feihe.com.key;  #这里是ssl key文件存放的绝对路径
114 
115         ssl_session_cache    shared:SSL:1m;
116         ssl_session_timeout  5m;
117 
118 
119         ssl_ciphers  HIGH:!aNULL:!MD5;
120         ssl_prefer_server_ciphers   on;
121 
122         location / {
123             root   html/;
124             index  index.html index.htm;
125         }
126 
127 
128         location /coa/ {
129             proxy_pass http://localhost:8080;
130         }
131 
132         location /front/ {
133             proxy_pass http://localhost:8080;
134         }
135     }
136 
137 }

猜你喜欢

转载自www.cnblogs.com/haw2106/p/10113471.html