设为首页 收藏本站
查看: 689|回复: 0

[经验分享] Nginx简介与源码编译安装

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-29 09:02:12 | 显示全部楼层 |阅读模式
1.Nginx简介
         Nginx官方站点:http://nginx.orgNginx官方文档:http://nginx.org/en/docs
Nginx是一款免费,开源,轻量级高性能的网站服务器(http server)和反向代理(reverse proxy)服务器程序,能够反向代理http协议和mail协议。
Nginx原作者于2002年开发nginx,正式第一版于2004年发布,至今nginx已经在web server占据了很大的市场份额如图(可以在http://news.netcraft.com/archives/category/web-server-survey/ 查看到):
wKioL1Sei5SAISGzAAK0hbjAkto371.jpg
从这张图可以看出nginx已经在web server中排名第三了,并且自从nginx出来以后市场份额一直有增无减,这是最新发布的统计数据2014年12月18日发布的。
Nginx与Apache的异同:它们都为http服务软件,都采用模块化设计结构并且都支持动态语言(php、python、perl)等,同时都支持正反向代理、虚拟主机、URL重写、压缩资源、SSL加密等功能,最大差别是apache的处理速度慢并且占用很多系统内存资源,
2.Nginx的安装与配置(编译安装)
      首先下载Nginx源码包到本地并且解压
wget http://nginx.org/download/nginx-1.6.2.tar.gz     && tar xf nginx-1.6.2.tar.gz
使用./configure --help查看帮助
./configure --help 或者安装一个第三方软件包(bash-completion)手动编译时可以快速查看支持的参数
9047a41306331987.png
可以从图中看出来nginx1.6.2可使用的参数有100种,介绍下将nginx作为http服务器的常用参数吧
--conf-path           设置nginx的配置文件所在的路径需要指明配置文件的文件名(默认为安装目录下的conf/nginx.conf)
--error-log-path    设置nginx的错误日志文件所在的路径需要指明日志文件的文件名(默认为安装目录下的logs/error.log)
--group                 设置nginx运行时以什么组来运行worker进程(默认为nobody组)
--user                   设置nginx运行时以什么用户来运行worker进程(默认为nobody用户)
--prefix                 指明nginx的安装所在路径
--sbin-path           指明nginx的sbin目录所在的目录(默认为安装目录下sbin/nginx)
--pid-path             指明nginx的pid文件所在的路径需要指明pid文件名(默认为安装目录下的logs/nginx.pid)
--lock-path            指明nginx的lock文件所在的路径
--http-log-path      指明nginx的访问文件所在路径(默认为安装目录下的log/access.log)
--with-http_gzip_static_module      启用gzip压缩模块
--with-http_ssl_mdule                    启用ssl模块

./configure --prefix=/usr/local/nginx
--conf-path=/usr/local/nginx/conf/nginx.conf
--http-log-path=/usr/local/nginx/logs/access.log
--user=nginx
--group=nginx
--error-log-path=/usr/local/nginx/logs/error.log
--pid-path=/usr/local/nginx/var/nginx.pid
--lock-path=/usr/local/nginx/var/nginx.lock
--with-http_gzip_static_module
--with-http_ssl_module

确定没有任何错误后使用make && make install 安装
3.添加服务启动脚本
将如下脚本添加至/etc/rc.d/init.d/nginx目录下,然后使用chmod u+x /etc/rc.d/init.d/nginx 将nginx添加至系统服务chkconfig --add nginx如果需要开机启动则可以输入chkconfig nginx on即可
#!/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"


lockfile=/usr/local/nginx/var/nginx.lock

make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*//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




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-39502-1-1.html 上篇帖子: 编译安装配置nginx1.6以及其一些基本配置等 下篇帖子: Nginx配置文件语法高亮显示
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表