nginx实战——介绍以及安装方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chunyang315/article/details/81008255

Nginx介绍

1)常见webserver
老牌:httpd(早期叫Apache) , 开源 市场份额最高
微软:IIS
经量:Lighttpd 性能高,低能耗,功能欠缺

2)Nginx诞生
2004年10月发布,由俄国人开发

3)Nginx官网 ,版本
最新为1.40.0 stable(稳定版) 官网:www.nginx.org
国内分支Tengine (http://tengine.taobao.org/)

4)Nginx功能介绍
http服务,反向代理,负载均衡,邮件代理,缓存加速,SSL ,flv/mp4流媒体


Nginx安装-yum安装

A.
1)先安装epel-release
2)然后再安装nginx
此方法安装的版本比较老。

B.
1) vi创建/etc/yum.repos.d/nginx.repo
2)将写入以下内容,保存退出

[nginx]
name=nignx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

3)先禁用epel.repo yum list |grep nginx 查看nginx版本
这里写图片描述

4)再次yum安装Nginx最可以安装最新的稳定版本

5)安装完成后启动nginx服务并查看端口

systemctl start nginx

netstat -lntp 

6) 使用nginx -v 查看当前使用的版本(适用yum安装。如果是源码包安装 可能不会找到nginx 命令 需要使用完整路径 例如

/usr/local/nginx/sbin/nginx -v)

[root@ZCY03 ~]# nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --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 --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

7)如果之前安装过较老的版本,可以不卸载直接用yum安装最新的1.14.0版本。需要重启nginx服务才会更新为1.14.0版本。

8)在浏览器中输入IP进行访问,如果不能访问,可能是iptables规则导致,可以在暂停firewalld服务来查看是否是因为它的原因

systemctl stop firewalld     //停止firewalld服务

9)如果有防火墙,不想关闭防火墙,则需要增加开放80端口命令

iptables -I INPUT -p tcp  --dport 80 -j ACCEPT

nginx安装-源码安装

1)在/usr/local/src 下载nginx安装包

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

2) 进行解压缩

tar zxvf nginx-1.14.0.tar.gz 

3)进入到解压缩的目录后,执行以下命令

./configure --prefix=/usr/local/nginx

源码文件存在于/usr/local/src/nginx-1.14.0/core/目录下,可以使用的配置参数可以./configure –help查看

4)执行完成后,可以执行echo $? 来查看上一步的操作是否有问题,显示为0再接着执行make && make install

5)安装完成后可以再/usr/local/nginx下看到以下4个目录

conf  html  logs  sbin 

conf下是它的配置文件
html下是它的默认页
logs下是它存放日志的地方
sbin下是它的可执行文件

6) 查看它的版本

/usr/local/nginx/sbin/nginx -V

7)由于没有配置启动脚本与配置文件,所以启动方式有所不同。(如果80端口被占用,那么启动会报错)

/usr/local/nginx/sbin/nginx 

8)如果修改了配置文件,需要重启nginx,那么步骤如下

a. pkill nginx 或killall nginx /杀死nginx进程,停止nginx服务
b. /usr/local/nginx/sbin/nginx -t /检测配置文件语法错误
c. /usr/local/nginx/sbin/nginx -s reload //重新加载配置 ,并启动服务

9)如果没有安装killall这个命令,可以使用yum安装

yum  install  -y  psmisc

10)如果觉得以上启动方式太麻烦,可以创建一个启动脚本vi /etc/init.d/nginx

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}

stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}

reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart()
{
    stop
    start
}

configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL

修改该脚本的权限为755,那么之后启动nginx可以使用脚本

/etc/init.d/nginx  start/restart/stop 

11)想开机启动nginx,可以将nginx服务加入到服务列表中即可

chkconfig  --add   nginx 

chkconfig    nginx     on 

猜你喜欢

转载自blog.csdn.net/chunyang315/article/details/81008255