Heim  >  Artikel  >  Backend-Entwicklung  >  CentOS安装Nginx服务器 centos nginx 启动 centos6.5 nginx centos7 安装 ngin

CentOS安装Nginx服务器 centos nginx 启动 centos6.5 nginx centos7 安装 ngin

WBOY
WBOYOriginal
2016-07-29 08:49:50925Durchsuche

本文主要介绍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教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn