linux 編譯安裝nginx,配置自啟動指令碼

來源:互聯網
上載者:User

在公司的suse伺服器裝nginx,記錄下安裝過程:

參照這篇文章:Linux 從源碼編譯安裝 Nginx:

1.1、準備 pcre 庫
pere 是為了讓 nginx 支援Regex。只是準備,並不安裝,是為了避免在64位系統中出現錯誤。

 代碼如下 複製代碼

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.11.tar.gz 

tar -zxf pcre-8.11.tar.gz

1.3、準備 zlib 庫
同樣只是準備,並不安裝,是為了避免在64位系統中出現錯誤。

 代碼如下 複製代碼

wget http://zlib.net/zlib-1.2.5.tar.gz
tar -zxf zlib-1.2.5.tar.gz

2.1、下載、建立臨時目錄

 代碼如下 複製代碼

wget http://nginx.org/download/nginx-1.5.5.tar.gz //在公司是下載到本地再rz -by上傳到伺服器
tar -zxf nginx-1.5.5.tar.gz
cd nginx-1.5.5
mkdir -p /var/tmp/nginx

2.2、編譯與安裝

詳細編譯配置選項見《Nginx編譯參數解析》

 代碼如下 複製代碼

root64:~ # ./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_dav_module --with-http_flv_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-mail  --with-pcre=../pcre-8.11 --with-zlib=../zlib-1.2.5 --with-debug --http-client-body-temp-path=/var/tmp/nginx/client --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
root64:~ # make && make install
root64:~ # ln -s /usr/local/nginx/sbin/nginx /usr/sbin/


--prefix #nginx安裝目錄,預設在/usr/local/nginx
--pid-path #pid問件位置,預設在logs目錄
--lock-path #lock問件位置,預設在logs目錄
--with-http_ssl_module #開啟HTTP SSL模組,以支援HTTPS請求。
--with-http_dav_module #開啟WebDAV擴充動作模組,可為檔案和目錄指定許可權
--with-http_flv_module #支援對FLV檔案的拖動播放
--with-http_realip_module #支援顯示真實來源IP地址
--with-http_gzip_static_module #預壓縮檔傳前檢查,防止檔案被重複壓縮
--with-http_stub_status_module #取得一些nginx的運行狀態
--with-mail #允許POP3/IMAP4/SMTP代理模組
--with-mail_ssl_module #允許POP3/IMAP/SMTP可以使用SSL/TLS
--with-pcre=../pcre-8.11 #注意是未安裝的pcre路徑
--with-zlib=../zlib-1.2.5 #注意是未安裝的zlib路徑
--with-debug #允許調試日誌
--http-client-body-temp-path #用戶端請求臨時檔案路徑
--http-proxy-temp-path #設定http proxy臨時檔案路徑
--http-fastcgi-temp-path #設定http fastcgi臨時檔案路徑
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi #設定uwsgi 臨時檔案路徑
--http-scgi-temp-path=/var/tmp/nginx/scgi #設定scgi 臨時檔案路徑

2.3、開機自啟動 nginx 指令碼

 代碼如下 複製代碼

/etc/init.d/nginx
#! /bin/bash
# Description: Startup script for webserver on CentOS. cp it in /etc/init.d and
# chkconfig --add nginx && chkconfig nginx on
# then you can use server command control nginx
#
# chkconfig: 2345 08 99
# description: Starts, stops nginx

set -e
PATH=$PATH:/usr/local/nginx/sbin/
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/nginx.conf
PIDFILE=/var/run/nginx.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

d_start() {
$DAEMON -c $CONFIGFILE || echo -n " already running"
}

d_stop() {
kill -QUIT `cat $PIDFILE` || echo -n " not running"
}

d_reload() {
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
exit 0

將nginx添加到自啟動服務中並啟動:

 代碼如下 複製代碼

chmod 700 /etc/init.d/nginx
chkconfig --add nginx
chkconfig --level 2345 nginx on
/etc/init.d/nginx start

再配置每天自動切割nginx日誌指令碼:

 代碼如下 複製代碼

 vi /usr/local/nginx/sbin/cut_nginx_log.sh

#!/bin/bash
# This script run at 00:00
# The Nginx logs path
 logs_path="/usr/local/nginx/logs/"
 logs_bak_path="/data/logs/nginx/"

 mkdir -p ${logs_bak_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
 cp ${logs_path}access.log ${logs_bak_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
 rm -rf ${logs_path}*.log
 kill -USR1 `cat /var/run/nginx.pid`

安裝完成 :)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.