【nginx】How to compile and install nginx1.15.3 on CentOS7

一、实验环境

操作系统: CentOS7.2 Minimal

IP: 192.168.1.105

nginx版本: 1.15.3

二、安装编译环境

nginx源码都是用C/C++写的,所以需要在编译用的CentOS 7服务器上安装gcc和gcc-c++等相关软件包。

# yum -y install vim epel-release wget

安装编译工具

# yum -y install  gcc  gcc-c++  autoconf automake make

安装依赖库

#  yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed libtool zlib zlib-devel pcre pcre-devel patch

三、创建用户及用户组

# groupadd -r nginx 

# useradd  -r  -g  nginx  -s/sbin/nologin  -d  /usr/local/nginx  -M  nginx

四、创建相关目录

1.创建编译后的nginx存放目录

# mkdir -pv /usr/local/nginx

2. 创建缓存目录

# mkdir -pv /usr/local/nginx/tmp/{client_body,proxy,fastcgi,uwsgi,scgi}

3. 创建logs、etc 目录

# mkdir  -pv  /usr/local/nginx/{logs,etc}

五、软件下载及解压

# wget http://nginx.org/download/nginx-1.15.3.tar.gz

# tar -zxvf nginx-1.15.3.tar.gz


nginx第三方模块:nginx-sticky-module,基于cookie的会话保持

# wget  https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz

# tar -zxvf /master.tar.gz 

# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/   nginx-sticky-module/

六、编译

# cd  nginx-1.15.3 


############################################

# ./configure \

--prefix=/usr/local/nginx \

--user=nginx \

--group=nginx \

--sbin-path=/usr/local/nginx/sbin/nginx \

--conf-path=/usr/local/nginx/etc/nginx.conf \

--error-log-path=/usr/local/nginx/logs/error.log \

--http-log-path=/usr/local/nginx/logs/access.log \

--pid-path=/var/run/nginx.pid  \

--lock-path=/usr/local/nginx/nginx.lock \

--http-client-body-temp-path=/usr/local/nginx/tmp/client_body \

--http-proxy-temp-path=/usr/local/nginx/tmp/proxy \

--http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi \

--http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi \

--http-scgi-temp-path=/usr/local/nginx/tmp/scgi \

--with-http_ssl_module \

--with-http_v2_module \

--with-http_dav_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 \

--add-module=/root/nginx-sticky-module \

--with-debug \

--with-file-aio \

--with-mail \

--with-mail_ssl_module \

--with-stream \

--with-ld-opt="-Wl,-E"

#########################################

编译选项说明


# make && make install


# ll  -R  /usr/local/nginx 


查看nginx版本和编译参数

# /usr/local/nginx/sbin/nginx -V 2>&1 | sed 's/ --/\n--/g' | egrep --color '.*path.*|$'



七、编写启动脚本或者unit文件,二选一

启动脚本

# vim  /etc/init.d/nginx

####################################################

#! /bin/bash

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig:  - 85 15

# description:  The nginx HTTP and reverse proxy server

#             

#

# processname: nginx

# config:      /etc/nginx/nginx.conf

# 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/etc/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile="/var/lock/subsys/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

    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

#####################################################


unit文件

# vim /etc/systemd/system/nginx.service

##############################################

[Unit]

Description=The nginx HTTP and reverse proxy server

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

PIDFile=/run/nginx.pid

# Nginx will fail to start if /run/nginx.pid already exists but has the wrong

# SELinux context. This might happen when running `nginx -t` from the cmdline.

# https://bugzilla.redhat.com/show_bug.cgi?id=1268621

ExecStartPre=/usr/bin/rm -f /run/nginx.pid

ExecStartPre=/usr/local/nginx/sbin/nginx -t

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/bin/kill -s HUP $MAINPID

KillSignal=SIGQUIT

TimeoutStopSec=5

KillMode=process

PrivateTmp=true

[Install]

WantedBy=multi-user.target

##############################################

注:   /var/run/ --------> /run/

八、启动nginx 服务

关闭selinux

# setenforce 0

# sed  -i  's/^SELINUX=.*/SELINUX=permissive/g'  /etc/selinux/config

如果选用启动脚本

#  chmod +x /etc/init.d/nginx 

# chkconfig --add nginx

# chkconfig nginx on

# service nginx start 

# service nginx status 


如果选用unit文件

# systemctl daemon-reload

# systemctl enable nginx 

# systemctl start nginx

# systemctl status nginx


九、用非root用户启动nginx的问题

用编译好的rpm包安装的nginx,master进程的运行用户是root,无法更改,worker进程的运行用户默认是nginx(可以更改),可以监听1024以下端口。

如果需要以sysV init 或者systemd启动nginx,那么编译nginx时,pid文件路径有要求  --pid-path=/var/run/nginx.pid ,或者更改配置文件中的pid文件生成的默认路径,否则用启动脚本或者unit文件启动服务失败!


 # vim /usr/local/nginx/etc/nginx.conf

# systemctl restart  nginx 

# systemctl status nginx 



如果因为安全加固原因,nginx服务的所有进程需要以非root用户运行,那么必须使用编译的nginx

例如以nginx用户,那么需要注意:

1  创建的nginx用户需要能被切换过去,也就是 shell不能为 /sbin/nologin,否则无法执行 su - nginx  -c  "/usr/local/nginx/sbin.nginx"

# groupadd nginx

# useradd nginx  -d /home/nginx 

2 相应的文件目录属主属组更改为nginx用户 , 需 chown -R  nginx:nginx  /usr/local/nginx

3. nginx配置文件中,监听端口需大于1024,否则会报权限拒绝


4 编译nginx时,pid文件路径能自定义 ,一般无需编译时指定,默认为logs/nginx.pid

# vim /usr/local/nginx/etc/nginx.conf

# su - nginx -c "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/etc/nginx.conf"

# ps aux | grep nginx

# ss -tan 




十、参考

CentOS 7.5 编译安装 Nginx 1.15.3

https://segmentfault.com/a/1190000016498647

nginx最新稳定版的rpm安装

https://www.jianshu.com/p/c2f579c44055?tdsourcetag=s_pcqq_aiomsg

nginx会话保持之nginx-sticky-module模块

https://www.jianshu.com/p/6d973db47ee4

nginx 编译参数详解

http://www.ttlsa.com/nginx/nginx-configure-descriptions

nginx作为web服务以及nginx.conf详解

http://www.cnblogs.com/f-ck-need-u/p/7576137.html#nginx

How to Install Nginx on CentOS 7 / RHEL 7

https://webhostinggeeks.com/howto/how-to-install-nginx-on-centos-7-rhel-7

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

推荐阅读更多精彩内容