• centos 7 下nginx的安装


    安装所需环境

    nginx 是C语言开发,建议在Linux上运行,当然,也可以安装Windows版本,本篇则使用 Centos 7 作为安装环境。

    一. gcc 安装
    安装 nginx 需要先将官网下载下来的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

    yum install gcc-c++

    二. PCRE pcre-devel 安装PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

    yum install -y pcre pcre-devel

    三. zlib 安装
    zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

    yum install -y zlib zlib-devel

    四. OpenSSL 安装OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

     

    yum install -y openssl openssl-devel

    官网下载

    1.直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html

    nginx.png

    2.使用wget命令下载(推荐)。

    wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
    -c选项:断点续传下载文件(wget -help查看)

    nginx-wget.png

    我下载的是1.10.1版本,这个是目前的稳定版。

    解压

    依然是直接命令:

    tar -zxvf nginx-1.10.1.tar.gz
    cd nginx-1.10.1

    配置

    其实在 nginx-1.10.1 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。

    1.使用默认配置

    ./configure

    2.自定义配置(不推荐)

    ./configure 
    --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

    注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

    编译安装

    make && make install

    查找安装路径:

    whereis nginx

    nginx-whereis.png

    启动、停止nginx

    cd /usr/local/nginx/sbin/
    ./nginx
    ./nginx -s stop
    ./nginx -s quit
    ./nginx -s reload

    ./nginx -s quit : 此方式停止步骤是待nginx进程处理任务完毕进行停止。./nginx -s stop: 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

    查询nginx进程:

    ps aux | grep nginx

    重启 nginx

    1.先停止再启动(推荐):

    对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:

    ./nginx -s quit
    ./nginx

    2.重新加载配置文件:

    当 nginx的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 nginx再启动 nginx 即可将配置信息在 nginx 中生效,如下:

    ./nginx -s reload

    3.centos7 防火墙设置(和7以前版本差别较大) 

    安装防火墙服务:

    yum -y install firewalld systemd

    查看防火墙状态:

    firewall-cmd --state

    可以直接关闭防火墙

    systemctl stop firewalld

    或者只开启80端口

    firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
    firewall-cmd --reload   (开启后需要重启防火墙使配置生效)

    查看开启端口情况

    firewall-cmd --zone=public --query-port=80/tcp

    删除开启的端口

    firewall-cmd --zone=public --remove-port=80/tcp --permanent

    在浏览器访问可以看到这样的页面:

    nginx-welcome.png

    开机自启动

    即在rc.local增加启动代码就可以了。

    vi /etc/rc.local

    增加一行 /usr/local/nginx/sbin/nginx设置执行权限:

    chmod 755 rc.local

    nginx-rclocal.png

    到这里,nginx就安装完毕了,启动、停止、重启操作也都完成了,当然,你也可以添加为系统服务,我这里就不在演示了。

    参考:http://blog.mayongfa.cn/83.html

  • 相关阅读:
    C++处理Json串——jsoncpp库
    古典文学--本经阴符七术
    古典文学--素书
    网络编程之getaddrinfo
    网络编程之addrinfo
    跳表数据结构
    MySQL 编译安装并且开启DEBUG模式
    volatile关键字详解
    istringstream、ostringstream、stringstream 类简介
    Selenium入门21 Select操作
  • 原文地址:https://www.cnblogs.com/codestarer/p/13635574.html
Copyright © 2020-2023  润新知