[TOC]
由于yum安裝比較簡單,這里就不做介紹了。下面是我個人安裝源碼安裝nginx的過程。
### 依賴關系安裝以及安裝包
* * * * *
需要下載的安裝包openssl,zlib,pcre,nginx;鏈接: https://pan.baidu.com/s/1c7ixeY 密碼: r35q
版本號如下圖,也可自行搜索下載。

* * * * *
其中openssl,zlib,pcre 安裝方法如下(本人這里未安裝,我是和nginx一起安裝的,安裝nginx會介紹):
* * * * *

* * * * *
zlib和pcre方法類似,就不宜一介紹了,或者是直接使用yum install -y openssl zlib pcre的方法安裝依賴關系
### 源碼安裝
* * * * *
首先添加用戶nginx,實現以之運行nginx進程:

* * * * *
下面就開始編譯安裝了:
~~~
tar xzf nginx-1.8.0.tar.gz
cd nginx-1.8.0
~~~
~~~
./configure \
--prefix=/app/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/app/nginx/conf/nginx.conf \
--error-log-path=/app/nginx/log/nginx/error.log \
--http-log-path=/app/nginx/log/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_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/app/nginx/tmp/client/ \
--http-proxy-temp-path=/app/nginx/tmp/proxy/ \
--http-fastcgi-temp-path=/app/nginx/tmp/fcgi/ \
--http-uwsgi-temp-path=/app/nginx/tmp/uwsgi \
--http-scgi-temp-path=/app/nginx/scgi \
--with-pcre=/data/nginx_softwave/pcre \
--with-openssl=/data/nginx_softwave/openssl \
--with-zlib=/data/nginx_softwave/zlib
~~~
* * * * *
* * * * *
對于上面的編譯配置情況這里只說明下最后三項(with-pcre,with-openssl,with-zlib)后面的路徑都是源碼包解壓后的路徑,它會在安裝nginx的時候自動去從這些路徑去安裝這些依賴關系。若在前面安裝這些依賴關系,則后面三個選項可以去掉。關于configure的參數說明后面會有一節單獨說明。
下圖是配置情況:

* * * * *
接下來執行 make && make install
等待編譯安裝完成即可。
* * * * *
測試以及啟動nginx
~~~
nginx -t #測試nginx是否安裝成功
nginx #啟動nginx
ps -ef | grep nginx | grep -v grep #檢查nginx是否啟動成功
nginx -s stop #關閉nginx服務
ps -ef | grep nginx | grep -v grep #檢查nginx是否已經關閉
~~~
若在啟動的過程中報錯,類似‘emerg] 19481#0: mkdir() "/app/nginx/tmp/client" failed (2: No such file or directory’的錯誤的時候,手動mkdir -p /app/nginx/tmp 即可
我安裝完成測試結果如下:

* * * * *
下面是訪問結果:

### 啟動腳本
* * * * *
到這里我們的基本安裝已經完成了,但是為了我們的nginx服務更加方便的管理,我們可以將nginx加入init啟動服務。針對我上面的配置情況,我們需要編寫一個起停的控制腳本。
起停腳本:
vim nginx_init.sh
~~~
#!/bin/sh
#
# 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
# config: /etc/sysconfig/nginx
# 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/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
~~~
* * * * *
還需要做如下事情:
~~~
cp ./nginx_init.sh /etc/init.d/nginx
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
~~~
* * * * *
然后就可以做啟動停止的測試了
~~~
service nginx start
service nginx status
service nginx reload
service nginx stop
~~~