Red Hat Linux7 安装Nginx教程(超详细加注解版)

一、Nginx说明:Nginx是一款高性能的HTTP服务器,以及反向代理服务器,其由俄罗斯Igor Sysoev创始,通过各类变迁得来。在2008年,Igor Sysoev所在的公司打算将其公司的wordpress博客平台搬到软件上去,当时使用的是Apache,可是当其公司工程师修改安装设置的时候,Apache居然死机了,因而其发现此Apache在流量比较大的时候,根本不稳定,因而其公司撤除了Apache,将全部的精力投入到当时并不成熟的Nginx开源项目上,5年后,其公司的wordpress仍然高效的、稳定的运行在Nginx上。可想而知,Nginx超强的稳定性、丰富的功能集、示例配置文件和低系统资源消耗是让它远近闻名的最大特色。html

在我国,使用Nginx的大型网站不少,好比百度、淘宝、腾讯、新浪博客、网易新闻、酷6网、豆瓣网。。。linux

注:(1)HTTP,超文本传输协议,主要应用在web界面;nginx

(2)反向代理服务器:你们都有过这样的经历,拨打10086客服电话,可能一个地区的10086客服有几个或者几十个,你永远都不须要关心在电话那头的是哪个,叫什么,男的,仍是女的,漂亮的仍是帅气的,你都不关心,你关心的是你的问题能不能获得专业的解答,你只须要拨通了10086的总机号码,电话那头总会有人会回答你。那么这里的10086总机号码就是咱们说的反向代理。客户不知道真正提供服务人的是谁。
c++

反向代理隐藏了真实的服务端,当咱们请求 www.baidu.com 的时候,就像拨打10086同样,背后可能有成千上万台服务器为咱们服务,但具体是哪一台,你不知道,也不须要知道,你只须要知道反向代理服务器是谁就行了,www.baidu.com 就是咱们的反向代理服务器,反向代理服务器会帮咱们把请求转发到真实的服务器那里去。web

Nginx就是性能很是好的反向代理服务器,用来作负载均衡。正则表达式

二、Nginx基本特征有:(1)处理静态文件,索引文件以及自动索引,打开文件描述符缓冲。(2)无缓存的反向代理加速,简单的负载均衡和容错。(3)模块化的结构,包括gzipping,byte ranges,chunked responses以及SSI-filter等filter,若是由FastCGI或其它代理服务器处理蛋液中存在的多个SSI,则这项处理能够并行运行,而不须要相互等待。(4)支持SSL和TLSSNI。shell

三、Nginx软件下载。因为Nginx的安装须要依赖openssl库、Zlib库、Pcre库,因此须要下载这三种类型的库,以及Nginx的安装包。浏览器

注: (1)  openssl库是为了SSL功能服务的,SSL是安全套接层,是为网络通讯提供安全及数据完整性的一种安全协议。缓存

(2)Zlib库是为Gzip模块服务的,zlib是提供数据压缩用的函式库。安全

(3)Pcre库是rewrite模块服务的,是正则表达式的一种库。

其四种类型的数据包文件,下载连接以下:连接:https://pan.baidu.com/s/11g5igpfIBGOR_-WlBphEqA 密码:373n

四、将下载的Nginx安装包以及openssl、Zlib、Pcre库经过Xshell软件上传至Redhat Linux系统中,操做步骤以下:

1、经过xshell登陆linux系统


2、配置yum软件仓库。

[root@localhost ~]# vi /etc/yum.repos.d/rhel.7.repo 


3、挂载系统镜像

[root@localhost ~]# mount /dev/cdrom /media/cdrom/
mount: /dev/sr0 写保护,将以只读方式挂载
您在 /var/spool/mail/root 中有邮件

[root@localhost ~]# 

4、经过yum源安装lrzsz


5、输入rz,将4中压缩包经过xshell上传至linux系统中


五、在linux系统上建立空白文件夹,将上述4中安装包mv至新文件夹中。


[root@localhost ~]# mkdir -p  /usr/local/yyy/nginx/soft/

[root@localhost ~]# mv openssl-1.1.0h.tar.gz pcre-8.38.tar.gz zlib-1.2.11.tar.gz nginx-1.13.12.tar.gz /usr/local/yyy/nginx/soft/

[root@localhost ~]# cd /usr/local/yyy/nginx/soft/

[root@localhost soft]# ll

六、安装SSL功能,须要安装 openssl 操做以下:

[root@localhost soft]# tar xzvf openssl-1.1.0h.tar.gz     //解压缩openssl-1.1.0h.tar.gz
[root@localhost soft]# cd openssl-1.1.0h/               
[root@localhost openssl-1.1.0h]# ./config                //编译源码包代码

[root@localhost openssl-1.1.0h]# make && make install  //生成二进制安装程序,而且经过二进制服务程序进行安装。

七、安装Gzip模块,须要安装zlib库,操做以下:

[root@localhost soft]# tar xzvf zlib-1.2.11.tar.gz 
[root@localhost soft]# cd zlib-1.2.11/
[root@localhost zlib-1.2.11]# ./con
configure  contrib/   
[root@localhost zlib-1.2.11]# ./configure 

[root@localhost zlib-1.2.11]# make && make install

八、安装rewrite模块,须要先安装pcre库,可是在安装pcre库的过程当中,须要先经过yum源安装gcc gcc-c++ 。操做步骤以下:

yum install -y gcc gcc-c++

[root@localhost soft]# tar pcre-8.38.tar.gz 
[root@localhost soft]# cd pcre-8.38/
[root@localhost pcre-8.38]# ./configure 

[root@localhost pcre-8.38]# make && make install

九、安装Nginx服务。操做以下:

[root@localhost soft]# tar xzvf nginx-1.13.12.tar.gz 
[root@localhost soft]# cd nginx-1.13.12/
[root@localhost nginx-1.13.12]# ./configure 

[root@localhost nginx-1.13.12]# make && make install

十、监测Nginx服务是否安装成功。

以下图,说明,nginx安装成功。


注:若是你登陆linux系统不是root用户,那么运行。./nginx -t 出现运行出错,则是没有权限执行,那么须要你su 到超级用户中,使用chmod -R 777 user/local/nginx/ 命令,给此文件加权限。而后在进入普通用户,cd/user/local/nginx/sbin/下执行./nginx -t 便可。

十一、因为nginx使用80端口号,则须要将80端口号添加到iptables中,操做以下:

[root@localhost sbin]# vi /etc/sysconfig/iptables


上述配置完成后,打开win浏览器,输入linux的ip地址,个人是http://192.168.232.128/  出现以下图,则说明nginx安装完成。


十二、优化Nginx开机自启动,使用server命令便可。操做步骤以下:

[root@localhost ~]# cd /etc/init.d/

[root@localhost init.d]# vi nginx 

将以下脚步文件,复制到nginx文件中。

#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig: - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#   proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config: /etc/nginx/nginx.conf 
# config: /etc/sysconfig/nginx 
# pidfile: /var/run/nginx.pid 
# Source function library. 
. /etc/rc.d/init.d/functions 
# Source networking configuration. 
. /etc/sysconfig/network 
# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 
    nginx="/usr/local/nginx/sbin/nginx" 
    prog=$(basename $nginx) 
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
    lockfile=/var/lock/subsys/nginx 
 
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

而后wq,保存退出!

[root@localhost init.d]# chmod  777 nginx   //修改此nginx文件的权限
[root@localhost init.d]# chkconfig --add nginx   // 将脚本文件添加至chkconfig中,以便让chkconfig检查、设置系统的各类服务。

[root@localhost init.d]# chkconfig --level 35 nginx on //设备nginx开机在3和5级别自动启动。

检查脚本是否有效:以下


》》》》》》》》》到此 Red Hat Linux7 安装Nginx教程已经写完了!《《《《《《

后续我将继续更新,有关于Nginx的基本使用等知识。谢谢你们!!