CentOS下的Nginx安装配置
这里演示在CentOS使用yum快速安装Nginx,在Ubuntu,Debian下对应apt-get工具,步骤类似:
1)检查系统是否已经有CentOS的rpm安装包
1
rpm -qa nginx
2)如果没有,或者没有需要的版本,通过yum查询仓库
1
yum search nginx
3)选择需要的版本进行下载,如果yum库没有需要的版本,可以添加remi源,或者wget官网的tar包重新编译;
首选要确保系统中有www用户组和www用户,nginx默认归属该用户,当然也可以自己重新指定其他用户组,但是在nginx安装后需要更改nginx.conf中的"use www www"为指定的用户组和用户‘
1
yum -y install nginx
安装结束后,可以通过 chkconfig nginx on 查看nginx的安装信息,和安装路径等;
为了方便演示,假设安装路径在 /usr/local/nginx
4)将nginx的sbin包添加到PATH环境变量方便nginx工具的快速调用;
vim 编辑 /etc/pofile 文件,添加以下文本后,再通过 "source /etc/profile" 更新配置;
1
export PATH=$PATH:/usr/local/nginx/sbin
更新配置后重启 nginx 指令
1
nginx -s reload
当然也可以通过把nginx注册为service来快速开启关闭nginx,在/etc/init.d/ 下创建文件 nginx,添加以下内容:
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
1
2
之后执行:
1
chmod a+x nginx
2
chkconfig --add nginx
便可以通过以下快速启动,关闭,重启nginx
1
service nginx start
2
service nginx stop
3
service nginx restart