yum安装nginx
Centos默认的yum源里没有nginx,需要手动添加源,有两种方法:
使用nginx提供的一个源设置安装包
nginx下载页面:http://nginx.org/en/download.html
nginx提供4个版本的系统的源:
我选择Centos6,文件名为:nginx-release-centos-6-0.el6.ngx.noarch.rpm。
在服务器上安装:
rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
之后便可以使用yum快速安装了:
yum install nginx
手动添加nginx源
cd /etc/yum.repos.d vim nginx.repo #内容为:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=
enabled=
baseurl中centos和6可以更改为自己的版本,比如redhat 5版本的链接为:
baseurl=http://nginx.org/packages/rhel/5/$basearch/
详情可以自己看nginx的下载页面,包括其他系统的源安装方法。
相关路径
nginx可执行文件:/usr/sbin/nginx
nginx配置目录:/etc/nginx
nginx log目录:/var/log/nginx
编译安装nginx
我的nginx编译安装代码:
yum install perl-ExtUtils-Embed.x86_64 pcre-devel.x86_64
#根据自己的需求,选择参数,可用的参数可以使用./configure --help命令查看
./configure --prefix=/opt/nginx
--with-http_gzip_static_module
--with-http_perl_module
--with-pcre
--with- rtsig_module
--with-select_module
--with-poll_module
--with-file-aio make
make install
为编译安装的nginx配置init脚本:
vim /etc/init.d/nginx
#内容取自yum安装的nginx后,生成的文件,改下里面的路径即可使用了
#!/bin/sh
#
# nginx Startup script for nginx
#
# chkconfig: -
# processname: nginx
# config: /opt/nginx/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /opt/nginx/logs/nginx.pid
# description: nginx is a HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start:
# Default-Stop:
# Short-Description: start and stop nginx
### END INIT INFO # Source function library.
. /etc/rc.d/init.d/functions if [ -f /etc/sysconfig/nginx ]; then
. /etc/sysconfig/nginx
fi prog=nginx
nginx=${NGINX-/opt/nginx/sbin/nginx}
conffile=${CONFFILE-/opt/nginx/conf/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/opt/nginx/logs/nginx.pid}
SLEEPMSEC=
RETVAL= start() {
echo -n $"Starting $prog: " daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
RETVAL=$?
echo
[ $RETVAL = ] && touch ${lockfile}
return $RETVAL
} stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} ${prog}
RETVAL=$?
echo
[ $RETVAL = ] && rm -f ${lockfile} ${pidfile}
} reload() {
echo -n $"Reloading $prog: "
killproc -p ${pidfile} ${prog} -HUP
RETVAL=$?
echo
} upgrade() {
oldbinpidfile=${pidfile}.oldbin configtest -q || return
echo -n $"Staring new master $prog: "
killproc -p ${pidfile} ${prog} -USR2
RETVAL=$?
echo
/bin/usleep $SLEEPMSEC
if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
echo -n $"Graceful shutdown of old $prog: "
killproc -p ${oldbinpidfile} ${prog} -QUIT
RETVAL=$?
echo
else
echo $"Upgrade failed!"
return
fi
} configtest() {
if [ "$#" -ne ] ; then
case "$1" in
-q)
FLAG=$
;;
*)
;;
esac
shift
fi
${nginx} -t -c ${conffile} $FLAG
RETVAL=$?
return $RETVAL
} rh_status() {
status -p ${pidfile} ${nginx}
} # See how we were called.
case "$1" in
start)
rh_status >/dev/null >& && exit
start
;;
stop)
stop
;;
status)
rh_status
RETVAL=$?
;;
restart)
configtest -q || exit $RETVAL
stop
start
;;
upgrade)
upgrade
;;
condrestart|try-restart)
if rh_status >/dev/null >&; then
stop
start
fi
;;
force-reload|reload)
reload
;;
configtest)
configtest
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
RETVAL=
esac exit $RETVAL
使用chkconfig安装为安装启动项
chkconfig --add nginx
chkconfig nginx on nginx命令:
启动:nginx
停止:nginx -s stop
重置生效:nginx -s reload