首頁  >  文章  >  後端開發  >  CentOS安裝Nginx伺服器 centos nginx 啟動 centos6.5 nginx centos7 安裝 ngin

CentOS安裝Nginx伺服器 centos nginx 啟動 centos6.5 nginx centos7 安裝 ngin

WBOY
WBOY原創
2016-07-29 08:49:50926瀏覽

本文主要介绍Nginx在linux下的安装与设置。
Nginx是一个高性能的web反向代理服务器,详情参考http://nginx.org/
中文文档http://www.nginx.cn/doc

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开发包的意思):

<code><span># yum install -y pcre pcre-devel  </span><span># yum install -y zlib zlib-devel  </span><span># yum install -y openssl openssl-devel  </span></code>

2、下载并安装

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

<code><span># mkdir nginx-src && cd nginx-src  </span><span># wget http://nginx.org/download/nginx-1.7.3.tar.gz  </span><span># tar xzf nginx-1.7.3.tar.gz   </span><span># cd nginx-1.7.3  </span><span># ./configure  </span><span># make  </span><span># make install  </span><span># whereis nginx  </span>
nginx: /usr/local/nginx  </code>

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

<code># ./nginx -h

nginx version: nginx/<span>1.7</span><span>.3</span>Usage: nginx [-?hvVtq] [-s <span>signal</span>] [-c filename] [-p prefix] [-g directives]  
Options:  
  -?,-h         : this help  
  -v            : show version <span>and</span><span>exit</span>  -V            : show version <span>and</span> configure options <span>then</span><span>exit</span>  -t            : test <span>configuration</span><span>and</span><span>exit</span>  -q            : suppress non-error messages during <span>configuration</span> testing  
  -s <span>signal</span>     : send <span>signal</span><span>to</span> a master <span>process</span>: stop, quit, reopen, reload  
  -p prefix     : set prefix path (<span>default</span>: /usr/local/nginx/)  
  -c filename   : set <span>configuration</span><span>file</span> (<span>default</span>: conf/nginx.conf)  
  -g directives : set global directives <span>out</span><span>of</span><span>configuration</span><span>file</span></code>

启动:nginx
停止:nginx -s stop

3、添加到系统服务

使用命令# vi /etc/init.d/nginx,打开编辑器,输入如下内容:

<code><span>#!/bin/sh  </span><span># chkconfig: 2345 85 15  </span><span># Startup script for the nginx Web Server  </span><span># description: nginx is a World Wide Web server.   </span><span># It is used to serve HTML files and CGI.  </span><span># processname: nginx  </span><span># pidfile: /usr/local/nginx/logs/nginx.pid  </span><span># config: /usr/local/nginx/conf/nginx.conf  </span>PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  
DESC=<span>"nginx deamon"</span>NAME=nginx  
DAEMON=/usr/local/nginx/sbin/<span>$NAME</span>SCRIPTNAME=/etc/init.d/<span>$NAME</span>test -x <span>$DAEMON</span> || <span>exit</span><span>0</span><span><span>d_start</span></span>(){  
  <span>$DAEMON</span> || <span>echo</span> -n <span>"already running"</span>}  

<span><span>d_stop</span></span>(){  
  <span>$DAEMON</span><span>-s</span> quit || <span>echo</span> -n <span>"not running"</span>}  


<span><span>d_reload</span></span>(){  
  <span>$DAEMON</span><span>-s</span> reload || <span>echo</span> -n <span>"can not reload"</span>}  

<span>case</span><span>"<span>$1</span>"</span><span>in</span>start)  
  <span>echo</span> -n <span>"Starting <span>$DESC</span>: <span>$NAME</span>"</span>  d_start  
  <span>echo</span><span>"."</span>;;  
stop)  
  <span>echo</span> -n <span>"Stopping <span>$DESC</span>: <span>$NAME</span>"</span>  d_stop  
  <span>echo</span><span>"."</span>;;  
reload)  
  <span>echo</span> -n <span>"Reloading <span>$DESC</span> conf..."</span>  d_reload  
  <span>echo</span><span>"reload ."</span>;;  
restart)  
  <span>echo</span> -n <span>"Restarting <span>$DESC</span>: <span>$NAME</span>"</span>  d_stop  
  sleep <span>2</span>  d_start  
  <span>echo</span><span>"."</span>;;  
*)  
  <span>echo</span><span>"Usage: <span>$ScRIPTNAME</span> {start|stop|reload|restart}"</span> >&<span>2</span><span>exit</span><span>3</span>;;  
<span>esac</span><span>exit</span><span>0</span></code>

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

<code><span># chmod +x /etc/init.d/nginx  </span><span># chkconfig --add nginx  </span><span># chkconfig nginx on/off  </span><span># chkconfig --list nginx  </span>
nginx <span>0</span>:off <span>1</span>:off <span>2</span>:<span><span>on</span></span><span>3</span>:<span><span>on</span></span><span>4</span>:<span><span>on</span></span><span>5</span>:<span><span>on</span></span><span>6</span>:off  </code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了CentOS安装Nginx服务器,包括了centos,nginx方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn