安装nginx之前,首先查看下gcc编译器是否安装(nginx用c编写) 用gcc -v查看版本
安装需要的文件
# yum install -y pcre pcre-devel
# yum install -y zlib zlib-devel
# yum install -y openssl openssl-devel
下载nginx进行安装
# 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
停止:nginx -s stop
3、添加到系统服务
使用命令“# vi /etc/init.d/nginx”,打开编辑器,输入如下内容
#!/bin/bash
# chkconfig: -85 15
# Startup script for the nginx Web Server
# description: nginx is a World Wide Web server.It is used to serve
# 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