Len's Study-Log

集中一点,登峰造极!

0%

Nginx 安装教程

环境

  • CentOS 7.6 64位
  • Nginx version 1.12.1

安装 Nginx

  1. 新建目录 mkdir /usr/local/nginx

  2. 将 Nginx 安装包解压到该目录,tar zxvf /root/nginx/nginx-1.12.2.tar.gz -C /usr/local/nginx

  3. 预安装额外依赖:

    1
    2
    yum -y install pcre-devel
    yum -y install openssl openssl-devel
  4. 编译&安装

    1
    2
    3
    cd /usr/local/nginx/nginx-1.12.2/
    ./configure
    make && make instal
  5. 安装完成后, Nginx的可执⾏⽂件位置位于 /usr/local/nginx/sbin/nginx

    • 启动 Nginx:/usr/local/nginx/sbin/nginx
    • 关闭 Nginx:/usr/local/nginx/sbin/nginx -s stop
    • 修改了配置⽂件需要重新加载 Nginx: /usr/local/nginx/sbin/nginx -s reload
    • 注意配置⽂件位于: /usr/local/nginx/conf/nginx.conf

配置 Nginx

安装 SSL 证书

编辑 nginx.conf 文件 vi /usr/local/nginx/conf/nginx.conf

这里参考腾讯的 Nginx配置文档:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
#SSL 默认访问端口号为 443
listen 443 ssl;
#请填写绑定证书的域名
server_name cloud.tencent.com;
#请填写证书文件的相对路径或绝对路径
ssl_certificate cloud.tencent.com_bundle.crt;
#请填写私钥文件的相对路径或绝对路径
ssl_certificate_key cloud.tencent.com.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1.2 TLSv1.3;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
#网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
#例如,您的网站主页在 Nginx 服务器的 /etc/www 目录下,则请修改 root 后面的 html 为 /etc/www。
root html;
index index.html index.htm;
}
}

注意:Nginx 版本为 nginx/1.15.0 以上请使用 listen 443 ssl 代替 listen 443ssl on

如果 Nginx 没有安装 http_ssl_module 模块,可能会出现错误:

1
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/loca...

解决办法:

1
2
3
4
5
6
7
8
9
10
11
12
13
/usr/local/nginx/sbin/nginx -s stop

cd /usr/local/nginx/nginx-1.12.2

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make

cp ./objs/nginx /usr/local/nginx/sbin/

/usr/local/nginx/sbin/nginx


把 http 请求转成 https

(http 默认请求端口是80)

1
2
3
4
5
6
7
# http请求自动转为https请求
server {
listen 80;
server_name lenqq.cn www.lenqq.cn;
return 301 https://$host$request_uri;

}