centos7.2编译安装nginx-1.10.2

来源:互联网 发布:js根据斜线截取字符串 编辑:程序博客网 时间:2024/05/29 10:36

centos7.2编译安装nginx-1.10.2


  • centos72编译安装nginx-1102
    • 1- 编译环境安装
    • 2- 下载安装文件并解压
    • 3- 用户和目录准备
    • 4- 编译nginx-1102
    • 5- 配置nginx启动脚本
    • 6- 启动nginx


1- 编译环境安装

安装gcc编译环境,安装下载工具wget。

yum -y groupinstall "Development Tools"yum -y install wget

2- 下载安装文件并解压

从官方网站下载nginx,以及依赖的pcre,openssl和zlib,需要注意版本,不要使用新版的pcre2。

#切换目录cd /usr/local/src#下载依赖文件pcre,openssl,zlibwget  -c   http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.bz2wget  -c https://www.openssl.org/source/openssl-1.0.2j.tar.gzwget -c http://zlib.net/zlib-1.2.8.tar.gz#下载nginxwget -c http://nginx.org/download/nginx-1.10.2.tar.gz

解压文件。

tar -zxvf nginx-1.10.2.tar.gztar -jxvf pcre-8.38.tar.bz2tar -zxvf zlib-1.2.8.tar.gztar -zxvf openssl-1.0.2j.tar.gz

3- 用户和目录准备

#新建系统账号nginxuseradd -r  nginx -s /sbin/nologin -M #新建nginx需要的目录cd /var/tmp/ mkdir -p /var/tmp/nginx/{client_body,proxy,fastcgi,uwsgi,scgi}chown -R nginx /var/tmp/nginx

4- 编译nginx-1.10.2

具体编译参数,需要依据实际情况修改。

cd /usr/local/src/nginx-1.10.2./configure \--prefix=/usr/local/nginx \        --sbin-path=/usr/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.pid  \        --lock-path=/var/lock/nginx.lock \--user=nginx \                         --group=nginx \                        --with-http_ssl_module \                   --with-http_realip_module \--with-http_stub_status_module \--with-http_gzip_static_module \   --with-pcre=../pcre-8.38 \             --with-zlib=../zlib-1.2.8 \   --with-openssl=../openssl-1.0.2j   \      --with-debug \       --http-client-body-temp-path=/var/tmp/nginx/client_body \--http-proxy-temp-path=/var/tmp/nginx/proxy \--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \--http-scgi-temp-path=/var/tmp/nginx/scgi \--with-streammakemake install

5- 配置nginx启动脚本

vi /etc/init.d/nginx chmod +x /etc/init.d/nginxchkconfig --add nginxchkconfig nginx on

nginx脚本内容如下,可根据实际情况修改nginx和NGINX_CONF_FILE参数。

#! /bin/bash## 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# 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 0nginx="/usr/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/nginx.lockstart() {    [ -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

6- 启动nginx

#启动nginx服务systemctl start nginx.service#查看端口监听ss -tlnp|grep :80

参考文章:
[1].https://typecodes.com/web/centos7compilenginx.html
[2].https://typecodes.com/web/nginxserviceoptshell.html
[3].http://nginx.org/en/docs/configure.html

0 0