Centos7 nginx 搭建https服务器

Nginx简介

Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

实验环境:

系统版本:centos7x3.10.0-514.el7.x86_64

Nginx版本:nginx1.14.0

关闭防火墙并禁止开机自启

systemctl stop firewalld.service
systemctl disable firewalld

关闭selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

修改主机名

vi /etc/hostname

nginx.wangfeiyu.com

扫描二维码关注公众号,回复: 3513517 查看本文章

域名绑定IP

vi /etc/hosts

Centos7 nginx 搭建https服务器

重启 reboot

安装nginx服务

1、安装nginx依赖环境包

yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

2、官网下载nginx1.14.0压缩包

wget https://nginx.org/download/nginx-1.14.0.tar.gz

3、解压nginx

tar zxf nginx-1.14.0.tar.gz

4、进入解压目录

cd nginx-1.14.0

5、编译nginx

1)默认编译方式

. /configure

2)自定义编译选项

. /configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
--with-http_ssl_module
注:以上为默认编译方式和具体指定的编译方式,任选以上这两种之一即可。--with-http_ssl_module这个选项是https的重要模块必须安装。

3)本文中使用的编译安装方式

./configure
--prefix=/usr/local/nginx
--with-http_stub_status_module
--with-http_ssl_module
Centos7 nginx 搭建https服务器
注:以上--with-http_ssl_module这个模块是https的关键,必须安装!

6、安装nginx

make && make install

7、启动nginx

方式一

1)启动nginx

/usr/local/nginx/sbin/nginx
Centos7 nginx 搭建https服务器

2)关闭nginx

/usr/local/nginx/sbin/nginx -s stop

3)重启nginx

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

注:如果嫌以上方式太麻烦,可以做软连接ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx或者在全局环境变量里增加nginx环境变量,然后直接使用nginx即可!

方式二

1)编辑nginx服务启动文件

vi /etc/init.d/nginx

#! /bin/bash
#chkconfig: - 85 15
PATH=/usr/local/nginx
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -e "\033[32m nginx already running \033[0m"
}
do_stop() {
$DAEMON -s stop || echo -e "\033[31m nginx not running \033[0m"
}
do_reload() {
$DAEMON -s reload || echo -e "\033[31m nginx can't reload \033[0m"
}
case "$1" in
start)
echo -e "\033[32m $NAME running \033[0m"
do_start
;;
stop)
echo -e "\033[31m $NAME stoping \033[0m"
do_stop
;;
reload|graceful)
echo -e "\033[32m $NAME configuration...\033[0m"
do_reload
;;
restart)
echo -e "\033[32m Restarting : $NAME \033[0m"
do_stop
do_start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

注:切记编辑完启动脚本以后一定要给予执行权限,不然启动无效!
2)设置启动文件执行权限

chmod +x /etc/init.d/nginx

3)启动nginx

//设置开机自启

chkconfig nginx on

//启动nginx

/etc/init.d/nginx start

//重启nginx

/etc/init.d/nginx restart

//查看nginx服务启动状态

chkconfig --list

//查看nginx服务是否开启

netstat -antupl | grep nginx

8、开机启动nginx

1)编辑开机启动文件

vi /etc/rc.local
添加一行/usr/local/nginx/sbin/nginx

2)设置启动文件权限

chmod 755 /etc/rc.local

注:如果使用方式二脚本启动服务,那么这步可以省略!

9、访问测试

访问地址:http://192.168.152.77

猜你喜欢

转载自blog.51cto.com/13043516/2298296