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整合的部分將在單獨的文章再介紹,如有毛病,歡迎指正。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容