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开发包的意思):
- # yum install -y pcre pcre-devel
- # yum install -y zlib zlib-devel
- # yum install -y openssl openssl-devel
2、下载并安装
创建目录(nginx-src)并进去;然后,从官方地址(http://nginx.org/)下载,解压,配置,编译,安装:
- # mkdir nginx-src && cd nginx-src
- # wget http://nginx.org/download/nginx-1.7.3.tar.gz
- # tar xzf nginx-1.7.3.tar.gz
- # cd nginx-1.7.3
- # ./configure
- # make
- # make install
- # whereis nginx
- nginx: /usr/local/nginx
默认的安装路径为:/usr/local/nginx;跳转到其目录下sbin路径下,便可以启动或停止它了。
- # ./nginx -h
- nginx version: nginx/1.7.3
- Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
- Options:
- -?,-h : this help
- -v : show version and exit
- -V : show version and configure options then exit
- -t : test configuration and exit
- -q : suppress non-error messages during configuration testing
- -s signal : send signal to a master process: stop, quit, reopen, reload
- -p prefix : set prefix path (default: /usr/local/nginx/)
- -c filename : set configuration file (default: conf/nginx.conf)
- -g directives : set global directives out of configuration file
启动:nginx
测试是否正常:在浏览器输入服务器所在主机IP(202.116.147.88:80)测试是否成功。如果80端口被apache占用,可视情况修改nginx端口号或apache端口号。
停止:nginx -s stop
3、添加到系统服务
使用命令“# vi /etc/init.d/nginx”,打开编辑器,输入如下内容:
- #!/bin/sh
- # chkconfig: 2345 85 15
- # Startup script for the nginx Web Server
- # description: nginx is a World Wide Web server.
- # It is used to serve HTML files and CGI.
- # processname: nginx
- # pidfile: /usr/local/nginx/logs/nginx.pid
- # config: /usr/local/nginx/conf/nginx.conf
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- DESC="nginx deamon"
- NAME=nginx
- DAEMON=/usr/local/nginx/sbin/$NAME
- SCRIPTNAME=/etc/init.d/$NAME
- test -x $DAEMON || exit 0
- d_start(){
- $DAEMON || echo -n "already running"
- }
- d_stop(){
- $DAEMON -s quit || echo -n "not running"
- }
- d_reload(){
- $DAEMON -s reload || echo -n "can not reload"
- }
- case "$1" in
- start)
- 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: $NAME"
- d_stop
- sleep 2
- d_start
- echo "."
- ;;
- *)
- echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2
- exit 3
- ;;
- esac
- exit 0
保存退出后,再使用下面的命令,使其可执行;然后,添加配置并查看。
可用chkconfig修改其值,也可用ntsysv工具改变是否自启动。
- # chmod +x /etc/init.d/nginx
- # chkconfig --add nginx
- # chkconfig nginx on/off
- # chkconfig --list nginx
- nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
至此,nginx已经安装完成。
附pcre的源码安装方法:
PCRE(Perl Compatible Regular Expressions)是一个轻量级的Perl函数库,包括 perl 兼容的正则表达式库。它比Boost之类的正则表达式库小得多。PCRE十分易用,同时功能也很强大,性能超过了POSIX正则表达式库和一些经典的正则表达式库。
1. PCRE目前最新版本为8.36,可以点这里进行下载。
2. 使用tar -zxvf pcre-8.36.tar.gz进行解压。
3. 运行 chmod -R 777 /pcre-8.36 对当前文件夹授予全部读写权限。
4. 切换到/pcre-8.36目录下,运行 ./configure 进行pcre初始化配置,会在控制台打印出一大堆的输出信息。
5. 执行make操作,进行编译。
[root@server06 pcre-8.36]# make
rm -f pcre_chartables.c
ln -s ./pcre_chartables.c.dist pcre_chartables.c
make all-am
make[1]: Entering directory `/opt/nginx/pcre-8.36'
CC libpcre_la-pcre_byte_order.lo
CC libpcre_la-pcre_compile.lo
CC libpcre_la-pcre_config.lo
CC libpcre_la-pcre_dfa_exec.lo
CC libpcre_la-pcre_exec.lo
CC libpcre_la-pcre_fullinfo.lo
CC libpcre_la-pcre_get.lo
CC libpcre_la-pcre_globals.lo
CC libpcre_la-pcre_jit_compile.lo
CC libpcre_la-pcre_maketables.lo
CC libpcre_la-pcre_newline.lo
CC libpcre_la-pcre_ord2utf8.lo
CC libpcre_la-pcre_refcount.lo
CC libpcre_la-pcre_string_utils.lo
CC libpcre_la-pcre_study.lo
CC libpcre_la-pcre_tables.lo
CC libpcre_la-pcre_ucd.lo
CC libpcre_la-pcre_valid_utf8.lo
CC libpcre_la-pcre_version.lo
CC libpcre_la-pcre_xclass.lo
CC libpcre_la-pcre_chartables.lo
CCLD libpcre.la
CC libpcreposix_la-pcreposix.lo
CCLD libpcreposix.la
CXX libpcrecpp_la-pcrecpp.lo
CXX libpcrecpp_la-pcre_scanner.lo
CXX libpcrecpp_la-pcre_stringpiece.lo
CXXLD libpcrecpp.la
CC pcretest-pcretest.o
CC pcretest-pcre_printint.o
CCLD pcretest
CC pcregrep-pcregrep.o
CCLD pcregrep
CXX pcrecpp_unittest-pcrecpp_unittest.o
CXXLD pcrecpp_unittest
CXX pcre_scanner_unittest-pcre_scanner_unittest.o
CXXLD pcre_scanner_unittest
CXX pcre_stringpiece_unittest-pcre_stringpiece_unittest.o
CXXLD pcre_stringpiece_unittest
make[1]: Leaving directory `/opt/nginx/pcre-8.36'
6. 运行 [root@server06 pcre-8.36]# make install,进行安装,至此PCRE安装完成。
[root@server06 pcre-8.36]# make install