nginx在linux环境下安装

跟風遠走 提交于 2020-12-11 11:52:56

1、安装依赖包

yum install autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel

2、下载nginx

cd /usr/local/nginx

wget http://nginx.org/download/nginx-1.12.2.tar.gz

3、解压安装

tar -zxvf xxx.tar.gz

mkdir /usr/local/nginx

./configure --prefix=/usr/local/nginx --with-stream (--with-stream 加入tcp模块)

make

make 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.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

9、设置文件权限,加入到系统启动项中

chmod +x /etc/rc.d/init.d/nginx

chkconfig --add nginx

chkconfig --level 2345 nginx on

chkconfig --list nginx

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!