1、安装依赖包
yum install autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
2、下载nginx
cd /usr/local/nginxwget http://nginx.org/download/nginx-1.12.2.tar.gz
3、解压安装
tar -zxvf xxx.tar.gzmkdir /usr/local/nginx./configure --prefix=/usr/local/nginx --with-stream (--with-stream 加入tcp模块)makemake install
4、防火墙允许80端口通过
vi /etc/sysconfig/iptables在 -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -------------redhat -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -------------centos下添加: -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
5、重启防火墙
/etc/init.d/iptables restart 或者 service iptables restart
6、启动
./usr/local/nginx/sbin/nginx
7、停止
./usr/local/nginx/sbin/nginx -s stop
8、设置开机自启动
vi /etc/rc.d/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.confnginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/var/run/nginx.pidRETVAL=0prog="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 1fi 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" instart) start ;;stop) stop ;;reload) reload ;;restart) stop start ;;status) status $prog RETVAL=$? ;;*) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1esacexit $RETVAL
9、设置文件权限,加入到系统启动项中
chmod +x /etc/rc.d/init.d/nginxchkconfig --add nginxchkconfig --level 2345 nginx onchkconfig --list nginx