安装nginx,包含ssl模块,如果已经安装nginx则配置即可

原文:https://blog.csdn.net/cxm19881208/article/details/62047392

使用https来访问,一般都需要用域名来访问(IP貌似也可以,我没有深入研究),既然是域名,我比较常用的是实用nginx来做代理,一般来说安装起来应该很简单,但是还是遇到了一些有些非正常的、让人觉得很蛋疼很sb的问题,在这里做一个记录。

安装nginx(支持https,即开启SSL模块),需要首先安装pcre、zlib、openssl;安装openssl看我的另一篇文章:安装openssl

遇到的问题:我的电脑上本来已经有了zlib、openssl了,但是安装nginx的时候还是报error,说找不到zlib和openssl。

zlib我就直接重新安装了,但是openssl昨天刚刚重新安装了1.1.0最新版本的,为什么还是会报找不到的错误呢?然后想了一下,会不会就是因为充装了,所以路径和nginx配置文件中openssl的路径不一样呢,所以我就使用yum重装openssl,yum默认安装的是openssl1.0.1版本的,太旧了,所以安装好nginx以后,我又重新把openssl安装了一遍,更新成最新的。下面说一下安装nginx的步骤:

1、安装jdk,系统默认的jdk版本不对,会造成后续很多问题,所以第一件事就是充装jdk,自己去网上找jdk,然后传到服务器上进行安装,并配置环境变量。

2、安装zlib,官网 http://zlib.net/ ,到上面下载个新版本,然后传到服务器上进行安装 (http://prdownloads.sourceforge.net/libpng/zlib-1.2.11.tar.gz?download)

[root@localhost]tar zxvf zlib-1.2.11.tar.gz
[root@localhost] cd zlib-1.2.11
[root@localhost]  ./configure && make && make install

3、安装pcre,官网 https://sourceforge.net/projects/pcre/files/pcre/ ,到上面下载个新版本,然后传到服务器进行安装

[root@localhost] tar zxvf pcre-8.40.tar.gz
[root@localhost] cd pcre-8.40
[root@localhost]  ./configure && make && make install

4、安装openssl,官网 https://www.openssl.org/source/ ,看我另一篇文章:安装openssl

5、安装nginx,官网 http://nginx.org/download/ ,到上面下载个新版本,然后传到服务器进行安装。

[root@localhost]tar zxvf nginx-1.8.0.tar.gz
[root@localhost] cd nginx-1.8.0

然后一步一步执行,如果报错看的比较清楚:

首先执行 ./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module

这里如果没有安装上面的pcre  zlib   openssl 会报错,我遇到的比较蛋疼的是我已经安装过了openssl最新版本,但还是报错:

checking for OpenSSL library … not found

checking for OpenSSL library in /usr/local/ … not found

checking for OpenSSL library in /usr/pkg/ … not found

checking for OpenSSL library in /opt/local/ … not found

 

./configure: error: SSL modules require the OpenSSL library.

You can either do not enable the modules, or install the OpenSSL library

into the system, or build the OpenSSL library statically from the source

with nginx by using –with-openssl=<path> option.

看意思就是说找不到openssl库,这就很郁闷了,明明我已经安装过了,而且通过 openssl version指令可以看到我的openssl版本号;最后没办法了,只能按照上面的提示,在后面多一个配置项:–with-openssl=/home/softback/openssl-1.1.0e ,就执行通过了,我的完整的配置项是:

./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –add-module=/home/softback/echo-nginx-module-0.60 –with-openssl=/home/softback/openssl-1.1.0e

 

配置好以后,执行 make && make install

一般configure命令执行通过,make和install都会很顺利。

6、安装好以后,启动nginx

cd /usr/local/nginx/sbin/

./nginx

出现错误提示
[root@localhost lib]# error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
原因   在64位机器上nginx读取的pcre文件为/lib64/libpcre.so.1文件,默认安装pcre时libpcre.so文件安装在/usr/local/lib/目录下,所以输入/opt/nginx/sbin/nginx -V 找不到文件路径!!

1.首先确定安装了pcre.

2.切换路径: cd /usr/local/lib  执行   ln -s /usr/local/lib/libpcre.so.1 /lib64/

3.root权限下添加软链接 /usr/local/lib/libpcre.so.1 到 /lib64/ :  ln -s /usr/local/lib/libpcre.so.1 /lib64/

然后再启动就可以了。

7、~~万一我们服务器上已经安装过了nginx怎么办呢?怎样才能做到不重新安装支持https呢?

解决方法:如果已经安装了nginx,则只需要开启SSL模块即可。

首先查看nginx现有的模块:

查看nginx原有的模块

1
cd /usr/local/nginx/sbin/nginx
./nginx -V

在configure arguments:后面显示的原有的configure参数如下:


 

那么就应该

切换到源码包:


 

我们的新配置信息就应该这样写:


 

运行上面的命令即可,等配置完

配置完成后,运行命令

1
make

这里不要进行make install,否则就是覆盖安装

然后备份原有已安装好的nginx


 

然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)


 

然后启动nginx,仍可以通过命令查看模块是否已经加入成功

////////以下部分未做测试/////////

Nginx 配置Http和Https共存


 

把ssl on;这行去掉,ssl写在443端口后面。这样http和https的链接都可以用

Nginx 配置SSL安全证书重启避免输入密码

可以用私钥来做这件事。生成一个解密的key文件,替代原来key文件。

1
openssl rsa - in  server.key - out  server.key.unsecure

Nginx SSL性能调优


 

这部分参考别家的文章:http://www.cnblogs.com/piscesLoveCc/p/6120875.html

////////以上部分未做测试/////////

8、nginx相关配置,看我的另一篇文章:配置nginx代理https