设为首页收藏本站

全球主机交流论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
热搜: discuz
查看: 1109|回复: 0
打印 上一主题 下一主题

Centos6.2_(64位)服务器环境配置:源码编译Nginx

[复制链接]
  • TA的每日心情
    擦汗
    2020-8-11 18:34
  • 签到天数: 243 天

    [LV.8]以坛为家I

    跳转到指定楼层
    楼主
    发表于 2013-10-18 16:17:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

    上一回讲到配置环境的一些前期工作,以及编译安装Mysql5.3,如果不想错过任何细节,请阅读《(Centos6.2_64位)服务器环境配置:源码编译Mysql》一文。

    今天趁下班前的一点空余时间,接着把Nginx部分也写写。

    还 记得上一篇的一些约定吧:存放下载安装包的位置是/setup,目标软件都指定安装目录:/apps。由于Nginx可以使用正则表达式来匹配访问路径, 要正常使用此功能就保证安装有Pcre库,如果你已经接着上一篇操作过来,就可以不用考虑这一点,因为此库已经在安装列表里加入。现在可以重温下这段命 令,它一次过就把所需要的库都安装了。

    #yum install gcc gcc-c++ gcc-g77 pcre-devel openssl-devel bison autoconf automake make cmake libcurl-devel gd-devel zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel*

    以 上命令可以大胆执行,如果已经安装的库会就会自动跳过,也不会对系统产生副 作用。如果的Nginx作用可大了,不仅可以做功能强大的反向代理服务器,还加入了对视频拖动的支持,如:FLV、MP4等主流网络视频格式,如果利用 Nginx用简单的视频服务器,就要根据情况加入相关的编译参数,下方会简单说到,或者我会单独针对Nginx配置成视频服务器再写一篇相关文章。

    其它不多说了,下面开始转入正题。所有操作为Shell窗口进行,以#号开始,//为中文注释,执行的时候不要。

    #cd /setup

    #wgethttp://nginx.org/download/nginx-1.2.8.tar.gz //Nginx官网目前最新的稳定版本是1.2.8

    #groupadd www //为了确保系统安全,新建Nginx的专门执行用户,现在咱们就用www分别建立用户组和对应同名用户

    # useradd -g www -s /sbin/nologin -M www //创建名为www的用户并加入到www的用户组,并且禁止该用户登录shell

    #tar zxf nginx-1.2.8.tar.gz //解压文件,之后会自动生成nginx-1.2.8目录

    #cd nginx-1.2.8

    # ./configure --prefix=/apps/nginx --user=www --group=www --with-http_stub_status_module --with-pcre --with-http_ssl_module --with-http_realip_module --with-sha1-asm //指定了/apps/nginx为安装目录、运行Nginx的用户及用户组,还有几个常用的组件,这个都得根据自己的实际情况而定,我把Nginx状态 监控模块、正则模板、SSL模块等加入,方便后面使用。在这里再提下状态监控模块,可以让管理者轻松获取当前Nginx的运行情况,所以很有必要。当执行 完毕之后,会有以下摘要信息:

    Configuration summary

    + using system PCRE library

    + using system OpenSSL library

    + md5: using OpenSSL library

    + sha1: using OpenSSL library

    + using system zlib library


    nginx path prefix: "/apps/nginx"

    nginx binary file: "/apps/nginx/sbin/nginx"

    nginx configuration prefix: "/apps/nginx/conf"

    nginx configuration file: "/apps/nginx/conf/nginx.conf"

    nginx pid file: "/apps/nginx/logs/nginx.pid"

    nginx error log file: "/apps/nginx/logs/error.log"

    nginx http access log file: "/apps/nginx/logs/access.log"

    nginx http client request body temporary files: "client_body_temp"

    nginx http proxy temporary files: "proxy_temp"

    nginx http fastcgi temporary files: "fastcgi_temp"

    nginx http uwsgi temporary files: "uwsgi_temp"

    nginx http scgi temporary files: "scgi_temp"

    以上信息表明配置成功

    # make //根据配置信息执行编译操作,这一步才会生成相关的二进制文件,但是生成的文件还是原来的目录里。

    # make install //把所编译生成的文件,根据配置复制到对应的目录,如果没有的目录就会自动创建,完成这一步就算是编译OK了,接着可以测试下Nginx能否成功运行。

    #/apps/nginx/sbin/nginx //执行此命令就可以启动Nginx了,只要用浏览器打开http://ip,就可以看到Welcome Nginx的介面。至此Nginx就算安装完成了。但还有另外新问题,中如何让Nginx开机启动呢?这个问题不难解决,写个脚本就行。

    #vi /edt/init.d/nginx //创建名为nginx的脚本文件,输入以下内容,包括前边的#

    #!/bin/sh
    # www.5ishare.com
    # 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: /apps/nginx/conf/nginx.conf
    # pidfile: /apps/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="/apps/nginx/sbin/nginx"
    NGINX_CONF_FILE="/apps/nginx/conf/nginx.conf"
    prog=$(basename $nginx)
    lockfile=/tmp/nginx.pid
    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
    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

    chmod +x /etc/init.d/nginx //设置脚本为可执行

    chkconfig nginx on //设置该名称的脚本为开机启动

    这个脚本用法如下:

    #services nginx start //启动服务

    #services nginx stop //停止服务

    #services nginx restart //重启服务

    #services nginx reload //重新加载配置

    start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest 是全部可用命令。










    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|小黑屋|Archiver|手机版|中国U网    

    GMT+8, 2024-5-17 22:03 , Processed in 0.080252 second(s), 25 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

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