微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Centos6.5安装Nginx

Nginx是一款高性能反向代理软件,现在应用极其广泛,软件负载均衡一般有Nginx和LVS两种方式,其中Nginx更为流行,因为它安装配置简单易于维护,还能缓存一些静态资源。
首先安装依赖库
1、安装g++

yum install gcc gcc-c++

提示
Another app is currently holding the yum lock; waiting for it to exit…
使用

rm -f /var/run/yum.pid

强制关闭yum进程
2、安装pcre库

cd /usr/fuyuwei/software
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
tar -zxvf pcre-8.37.tar.gz -C /usr/local
cd /usr/local/pcre-8.37/
./configure
make
make install

3、安装zlib库

cd /usr/fuyuwei/software
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz -C /usr/local
cd /usr/local/zlib-1.2.11
./confiure
make
make install

4、安装openssl

cd /usr/fuyuwei/software
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
tar -zxvf openssl-1.0.1t.tar.gz -C /usr/local
cd /usr/local/openssl-1.0.1t
./configure
make
make install

5、安装Nginx

cd /usr/fuyuwei/software
wget http://Nginx.org/download/Nginx-1.1.10.tar.gz
tar -zxvf Nginx-1.1.10.tar.gz -C /usr/local
cd /usr/local
#去掉版本号,要不编译之后sbin会在另一个文件夹下
mv Nginx-1.1.10 Nginx
cd /usr/local/Nginx
./configure
make
make install

6、启动Nginx

#80端口可能被Apache占用,修改端口号为8090
vim /usr/local/Nginx/conf/Nginx.conf
server {
        listen       8090;
        server_name  localhost;
        #charset koi8-r;
        #access_log logs/host.access.log main;
        location / {
            root   html;
            index  index.html index.htm;
        }
/usr/local/Nginx/sbin/Nginx -c /usr/local/Nginx/config/
#报错提示
/usr/local/Nginx/sbin/Nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
#执行
ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.0.1
#创建日志文件
touch /usr/local/Nginx/logs/error.log
touch /usr/local/Nginx/logs/access.log
#再次启动
netstat -tulnp|grep 8090
tcp        0      0 0.0.0.0:8090                0.0.0.0:*                   LISTEN      27172/Nginx   
#关闭防火墙
services iptables stop

7、测试访问

8、Nginx的重启、关闭、启动
停止:

#查看进程号
ps -ef|grep Nginx
#从容停止Nginx 
kill -QUIT master进程号  
#快速停止Nginx 
kill -TERM master进程号  
#强制停止Nginx 
kill -9 master进程号

重启:

cd /usr/local/Nginx/sbin
./Nginx -s reload
#或者查找当前Nginx进程号,然后输入命令:kill -HUP 进程号 实现重启Nginx服务

9、验证Nginx.conf配置文件是否正确

cd /usr/local/Nginx/sbin/
./Nginx -t
Nginx: the configuration file /usr/local/Nginx/conf/Nginx.conf Syntax is ok
Nginx: configuration file /usr/local/Nginx/conf/Nginx.conf test is successful

10、Nginx启动脚本

#!/bin/sh 
# chkconfig: 2345 85 15 
# description:Nginx Server 

Nginx_HOME=/usr/local/Nginx  
Nginx_SBIN=$Nginx_HOME/sbin/Nginx  
Nginx_CONF=$Nginx_HOME/conf/Nginx.conf  
Nginx_PID=$Nginx_HOME/logs/Nginx.pid  

Nginx_NAME="Nginx"  

. /etc/rc.d/init.d/functions  

if [ ! -f $Nginx_SBIN ]  
then  
    echo "$Nginx_NAME startup: $Nginx_SBIN not exists! "  
    exit  
fi  

start() {  
    $Nginx_SBIN -c $Nginx_CONF  
    ret=$?  
    if [ $ret -eq 0 ]; then  
        action $"Starting $Nginx_NAME: " /bin/true  
    else  
        action $"Starting $Nginx_NAME: " /bin/false  
    fi  
}  

stop() {  
    kill `cat $Nginx_PID`  
    ret=$?  
    if [ $ret -eq 0 ]; then  
        action $"Stopping $Nginx_NAME: " /bin/true  
    else  
        action $"Stopping $Nginx_NAME: " /bin/false  
    fi  
}  

restart() {  
    stop  
    start  
}  

check() {  
    $Nginx_SBIN -c $Nginx_CONF -t  
}  


reload() {  
    kill -HUP `cat $Nginx_PID` && echo "reload success!"  
}  

relog() {  
    kill -USR1 `cat $Nginx_PID` && echo "relog success!"  
}  

case "$1" in  
    start)  
        start  
        ;;  
    stop)  
        stop  
        ;;  
    restart)  
        restart  
        ;;  
    check|chk)  
        check  
        ;;  
    status)  
        status -p $Nginx_PID  
        ;;  
    reload)  
        reload  
        ;;  
    relog)  
        relog  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|restart|reload|status|check|relog}"  
        exit 1  
esac

上面是Nginx的启动脚本,只要把它拷贝至

/etc/init.d

目录下,就可以通过

service Nginx start

等目录操作Nginx

11、Nginx平滑升级
使用新的可执行程序替换旧的可执行程序,对于编译安装的Nginx可以将新版本编译安装到旧版本的Nginx安装路径中。发送以下指令

kill -USR2 旧版本的Nginx的 master pid

#旧版本的Nginx主进程重命名他的.pid魏.oldbin,然后执行新版本的Nginx的可执行程序,依次启动新的主进程和新的工作进程

此时新旧版本的Nginx实例会同时运行,要逐步停止旧版本的Nginx实例必须发送WINCH新号给旧的主进程,然后他的工作进程就将开始从容关闭

-WINCH 旧版本的Nginx的 master pid kill -WINCH 旧版本的Nginx的 master pid

一段时间后,旧的工作进程处理了所有已连接请求退出,之后由新的工作进程来处理新来的请求。
这时候我们可以选择使用旧版本还是新版本

kill -TERM newpid

#Nginx将在不重载配置文件的情况下启动他的工作进程
kill -HUP oldpid
#从容关闭其工作进程
kill -QUIT newpid
#强制退出
kill -TERM newpid

原文地址:https://www.jb51.cc/centos/375552.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。