二进制安装nginx——新手上路

什么是Nginx?

Nginx (engine x) 是一个使用c语言开发的高性能的http服务器及反向代理服务器。,也是一个IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。

其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

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

Nginx的应用场景

      1、 http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。

      2、 虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。

基于端口的,不同的端口

基于域名的,不同域名

  3、 反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

准备工作

[root@1 ~]# cat /etc/redhat-release         #查看系统版本

CentOS Linux release 7.2.1511 (Core)

[root@1 ~]# uname -r                            #查看系统内核版本

3.10.0-327.el7.x86_64

[root@1 ~]# uname -m                           #查看系统是否64位

x86_64

首先我们还是做服务之前先把奇葩的防火墙给关掉,因为很多规则会撞到墙。

[root@1 ~]# iptables -F

[root@1 ~]# iptables -X

[root@1 ~]# iptables -Z

[root@1 ~]# iptables -L  

[root@1~]#setenforce 0

[root@1 ~]#vi /etc/sysconfig/selinux               #修改SELINUX

[root@1 opt]# yum install  pcre-devel  zlib-devel              #安装pcre和zlibgzip模块,需要 zlib 库,rewrite模块需要 pcre 库

[root@1 ~]# yum info pcre-devel             #info:显示指定的rpm软件包的描述信息和概要信息;我们显示一下pcre的详细信息

[root@1 ~]# useradd -s /sbin/nologin -M nginx                   #创建一个nginx的用户

[root@1 ~]# id nginx                                                           #看nginx的id信息

[root@1 ~]# tar zxf nginx-1.11.2.tar.gz                    #我上传一个包,然后进行解压缩nginx-xx.tar.gz包,或者获取nginx,在http://nginx.org/en/download.html上可以获取当前最新的版本。

获知用wget下载

[root@1 nginx-1.11.2]# ./configure --prefix=/usr/local/nginx--user=nginx --group=nginx           #配置文件以及用户等信息,要在nginx目录下面执行

[root@1 nginx-1.11.2]# make && make install        #编译安装

[root@1 ~]# /usr/local/nginx--user\=nginx/sbin/nginx         #启动一下应用

[root@1 ~]# ss -tannml | grep 80                           看一下80端口

这个时候登陆IP地址就可以显示了

接下来写一个脚本可以启动nginx

我们先把服务关闭

[root@1 ~]# ps aux | grep nginx               #看一下nginx后台进程占用端口

[root@1 ~]# kill -9 4761

[root@1 ~]# kill -9 4762                           #把端口关闭

这个时候可以发现网页打不开了

[root@1 ~]# vi nginx.sh             #写脚本

#!/bin/bash

# chkconfig: 2345 97 25

#description nginx-server-scryt

nginx=/usr/local/nginx--user\=nginx/sbin/nginx

case "$1" in

  start )

        netstat -anlpt | grep nginx

        if [ $? -eq 0 ]

         then

           echo "nginx service running!"

        else

           echo "nginx service not runing!"

           $nginx

        fi

      ;;

  restart)

        $nginx -s reload

        if [ $? -eq 0 ]

         then

           echo "nginx server is begin restart"

        else

           echo "nginx server restart"

        fi

      ;;

     stop)

         $nginx -s stop

        if [ $? -eq 0 ]

         then

           echo "nginx server is stop"

        else

           echo "nginx server stop,try again"

        fi

      ;;

   status)

        netstat -anlpt | grep nginx

        if [ $? -eq 0 ]

         then

            echo "nginx server is running!"

        else

            echo "nginx server is not runing.try to restart"

        fi

      ;;

     *)

      echo "Please enter (start|restart|stop|status)"

      ;;

esac

exit 0

[root@1 ~]# chmod 755 nginx.sh                    #加权限

[root@1 ~]# cp nginx.sh  nginx        #复制成目录

[root@1 ~]# cp nginx /etc/init.d/              #把nginx目录给复制到/etc/iniat.d目录下

[root@1 init.d]# chkconfig --add nginx           #我把nginx添加到开机自启动

[root@1 init.d]# chkconfig --list nginx             #列出nginx开启

[root@1 init.d]# service nginx start           #启动服务

猜你喜欢

转载自blog.csdn.net/VickHUC/article/details/82868014