离线安装nginx并配置https

目录

1.离线安装nginx及依赖

2.配置https

3.没有openssl模块时添加模块

4.使用yum安装的ssl无法找到一直报错时:./configure: error: SSL modules require the OpenSSL library.


1.离线安装nginx及依赖

链接:https://pan.baidu.com/s/1bXrHV6I0W6yYLo6elhyakA 
提取码:9e5w 

1.上面是我收集起来的本次需要用的,包括nginx(1.13.9)跟环境所需的包。

     解压出来后,上传到linux里面,我放在/usr/local/src中(这个路径自己随意放就可以了)

2.进入到你放的nginx整个文件夹所在的位置,我是       cd /usr/local/src/nginx/gcc ,先进入gcc文件夹,执行以下命令:

    rpm -Uvh *.rpm --nodeps --force,在进入到    cd /usr/local/src/nginx/gcc-c++  文件夹,执行以下命令,表示安装:

    rpm -Uvh *.rpm --nodeps --force

3.

安装完成后可通过如下命令检测是否安装成功

gcc -v

g++ -v

版本可能不一样,但出现类似文字即可。

4.下一步就是安装PCRE,cd /usr/local/src/nginx  先把pcre解压出来   tar -zxvf pcre-8.35.tar.gz,解压出来之后就要开始安装了

cd pcre-8.35

./configure

make

make install

5.再来安装libtool ,cd /usr/local/src/nginx    先把libtool 解压出来   tar -zxvf libtool-2.4.2.tar.gz,解压出来之后就要开始安装了

     cd libtool-2.4.2

     ./configure

     make

     make install

       好了,环境到这里就配置的差不多了,应该不会有问题了。

6.安装nginx,  cd /usr/local/src/nginx    先把nginx解压出来   tar -zxvf nginx-1.13.9.tar.gz,解压出来之后就要开始安装了

     cd nginx-1.13.9

     ./configure

     make

     make install

7.常用命令

启动nginx   :   nginx安装目录地址 -c nginx配置文件地址

安装后的路径好像会被变到/usr/local  里面

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

/usr/local/nginx/sbin/nginx -s stop (quit)                                       停止nginx

/usr/local/nginx/sbin/nginx -s reload                                             重启nginx

netstat -tunlp                                                                                 查看端口占用

netstat -tunlp |grep                                                                        查看指定端口

nginx的配置文件为安装目录下的nginx目录中的nginx.conf,默认端口为80,启动后出现如下页面即为启动成功

访问地址是:服务器地址:80(如:128.232.236.82:80)需要注意80端口没有被占用

安装完成


2.配置https

下载安装openssl:

可从这里选择适合自己的版本下载,我下载的是:Win64 OpenSSL v1.1.0b Light,下载完成后,直接双击安装(安装选项一般默认即可)

3.生成证书

在nginx根目录下新建ssl文件夹(名字可以自己定),并在命令窗口进入此目录,按照如下的几个命令,完成证书创建过程。

 


#此步用于生成私钥,会提示输入密码,密码后面步骤需要用到;jason.key为私钥的名字,文件名可自己定
openssl genrsa -des3 -out jason.key 1024
#此步用于生成csr证书,jason.key为上一步骤生成的私钥名,jason.csr为证书,证书文件名可自定
#在此步过程中,会交互式输入一系列的信息(所在国家、城市、组织等),其中Common Name一项代表nginx服务访问用到的域名,我这里是本地测试,所以可以随意定一个jason.com,并在本地host文件中将此域名映射为127.0.0.1
openssl req -new -key jason.key -out jason.csr
#此步用于去除访问密码,如果不执行此步,在配置了ssl后,nginx启动会要求输入密码
#jason.key为需要密码的key,jason-np.key为去除访问密码的key文件
#操作过程中会要求输入密码,密码为生成key文件时的密码
openssl rsa -in jason.key -out jason-np.key
#此步用于生成crt证书
#jason.crt为第2步生成的csr证书名称,jason.crt为要生成的证书名称
openssl x509 -req -days 366 -in jason.csr -signkey jason-np.key -out jason.crt

经过以上几个步骤,证书生成完毕,ssl文件夹下的jason.crt和jason-np.key为我们后续要使用的文件。

注:在执行openssl命令时,可能会出现提示找不到openssl配置文件:

can't open config file: /etc/ssl/openssl.cnf

实际配置文件存在,但不在这个目录(可以在openssl安装目录找到),我们可以直接在命令窗口设置下环境变量,使其指向正确的位置

#请根据你自己的安装目录调整
set OPENSSL_CONF=C:\OpenSSL-Win64\bin\openssl.cfg

4.nginx配置ssl

打开nginx目录下conf\nginx.conf文件,找到HTTPS server的配置,将配置项前面的注释符号去掉

修改前配置内容如下:

# HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    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;
    #    }
    #}
 

修改后配置内容如下:

# HTTPS server

    server {
        listen       443 ssl;
        server_name  front;

        ssl_certificate      ./ssl/jason.crt;
        ssl_certificate_key  ./ssl/jason-np.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;
        }
    }
 

5.配置重定向,http请求自动跳转到https

如果我们希望强制使用https,可以将http的请求重定向到https,只需要在http对应server配置中添加rewrite

server {
        listen       80;
        server_name  jason.com;
        rewrite ^(.*) https://$server_name$1 permanent;
        #省略其他配置......
}
6.启动nginx

命令窗口进入到nginx根目录,使用以下命令重新加载nginx配置文件

nginx -s reload
这里有两点需要注意:

1)由于nginx的cache模块需要用到共享内容,所以官方提示在window Vista之后的版本是不支持cache模块的,所以在配置文件中,ssl_session_cache一行不要注释掉,否则启动会报错

2)nginxssl使用的是443端口,如果系统中443已经被占用,在nginx的error.log文件中会有报错,且无法正常启动

bind() to 0.0.0.0:443 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

遇到这个问题,可在命令窗口通过以下命令查找占用端口的进程

netstat -ano|findstr "443"

结果列表中第2列代表进程监听的ip和端口,最后一列代表进程id,可根据id杀掉对应进程即可(有些进程是由系统服务启动的,需要关闭服务才行)。

正常启动后,浏览器中输入https://jason.com(jason.com是在第3步第2个命令中指定的),出现nginx欢迎页面,说明已配置成功。


3.没有openssl模块时添加模块

 

缺少http_ssl_module模块时,会报此类错误。下面是错误显示
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:60
这是因为前面编译nginx的时候用了不带SSL支持的编译配置。
1.切换到源码包
cd nginx-1.12.0(以自己的目录为准)
2.查看现有的模块
/usr/local/nginx/sbin/nginx -V
configure arguments:这里为空时,表示未配置ssl模块
3.添加SSL支持参数重新编译
进入源码包 cd nginx-1.12.0
执行新的配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
完成后使用make命令,不要在使用make install
4.备份并覆盖原有的nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
此时要确保nginx是停止状态
停止nginx运行:/usr/local/nginx/sbin/nginx -s stop
覆盖原有的nginx
cp ./objs/nginx /usr/local/nginx/sbin/
提示是否覆盖,输入yes然后回车
5.启动并查看
启动: /usr/local/nginx/sbin/./nginx
查看:/usr/local/nginx/sbin/nginx -V 会显示以下内容,表示成功
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module


4.使用yum安装的ssl无法找到一直报错时:./configure: error: SSL modules require the OpenSSL library.

安装openresty时,执行 ./configure时一直报:./configure: error: SSL modules require the OpenSSL library.

./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没安装成功,再次yum install  openssl,但是发现明明已经是有openssl存在的了。。。

看错误提示:

or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

很开心,按上面的指示,应该是要指定openssl的位置,于是which openssl找到openssl的安装位置, ./configure时增加参数,--with-openssl=/usr/local/bin/openssl,(这一步是错误的!!!)

./configure是通过了,但是,在执行gmake时就出现了新问题,
gmake[2]: *** [/usr/local/ssl/.openssl/include/openssl/ssl.h] Error 127
gmake[2]: Leaving directory `/root/openresty-1.11.2.2/build/nginx-1.11.2'
gmake[1]: *** [build] Error 2
gmake[1]: Leaving directory `/root/openresty-1.11.2.2/build/nginx-1.11.2'
gmake: *** [all] Error 2
究其原因,上面提示的应该是一个source的位置,而不应该是openssl的安装位置。。。
参考博客:https://www.phpsong.com/2930.html

但是openssl是通过yum install安装的,没有存到它的源码包(还是说我没找到?)
只能再到官网下载一个,https://www.openssl.org/source/openssl-1.1.1d.tar.gz
找到不你需要的版本没关系,找到一个版本相近的包,复制其下载链接,将后面的openssl版本替换成你需要的即可
(可以通过openssl vesion查看已经安装了的openssl的版本信息)

最后解压openssl源码包到一个位置
最终的 ./configure的参数形式为
./configure --prefix=/opt/openresty \
            --with-luajit \
            --without-http_redis2_module \
            --with-http_iconv_module \
            --with-http_postgres_module \
            --with-openssl=/OpenResty/package/openssl-1.1.0e

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我来解答你的问题。 首先,你需要在Ubuntu20.04上安装Nginx。如果你的机器无法连接互联网,需要先在有互联网连接的机器上下载Nginx安装包及其依赖,然后将这些文件拷贝到离线机器上进行安装。具体步骤如下: 1. 在有互联网连接的机器上下载Nginx和其依赖库,可以使用命令:`sudo apt-get download nginx`,该命令会下载Nginx及其依赖库的deb包到当前目录下。 2. 将下载好的deb包和依赖库拷贝到离线机器上,使用命令:`sudo dpkg -i xxx.deb`进行安装,xxx代表对应的deb包名称。 3. 安装完成后,使用命令:`sudo systemctl start nginx`启动Nginx服务。 接下来,你需要配置负载均衡。可以通过以下步骤实现: 1. 编辑Nginx配置文件:`sudo vi /etc/nginx/nginx.conf`。 2. 在http块中添加upstream模块的配置,如下所示: ``` http { upstream backend { server backend1.example.com weight=5; server backend2.example.com; server backend3.example.com; server backend4.example.com; } ... } ``` 其中,backend1.example.com、backend2.example.com等为你要进行负载均衡的服务器地址,weight=5代表backend1.example.com的权重为5,权重越高,被选中的概率越大。 3. 在server块中添加location模块的配置,如下所示: ``` http { ... server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } } ``` 其中,example.com为你的域名,http://backend为upstream模块中定义的负载均衡后端服务器地址,$host和$remote_addr为Nginx的内置变量,用于设置HTTP头信息。 4. 保存配置文件并重新加载Nginx配置:`sudo nginx -t && sudo nginx -s reload`。 至此,你已经成功配置Nginx负载均衡。当用户访问example.com时,Nginx会根据upstream模块中定义的负载均衡算法,将请求转发到后端服务器进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

余生大大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值