Nginx安装

随着计算机的发展,形形色色的Web站点成为直接面向用户的中坚力量,在各种网站服务器软件中,除了Apache外,还有一款轻量级的Web软件——Nginx,由俄罗斯人开发,其稳定、高效的特性逐渐被越来越多的用户认可。

Nginx(发音engine)专为性能优化而开发,其最知名的优点是他的稳定性和低系统资源消耗,以及对HTTP并发连接的高处理能力(单台物理服务器可支持5W个并发请求),正因如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择。

1.0 编译安装Nginx

Nginx获取

[root@Web_1 ~]# wget http://nginx.org/download/nginx-1.6.3.tar.gz
–2016-08-10 16:49:17– http://nginx.org/download/nginx-1.6.3.tar.gz
Resolving nginx.org… 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org|206.251.255.63|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 805253 (786K) [application/octet-stream]
Saving to: “nginx-1.6.3.tar.gz”

100%[=====================================================================================================>] 805,253 11.9K/s in 55s

2016-08-10 16:50:14 (14.2 KB/s) - “nginx-1.6.3.tar.gz” saved [805253/805253]

[root@Web_1 ~]# mv nginx-1.6.3.tar.gz /opt/src/
[root@Web_1 ~]# cd /opt/src/
1.1 安装前提

Nginx的配置及运行需要pcre、zlib-devel等软件包的支持,因此需要提前安装,以便提供相应的库和头文件,确保Nginx的安装顺利完成。

[root@Web_1 src]# rpm -qa pcre-devel zlib-devel openssl-devel  #查看是否安装
[root@Web_1 src]# yum -y install pcre zlib-devel #若没有安装,则可以使用此命令进行安装
[root@Web_1 src]# yum -y install gcc gcc-c++ ncurses-devel per #系统依赖包安装

1.2 创建运行用户、组

Nginx默认以nobody用户运行,建议使用单独用户

[root@Web_1 src]# useradd -r -M -s /sbin/nologin nginx  #创建Nginx系统用户
[root@Web_1 src]# id nginx
uid=498(nginx) gid=498(nginx) groups=498(nginx)

1.3 编译安装Nginx

[root@Web_1 src]# tar zxf nginx-1.6.3.tar.gz
[root@Web_1 src]# cd nginx-1.6.3
[root@Web_1 nginx-1.6.3]# ./configure –prefix=/opt/nginx –user=nginx –group=nginx –with-http_ssl_module –with-http_gzip_static_module –with-http_gunzip_module –with-http_stub_status_module –with-pcre
[root@Web_1 nginx-1.6.3]# make && install

1.4启动Nginx

[root@Web_1 nginx-1.6.3]# ln -s /opt/nginx/sbin/nginx /usr/local/sbin/  #为主程序做链接
[root@Web_1 nginx-1.6.3]# nginx -t #语法测试
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@Web_1 nginx-1.6.3]# nginx #启动Nginx

1.5添加Nginx服务脚本

在/etc/init.d/下创建nginx文件,并写入以下内容

#!/bin/sh
#

#nginx - this script starts and stops the nginx daemin
#

chkconfig: - 85 15

description: Nginx is an HTTP(S) server, HTTP(S) reverse \

proxy and IMAP/POP3 proxy server

processname: nginx

config: /usr/local/nginx/conf/nginx.conf

pidfile: /usr/local/nginx/logs/nginx.pid

Source function library.

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

Source networking configuration.

. /etc/sysconfig/network

Check that networking is up.

[ “$NETWORKING” = “no” ] && exit 0

nginx=”/opt/nginx/sbin/nginx”
prog=$(basename $nginx)

NGINX_CONF_FILE=”/opt/nginx/conf/nginx.conf”

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $”Starting $prog: “
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $”Stopping $prog: “
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
start
}

reload() {
configtest || return $?
echo -n $”Reloading $prog: “
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case “$1” in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
exit 2
esac

添加为系统服务

[root@Web_1 nginx-1.6.3]# chmod +x /etc/init.d/nginx
[root@Web_1 nginx-1.6.3]# chkconfig –add nginx
[root@Web_1 nginx-1.6.3]# chkconfig nginx on
[root@Web_1 nginx-1.6.3]# chkconfig –list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

测试

[root@Web_1 nginx-1.6.3]# service nginx restart
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]

1.6访问Nginx服务器

[root@Web_1 nginx-1.6.3]# iptables -I INPUT -p tcp –dport 80 -j ACCEPT  #允许对80端口的访问
[root@Web_1 nginx-1.6.3]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@Web_1 nginx-1.6.3]# curl 127.0.0.1 #本地测试

 

-------------本文结束感谢您的阅读-------------