首页 > 代码库 > nginx安装与配置

nginx安装与配置

对于centos,

首先,安装一些依赖:

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

1创建并进入安装包存放目录

mkdir nginx-src && cd nginx-src

2 wget下载 nginx1.7 

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  

 ./configure  

 make  

 make install  

5 nginx默认安装在/usr/local/nginx目录下

 whereis nginx  

  1. nginx: /usr/local/nginx 

 

6.管理Nginx

启动:/usr/local/nginx/sbin/nginx 

nginx -s signal

  • stop — fast shutdown
  • quit — graceful shutdown
  • reload — reloading the configuration file
  • reopen — reopening the log files

停止:nginx -s stop/quit

重新加载:nginx -s reload

当然也可以将nginx 加入系统服务

在/etc/init.d/目录下创建如下内容的nginx脚本

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

赋予其可执行权限:

chmod +x 

chkconfig --add nginx

之后就可使用service nginx start/stop/restart/reload/status管理nginx.

 访问 http://ip:80,如果出现如下欢迎页则一切成功,否则查看防火墙是否关闭或者拦截了80端口。

技术分享

 

nginx安装与配置