标签:nginx
| 操作系统 | CentOS release 6.5 (Final) | 
| 服务器IP | 192.168.1.200 | 
| nginx版本 | nginx-1.9.1 | 
| pcre版本 | 8.30 | 
| openssl | 1.0.2a | 
| zlib | 1.2.7 | 
| 源码包解压目录 | /usr/local/src | 
| 源码包安装目录 | /usr/local/softname | 
| nginx安装目录 | /usr/local/nginx-1.9.1/ | 
安装前准备
[root@lb ~]# iptables -F    ##清空防火墙
[root@lb ~]# iptables -X
[root@lb ~]# iptables -Z
[root@lb ~]# /etc/init.d/iptables save
iptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定]
[root@lb ~]# setenforce 0 ##临时关闭selinux
[root@lb ~]# sed -i ‘/SELINUX/s/enforcing/disabled/‘ /etc/selinux/config ##永久关闭selinux
[root@lb ~]# yum -y install vim ntpdate wget gcc gcc-c++ ##安装必要的软件
[root@lb ~]# ntpdate 0.pool.ntp.org ##同步时间服务器
[root@lb ~]# hwclock
安装pcre
[root@lb ~]# tar xvfz pcre-8.30.tar.gz -C /usr/local/src/
[root@lb ~]# cd /usr/local/src/pcre-8.30
[root@lb pcre-8.30]# ./configure --prefix=/usr/local/pcre-8.30
[root@lb pcre-8.30]# make && make install
安装openssl
[root@lb ~]# tar xvfz openssl-1.0.2a.tar.gz -C /usr/local/src/
[root@lb ~]# cd /usr/local/src/openssl-1.0.2a
[root@lb openssl-1.0.2a]# ./config --prefix=/usr/local/openssl-1.0.2a
[root@lb openssl-1.0.2a]# make && make install
安装zlib
[root@lb ~]# tar xvfz zlib-1.2.7.tar.gz -C /usr/local/src/
[root@lb ~]# cd /usr/local/src/zlib-1.2.7/
[root@lb zlib-1.2.7]# ./configure --prefix=/usr/local/zlib-1.2.7
[root@lb zlib-1.2.7]# make && make install
安装nginx
[root@lb ~]# groupadd www
[root@lb ~]# useradd -g www -s /sbin/nologin www
[root@lb ~]# tar xvfz nginx-1.9.1.tar.gz -C /usr/local/src/
[root@lb ~]# cd /usr/local/src/nginx-1.9.1/
[root@lb nginx-1.9.1]# ./configure \ ##编译安装
--prefix=/usr/local/nginx-1.9.1 \ ##指定nginx安装目录
--sbin-path=/usr/local/nginx-1.9.1/sbin/nginx \ ##可执行文件目录
--conf-path=/etc/nginx/nginx.conf \ ##主配置文件
--error-log-path=/var/log/nginx/error.log \ ##错误日志文件
--http-log-path=/var/log/nginx/access.log \ ##访问日志文件
--pid-path=/var/run/nginx/nginx.pid \ ##进程PID文件
--lock-path=/var/lock/nginx.lock \ ##锁文件
--user=www \ ##运行nginx非特权用户
--group=www \ ##运行nginx非特权用户组
--with-http_ssl_module \ ##支持https请求
--with-http_realip_module \ ##允许从请求标头更改客户端的IP地址值
--with-http_gzip_static_module \ ##在线实时压缩输出数据流
--with-http_stub_status_module \ ##获取Nginx自上次启动以来的工作状态
--with-pcre=/usr/local/src/pcre-8.30 \ ##pcre库文件目录,指源码解压目录
--with-zlib=/usr/local/src/zlib-1.2.7 \ ##zlib库文件目录,指源码解压目录
--with-openssl=/usr/local/src/openssl-1.0.2a ##openssl库文件目录,指源码解压目录
# make && make install
编写nginx控制脚本
[root@lb nginx-1.9.1]# vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# 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
# pidfile:     /var/run/nginx/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-1.9.1/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile="/var/lock/nginx.lock"
 
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
    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@lb nginx-1.9.1]# chmod +x /etc/init.d/nginx
[root@lb nginx-1.9.1]# chkconfig nginx on
[root@lb nginx-1.9.1]# chkconfig --list | grep nginx
nginx           0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
[root@lb nginx-1.9.1]# /etc/init.d/nginx start
[root@lb nginx-1.9.1]# netstat -tnlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29070/nginx
安装nginx.vim 使nginx配置文件支持语法检查
[root@lb nginx-1.9.1]# mkdir -p ~/.vim/syntax
[root@lb nginx-1.9.1]# mv /root/nginx.vim ~/.vim/syntax
[root@lb nginx-1.9.1]# cat >> ~/.vim/filetype.vim <<EOF
au BufRead,BufNewFile /etc/nginx/* set ft=nginx
EOF
本文出自 “所谓的命运 不同的选择” 博客,请务必保留此出处http://dongshi.blog.51cto.com/5145353/1718485
标签:nginx
原文地址:http://dongshi.blog.51cto.com/5145353/1718485