Nginx的编译安装


Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性:

作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型.

作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。

作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。

Nginx 安装非常的简单,配置文件 非常简洁(还能够支持perl语法),Bugs非常少的服务器: Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在 不间断服务的情况下进行软件版本的升级。
 
编译环境:
[root@www ~]# yum groupinstall Development Tools
[root@www ~]# yum install pcre-devel openssl-devel

获取nginx程序源码包:

[root@www ~]# ll
total 888
drwxr-xr-x  9 1001 1001  4096 Dec 27 02:19 nginx-1.6.2
-rw-r--r--  1 root root 804164 Sep 16 22:45 nginx-1.6.2.tar.gz
-rw-r--r--  1 root root  29542 Apr 23  2014 nginx.vim

解压源码包:

[root@www ~]# tar xf nginx-1.6.2.tar.gz
[root@www ~]# cd nginx-1.6.2

创建nginx管理用户:

[root@www nginx-1.6.2]# useradd -M -s /sbin/nologin nginx
[root@www nginx-1.6.2]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)

源码编译安装nginx:

我们编译安装时需要定义各种配置需要的目录进行,所以需要新建一些目录:

日志存储目录:

[root@www ~]# mkdir /var/log/nginx

各种缓存目录,客户端,代理,fastcgi目录:

[root@www ~]# mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi}
mkdir: created directory `/var/tmp/nginx'
mkdir: created directory `/var/tmp/nginx/client'
mkdir: created directory `/var/tmp/nginx/proxy'
mkdir: created directory `/var/tmp/nginx/fastcgi'
[root@www ~]#

可以使用如下命令查看帮助文件:

[root@www nginx-1.6.2]# ./configure --help |less

开始编译nginx:

[root@www nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi

安装nginx:

[root@www nginx-1.6.2]# make && make install

 检查安装:

查看安装目录

[root@www ~]# ls /usr/local/nginx/
html  sbin
[root@www ~]# ls /usr/local/nginx/sbin/
nginx

查看配置文件目录:

[root@www ~]# ls /etc/nginx/
fastcgi.conf            fastcgi_params.default  mime.types              nginx.conf.default      uwsgi_params           
fastcgi.conf.default    koi-utf                mime.types.default      scgi_params            uwsgi_params.default   
fastcgi_params          koi-win                nginx.conf              scgi_params.default    win-utf

环境变量设置:

[root@www ~]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@www ~]# source /etc/profile.d/nginx.sh

查看nginx的版本:

[root@www ~]# nginx -v
nginx version: nginx/1.6.2

 测试nginx的配置文件:

[root@www ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

提供nginx服务脚本:

[root@www ~]# vim /etc/rc.d/init.d/nginx
#!/bin/bash
#
# 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="/etc/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
  # make required directories
  user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
  options=`$nginx -V 2>&1 | grep 'configure arguments:'`
  for opt in $options; do
      if [ `echo $opt | grep '.*-temp-path'` ]; then
          value=`echo $opt | cut -d "=" -f 2`
          if [ ! -d "$value" ]; then
              # echo "creating" $value
              mkdir -p $value && chown -R $user $value
          fi
      fi
  done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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
    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@www ~]# chkconfig --list nginx
service nginx supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add nginx')
[root@www ~]# chkconfig --add nginx
[root@www ~]# chkconfig nginx on
 

设置服务脚本开启执行权限:

[root@www ~]# chmod +x /etc/rc.d/init.d/nginx

启动nginx服务器:
[root@www ~]# vim /etc/rc.d/init.d/nginx
[root@www ~]# service nginx start           
Starting nginx:                                            [  OK  ]
[root@www ~]# ss -tunl |grep 80
tcp    LISTEN    0      128                  :::38804                :::*   
tcp    LISTEN    0      128                    *:80                    *:*

简单的nginx基础介绍完毕!

--------------------------------------分割线 --------------------------------------

CentOS 6.2实战部署Nginx+MySQL+PHP

使用Nginx搭建WEB服务器

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程

CentOS 6.3下Nginx性能调优

CentOS 6.3下配置Nginx加载ngx_pagespeed模块

CentOS 6.4安装配置Nginx+Pcre+php-fpm

Nginx安装配置使用详细笔记

Nginx日志过滤 使用ngx_log_if不记录特定日志

--------------------------------------分割线 --------------------------------------

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:

相关内容