您的位置:首页 > 运维架构 > Nginx

Centos6.5安装Nginx

2017-10-17 17:19 260 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: