CentOS安装nginx教程
用putty登录,运行以下命令:
#!/bin/bash
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
wget http://nginx.org/download/nginx-1.10.1.tar.gz
tar zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1
./configure --prefix=/usr/local/nginx
make && make install
service httpd stop
yum -y remove httpd
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#设置开机启动
echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.d/rc.local
Shell安装脚本:
#!/bin/bash
# 配置系统环境
yum install -y gcc gcc-c++ make autoconf automake wget screen iptables sendmail zip unzip perl perl-devel
yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel
# 备份配置文件
if [ ! -d 'conf_backup'];then
mkdir 'conf_backup'
fi
if [ -f '/etc/init.d/nginx' ]; then
mv /etc/init.d/nginx ./conf_backup/
fi
if [ -f '/etc/nginx.conf' ]; then
mv /etc/nginx.conf ./conf_backup/
fi
# 检查文件
if [ ! -s 'nginx-1.6.0.tar.gz' ]; then
wget -c http://nginx.org/download/nginx-1.6.0.tar.gz
fi
####################################### 开始安装Nginx #######################################
# 新建www用户
useradd -s /sbin/nologin www
# 解压并安装
tar zxvf nginx-1.6.0.tar.gz
cd nginx-1.6.0
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make && make install
cd ..
# 服务脚本
echo '#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
case "$1" in
start)
echo -n "Starting $NAME... "
if netstat -tnpl | grep -q nginx;then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if ! netstat -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if netstat -tnpl | grep -q nginx; then
PID=`pidof nginx`
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped"
exit 0
fi
;;
force-quit)
echo -n "Terminating $NAME... "
if ! netstat -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi
kill `pidof $NAME`
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$SCRIPTNAME stop
sleep 1
$SCRIPTNAME start
;;
reload)
echo -n "Reload service $NAME... "
if netstat -tnpl | grep -q nginx; then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, cant reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|force-quit|restart|reload|status|configtest}"
exit 1
;;
esac' > /etc/init.d/nginx
chmod +x /etc/init.d/nginx
# 快捷方式
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/nginx.conf /etc/nginx.conf
# 设置防火墙开启80端口
if [ -z "`/sbin/iptables -nL | grep dpt:80`" ];then
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
service iptables restart
fi
# 设置Nginx开机启动
chkconfig nginx --add
chkconfig nginx on
# 安装完成
service nginx start