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

Nginx介绍与安装

2016-07-24 09:52 585 查看


1.1  Nginx

1.1.1  什么是nginx

是一个使用c语言开发的高性能的http服务器及反向代理服务器。
Nginx是一款高性能的http服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor
Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。
 

1.1.2  Nginx的应用场景

http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。

虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。

反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

 

1.1.3  Nginx的安装

Nginx一般推荐安装到linux系统,而且要安装c语言的编译环境gcc。

 

1.1.3.1        下载:

进入http://nginx.org/en/download.html下载nginx1.8.0版本(当前最新稳定版本)。
nginx-1.8.1.tar.gz

1.1.3.2        先安装nginx依赖的包:

nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境。

gcc

      安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum
install gcc-c++

PCRE

      PCRE(PerlCompatible Regular Expressions)是一个Perl库,包括 perl兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

yuminstall -y pcre pcre-devel

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

zlib

      zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

yuminstall -y zlib zlib-devel

 

openssl

      OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

      nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yuminstall -y openssl openssl-devel

 

1.1.3.2        安装步骤

第一步:把nginx的源码上传到linux系统/home/mark下

第二步:把压缩包解压缩。

第三步:进行configure。

mkdir -p /var/temp/nginx

cd /home/mark/

./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/nginx/client\

--http-proxy-temp-path=/var/temp/nginx/proxy\

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi\

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi\

--http-scgi-temp-path=/var/temp/nginx/scgi

 

注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

第四步:make

第五步:makeinstall
然后cd /nginx/sbin
./nginx就可以启动Nginx

第六步:设置启动服务

cd /etc/init.d

vim nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/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="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

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
killall -9 nginx
}

restart() {
configtest || return $?
stop
sleep 1
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@com init.d]# chkconfig nginx on

[root@com init.d]# chkconfig nginx --list

nginx           0:off 1:off 2:on 3:on 4:on 5:on 6:off

第八步:启动nginx

chmod 755 /etc/init.d/nginx

[root@com init.d]# service nginx start

Starting nginx:         
dd63
                                   [  OK  ]

第九步:查看Nginx进程(两个进程正常)

[root@com init.d]# ps -aux|grep nginx

Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ

root      9935  0.0  0.0  23848   792 ?        Ss   22:54   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nobody    9937  0.0  0.1  24276  1376 ?        S    22:54   0:00 nginx: worker process                                         

root      9946  0.0  0.0 103312   840 pts/1    S+   22:56   0:00 grep nginx

[root@com init.d]#

第十步:查看端口

[root@com init.d]# netstat -lntp | grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      9935/nginx

第十一步:设置开通80端口防火墙

vim /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

:wq!

service iptables restart

第十二步:在物理主机访问Nginx主页

http://192.168.86.130/

就会看到欢迎页面!

 

1.1.1  Nginx的配置

在/usr/local/nginx/conf目录下nginx.conf文件是nginx的配置文件。

 

1.1.2  使用nginx配置虚拟机

1.1.2.1        通过端口区分虚拟机

在nginx.conf文件中添加一个Service节点,修改端口号就可以

server {

       listen      81;

       server_name localhost;

 

       #charset koi8-r;

 

       #access_log logs/host.access.log main;

 

       location / {

           root  html81;

           index index.html
index.htm;

       }

  }
 

1.1.2.2        通过域名区分虚拟机

server {

       listen      80;

       server_name test3.taotao.com;

 

       #charset koi8-r;

 

       #access_log logs/host.access.log main;

 

       location / {

           root  html-test3;

           index index.html
index.htm;

       }

  }
 

修改配置后需要重新加载配置文件。

更改本地DNS

 




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: