夜雨倚琴 2020-04-11
直接使用yum方式安装的ngninx默认是不支持ssl的,所以需要从代码编译安装
http://nginx.org/en/download.html
这里我们还是选择的稳定版1.16.1
yum install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
切换到/usr/local/src目录下,下载源代码解压
cd /usr/local/src wget http://nginx.org/download/nginx-1.16.1.tar.gz tar -zxvf nginx-1.16.1.tar.gz
cd /usr/local/src/nginx-1.16.1 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
编译成功后的截图
编译报错的解决办法,没有报错直接跳过
报错:./configure: error: the HTTP rewrite module requires the PCRE library.
问题原因:编译安装nginx需要pcre包,未安装的话会报错
解决办法:
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
报错:File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:
问题原因:yum采用python作为命令解释器,原来系统自带的python解释器为python2.7,系统默认的解释器为python3.7
解决办法:
vim /usr/bin/yum
修改第一行为:#!/usr/bin/python2.7
vi /usr/libexec/urlgrabber-ext-down
修改第一行为:#!/usr/bin/python2.7
报错:File "/bin/yum-config-manager", line 135 except yum.Errors.RepoError, e:
问题原因:yum采用python作为命令解释器,原来系统自带的python解释器为python2.7,系统默认的解释器为python3.7
解决办法:
vim /bin/yum-config-manager
修改第一行为:#!/usr/bin/python2.7
报错:./configure: error: SSL modules require the OpenSSL library.
问题原因: 缺少SSL库
解决办法:yum -y install openssl openssl-devel
如果nginx已经安装了的话用指令
make
如果没有安装或者已卸载的话
make && make install
查看编译后的nginx版本
/usr/local/src/nginx-1.16.1/objs/nginx -V
查看安装后的nginx版本
/usr/local/nginx/sbin/nginx -V
cp objs/nginx /usr/local/nginx/sbin/
复制bin文件后再核对下版本
/usr/local/nginx/sbin/nginx -V
vim /lib/systemd/system/nginx.service
复制以下内容保存(按ESC后:wq保存退出)
[Unit] Description=nginx service After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
设置开机自启动
systemctl enable nginx
启动、查看状态、重启nginx,指令3件套
systemctl start nginx systemctl status nginx systemctl restart nginx
可以reboot重启机器后再查看nginx状态
一般修改配置后用 nginx -s reload 重启
nginx -s reload
出现以下提示是没有配置环境变量
打开/etc/profile
vim /etc/profile
在最后一行 追加下面2行配置(ESC :wq 保存退出)
PATH=$PATH:/usr/local/nginx/sbin export PATH
使配置生效后,就可以用nginx -s reload
source /etc/profile
把阿里云申请的证书放到目录 /usr/local/nginx/conf/cert (文件也可以自己指定)
配置域名解析
vi /usr/local/nginx/conf/nginx.conf
增加配置,把下面的www.yuming.com改成自己的域名
server { listen 80; server_name www.yuming.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; // nginx1.15之后用这个语法 server_name wwww.yuming.com; ssl_certificate cert/www.yuming.com.pem; ssl_certificate_key cert/www.yuming.com.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-NginX-Proxy true; proxy_pass http://localhost:8001; proxy_redirect off; } }