Linux(CentOS)下,下载安装Nginx并配置

1、准备工作
选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL。
Nginx是C写的,需要用GCC编译;Nginx的Rewrite和HTTP模块会用到PCRE;Nginx中的Gzip用到zlib;
用命令“# gcc”,查看gcc是否安装;如果出现“gcc: no input files”信息,说明已经安装好了。
否则,就需要用命令“# yum install gcc”,进行安装了!一路可能需要多次输入y,进行确认。
安装好后,可以再用命令“#gcc”测试,或者用命令“# gcc -v”查看其版本号。
同样方法,用如下命令安装PCRE,zlib,OpenSSL(其中devel,是develop开发包的意思):

  1. # yum install -y pcre pcre-devel  
  2. # yum install -y zlib zlib-devel  
  3. # yum install -y openssl openssl-devel  


2、下载并安装
创建目录(nginx-src)并进去;然后,从官方地址(http://nginx.org/)下载,解压,配置,编译,安装:

  1. # mkdir nginx-src && cd nginx-src  
  2. # wget http://nginx.org/download/nginx-1.7.3.tar.gz  
  3. # tar xzf nginx-1.7.3.tar.gz   
  4. # cd nginx-1.7.3  
  5. # ./configure  
  6. # make  
  7. # make install  
  8. # whereis nginx  
  9. nginx: /usr/local/nginx  

默认的安装路径为:/usr/local/nginx;跳转到其目录下sbin路径下,便可以启动或停止它了。

  1. # ./nginx -h  
  2. nginx version: nginx/1.7.3  
  3. Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]  
  4. Options:  
  5.   -?,-h         : this help  
  6.   -v            : show version and exit  
  7.   -V            : show version and configure options then exit  
  8.   -t            : test configuration and exit  
  9.   -q            : suppress non-error messages during configuration testing  
  10.   -s signal     : send signal to a master process: stop, quit, reopen, reload  
  11.   -p prefix     : set prefix path (default: /usr/local/nginx/)  
  12.   -c filename   : set configuration file (default: conf/nginx.conf)  
  13.   -g directives : set global directives out of configuration file  

启动:nginx
停止:nginx -s stop

3、添加到系统服务
使用命令“# vi /etc/init.d/nginx”,打开编辑器,输入如下内容:

  1. #!/bin/sh  
  2. # chkconfig: 2345 85 15  
  3. # Startup script for the nginx Web Server  
  4. # description: nginx is a World Wide Web server.   
  5. # It is used to serve HTML files and CGI.  
  6. # processname: nginx  
  7. # pidfile: /usr/local/nginx/logs/nginx.pid  
  8. # config: /usr/local/nginx/conf/nginx.conf  
  9.   
  10. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  
  11. DESC="nginx deamon"  
  12. NAME=nginx  
  13. DAEMON=/usr/local/nginx/sbin/$NAME  
  14. SCRIPTNAME=/etc/init.d/$NAME  
  15.   
  16. test -x $DAEMON || exit 0  
  17.   
  18. d_start(){  
  19.   $DAEMON || echo -n "already running"  
  20. }  
  21.   
  22. d_stop(){  
  23.   $DAEMON -s quit || echo -n "not running"  
  24. }  
  25.   
  26.   
  27. d_reload(){  
  28.   $DAEMON -s reload || echo -n "can not reload"  
  29. }  
  30.   
  31. case "$1" in  
  32. start)  
  33.   echo -n "Starting  DESC:
  • NAME"  
  •   d_start  
  •   echo "."  
  • ;;  
  • stop)  
  •   echo -n "Stopping  DESC:
  • NAME"  
  •   d_stop  
  •   echo "."  
  • ;;  
  • reload)  
  •   echo -n "Reloading $DESC conf..."  
  •   d_reload  
  •   echo "reload ."  
  • ;;  
  • restart)  
  •   echo -n "Restarting  DESC:
    1. NAME"  
    2.   d_stop  
    3.   sleep 2  
    4.   d_start  
    5.   echo "."  
    6. ;;  
    7. *)  
    8.   echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2  
    9.   exit 3  
    10. ;;  
    11. esac  
    12.   
    13. exit 0  

    保存退出后,再使用下面的命令,使其可执行;然后,添加配置并查看。
    可用chkconfig修改其值,也可用ntsysv工具改变是否自启动。

      1. # chmod +x /etc/init.d/nginx  
      2. # chkconfig --add nginx  
      3. # chkconfig nginx on/off  
      4. # chkconfig --list nginx  
      5. nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值