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

[经验分享] 编译安装配置nginx1.6以及其一些基本配置等

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-12-29 09:01:34 | 显示全部楼层 |阅读模式
实验环境:
    centos 6.6 【该节点IP为172.16.3.101】
    配置好的yum源【可以把yum源指向搜狐或者阿里的镜像站点,也可以是自己的光盘】

编译安装配置nginx1.6
    # 安装之前最好先把如下包组安装上
        [iyunv@localhost httpd-2.2.29]# yum grouplist | grep -i 'develop'
           Additional Development
           Development tools
           Server Platform Development
           Desktop Platform Development
    # 安装方法如下:
        yum groupinstall 'Additional Development' 'Development tools' 'Server Platform Development' 'Desktop Platform Development'   
    # 获取nginx1.6二进制源码格式安装包
        可以去如下链接下载源码安装包,我这里用的是1.6的,http://nginx.org/en/download.html
    # 解压缩
        tar xf nginx-1.6.2.tar.gz
    cd nginx-1.6.2
    # 由于nginx需要nginx系统用户启动,所以此处需要创建系统用户nginx
        useradd -r nginx
    # 安装三部曲
        # ./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
            --prefix                        # nginx安装目录
            --conf-path                     # nginx配置文件路径
            --user                             # 指定启动nginx的系统用户
            --group                        # 指定启动nginx的系统组
            --error-log-path                # 错误日志文件路径
            --http-log-path                # 访问日志路径
            --pid-path                         # pid文件路径
            --lock-path                    # 锁文件路径
            --with-http_ssl_module          # 启用ssl模块
            --with-http_stub_status_module  # 启用stub_status_module模块
            --with-http_gzip_static_module  # 启用gzip_static_module模块
            --with-http_flv_module             # 启用流媒体模块
            --with-http_mp4_module             # 启用mp4流媒体模块
            --http-client-body-temp-path   # 客户端请求报文包体临时文件路径  
            --http-proxy-temp-path          # 报文反向代理临时文件路径
            --http-fastcgi-temp-path        # 报文FastCGI临时文件路径
        make
        make install
    # 添加PATH环境变量
        vim /etc/profile.d/nginx.sh
        # 向里面加入如下语句
        export PATH=/usr/local/nginx/sbin:$PATH
        # 然后source一下
        . /etc/profile.d/nginx.sh
    # 对nginx配置文件进行语法检查
        [iyunv@localhost nginx-1.6.2]# nginx -t
        nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
        nginx: [emerg] mkdir() "/var/tmp/nginx/client" failed (2: No such file or directory)
        nginx: configuration file /etc/nginx/nginx.conf test failed
    # 从上面可以看出其配置文件的语法是OK但是,有些目录在编译安装配置nginx的时候没有自动创建,此处需要手动创建其目录
    # 创建目录
        mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi}  
    # 再进行检查
        [iyunv@localhost nginx-1.6.2]# nginx -t
        nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
        nginx: configuration file /etc/nginx/nginx.conf test is successful   
    # 可以看出其语法OK
    # 启动nginx
    nginx
    # 查看80端口是否开启
    ss -tnl
    # 也可以看一下nginx进程详细信息
    ps aux | grep nginx
为nginx提供SysV init脚本:      
    # 新建文件/etc/rc.d/init.d/nginx.conf
    vim /etc/rc.d/init.d/nginx.conf
    # 向里面添加如下内容
#!/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/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
    # 此处注意,定位至如下行
        NGINX_CONF_FILE="/usr/local/nginx/nginx.conf"
    # 把其修改为如下:主要是要修改配置文件路径
        NGINX_CONF_FILE="/etc/nginx/nginx.conf"
    # 然后给其执行权限
        chmod u+x /etc/nginx/nginx.conf
    # 添加至chkconfig列表中
        root@localhost nginx]# chkconfig --list nginx
        service nginx supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add nginx')
        [iyunv@localhost nginx]# chkconfig --add nginx
    # 启动nginx服务
        service nginx start
    # 可以看到80端口已经打开
        ss -tnl
    # 测试,此时在远端浏览器中输入:http://172.16.3.101
    # 如若看到nginx的欢迎信息则表明安装成功(^_^),反之,没有成功。

为nginx的配置文件设置语法高亮和代码自动缩进
    语法高亮的确实很好啊
    vim /etc/nginx/nginx.conf
    # 此时可以看到文件内容没有语法高亮
    # 获取nginx.vim文件
        http://www.vim.org/scripts/script.php?script_id=1886
        由于官方网站不容易登陆,我已经把它放在附件里面了,有需要的同学可以下载
    # 创建目录~/.vim/syntax/,然后把nginx.vim放于其中
        mkdir ~/.vim/syntax
        mv nginx.vim ~/.vim/systax
    vim ~/.vim/filetype.vim
    # 向其中添加如下内容
        au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
        # 其中/etc/nginx是nginx配置文件目录
    # 保存退出
    # 此时再去打开/etc/nginx/nginx.conf文件,可以看到其内容已经语法高亮了
    # 还可以对/etc/nginx/nginx.conf文件进行格式自动缩进
        vim /etc/nginx/nginx.conf
        # 然后输入gg,然后输入=,然后输入G
        # 经过上面的操作可以看到,代码的缩进变得很规范
     
elinks简单用法:
    # 安装其安装包
        yum -y install elinks
    # 该软件是一个文本模式的浏览器
    # 可以通过如下方式访问
         elinks  http://127.0.0.1/
         # 该方式访问之后不会直接返回
    # 以下方式,可以直接把访问结果输出到屏幕
        [iyunv@localhost ~]# elinks -dump http://localhost/
                                       Welcome to nginx!

           If you see this page, the nginx web server is successfully installed and
           working. Further configuration is required.

           For online documentation and support please refer to [1]nginx.org.
           Commercial support is available at [2]nginx.com.

           Thank you for using nginx.

        References

           Visible links
           1. http://nginx.org/
           2. http://nginx.com/     


运维网声明 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-39501-1-1.html 上篇帖子: nginx学习笔记之基于端口的虚拟主机基于主机名的虚拟主机root、alias、index配置 下篇帖子: Nginx简介与源码编译安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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