1. 程式人生 > >CentOS 7.4 編譯安裝 Nginx1.15.2

CentOS 7.4 編譯安裝 Nginx1.15.2

本文主要記錄如何在CentOS7.4中編譯安裝Nginx官方最新的1.15.2版本。由於像NginxMysqlPHP7的的原始碼都是用C/C++寫的,所以自己的CentOS 7.4伺服器上必須要安裝gccg++軟體。

安裝環境

  • 系統:Centos 7.4
  • 軟體:Nginx 1.15.2
  • 依賴軟體:Pcre、Zlib、Openssl

依賴庫配置,編譯和安裝Nginx

先建立一個名為nginx沒有登入許可權的使用者和一個名為nginx的使用者組,然後安裝nginx所需的依賴庫和依賴包,最後通過.configure進行安裝的詳細配置。

> 安裝gcc gcc-c++ C
/C++語言 編譯環境 [root@lightserver.cn ~]# yum -y install gcc gcc-c++ autoconf automake make > yum安裝nginx必須的依賴庫 [root@lightserver.cn ~]# yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed libtool zlib zlib-devel pcre pcre-devel patch > 新建資料夾 [root@lightserver
.cn ~]# make dir soft && cd soft > 官網下載Nginx1.15.2的tar包,然後解壓到伺服器上 [root@lightserver.cn ~]# wget http://nginx.org/download/nginx-1.15.2.tar.gz > 下載pcre的tar包並解壓,以便支援NginxRewrite功能 [root@lightserver.cn ~]# wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz > 下載zlib的tar包並解壓,以便支援NginxGzip
壓縮功能 [root@lightserver.cn ~]# wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz > nginx第三方模組—nginx-sticky-module的使用(基於cookie的會話保持 [root@lightserver.cn ~]# wget https://www.openssl.org/source/openssl-1.1.1-pre8.tar.gz [root@lightserver.cn ~]# https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz [root@lightserver.cn ~]# tar -zxvf nginx-1.15.2.tar.gz [root@lightserver.cn ~]# tar -zxvf pcre-8.42.tar.gz [root@lightserver.cn ~]# tar -zxvf zlib-1.2.11.tar.gz [root@lightserver.cn ~]# tar -zxvf openssl-1.1.1-pre8.tar.gz [root@lightserver.cn ~]# mkdir -p /var/tmp/nginx/{client_body,proxy,fastcgi,uwsgi,scgi} [root@lightserver.cn ~]# chown -R nginx:nginx /var/tmp/nginx

建立使用者及使用者組

> 新建nginx使用者和nginx組
[root@lightserver.cn ~]#  groupadd -r nginx
[root@lightserver.cn ~]#  useradd -r -g nginx -s /sbin/nologin -d /usr/local/nginx -M nginx

nginx 編譯引數

> 進入nginx目錄
[[email protected] ~]#  cd ~/soft/nginx-1.15.2

> 編譯安裝
> 編譯時,複製一下程式碼,末尾不能有空格,不能有註釋
[[email protected] ~]#  ./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_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 \
--with-pcre=../pcre-8.42 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1-pre8 \
--with-debug \
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--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 \
--with-ld-opt="-Wl,-E"

編譯選項說明

  • --prefix=PATH: 指定nginx的安裝目錄。預設 /usr/local/nginx
  • --sbin-path=PATH:設定nginx可執行檔案的名稱。預設情況下,檔案指向 安裝目錄/sbin/nginx
  • --conf-path=PATH:設定nginx.conf配置檔案的名稱。nginx允許使用不同的配置檔案啟動,通過在命令列引數中指定它 。預設情況下,檔案指向 安裝目錄/conf/nginx.conf
  • --pid-path=PATH:設定儲存主程序ID檔案nginx.pid的名稱。預設情況下,檔案指向 安裝目錄/logs/nginx.pid
  • --error-log-path=PATH:設定錯誤,警告和診斷檔案的名稱。預設情況下,檔案指向 安裝目錄/logs/error.log
  • --http-log-path=PATH:設定HTTP伺服器的請求日誌檔案的名稱。預設情況下,檔案指向 安裝目錄/logs/access.log
  • --lock-path=PATH:安裝檔案鎖定,防止安裝檔案被別人利用,或自己誤操作。
  • --user=nginx:指定程式執行時的非特權使用者。安裝完成後,可以隨時在nginx.conf配置檔案更改user。預設使用者名稱為nobody
  • --group=nginx:指定程式執行時的非特權使用者所在組名稱。預設情況下,組名稱設定為非root使用者的名稱。
  • --with-http_realip_module 啟用ngx_http_realip_module支援(這個模組允許從請求標頭更改客戶端的IP地址值,預設為關)
  • --with-http_ssl_module :啟用ngx_http_ssl_module支援(使支援https請求,需已安裝openssl)
  • --with-http_stub_status_module :啟用ngx_http_stub_status_module支援(獲取nginx自上次啟動以來的工作狀態)
  • --with-http_gzip_static_module :啟用ngx_http_gzip_module支援(該模組同–without-http_gzip_module功能一樣)
  • --http-client-body-temp-path=PATH :設定http客戶端請求臨時檔案路徑
  • --http-proxy-temp-path=PATH:設定http代理臨時檔案路徑
  • --http-fastcgi-temp-path=PATH:設定http fastcgi臨時檔案路徑
  • --http-uwsgi-temp-path=PATH:設定http scgi臨時檔案路徑
  • --with-pcre:設定pcre庫的原始碼路徑,如果已通過yum方式安裝,使用–with-pcre自動找到庫檔案。使用–with-pcre=PATH時,需要從PCRE網站下載pcre庫的原始碼(版本8.4)並解壓,剩下的就交給Nginx的./configure和make來完成。perl正則表示式使用在location指令和 ngx_http_rewrite_module模組中。
  • --with-zlib=PATH:指定 zlib(版本1.2.11)的原始碼解壓目錄。在預設就啟用的網路傳輸壓縮模組ngx_http_gzip_module時需要使用zlib
  • --with-http_ssl_module:使用https協議模組。預設情況下,該模組沒有被構建。前提是openssl已安裝
  • --add-module=PATH : 新增第三方外部模組,如nginx-sticky-module-ng或快取模組。每次新增新的模組都要重新編譯(Tengine可以在新加入module時無需重新編譯)

註釋版

./configure \
--prefix=/usr/local/nginx \                     [Nginx安裝目錄]
--sbin-path=/usr/sbin/nginx \.                  [Nginx的sbin目錄]
--conf-path=/etc/nginx/nginx.conf \             [Nginx的配置檔案]
--error-log-path=/var/log/nginx/error.log \     [Nginx的錯誤日誌]
--http-log-path=/var/log/nginx/access.log \     [Nginx的訪問日誌]
--pid-path=/var/run/nginx.pid  \                [Nginx的程序ID]
--lock-path=/var/lock/nginx.lock \              [Nginx的程序鎖]
--user=nginx \                                  [Nginx所屬使用者]
--group=nginx \                                 [Nginx所屬使用者組]
--with-http_ssl_module \                        [Nginx的ssl模組]
--with-http_v2_module \ 
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \                     [記錄原始客戶端的IP地址]
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \                [監控 Nginx 的當前狀態]
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \                [Nginx的gzip壓縮模組]
--with-http_perl_module \          
--with-pcre=../pcre-8.42 \                      [pcre的安裝目錄]
--with-zlib=../zlib-1.2.11 \                    [zlib的安裝目錄]
--with-openssl=../openssl-1.1.1-pre8 \          [openssl的安裝目錄]
--with-debug \                                  [允許DEBUG]
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--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 \                                 [Nginx特有的stream模組]
--with-ld-opt="-Wl,-E"                          [gcc的編譯優化]

配置過程大概需要5分鐘左右

配置完後,就可以直接編譯和安裝了

直接使用執行這條命令 [[email protected] ~]# make && make install 進行安裝即可

配置Nginx,使之正常工作

[[email protected] ~]#  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啟動指令碼

> 改變檔案許可權
[root@lightserver.cn ~]#chmod +x /etc/init.d/nginx

> 新增到系統服務
[root@lightserver.cn ~]# chkconfig --add nginx

> 設定系統為開機啟動
[root@lightserver.cn ~]# chkconfig nginx on

啟動nginx伺服器

[root@lightserver.cn ~]# systemctl start nginx

啟動服務狀態

[root@lightserver.cn ~]# systemctl status nginx

修改nginx配置檔案

> 
[[email protected] ~]#  vi /etc/nginx/nginx.conf

> 
# 執行使用者
#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;
    #    }
    #}

}

注意不要忘記在 /usr/local/nginx/ 建立 logs 目錄

[root@lightserver.cn ~]# mkdir -pv /usr/local/nginx/logs/

CentOS 新增開放80TCP埠

> 加入開放埠到配置檔案

[[email protected].cn ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent

--zone=public 新增時區

--add-port=80/tcp 新增埠

--permanent 永久生效

> 載入防火牆新配置檔案(  root 身份輸入以下命令,重新載入防火牆,並不中斷使用者連線,即不丟失狀態資訊. )

[[email protected].cn ~]# firewall-cmd --reload