微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CentOS7.3编译安装Nginx1.10.1

编译环境安装

yum -y gcc
    yum -y gcc++
    yum -y gcc-c++
    yum -y install wget

下载安装文件并解压

> 创建文件夹并进入

    mkdir soft && cd soft
    
    > 下载依赖文件pcre,openssl,zlib

    wget  -c  http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
    wget  -c https://www.openssl.org/source/openssl-1.0.2l.tar.gz
    wget -c http://zlib.net/zlib-1.2.11.tar.gz

    > 下载Nginx

    wget -c http://Nginx.org/download/Nginx-1.10.1.tar.gz

    > 解压文件

    tar -zxvf pcre-8.39.tar.gz
    tar -zxvf openssl-1.0.2l.tar.gz
    tar -zxvf zlib-1.2.11.tar.gz
    tar -zxvf Nginx-1.10.1.tar.gz

创建用户和目录

> 新建系统账号Nginx

    useradd -r  Nginx -s /sbin/nologin -M 

    > 新建Nginx需要的目录

    mkdir -p /var/tmp/Nginx/{client_body,proxy,fastcgi,uwsgi,scgi}

    > 递归改变目录所有者

    chown -R Nginx /var/tmp/Nginx

编译Nginx-1.10.1

cd ~/soft/Nginx-1.10.1

    ./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.39 \
    --with-zlib=../zlib-1.2.11 \
    --with-openssl=../openssl-1.0.2l \
    --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-stream

    make && make install

编辑服务脚本文件

> 创建文件并打开服务脚本文件

    vim /etc/init.d/Nginx

    > 编写脚本文件

    #! /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 0

    Nginx="/usr/sbin/Nginx"
    prog=$(basename $Nginx)

    Nginx_CONF_FILE="/etc/Nginx/Nginx.conf"

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

    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
        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启动脚本

> 改变文件权限

    chmod +x /etc/init.d/Nginx

    > 添加到系统服务

    chkconfig --add Nginx

    > 设置系统为开机启动

    chkconfig Nginx on

启动Nginx服务器

systemctl start Nginx

启动服务状态

[root@localhost soft]# systemctl status Nginx.service
    ● Nginx.service - SYSV: Nginx is an HTTP(S) server,HTTP(S) reverse proxy and IMAP/POP3 proxy server
       Loaded: loaded (/etc/rc.d/init.d/Nginx; bad; vendor preset: disabled)
       Active: active (running) since Tue 2017-06-13 11:47:44 EDT; 22s ago
         Docs: man:systemd-sysv-generator(8)
      Process: 40801 ExecStart=/etc/rc.d/init.d/Nginx start (code=exited,status=0/SUCCESS)
     Main PID: 40808 (Nginx)
       Memory: 1.0M
       CGroup: /system.slice/Nginx.service
               ├─40808 Nginx: master process /usr/sbin/Nginx -c /etc/Nginx/Nginx.conf
               └─40809 Nginx: worker process

    Jun 13 11:47:43 localhost.localdomain systemd[1]: Starting SYSV: Nginx is an HTTP(S) server,HTTP(S) reverse proxy and IMAP/POP3 proxy server...
    Jun 13 11:47:44 localhost.localdomain Nginx[40801]: Starting Nginx: [  OK  ]
    Jun 13 11:47:44 localhost.localdomain systemd[1]: Started SYSV: Nginx is an HTTP(S) server,HTTP(S) reverse proxy and IMAP/POP3 proxy server.
    [root@localhost soft]#

修改Nginx配置文件

# 运行用户
    #user  nobody;
    # 启动进程,通常设置成和cpu的数据相等
    worker_processes  1;

    # 全局错误日志及PID文件
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    #pid        logs/Nginx.pid;

    # 工作模式及连接数上限
    events {
        
        use    epoll;

        worker_connections  1024;
    }


    http {
        include       mime.types;
        default_type  application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;
        tcp_nodelay        on;

        gzip  on;
        gzip_disable "MSIE [1-6]";

        client_header_buffer_size    128k;
        large_client_header_buffers  4 128k;

        server {
            listen       80;
            server_name  www.Nginx.dev;


            #charset koi8-r;

            access_log  logs/Nginx.dev.access.log  main;

            location / {
                root   /data/www/html;
                index  index.PHP index.html index.htm;
            }

            #error_page  404              /404.html;

            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   /data/www/html;
            }

        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
        
            expires 30d;
        }

            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.PHP$ {
            #    proxy_pass   http://127.0.0.1;
            #}

            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            location ~ \.PHP$ {
            #    root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.PHP;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }

            # deny access to .htaccess files,if Apache's document root
            # concurs with Nginx's one
            #
            location ~ /\.ht {
                deny  all;
            }
        }


        # another virtual host using mix of IP-,name-,and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}


        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;

        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;

        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;

        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}

    }

CentOS7添加开放80TCP端口

> 加入开放端口到配置文件

    firewall-cmd --zone=public --add-port=80/tcp --permanent

    --zone=public 添加时区

    --add-port=80/tcp 添加端口

    --permanent 永久生效

    > 加载防火墙新配置文件( 以 root 身份输入以下命令,重新加载防火墙,并不中断用户连接,即不丢失状态信息. )

    firewall-cmd --reload

希望本文对你的工作和学习有所帮助

如果觉得还不错怎么感谢我呢? 妈呀! 点赞啊!

Good Luck! from warnerwu at 2017.06.24 PM

原文地址:https://www.jb51.cc/centos/377070.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。