Nginx 1.12.1 编译安装与配置

背景:本人博客自2014年上线以来,一直使用阿里云ECS最低配的实例,由于最近阿里云ECS进行了升级迁移,原来的低配实例已经不存在了,升级后实例的配置有所提升,当然价格更高了,为了更好的发挥服务器性能,所以就想利用空闲时间对整站进行升级,包含阿里云ecs更换系统盘MySQL 5.7.19 编译安装与配置, Nginx 1.12.1 编译安装与配置, PHP 7.1.9 编译安装与配置等。

服务器环境
CentOS 6.3 64位 全新纯净的系统 / 1核1GB / 经典网络 1MB

进入Nginx下载页面,如果你需要下载nginx-1.12.1.tar.gz版本,请点击此处下载
选择 Stable 稳定版本下载
进入/usr/local/src目录,一般我喜欢把下载的文件放在此目录,根据自己的喜好设定

[root@iZ2864f6btwZ src]# cd /usr/local/src

使用wget下载Nginx文件,如果wget没有安装,yum -y install wget即可安装

[root@iZ2864f6btwZ src]# wget https://nginx.org/download/nginx-1.12.1.tar.gz

安装编译所需的常用组件和依赖包 [ 参考于网络博客 ]

[root@iZ2864f6btwZ src]# yum -y install gcc gcc-c++ pcre-devel openssl-devel openssl libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed

创建nginx用户组和用户,用来运行nginx服务器, -g指定用户组, -r创建系统用户,-M不创建用户主目录
[root@iZ2864f6btwZ src]# groupadd nginx
[root@iZ2864f6btwZ src]# useradd -r -g nginx -s /bin/false -M nginx
解压Nginx,进入 nginx-1.12.1 目录
[root@iZ2864f6btwZ src]# tar zxvf nginx-1.12.1.tar.gz
[root@iZ2864f6btwZ src]# cd nginx-1.12.1
安装pcre用于Rewrite,当前版本8.41,如果你需要下载其它版本,请点击此处
[root@iZ2864f6btwZ nginx-1.12.1]# wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.41/pcre-8.41.tar.gz
[root@iZ2864f6btwZ nginx-1.12.1]# tar zxvf pcre-8.41.tar.gz
安装zlib用于Gzip压缩,当前版本1.2.11,其它版本请点击此处
[root@iZ2864f6btwZ nginx-1.12.1]# wget https://jaist.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
[root@iZ2864f6btwZ nginx-1.12.1]# tar zxvf zlib-1.2.11.tar.gz
创建 Nginx 安装所需目录
[root@iZ2864f6btwZ nginx-1.12.1]# mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
[root@iZ2864f6btwZ nginx-1.12.1]# mkdir -p /var/run/nginx
生成 Makefile,所有扩展参考于网络博客,可根据个人需要安装相应的扩展
[root@iZ2864f6btwZ nginx-1.12.1]#  ./configure \
 --prefix=/usr/local/nginx \
 --sbin-path=/usr/local/nginx/bin/nginx \
 --conf-path=/usr/local/nginx/conf/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  \
 --lock-path=/var/lock/nginx.lock \
 --user=nginx \
 --group=nginx \
 --with-http_ssl_module \
 --with-http_flv_module \
 --with-http_realip_module \
 --with-http_addition_module \
 --with-http_xslt_module \
 --with-http_stub_status_module \
 --with-http_sub_module \
 --with-http_random_index_module \
 --with-http_degradation_module \
 --with-http_secure_link_module \
 --with-http_gzip_static_module \
 --with-http_perl_module \
 --with-pcre=pcre-8.41 \
 --with-zlib=zlib-1.2.11 \
 --with-debug \
 --with-file-aio \
 --with-mail \
 --with-mail_ssl_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/fcgi/ \
 --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
 --http-scgi-temp-path=/var/tmp/nginx/scgi \
 --with-stream \
 --with-ld-opt="-Wl,-E"
configure执行完成

注意:
pcre 和 zlib 的路径一定要指定正确,与 pcre 和 zlib 的解圧路径一致

--with-pcre=pcre-8.41 \
--with-zlib=zlib-1.2.11 \

sbin、conf、pid、local的路径要和 创建 Nginx 安装所需目录 步骤中创建的目录一致!

--http-client-body-temp-path=/var/tmp/nginx/client/ \  
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \  
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \  
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \  
--http-scgi-temp-path=/var/tmp/nginx/scgi \  

每个\--with之间一定不要有空格,只能是换行符,否则复制到命令行执行时会报错,建议先使用sublime/editplus等编辑工具处理好格式后再复制到命令行执行

编译并安装

[root@iZ2864f6btwZ nginx-1.12.1]# make && make install

将nginx添加到环境变量,新建/etc/profile.d/nginx.sh,在nginx.sh文件添加export PATH=$PATH:/usr/local/nginx/bin/
[root@iZ2864f6btwZ nginx-1.12.1]# vim /etc/profile.d/nginx.sh
[root@iZ2864f6btwZ nginx-1.12.1]# source /etc/profile.d/nginx.sh
修改/usr/local/nginx目录权限

[root@iZ2864f6btwZ nginx-1.12.1]# chown nginx.nginx -R /usr/local/nginx/

启动 Nginx,如果你不能在任意目录下使用nginx启动nginx服务器,请检查有没有把/usr/local/nginx/bin/添加到PATH环境变量中

[root@iZ2864f6btwZ nginx-1.12.1]# nginx

查看启动状态
[root@iZ2864f6btwZ nginx-1.12.1]# ps -ef | grep nginx
root     16259     1  0 15:59 ?        00:00:00 nginx: master process nginx
nginx    16260 16259  0 15:59 ?        00:00:00 nginx: worker process
root     16263  7577  0 15:59 pts/1    00:00:00 grep --color=auto nginx
[root@iZ2864f6btwZ nginx-1.12.1]#
通过浏览器中输入服务器IP地址,如果能看到如下界面说明nginx服务器启动成功
启动成功

注意:
在生产环境中,我们一般会使用启动脚本来启动nginx来自启动服务器

新建nginx启动文件

[root@iZ2864f6btwZ conf]# 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:      /usr/local/nginx/conf/nginx.conf
# pidfile:    /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/bin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf "

lockfile=/var/lock/nginx.lock
nginx_run_path="/var/run/nginx/"
nginx_temp_path="/var/tmp/nginx/"

start() {
    if [ ! -d $nginx_run_path ];then
        mkdir -p  $nginx_run_path
        chown -R nginx.nginx $nginx_run_path
    fi

     if [ ! -d $nginx_temp_path ];then
      mkdir -p ${nginx_temp_path}"{client,proxy,fastcgi,uwsgi,scgi}"
      chown -R nginx:nginx $nginx_temp_path
    fi

    [ -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

注意:
上述脚本来自网上,仅供参考,注意NGINX_CONF_FILE、nginx、lockfile的路径保持和实际编译路径一致

添加执行权限,并加入开机自启动
[root@iZ2864f6btwZ conf]# chmod +x /etc/init.d/nginx  
[root@iZ2864f6btwZ conf]# chkconfig --add nginx  
[root@iZ2864f6btwZ conf]# chkconfig nginx on
使用 service 启动nginx
[root@iZ2864f6btwZ conf]# service nginx start
Starting nginx (via systemctl):                            [  OK  ]

注意:
service启动之前,请先停止nginx ( 如之前有使用nginx命令启动过服务器 )
[root@iZ2864f6btwZ conf]# nginx -s stop

查看nginx服务器启动状态
[root@iZ2864f6btwZ conf]# ps -ef | grep nginx
root     16686     1  0 16:51 ?        00:00:00 nginx: master process /usr/local/nginx/bin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx    16687 16686  0 16:51 ?        00:00:00 nginx: worker process
root     16690  7577  0 16:52 pts/1    00:00:00 grep --color=auto nginx
[root@iZ2864f6btwZ conf]#
再次打开浏览器输入服务器IP访问
启动成功

结尾:
至此,nginx服务器编译安装与配置已经全部完成,后续关于nginx与php整合的部分将在单独的文章再介绍,如有毛病,欢迎指正。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,783评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,360评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,942评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,507评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,324评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,299评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,685评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,358评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,652评论 1 293
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,704评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,465评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,318评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,711评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,991评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,265评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,661评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,864评论 2 335

推荐阅读更多精彩内容