• Nginx安装


     

    1. 安装前工作

    首先更新系统软件源,使用以下命令更新系统 -

    [root@localhost ~]# yum update
    
     

    有关两个命令的一点解释:
    yum -y update - 升级所有包,改变软件设置和系统设置,系统版本内核都升级
    yum -y upgrade - 升级所有包,不改变软件设置和系统设置,系统版本升级,内核不改变

    依赖包安装

    [root@localhost src]# yum -y install gcc gcc-c++ autoconf automake libtool make cmake
    [root@localhost src]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
    
     

    2. 下载Nginx安装源文件

    源码下载,可官网下载地址:http://nginx.org/en/download.html 下载并上传到服务器(这里选择最新稳定版本:nginx-1.10.3),如下图所示 -

    或直接在服务上执行以下命令下载 -

    [root@localhost ~]# cd /usr/local/src
    [root@localhost src]# wget -c http://nginx.org/download/nginx-1.10.3.tar.gz
    
     

    解压上面下载的文件 -

    [root@localhost src]# tar zxvf nginx-1.10.3.tar.gz
    
     

    在编译之前还要做一些前期的准备工作,如:依懒包安装,Nginx用户和用户组等。

    3. 新建nginx用户及用户组

    使用 root 用户身份登录系统,执行以下命令创建新的用户。

    [root@localhost src]# groupadd nginx
    [root@localhost src]# useradd -g nginx -M nginx
    
     

    useradd命令的-M参数用于不为nginx建立home目录
    修改/etc/passwd,使得nginx用户无法bash登陆(nginx用户后面由/bin/bash改为/sbin/nologin),

    [root@localhost src]# vi /etc/passwd
    
     

    然后找到有 nginx 那一行,把它修改为(后面由/bin/bash改为/sbin/nologin):

    nginx:x:1002:1003::/home/nginx:/sbin/nologin
    

    4. 编译配置、编译、安装

    下面我们进入解压的nginx源码目录:/usr/local/src/ 执行以下命令 -

    [root@localhost ~]# cd /usr/local/src/nginx*
    [root@localhost nginx-1.10.3]# pwd
    /usr/local/src/nginx-1.10.3
    [root@localhost nginx-1.10.3]#
    [root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/nginx 
    --pid-path=/usr/local/nginx/run/nginx.pid 
    --with-http_ssl_module 
    --user=nginx 
     --group=nginx 
    --with-pcre 
    --without-mail_pop3_module 
    --without-mail_imap_module 
    --without-mail_smtp_module
    

    注意:上面的反斜杠 表示换行继续。

    --prefix=/usr/local/nginx 指定安装到 /usr/local/nginx 目录下。

    上面配置完成后,接下来执行编译 -

    [root@localhost nginx-1.10.3]# make
    [root@localhost nginx-1.10.3]# make install
    ... ...
    cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
    test -d '/usr/local/nginx/run' 
            || mkdir -p '/usr/local/nginx/run'
    test -d '/usr/local/nginx/logs' 
            || mkdir -p '/usr/local/nginx/logs'
    test -d '/usr/local/nginx/html' 
            || cp -R html '/usr/local/nginx'
    test -d '/usr/local/nginx/logs' 
            || mkdir -p '/usr/local/nginx/logs'
    make[1]: Leaving directory `/usr/local/src/nginx-1.10.3'
    [root@localhost nginx-1.10.3]#
    
     

    上面编译时间跟你的电脑配置相关,所以可能需要一些等待时间。

    查看安装后的程序版本:

    [root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx -v
    nginx version: nginx/1.10.3
    
     

    修改Nginx默认端口(可选):

    [root@localhost nginx-1.10.3]# vi /usr/local/nginx/conf/nginx.conf
    

    找到 -

    ... ...
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    ... ...
    
     

    把上面的 80 修改为你想要的端口,如:8080 。
    修改配置后验证配置是否合法:

    [root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    
     

    启动Nginx程序、查看进程 -

    [root@localhost nginx-1.10.3]# /usr/local/nginx/sbin/nginx
    [root@localhost nginx-1.10.3]# ps -ef | grep nginx
    root      29151      1  0 22:01 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
    nginx     29152  29151  0 22:01 ?        00:00:00 nginx: worker process
    root      29154   2302  0 22:01 pts/0    00:00:00 grep --color=auto nginx
    [root@localhost nginx-1.10.3]#
    
     

    nginx停止、重启
    未添加nginx服务前对nginx的管理只能通过一下方式管理:

    #  nginx 管理的几种方式 -
    # 启动Nginx 
    /usr/local/nginx/sbin/nginx 
    # 从容停止Nginx:
    kill -QUIT 主进程号 # 如上一步中的 ps 命令输出的 29151,就是 Nginx的主进程号
    # 快速停止Nginx:
    kill -TERM 主进程号
    # 强制停止Nginx:
    pkill -9 nginx
    # 平滑重启nginx
    /usr/nginx/sbin/nginx -s reload
    

    现在我们来看看安装的Nginx的运行结果,可以简单地使用curl命令访问localhost测试,结果如下 -

    [root@localhost nginx-1.10.3]# curl localhost
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    [root@localhost nginx-1.10.3]#
    

    或者也可以打开浏览访问目标服务器的IP,在本示例中,服务器的IP地址是:192.168.0.195,所以打开浏览器访问如下结果 -

    提示: 如果没有看到以上界面,在确保Nginx启动的前提下,检查SeLinux和防火墙是否已关闭。关闭防火墙命令:

    systemctl stop firewalld.service

  • 相关阅读:
    手机分辨率对应表
    本地加密解密工具类
    缓存清理的工具类
    Javadoc常见的标记和含义
    Android 禁止进入activity自动弹出键盘
    ListView
    可以展开和收起的的LinearLayout
    Android 编码规范
    ToastUtils
    防微信左滑删除的效果
  • 原文地址:https://www.cnblogs.com/wenhongyu/p/8400233.html
Copyright © 2020-2023  润新知