参考地址:https://help.aliyun.com/knowledge_detail/42863.html
参考地址:http://blog.csdn.net/defonds/article/details/44410359?locationNum=6&fps=1
参考地址:http://blog.163.com/qsc0624@126/blog/static/140324073201312734548701/
第一步:下载文件 openssl-1.0.2l.tar.gz nginx-1.13.4.tar.gz
# wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
# wget http://nginx.org/download/nginx-1.16.1.tar.gz
第二步:安装依赖包 PCRE 包和 zlib 包
# yum -y install pcre pcre-devel
#yum install -y zlib-devel
# tar -xvzf openssl-1.0.2l.tar.gz -C /usr/local/
第四步:解压并安装 nginx-1.13.4.tar.gz
# tar -xvzf nginx-1.13.4.tar.gz -C /usr/local
# cd /usr/local/nginx-1.13.4/
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-openssl=/usr/local/openssl-1.0.2l
# make
# make install
第五步:使用阿里云的免费证书
1.去阿里云的证书服务下载证书
2.在nginx的conf目录下创建cert目录,并将下载的全部文件拷贝到cert目录。
3.修改nginx的配置文件nginx.conf (可以参考阿里云证书下载页面配置修改说明)
4.注意服务器有打开443访问权限
# vi /usr/local/nginx/conf/nginx.conf
# i (按 i 进入编辑模式,按 Esc 退出编辑模式)
# :x (退出编辑模式后,按 :x 保存并退出)
# /usr/local/nginx/sbin/nginx (重启nginx)
# /usr/local/nginx/sbin/nginx -t (验证Nginx是否安装成功)
# pkill -9 nginx (强制停止nginx)
第六步:设置Nginx开机自启动
1.添加/etc/init.d/nginx 启动脚本
# vi /etc/init.d/nginx (创建并编辑文件)
2.添加以下内容到nginx里:
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
注:红字部分改成自己机器相应路径,另外,编辑nginx文件内容时,最好通过vi命令来编辑。通过windows编辑好nginx文件再上传到linux上可能会出现回车换行符不兼容(bash: /etc/init.d/nginx: /bin/bash^M: bad interpreter: No such file or directory)
3.设置nginx文件的访问权限
# chmod a+x /etc/init.d/nginx (a+x 表示所有用户可执行all user can execute)
4.设置nginx 开机启动
# chkconfig nginx on
5.查看是否设置成功
# chkconfig --list