Nginx 编译安装http2教程

  1. 前言
    这次为大家带来的是 linux 下 Nginx 的编译安装http2教程!http2与http的区别和优势差异,这里就不再赘述了。相信了解的大都会选择http2,如果你也想尝试或者了解可以参考本教程来尝试安装!

还是老话:新手建议修改任何文件之前,先备份!如造成生产环境或者数据异常,请自行负责!

本次安装环境系统为:CentOS 6.9

  1. 安装准备

同样先更新系统,然后安装必要的依赖库文件等:
yum update
yum upgrade
yum install -y patch libtool gcc gcc-c++ autoconf automake zlib zlib-devel pcre-devel make unzip git wget

  1. 下载 openssl 安装包

这里我们下载官网最新的 1.1.0f 版本:
wget --no-check-certificate https://www.openssl.org/source/openssl-1.1.0f.tar.gz
解压:
tar zxvf openssl-1.1.0f.tar.gz
注意,记住openssl解压文件存放目录,后面编译安装nginx需要用到

  1. 下载 nginx安装包

同样这里我们下载官网最新的nginx 1.13.5版本:
wget -c http://nginx.org/download/nginx-1.13.5.tar.gz
解压并进入解压文件目录:
tar zxvf nginx-1.13.5.tar.gz
nginx-1.13.5

  1. 开始执行编译安装

a.生成/var/cache/nginx 目录,用于存放 Nginx 反向代理的缓存文件
mkdir -p /var/cache/nginx
b.新建nginx的运行用户www(这里用户自行设置)
useradd www
c.选择编译模块,这里我们要安装http2,则选择以下模块(如需要其他模块,请自行选择添加):
http_v2_module
http_ssl_module
d.本次教程的完整编译命令如下:
./configure --prefix=/usr/local/nginx --user=www --group=www --conf-path=/etc/nginx/nginx.conf --with-openssl=/nginx/openssl-1.1.0f --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_uwsgi_module --without-http_scgi_module --with-http_stub_status_module --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp
其中--prefix=/usr/local/nginx表示将nginx编译安装到/usr/local/nginx目录下,--user=www --group=www表示nginx运行的用户和组名称,--conf-path=/etc/nginx/nginx.conf表示nginx配置文件路径
特别注意:--with-openssl=/nginx/openssl-1.1.0f 这里就需要填写前面下载并解压的openssl文件路径,如没有填写或者路径错误,则编译就会出错!
选择好编译的模块后执行以上命令,执行编译!并等待滚屏完成!得到如下图:
20171002120938.jpg
检查各项路径,是否正常!
e.执行安装命令
make && make install
等待安装滚屏结束

  1. 测试是否正常运行

/usr/local/nginx/sbin/nginx -t
如图则表示测试默认配置文件正常
20171002125508.jpg

  1. 查看版本以及具体模块参数等

/usr/local/nginx/sbin/nginx -V
20171002130119.jpg

  1. 将nginx添加到系统服务

在/etc/init.d/目录下创建名为nginx脚本,并写入以下内容(注意修改一些文件的路径和nginx安装配置的一致)


脚本代码开始:

!/bin/sh

. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/run/nginx.lock

start() {

[ -x $nginx ] || exit 5 
[ -f $NGINX_CONF_FILE ] || exit 6 
echo -n $"Starting $prog: " 
daemon $nginx -c $NGINX_CONF_FILE 
retval=$? 
echo 
[ $retval -eq 0 ] && touch $lockfile 
return $retval 

}

stop() {

echo -n $"Stopping $prog: " 
killproc $prog -QUIT 
retval=$? 
echo 
[ $retval -eq 0 ] && rm -f $lockfile 
return $retval 

killall -9 nginx
}

restart() {

configtest || return $? 
stop 
sleep 1 
start 

}

reload() {

configtest || return $? 
echo -n $"Reloading $prog: " 
killproc $nginx -HUP 

RETVAL=$?

echo 

}

force_reload() {

restart 

}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {

status $prog 

}

rh_status_q() {

rh_status >/dev/null 2>&1 

}

case "$1" in

start) 
    rh_status_q && exit 0 
$1 
    ;; 
stop) 
    rh_status_q || exit 0 
    $1 
    ;; 
restart|configtest) 
    $1 
    ;; 
reload) 
    rh_status_q || exit 7 
    $1 
    ;; 
force-reload) 
    force_reload 
    ;; 
status) 
    rh_status 
    ;; 
condrestart|try-restart) 
    rh_status_q || exit 0 
        ;; 
*)    
  echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
    exit 2 

esac


脚本代码结束

  1. 添加脚本文件权限并开启服务

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
Nginx启动,停止,配置测试,重载
service nginx start
service nginx stop
service nginx configtest
service nginx reload

  1. 配置结束。

创建站点开启http2,测试http2是否正常!
A.在nginx配置文件/etc/nginx/nginx.conf中添加一行配置,方便多站点直接加载:
include /usr/local/nginx/vhost/*.conf; (这样直接创建主机站点配置文件,并上传到/usr/local/nginx/vhost/目录下即可直接读取配置)
20171002190317.jpg
B.创建主机站点配置文件www.xxx.com.conf

server {
    listen 443 ssl http2 default_server;
    server_name  www.xxx.com;
    root /data/wwwroot/www.xxx.com
    ssl_certificate      /ssl/cert.pem;
    ssl_certificate_key  /ssl/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;
    }
}

创建并上传到/usr/local/nginx/vhost/目录,重启nginx

C.测试地址
https://myssl.com/http2_check.html
20171002192554.jpg

标签: nginx, http2

添加新评论


Warning: in_array() expects parameter 2 to be array, null given in /www/users/HK1590886/WEB/usr/plugins/TopLamuLeimu/Plugin.php on line 85