Centos7 nginx安装

 一、安装准备

首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 所以执行如下命令安装

    $   yum install gcc-c++  
    $   yum install pcre pcre-devel  
    $   yum install zlib zlib-devel  
    $   yum install openssl openssl--devel

2.安装nginx

  1.  
    $ cd /usr/local/
  2.  
    $ wget http://nginx.org/download/nginx-1.8.0.tar.gz
  3.  
    $ tar -zxvf nginx-1.8.0.tar.gz
  4.  
    $ cd nginx-1.8.0
  5.  
    $ ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module
  6.  
    (注: --with-http_ssl_module:这个不加后面在nginx.conf配置ssl:on后,启动会报nginx: [emerg] unknown directive "ssl" in /opt/nginx/conf/nginx.conf 异常)
  7.  
    $ make && make install
     

    3.查找安装路径:

    whereis nginx

    修改配置文件 nginx.config
    4.关闭防火墙 CentOS 7.0默认使用的是firewall作为防火墙

    查看防火墙状态

    firewall-cmd --state
    • 1

    停止firewall

    systemctl stop firewalld.service
     
     

    启动、停止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.重新加载配置文件:
    当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:
    ./nginx -s reload

    启动成功后,在浏览器可以看到这样的页面:

    nginx-welcome.png

    开机自启动

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

    vi /etc/rc.local

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

    chmod 755 rc.local

    nginx-rclocal.png

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

     

原文地址:https://www.cnblogs.com/zhoading/p/10286697.html