Nginx搭建———LNMP环境搭建篇,nginx搭建lnmp


1.Nginx配置文件测试

<code class="hljs ruby has-numbering" >root<span class="hljs-variable" >@kallen</span><span class="hljs-symbol" >:/usr/local/nginx/sbin</span><span class="hljs-comment" ># nginx -t </span>
<span class="hljs-symbol" >nginx:</span> the configuration file /etc/nginx/nginx.conf syntax is ok 
<span class="hljs-symbol" >nginx:</span> configuration file /etc/nginx/nginx.conf test is successful</code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li></ul>

2.Nginx启动

<code class="hljs ruby has-numbering" >[root<span class="hljs-variable" >@kallen</span> ~]<span class="hljs-comment" ># /usr/local/nginx/sbin/nginx </span></code><ul class="pre-numbering" ><li >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" >[root<span class="hljs-variable" >@kallen</span> ~]<span class="hljs-comment" ># cd /usr/local/src/</span></code><ul class="pre-numbering" ><li >1</li></ul>
<code class="hljs ruby has-numbering" >[root<span class="hljs-variable" >@kallen</span> ~]<span class="hljs-comment" ># wget http://syslab.comsenz.com/downloads/linux/nginx-0.9.6.tar.gz</span>
[root<span class="hljs-variable" >@kallen</span> ~]<span class="hljs-comment" ># tar zxvf nginx-0.9.6.tar.gz</span>
[root<span class="hljs-variable" >@kallen</span> ~]<span class="hljs-comment" ># cd nginx-0.9.6</span></code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li></ul>
<code class="hljs haml has-numbering" >./configure --prefix=/usr/local/nginx 
-<span class="ruby" >-sbin-path=<span class="hljs-regexp" >/usr/local</span><span class="hljs-regexp" >/nginx/sbin</span><span class="hljs-regexp" >/nginx 
</span></span>-<span class="ruby" >-conf-path=<span class="hljs-regexp" >/usr/local</span><span class="hljs-regexp" >/nginx/conf</span><span class="hljs-regexp" >/nginx.conf 
</span></span>-<span class="ruby" >-error-log-path=<span class="hljs-regexp" >/usr/local</span><span class="hljs-regexp" >/nginx/logs</span><span class="hljs-regexp" >/error.log 
</span></span>-<span class="ruby" >-http-log-path=<span class="hljs-regexp" >/usr/local</span><span class="hljs-regexp" >/nginx/logs</span><span class="hljs-regexp" >/access.log 
</span></span>-<span class="ruby" >-pid-path=<span class="hljs-regexp" >/usr/local</span><span class="hljs-regexp" >/nginx/var</span><span class="hljs-regexp" >/nginx.pid 
</span></span>-<span class="ruby" >-lock-path=<span class="hljs-regexp" >/usr/local</span><span class="hljs-regexp" >/nginx/var</span><span class="hljs-regexp" >/nginx.lock 
</span></span>-<span class="ruby" >-http-client-body-temp-path=<span class="hljs-regexp" >/dev/shm</span><span class="hljs-regexp" >/nginx_temp/client</span>_body 
</span>-<span class="ruby" >-http-proxy-temp-path=<span class="hljs-regexp" >/dev/shm</span><span class="hljs-regexp" >/nginx_temp/proxy</span> 
</span>-<span class="ruby" >-http-fastcgi-temp-path=<span class="hljs-regexp" >/dev/shm</span><span class="hljs-regexp" >/nginx_temp/fastcgi</span> 
</span>-<span class="ruby" >-user=www --group=www 
</span>-<span class="ruby" >-with-cpu-opt=pentium4F 
</span>-<span class="ruby" >-without-select_module 
</span>-<span class="ruby" >-without-poll_module 
</span>-<span class="ruby" >-with-http_realip_module 
</span>-<span class="ruby" >-with-http_sub_module 
</span>-<span class="ruby" >-with-http_gzip_static_module 
</span>-<span class="ruby" >-with-http_stub_status_module 
</span>-<span class="ruby" >-without-http_ssi_module 
</span>-<span class="ruby" >-without-http_userid_module 
</span>-<span class="ruby" >-without-http_geo_module 
</span>-<span class="ruby" >-without-http_memcached_module 
</span>-<span class="ruby" >-without-http_map_module 
</span>-<span class="ruby" >-without-mail_pop3_module 
</span>-<span class="ruby" >-without-mail_imap_module 
</span>-<span class="ruby" >-without-mail_smtp_module 
</span>-<span class="ruby" >-with-pcre=<span class="hljs-regexp" >/usr/local</span><span class="hljs-regexp" >/src/pcre</span>-<span class="hljs-number" >8.32</span>/ 
</span>-<span class="ruby" >-with-zlib=<span class="hljs-regexp" >/usr/local</span><span class="hljs-regexp" >/zlib</span></span></code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li><li >4</li><li >5</li><li >6</li><li >7</li><li >8</li><li >9</li><li >10</li><li >11</li><li >12</li><li >13</li><li >14</li><li >15</li><li >16</li><li >17</li><li >18</li><li >19</li><li >20</li><li >21</li><li >22</li><li >23</li><li >24</li><li >25</li><li >26</li><li >27</li><li >28</li></ul>
<code class="hljs ruby has-numbering" >[root<span class="hljs-variable" >@kallen</span> ~]<span class="hljs-comment" ># make && make install </span>
[root<span class="hljs-variable" >@kallen</span> ~]<span class="hljs-comment" ># mkdir /dev/shm/nginx_temp</span></code><ul class="pre-numbering" ><li >1</li><li >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" ><span class="hljs-comment" >apt</span><span class="hljs-literal" >-</span><span class="hljs-comment" >get</span> <span class="hljs-literal" >-</span><span class="hljs-literal" >-</span><span class="hljs-comment" >install</span><span class="hljs-literal" >-</span><span class="hljs-comment" >suggests</span> <span class="hljs-comment" >install</span> <span class="hljs-comment" >gcc</span> <span class="hljs-comment" >g</span><span class="hljs-literal" >+</span><span class="hljs-literal" >+</span> <span class="hljs-comment" >make</span></code><ul class="pre-numbering" ><li >1</li></ul>

b. 下载相关软件

<code class="hljs avrasm has-numbering" >wget http://jaist<span class="hljs-preprocessor" >.dl</span><span class="hljs-preprocessor" >.sourceforge</span><span class="hljs-preprocessor" >.net</span>/project/pcre/pcre/<span class="hljs-number" >8.35</span>/pcre-<span class="hljs-number" >8.35</span><span class="hljs-preprocessor" >.tar</span><span class="hljs-preprocessor" >.gz</span>
wget http://zlib<span class="hljs-preprocessor" >.net</span>/zlib-<span class="hljs-number" >1.2</span><span class="hljs-number" >.8</span><span class="hljs-preprocessor" >.tar</span><span class="hljs-preprocessor" >.gz</span>
wget http://www<span class="hljs-preprocessor" >.openssl</span><span class="hljs-preprocessor" >.org</span>/source/openssl-<span class="hljs-number" >1.0</span><span class="hljs-number" >.1</span>g<span class="hljs-preprocessor" >.tar</span><span class="hljs-preprocessor" >.gz</span>
wget http://www<span class="hljs-preprocessor" >.canonware</span><span class="hljs-preprocessor" >.com</span>/download/jemalloc/jemalloc-<span class="hljs-number" >3.6</span><span class="hljs-number" >.0</span><span class="hljs-preprocessor" >.tar</span><span class="hljs-preprocessor" >.bz</span>2
wget http://tengine<span class="hljs-preprocessor" >.taobao</span><span class="hljs-preprocessor" >.org</span>/download/tengine-<span class="hljs-number" >2.0</span><span class="hljs-number" >.2</span><span class="hljs-preprocessor" >.tar</span><span class="hljs-preprocessor" >.gz</span></code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li><li >4</li><li >5</li></ul>

c. 安装Pcre

<code class="hljs avrasm has-numbering" >  tar zxvf pcre-<span class="hljs-number" >8.35</span><span class="hljs-preprocessor" >.tar</span><span class="hljs-preprocessor" >.gz</span>
  cd pcre-<span class="hljs-number" >8.35</span>
 ./configure --prefix=/usr/local/pcre-<span class="hljs-number" >8.35</span>
  make && make install</code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li><li >4</li></ul>

d. 安装Zlib

<code class="hljs avrasm has-numbering" >  tar zxvf zlib-<span class="hljs-number" >1.2</span><span class="hljs-number" >.8</span><span class="hljs-preprocessor" >.tar</span><span class="hljs-preprocessor" >.gz</span>
  cd zlib-<span class="hljs-number" >1.2</span><span class="hljs-number" >.8</span>
  ./configure --prefix=/usr/local/zlib-<span class="hljs-number" >1.2</span><span class="hljs-number" >.8</span>
  make && make install</code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li><li >4</li></ul>

[ERROR]-1:

<code class="hljs livecodeserver has-numbering" >./configure: error: <span class="hljs-operator" >the</span> HTTP gzip module requires <span class="hljs-operator" >the</span> zlib library. 
You can either disable <span class="hljs-operator" >the</span> module <span class="hljs-keyword" >by</span> <span class="hljs-keyword" >using</span> <span class="hljs-comment" >--without-http_gzip_module </span>
option, <span class="hljs-operator" >or</span> install <span class="hljs-operator" >the</span> zlib library <span class="hljs-keyword" >into</span> <span class="hljs-operator" >the</span> <span class="hljs-keyword" >system</span>, <span class="hljs-operator" >or</span> build <span class="hljs-operator" >the</span> zlib library 
statically <span class="hljs-built_in" >from</span> <span class="hljs-operator" >the</span> source <span class="hljs-operator" >with</span> nginx <span class="hljs-keyword" >by</span> <span class="hljs-keyword" >using</span> <span class="hljs-comment" >--with-zlib=<path> option</span></code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li><li >4</li></ul>

[ERROR]-2:

<code class="hljs http has-numbering" ><span class="hljs-attribute" >configure</span>: <span class="hljs-string" >error: You need a C++ compiler for C++ support.</span>
<span class="hljs-attribute" >make[1]</span>: <span class="hljs-string" >*** [/usr/local/src/pcre-8.32/Makefile] Error 1</span>
<span class="hljs-attribute" >make[1]</span>: <span class="hljs-string" >Leaving directory /home/kallen/MyDOC/nginx-1.8.0</span>
<span class="hljs-attribute" >make</span>: <span class="hljs-string" >*** [build] Error 2</span></code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li><li >4</li></ul>

安装完成后的配置信息如下:

<code class="hljs vhdl has-numbering" >[Nginx <span class="hljs-keyword" >Configuration</span> Summary]
<span class="hljs-keyword" >Configuration</span> summary
+ using PCRE <span class="hljs-keyword" >library</span>: /usr/local/src/pcre-<span class="hljs-number" >8.32</span>
+ OpenSSL <span class="hljs-keyword" >library</span> <span class="hljs-keyword" >is</span> <span class="hljs-keyword" >not</span> used
+ using builtin md5 code
+ sha1 <span class="hljs-keyword" >library</span> <span class="hljs-keyword" >is</span> <span class="hljs-keyword" >not</span> found
+ using zlib <span class="hljs-keyword" >library</span>: /usr/local/zlib
  nginx path prefix:<span class="hljs-string" >"/usr/local/nginx"</span>
  nginx binary <span class="hljs-keyword" >file</span>:<span class="hljs-string" >"/usr/local/nginx/sbin/nginx"</span>
  nginx <span class="hljs-keyword" >configuration</span> prefix:<span class="hljs-string" >"/usr/local/nginx/conf"</span>
  nginx <span class="hljs-keyword" >configuration</span> <span class="hljs-keyword" >file</span>:<span class="hljs-string" >"/usr/local/nginx/conf/nginx.conf"</span>
  nginx pid <span class="hljs-keyword" >file</span>:<span class="hljs-string" >"/usr/local/nginx/var/nginx.pid"</span>
  nginx error log <span class="hljs-keyword" >file</span>:<span class="hljs-string" >"/usr/local/nginx/logs/error.log"</span>
  nginx http <span class="hljs-keyword" >access</span> log <span class="hljs-keyword" >file</span>:<span class="hljs-string" >"/usr/local/nginx/logs/access.log"</span>
  nginx http client request <span class="hljs-keyword" >body</span> temporary files:<span class="hljs-string" >"/dev/shm/nginx_temp/client_body"</span>
  nginx http proxy temporary files:<span class="hljs-string" >"/dev/shm/nginx_temp/proxy"</span>
  nginx http fastcgi temporary files:<span class="hljs-string" >"/dev/shm/nginx_temp/fastcgi"</span>
  nginx http uwsgi temporary files:<span class="hljs-string" >"uwsgi_temp"</span>
  nginx http scgi temporary files:<span class="hljs-string" >"scgi_temp"</span></code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li><li >4</li><li >5</li><li >6</li><li >7</li><li >8</li><li >9</li><li >10</li><li >11</li><li >12</li><li >13</li><li >14</li><li >15</li><li >16</li><li >17</li><li >18</li><li >19</li></ul>

(2) 编写nginx启动脚本

<code class="hljs ruby has-numbering" >[root<span class="hljs-variable" >@kallen</span> init.d]<span class="hljs-comment" ># vi /etc/init.d/nginx</span></code><ul class="pre-numbering" ><li >1</li></ul>

写入以下内容:

<code class="hljs bash has-numbering" ><span class="hljs-shebang" >#!/bin/bash</span>
<span class="hljs-comment" >#        </span>
<span class="hljs-comment" ># Startup script for the Nginx HTTP Server</span>
<span class="hljs-comment" >#</span>
<span class="hljs-comment" ># Kallen Ding, Apr 23 2015</span>

NGINX_PATH=<span class="hljs-string" >"/usr/local/nginx/"</span>
OPTIONS=<span class="hljs-string" >"-c <span class="hljs-variable" >${NGINX_PATH}</span>conf/nginx.conf"</span>
prog=nginx
nginx=<span class="hljs-variable" >${NGINX_PATH}</span>sbin/nginx
pidfile=/var/run/nginx.pid

<span class="hljs-comment" ># Source function library.</span>
. /etc/rc.d/init.d/functions

<span class="hljs-function" >start</span>() {
    <span class="hljs-built_in" >echo</span> -n <span class="hljs-string" >"Starting <span class="hljs-variable" >$prog</span>: "</span>
    daemon --pidfile=<span class="hljs-variable" >${pidfile}</span> <span class="hljs-variable" >$nginx</span> <span class="hljs-variable" >$OPTIONS</span>
    RETVAL=$?
    <span class="hljs-built_in" >echo</span>
    <span class="hljs-keyword" >return</span> <span class="hljs-variable" >$RETVAL</span>
}

<span class="hljs-function" >stop</span>() {
    <span class="hljs-built_in" >echo</span> -n <span class="hljs-string" >"Stopping <span class="hljs-variable" >$prog</span>: "</span>
    killproc -p <span class="hljs-variable" >${pidfile}</span> <span class="hljs-variable" >$nginx</span>
    RETVAL=$?
    <span class="hljs-built_in" >echo</span>
}
<span class="hljs-function" >reload</span>() {
    <span class="hljs-built_in" >echo</span> -n $<span class="hljs-string" >"Reloading <span class="hljs-variable" >$prog</span>: "</span>
    killproc -p <span class="hljs-variable" >${pidfile}</span> <span class="hljs-variable" >$nginx</span> -HUP
    RETVAL=$?
    <span class="hljs-built_in" >echo</span>
}

<span class="hljs-comment" ># See how we were called.</span>
<span class="hljs-keyword" >case</span> <span class="hljs-string" >"<span class="hljs-variable" >$1</span>"</span> <span class="hljs-keyword" >in</span>
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  reload)
        reload
    ;;
  status)
    status <span class="hljs-variable" >$prog</span>
    RETVAL=$?
    ;;
  *)
    <span class="hljs-built_in" >echo</span> <span class="hljs-string" >"Usage: <span class="hljs-variable" >$prog</span> {start|stop|restart|reload|status}"</span>
    RETVAL=<span class="hljs-number" >3</span>
<span class="hljs-keyword" >esac</span>

<span class="hljs-keyword" >exit</span> <span class="hljs-variable" >$RETVAL</span>
</code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li><li >4</li><li >5</li><li >6</li><li >7</li><li >8</li><li >9</li><li >10</li><li >11</li><li >12</li><li >13</li><li >14</li><li >15</li><li >16</li><li >17</li><li >18</li><li >19</li><li >20</li><li >21</li><li >22</li><li >23</li><li >24</li><li >25</li><li >26</li><li >27</li><li >28</li><li >29</li><li >30</li><li >31</li><li >32</li><li >33</li><li >34</li><li >35</li><li >36</li><li >37</li><li >38</li><li >39</li><li >40</li><li >41</li><li >42</li><li >43</li><li >44</li><li >45</li><li >46</li><li >47</li><li >48</li><li >49</li><li >50</li><li >51</li><li >52</li><li >53</li><li >54</li><li >55</li><li >56</li><li >57</li><li >58</li><li >59</li><li >60</li><li >61</li><li >62</li></ul>

保存后,更改/etc/init.d/nginx的权限

<code class="hljs ruby has-numbering" >[root<span class="hljs-variable" >@kallen</span> ~]<span class="hljs-comment" ># chmod 755 /etc/init.d/nginx</span>
[root<span class="hljs-variable" >@kallen</span> ~]<span class="hljs-comment" ># chkconfig --add nginx</span>
[root<span class="hljs-variable" >@kallen</span> ~]<span class="hljs-comment" ># chkconfig nginx on</span></code><ul class="pre-numbering" ><li >1</li><li >2</li><li >3</li></ul>

】文中所有图片均来自Nginx官网.

github stack mail

相关内容

    暂无相关文章