http://blog.csdn.net/kallen_ding/article/details/45815551
1.Nginx配置文件测试
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/usr/local/nginx/sbin</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># nginx -t </span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">nginx:</span> the configuration file /etc/nginx/nginx.conf syntax is ok <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">nginx:</span> configuration file /etc/nginx/nginx.conf test is successful</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
2.Nginx启动
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> ~]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># /usr/local/nginx/sbin/nginx </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
3.Nginx负载均衡
Nginx Architecture:
Nginx LoadBalance:
Nginx High Availability:
Nginx Access Process:
Nginx 的 upstream 目前支持4 种方式的分配——
(1)轮询(默认) :
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
(2)weight :
指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。
(3)ip_hash :
每个请求按访问ip 的hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决session 的问题。可以针对同一个C 类地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕了才会换一个。
(4)fair(第三方):
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
(5)url_hash(第三方):
按访问url 的hash 结果来分配请求,使每个url 定向到同一个后端服务器,后端服务器为缓存时比较有效。
4.Nginx安装及配置
(1) nginx源码安装
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> ~]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd /usr/local/src/</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> ~]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># wget http://syslab.comsenz.com/downloads/linux/nginx-0.9.6.tar.gz</span> [root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> ~]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># tar zxvf nginx-0.9.6.tar.gz</span> [root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> ~]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd nginx-0.9.6</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">./configure --prefix=/usr/local/nginx -<span class="ruby" style="box-sizing: border-box;">-sbin-path=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/usr/local</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx/sbin</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx </span></span>-<span class="ruby" style="box-sizing: border-box;">-conf-path=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/usr/local</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx/conf</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx.conf </span></span>-<span class="ruby" style="box-sizing: border-box;">-error-log-path=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/usr/local</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx/logs</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/error.log </span></span>-<span class="ruby" style="box-sizing: border-box;">-http-log-path=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/usr/local</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx/logs</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/access.log </span></span>-<span class="ruby" style="box-sizing: border-box;">-pid-path=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/usr/local</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx/var</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx.pid </span></span>-<span class="ruby" style="box-sizing: border-box;">-lock-path=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/usr/local</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx/var</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx.lock </span></span>-<span class="ruby" style="box-sizing: border-box;">-http-client-body-temp-path=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/dev/shm</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx_temp/client</span>_body </span>-<span class="ruby" style="box-sizing: border-box;">-http-proxy-temp-path=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/dev/shm</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx_temp/proxy</span> </span>-<span class="ruby" style="box-sizing: border-box;">-http-fastcgi-temp-path=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/dev/shm</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/nginx_temp/fastcgi</span> </span>-<span class="ruby" style="box-sizing: border-box;">-user=www --group=www </span>-<span class="ruby" style="box-sizing: border-box;">-with-cpu-opt=pentium4F </span>-<span class="ruby" style="box-sizing: border-box;">-without-select_module </span>-<span class="ruby" style="box-sizing: border-box;">-without-poll_module </span>-<span class="ruby" style="box-sizing: border-box;">-with-http_realip_module </span>-<span class="ruby" style="box-sizing: border-box;">-with-http_sub_module </span>-<span class="ruby" style="box-sizing: border-box;">-with-http_gzip_static_module </span>-<span class="ruby" style="box-sizing: border-box;">-with-http_stub_status_module </span>-<span class="ruby" style="box-sizing: border-box;">-without-http_ssi_module </span>-<span class="ruby" style="box-sizing: border-box;">-without-http_userid_module </span>-<span class="ruby" style="box-sizing: border-box;">-without-http_geo_module </span>-<span class="ruby" style="box-sizing: border-box;">-without-http_memcached_module </span>-<span class="ruby" style="box-sizing: border-box;">-without-http_map_module </span>-<span class="ruby" style="box-sizing: border-box;">-without-mail_pop3_module </span>-<span class="ruby" style="box-sizing: border-box;">-without-mail_imap_module </span>-<span class="ruby" style="box-sizing: border-box;">-without-mail_smtp_module </span>-<span class="ruby" style="box-sizing: border-box;">-with-pcre=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/usr/local</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/src/pcre</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8.32</span>/ </span>-<span class="ruby" style="box-sizing: border-box;">-with-zlib=<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/usr/local</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/zlib</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li></ul>
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> ~]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># make && make install </span> [root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> ~]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># mkdir /dev/shm/nginx_temp</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
有的nginx版本编译时会因为pcre编译不过去,需要修改一下
--with-pcre=/usr/local/src/pcre-8.32
,前提是已经下载了pcre源码包pcre-7.8.tar.gz,并解压到/usr/local/src/pcre-8.32
,不需要编译pcre.
在实际安装过程中可能需要手动安装以下依赖包:
a. 安装依赖软件
<code class="hljs brainfuck has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">apt</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">get</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">install</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">-</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">suggests</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">install</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">gcc</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">g</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">+</span><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">+</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">make</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
b. 下载相关软件
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">wget http://jaist<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.dl</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sourceforge</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.net</span>/project/pcre/pcre/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8.35</span>/pcre-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8.35</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</span> wget http://zlib<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.net</span>/zlib-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.2</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.8</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</span> wget http://www<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.openssl</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.org</span>/source/openssl-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.0</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.1</span>g<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</span> wget http://www<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.canonware</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>/download/jemalloc/jemalloc-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.6</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.0</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bz</span>2 wget http://tengine<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.taobao</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.org</span>/download/tengine-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.0</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.2</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
c. 安装Pcre
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> tar zxvf pcre-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8.35</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</span> cd pcre-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8.35</span> ./configure --prefix=/usr/local/pcre-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8.35</span> make && make install</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
d. 安装Zlib
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> tar zxvf zlib-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.2</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.8</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</span> cd zlib-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.2</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.8</span> ./configure --prefix=/usr/local/zlib-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.2</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.8</span> make && make install</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
[ERROR]-1:
<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">./configure: error: <span class="hljs-operator" style="box-sizing: border-box;">the</span> HTTP gzip module requires <span class="hljs-operator" style="box-sizing: border-box;">the</span> zlib library. You can either disable <span class="hljs-operator" style="box-sizing: border-box;">the</span> module <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">by</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--without-http_gzip_module </span> option, <span class="hljs-operator" style="box-sizing: border-box;">or</span> install <span class="hljs-operator" style="box-sizing: border-box;">the</span> zlib library <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">into</span> <span class="hljs-operator" style="box-sizing: border-box;">the</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">system</span>, <span class="hljs-operator" style="box-sizing: border-box;">or</span> build <span class="hljs-operator" style="box-sizing: border-box;">the</span> zlib library statically <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">from</span> <span class="hljs-operator" style="box-sizing: border-box;">the</span> source <span class="hljs-operator" style="box-sizing: border-box;">with</span> nginx <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">by</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--with-zlib=<path> opti</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
[ERROR]-2:
<code class="hljs asciidoc has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">configure: error: You need a C<span class="hljs-code" style="box-sizing: border-box;">++ compiler for C+</span>+ support. make[1]: *** [/usr/local/src/pcre-8.32/Makefile] Error 1 make[1]: Leaving directory <span class="hljs-smartquote" style="box-sizing: border-box;">`/home/kallen/MyDOC/nginx-1.8.0'</span> make: *** [build] Error 2</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
安装完成后的配置信息如下:
<code class="hljs vhdl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[Nginx <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Configuration</span> Summary] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Configuration</span> summary + using PCRE <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">library</span>: /usr/local/src/pcre-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8.32</span> + OpenSSL <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">library</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> used + using builtin md5 code + sha1 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">library</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> found + using zlib <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">library</span>: /usr/local/zlib nginx path prefix:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/local/nginx"</span> nginx binary <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/local/nginx/sbin/nginx"</span> nginx <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">configuration</span> prefix:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/local/nginx/conf"</span> nginx <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">configuration</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/local/nginx/conf/nginx.conf"</span> nginx pid <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/local/nginx/var/nginx.pid"</span> nginx error log <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/local/nginx/logs/error.log"</span> nginx http <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">access</span> log <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span>:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/local/nginx/logs/access.log"</span> nginx http client request <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">body</span> temporary files:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/dev/shm/nginx_temp/client_body"</span> nginx http proxy temporary files:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/dev/shm/nginx_temp/proxy"</span> nginx http fastcgi temporary files:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/dev/shm/nginx_temp/fastcgi"</span> nginx http uwsgi temporary files:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"uwsgi_temp"</span> nginx http scgi temporary files:<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"scgi_temp"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul>
(2) 编写nginx启动脚本
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> init.d]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># vi /etc/init.d/nginx</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
写入以下内容:
<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-shebang" style="color: rgb(0, 102, 102); box-sizing: border-box;">#!/bin/bash</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># </span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># Startup script for the Nginx HTTP Server</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># Kallen Ding, Apr 23 2015</span> NGINX_PATH=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/local/nginx/"</span> OPTIONS=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"-c <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${NGINX_PATH}</span>conf/nginx.conf"</span> prog=nginx nginx=<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${NGINX_PATH}</span>sbin/nginx pidfile=/var/run/nginx.pid <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># Source function library.</span> . /etc/rc.d/init.d/functions <span class="hljs-function" style="box-sizing: border-box;">start</span>() { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> -n <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Starting <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$prog</span>: "</span> daemon --pidfile=<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${pidfile}</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$nginx</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$OPTIONS</span> RETVAL=$? <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$RETVAL</span> } <span class="hljs-function" style="box-sizing: border-box;">stop</span>() { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> -n <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Stopping <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$prog</span>: "</span> killproc -p <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${pidfile}</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$nginx</span> RETVAL=$? <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> } <span class="hljs-function" style="box-sizing: border-box;">reload</span>() { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> -n $<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Reloading <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$prog</span>: "</span> killproc -p <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${pidfile}</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$nginx</span> -HUP RETVAL=$? <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># See how we were called.</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">case</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$1</span>"</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> start) start ;; stop) stop ;; restart) stop start ;; reload) reload ;; status) status <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$prog</span> RETVAL=$? ;; *) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Usage: <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$prog</span> {start|stop|restart|reload|status}"</span> RETVAL=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">esac</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">exit</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$RETVAL</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li></ul>
保存后,更改/etc/init.d/nginx
的权限
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> ~]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chmod 755 /etc/init.d/nginx</span> [root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> ~]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chkconfig --add nginx</span> [root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@kallen</span> ~]<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># chkconfig nginx on</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
【注】文中所有图片均来自Nginx官网.