精通Nginx基础篇之安装配置

来源:互联网 发布:台海危机 知乎 编辑:程序博客网 时间:2024/05/17 22:01

一、Nginx介绍

Nginx是一款高性能的HTTP和反向代理服务器,能够选择高效的epoll(linux2.6内核)、kqueue(freebsd)、eventport(solaris10)作为网络I/O模型,能够支持高达50000个并发连接数的响应,而内存、CPU等系统资源消耗却非常低、运行非常稳定。

2008年12月:Nginx市场占有量为3354329、占有率为1.80%

1、选择Nginx的理由

1.1 支持高并发连接
通过top命令可以查看系统负载和CPU使用率
由于apache采用的是select网络I/O模型,处理大量连续的读写很低效

1.2 内存消耗少
在服务器3W并发连接下,开启10个Nginx进程消耗150MB内存(15MB*10),开启64个php-cgi进程消耗128MB内存(20MB*64),共消耗不到2GB内存,可以使用webbench做压力测试,运行依旧很快。

1.3 成本低廉
购买F5 BIG-IP、NetScaler等负载均衡交换机需要几十万RMB,而开源Nginx替代这些商业设备。

1.4 其他理由
网络配置简单,对比apache就知道了
支持rewrite重写规则,能够根据域名、URL的不同、将HTTP请求分到不同的后端服务器群组
内置的健康检查功能
节省带宽,支持GZIP压缩,可以添加浏览器本地缓存的Header头
支持热部署,能够在不间断服务的情况下、对软件版本进行升级

二、Nginx编译安装

1.安装准备

nginx依赖于pcre库,所以必须要先安装pcre库

yum install pcre pcre-devel

由于nginx的gzip module还依赖一个非常重要的组件zlib library,当然安装时你也可以屏蔽此module

yum install zlib zlib-devel

如果需要ssl module支持还要一个重要的组件openssl library

yum install openssl openssl-devel

获取nginx软件包,这里我们将nginx下载文件存放在home目录中

cd /homewget http://nginx.org/download/nginx-1.8.0.tar.gztar -zxvf nginx-1.8.0.tar.gzcd nginx-1.8.0./configure --prefix=/usr/local/nginx --with-http_ssl_modulemake && make install

注: # 这里我们将nginx安装到/usr/local/nginx中,如果在configure过程中遇到问题是请一一解决,直到看到Configuration summary的标识

安装成功后,我们进入nginx安装目录瞧瞧

cd /usr/local/nginx/lldrwxr-xr-x 2 root root 4096 1013 01:02 conf   # 配置文件存放目录drwxr-xr-x 2 root root 4096 1013 01:02 html   # 网页文件存放目录drwxr-xr-x 2 root root 4096 1013 01:02 logs   # 日志文件存放目录drwxr-xr-x 2 root root 4096 1013 01:02 sbin   # 二进制程序存放目录

为了操作方便,我们可以将nginx二进制程序文件加入环境变量中

vim /etc/profileexport PATH=$PATH:/usr/local/nginx/sbin          #在文件中间位置添加此行内容

注:添加环境变量后需要执行命令source /etc/profile,环境变量才能生效

我们来为nginx添加脚本来

touch /etc/init.d/nginxchmod 755 /etc/init.d/nginxvim /etc/init.d/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 0nginx="/usr/local/nginx/sbin/nginx"prog=$(basename $nginx)sysconfig="/etc/sysconfig/$prog"lockfile="/var/lock/subsys/nginx"pidfile="/var/run/${prog}.pid"NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"[ -f $sysconfig ] && . $sysconfigstart() {    [ -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 -p $pidfile $prog    retval=$?    echo    [ $retval -eq 0 ] && rm -f $lockfile    return $retval}restart() {    configtest_q || return 6    stop    start}reload() {    configtest_q || return 6    echo -n $"Reloading $prog: "    killproc -p $pidfile $prog -HUP    echo}configtest() {    $nginx -t -c $NGINX_CONF_FILE}configtest_q() {    $nginx -t -q -c $NGINX_CONF_FILE}rh_status() {    status $prog}rh_status_q() {    rh_status >/dev/null 2>&1}# Upgrade the binary with no downtime.upgrade() {    local oldbin_pidfile="${pidfile}.oldbin"    configtest_q || return 6    echo -n $"Upgrading $prog: "    killproc -p $pidfile $prog -USR2    retval=$?    sleep 1    if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then        killproc -p $oldbin_pidfile $prog -QUIT        success $"$prog online upgrade"        echo        return 0    else        failure $"$prog online upgrade"        echo        return 1    fi}# Tell nginx to reopen logsreopen_logs() {    configtest_q || return 6    echo -n $"Reopening $prog logs: "    killproc -p $pidfile $prog -USR1    retval=$?    echo    return $retval}case "$1" in    start)        rh_status_q && exit 0        $1        ;;    stop)        rh_status_q || exit 0        $1        ;;    restart|configtest|reopen_logs)        $1        ;;    force-reload|upgrade)        rh_status_q || exit 7        upgrade        ;;    reload)        rh_status_q || exit 7        $1        ;;    status|status_q)        rh_$1        ;;    condrestart|try-restart)        rh_status_q || exit 7        restart            ;;    *)        echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"        exit 2esac

添加完启动脚本后,我们需要修改nginx.conf中的pid的位置,建议将pid统一放到/var/run/目录中

vim /usr/local/nginx/conf/nginx.conf将 #pid logs/nginx.pid 修改为/var/run/nginx.pid

删除/usr/local/nginx/logs中的nginx.pid

脚本启动nginx,并添加到开机启动中

/etc/init.d/nginx startchkconfig nginx on

(^-^)至此nginx安装配置完成

0 0
原创粉丝点击