一、軟體配置資訊
CentOS 6.5
二、必要軟體準備
檢查安裝pcre,openssl,gzip指令如下:
<code>yum install -y zlib zlib-devel pcre pcre-devel openssl openssl-devel</code>
三、建立Nginx使用者與組
<code>[root@localhost src]# groupadd nginx </code>
<code>[root@localhost src]# useradd -r -g nginx -s /sbin/nologin -M nginx</code>
四、下載下傳解壓
Nginx可以從官網下載下傳:http://nginx.org/
也可以通過指令直接下載下傳,我的目前目錄是/usr/local/src:
<code>[root@localhost src]# wget http:</code><code>//nginx.org/download/nginx-1.10.1.tar.gz</code>
解壓:
<code>[root@localhost src]# tar zxvf nginx-</code><code>1.10</code><code>.</code><code>1</code><code>.tar.gz</code>
五、開始安裝
<code>[root@localhost src]# cd nginx-</code><code>1.10</code><code>.</code><code>1</code>
<code>[root@localhost nginx-</code><code>1.10</code><code>.</code><code>1</code><code>]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/</code><code>var</code><code>/log/nginx/error.log --http-log-path=/</code><code>var</code><code>/log/nginx/access.log --pid-path=/</code><code>var</code><code>/run/nginx.pid --lock-path=/</code><code>var</code><code>/run/nginx.lock --http-client-body-temp-path=/</code><code>var</code><code>/cache/nginx/client_temp --http-proxy-temp-path=/</code><code>var</code><code>/cache/nginx/proxy_temp --http-fastcgi-temp-path=/</code><code>var</code><code>/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/</code><code>var</code><code>/cache/nginx/uwsgi_temp --http-scgi-temp-path=/</code><code>var</code><code>/cache/nginx/scgi_temp --user=nginx --group=nginx --</code><code>with</code><code>-http_ssl_module --</code><code>with</code><code>-http_realip_module --</code><code>with</code><code>-http_addition_module --</code><code>with</code><code>-http_sub_module --</code><code>with</code><code>-http_dav_module --</code><code>with</code><code>-http_flv_module --</code><code>with</code><code>-http_mp4_module --</code><code>with</code><code>-http_gunzip_module --</code><code>with</code><code>-http_gzip_static_module --</code><code>with</code><code>-http_random_index_module --</code><code>with</code><code>-http_secure_link_module --</code><code>with</code><code>-http_stub_status_module --</code><code>with</code><code>-http_auth_request_module --</code><code>with</code><code>-file-aio --</code><code>with</code><code>-http_spdy_module --</code><code>with</code><code>-ipv6 --</code><code>with</code><code>-pcre </code>
<code>./configure \ </code>
<code>--prefix=/usr/local/nginx \ </code>
<code>--sbin-path=/usr/sbin/nginx \ </code>
<code>--conf-path=/usr/local/nginx/conf/nginx.conf \ </code>
<code>--error-log-path=/</code><code>var</code><code>/log/nginx/error.log \ </code>
<code>--http-log-path=/</code><code>var</code><code>/log/nginx/access.log \ </code>
<code>--pid-path=/</code><code>var</code><code>/run/nginx.pid \ </code>
<code>--lock-path=/</code><code>var</code><code>/run/nginx.lock \ </code>
<code>--http-client-body-temp-path=/</code><code>var</code><code>/cache/nginx/client_temp \ </code>
<code>--http-proxy-temp-path=/</code><code>var</code><code>/cache/nginx/proxy_temp \ </code>
<code>--http-fastcgi-temp-path=/</code><code>var</code><code>/cache/nginx/fastcgi_temp \ </code>
<code>--http-uwsgi-temp-path=/</code><code>var</code><code>/cache/nginx/uwsgi_temp \ </code>
<code>--http-scgi-temp-path=/</code><code>var</code><code>/cache/nginx/scgi_temp \ </code>
<code>--user=nginx \ </code>
<code>--group=nginx \ </code>
<code>--</code><code>with</code><code>-http_ssl_module \ </code>
<code>--</code><code>with</code><code>-http_realip_module \ </code>
<code>--</code><code>with</code><code>-http_addition_module \ </code>
<code>--</code><code>with</code><code>-http_sub_module \ </code>
<code>--</code><code>with</code><code>-http_dav_module \ </code>
<code>--</code><code>with</code><code>-http_flv_module \ </code>
<code>--</code><code>with</code><code>-http_mp4_module \ </code>
<code>--</code><code>with</code><code>-http_gunzip_module \ </code>
<code>--</code><code>with</code><code>-http_gzip_static_module \ </code>
<code>--</code><code>with</code><code>-http_random_index_module \ </code>
<code>--</code><code>with</code><code>-http_secure_link_module \ </code>
<code>--</code><code>with</code><code>-http_stub_status_module \ </code>
<code>--</code><code>with</code><code>-http_auth_request_module \ </code>
<code>--</code><code>with</code><code>-file-aio \ </code>
<code>--</code><code>with</code><code>-http_spdy_module \ </code>
<code>--</code><code>with</code><code>-ipv6 \ </code>
<code>--</code><code>with</code><code>-pcre </code>
<code> </code>
<code>[root@localhost nginx-</code><code>1.10</code><code>.</code><code>1</code><code>]# make </code>
<code>[root@localhost nginx-</code><code>1.10</code><code>.</code><code>1</code><code>]# make install</code>
六、啟動停止
啟動指令:
<code>[root@localhost nginx-</code><code>1.10</code><code>.</code><code>1</code><code>]# /usr/sbin/nginx</code>
測試,直接用curl指令讀取web資訊:
<code>[root@localhost </code><code>1.10</code><code>.</code><code>1</code><code>]# curl -s http:</code><code>//localhost | grep nginx.com</code>
關閉指令:
<code>[root@localhost nginx-</code><code>1.10</code><code>.</code><code>1</code><code>]# /usr/sbin/nginx -s stop </code>
<code> </code><code>reload,當你修改配置時,用此指令不用再重新開機就生效了:</code>
<code>[root@localhost nginx-</code><code>1.10</code><code>.</code><code>1</code><code>]# /usr/sbin/nginx -s reload</code>
七、配置Nginx為系統服務
添加如下檔案:
<code>vi /etc/init.d/nginx</code>
<code>#!/bin/sh</code>
<code>#</code>
<code># nginx - </code><code>this</code> <code>script starts and stops the nginx daemon</code>
<code># chkconfig: - </code><code>85</code> <code>15</code>
<code># description: Nginx </code><code>is</code> <code>an HTTP(S) server, HTTP(S) reverse \</code>
<code># proxy and IMAP/POP3 proxy server</code>
<code># processname: nginx</code>
<code># config: /etc/nginx.conf</code>
<code># config: /usr/local/nginx/conf/nginx.conf</code>
<code># pidfile: /</code><code>var</code><code>/run/nginx.pid</code>
<code># Source </code><code>function</code> <code>library.</code>
<code>. /etc/rc.d/init.d/functions</code>
<code># Source networking configuration.</code>
<code>. /etc/sysconfig/network</code>
<code># Check that networking </code><code>is</code> <code>up.</code>
<code>[ </code><code>"$NETWORKING"</code> <code>= </code><code>"no"</code> <code>] && exit </code><code>0</code>
<code>nginx=</code><code>"/usr/sbin/nginx"</code>
<code>prog=$(basename $nginx)</code>
<code>NGINX_CONF_FILE=</code><code>"/usr/local/nginx/conf/nginx.conf"</code>
<code>[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx</code>
<code>lockfile=/</code><code>var</code><code>/lock/subsys/nginx</code>
<code>make_dirs() {</code>
<code> </code><code># make required directories</code>
<code> </code><code>user=`$nginx -V </code><code>2</code><code>>&</code><code>1</code> <code>| grep </code><code>"configure arguments:"</code> <code>| sed </code><code>'s/[^*]*--user=\([^ ]*\).*/\1/g'</code> <code>-`</code>
<code> </code><code>if</code> <code>[ -z </code><code>"`grep $user /etc/passwd`"</code> <code>]; then</code>
<code> </code><code>useradd -M -s /bin/nologin $user</code>
<code> </code><code>fi</code>
<code> </code><code>options=`$nginx -V </code><code>2</code><code>>&</code><code>1</code> <code>| grep </code><code>'configure arguments:'</code><code>`</code>
<code> </code><code>for</code> <code>opt </code><code>in</code> <code>$options; </code><code>do</code>
<code> </code><code>if</code> <code>[ `echo $opt | grep </code><code>'.*-temp-path'</code><code>` ]; then</code>
<code> </code><code>value=`echo $opt | cut -d </code><code>"="</code> <code>-f </code><code>2</code><code>`</code>
<code> </code><code>if</code> <code>[ ! -d </code><code>"$value"</code> <code>]; then</code>
<code> </code><code># echo </code><code>"creating"</code> <code>$value</code>
<code> </code><code>mkdir -p $value && chown -R $user $value</code>
<code> </code><code>fi</code>
<code> </code><code>fi</code>
<code> </code><code>done</code>
<code>}</code>
<code>start() {</code>
<code> </code><code>[ -x $nginx ] || exit </code><code>5</code>
<code> </code><code>[ -f $NGINX_CONF_FILE ] || exit </code><code>6</code>
<code> </code><code>make_dirs</code>
<code> </code><code>echo -n $</code><code>"Starting $prog: "</code>
<code> </code><code>daemon $nginx -c $NGINX_CONF_FILE</code>
<code> </code><code>retval=$?</code>
<code> </code><code>echo</code>
<code> </code><code>[ $retval -eq </code><code>0</code> <code>] && touch $lockfile</code>
<code> </code><code>return</code> <code>$retval</code>
<code>stop() {</code>
<code> </code><code>echo -n $</code><code>"Stopping $prog: "</code>
<code> </code><code>killproc $prog -QUIT</code>
<code> </code><code>[ $retval -eq </code><code>0</code> <code>] && rm -f $lockfile</code>
<code>restart() {</code>
<code> </code><code>#configtest || </code><code>return</code> <code>$?</code>
<code> </code><code>stop</code>
<code> </code><code>sleep </code><code>1</code>
<code> </code><code>start</code>
<code>reload() {</code>
<code> </code><code>echo -n $</code><code>"Reloading $prog: "</code>
<code> </code><code>killproc $nginx -HUP</code>
<code> </code><code>RETVAL=$?</code>
<code>force_reload() {</code>
<code> </code><code>restart</code>
<code>configtest() {</code>
<code> </code><code>$nginx -t -c $NGINX_CONF_FILE</code>
<code>rh_status() {</code>
<code> </code><code>status $prog</code>
<code>rh_status_q() {</code>
<code> </code><code>rh_status >/dev/</code><code>null</code> <code>2</code><code>>&</code><code>1</code>
<code>case</code> <code>"$1"</code> <code>in</code>
<code> </code><code>start)</code>
<code> </code><code>rh_status_q && exit </code><code>0</code>
<code> </code><code>$</code><code>1</code>
<code> </code><code>;;</code>
<code> </code><code>stop)</code>
<code> </code><code>rh_status_q || exit </code><code>0</code>
<code> </code><code>restart|configtest)</code>
<code> </code><code>reload)</code>
<code> </code><code>rh_status_q || exit </code><code>7</code>
<code> </code><code>force-reload)</code>
<code> </code><code>force_reload</code>
<code> </code><code>status)</code>
<code> </code><code>rh_status</code>
<code> </code><code>condrestart|</code><code>try</code><code>-restart)</code>
<code> </code><code>;;</code>
<code> </code><code>*)</code>
<code> </code><code>echo $</code><code>"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"</code>
<code> </code><code>exit </code><code>2</code>
<code>esac</code>
修改檔案權限:
<code>chmod +x /etc/init.d/nginx</code>
檢視Nginx服務:
<code>chkconfig --list nginx </code>
<code>nginx </code><code>0</code><code>:off </code><code>1</code><code>:off </code><code>2</code><code>:on </code><code>3</code><code>:on </code><code>4</code><code>:on </code><code>5</code><code>:on </code><code>6</code><code>:off</code>
本文轉自奔跑在路上部落格51CTO部落格,原文連結http://blog.51cto.com/qiangsh/1846511如需轉載請自行聯系原作者
qianghong000