首页  >  文章  >  后端开发  >  LNMP环境搭建——Nginx篇

LNMP环境搭建——Nginx篇

WBOY
WBOY原创
2016-08-08 09:24:05952浏览

1.Nginx配置文件测试

<code>root<span>@kallen</span><span>:/usr/local/nginx/sbin</span><span># nginx -t </span><span>nginx:</span> the configuration file /etc/nginx/nginx.conf syntax is ok 
<span>nginx:</span> configuration file /etc/nginx/nginx.conf test is successful</code>

2.Nginx启动

<code>[root<span>@kallen</span> ~]<span># /usr/local/nginx/sbin/nginx </span></code>

3.Nginx负载均衡

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>[root<span>@kallen</span> ~]<span># cd /usr/local/src/</span></code>
<code>[root<span>@kallen</span> ~]<span># wget http://syslab.comsenz.com/downloads/linux/nginx-0.9.6.tar.gz</span>
[root<span>@kallen</span> ~]<span># tar zxvf nginx-0.9.6.tar.gz</span>
[root<span>@kallen</span> ~]<span># cd nginx-0.9.6</span></code>
<code>./configure --prefix=/usr/local/nginx 
-<span>-sbin-path=<span>/usr/local</span><span>/nginx/sbin</span><span>/nginx 
</span></span>-<span>-conf-path=<span>/usr/local</span><span>/nginx/conf</span><span>/nginx.conf 
</span></span>-<span>-error-log-path=<span>/usr/local</span><span>/nginx/logs</span><span>/error.log 
</span></span>-<span>-http-log-path=<span>/usr/local</span><span>/nginx/logs</span><span>/access.log 
</span></span>-<span>-pid-path=<span>/usr/local</span><span>/nginx/var</span><span>/nginx.pid 
</span></span>-<span>-lock-path=<span>/usr/local</span><span>/nginx/var</span><span>/nginx.lock 
</span></span>-<span>-http-client-body-temp-path=<span>/dev/shm</span><span>/nginx_temp/client</span>_body 
</span>-<span>-http-proxy-temp-path=<span>/dev/shm</span><span>/nginx_temp/proxy</span></span>-<span>-http-fastcgi-temp-path=<span>/dev/shm</span><span>/nginx_temp/fastcgi</span></span>-<span>-user=www --group=www 
</span>-<span>-with-cpu-opt=pentium4F 
</span>-<span>-without-select_module 
</span>-<span>-without-poll_module 
</span>-<span>-with-http_realip_module 
</span>-<span>-with-http_sub_module 
</span>-<span>-with-http_gzip_static_module 
</span>-<span>-with-http_stub_status_module 
</span>-<span>-without-http_ssi_module 
</span>-<span>-without-http_userid_module 
</span>-<span>-without-http_geo_module 
</span>-<span>-without-http_memcached_module 
</span>-<span>-without-http_map_module 
</span>-<span>-without-mail_pop3_module 
</span>-<span>-without-mail_imap_module 
</span>-<span>-without-mail_smtp_module 
</span>-<span>-with-pcre=<span>/usr/local</span><span>/src/pcre</span>-<span>8.32</span>/ 
</span>-<span>-with-zlib=<span>/usr/local</span><span>/zlib</span></span></code>
<code>[root<span>@kallen</span> ~]<span># make && make install </span>
[root<span>@kallen</span> ~]<span># mkdir /dev/shm/nginx_temp</span></code>

有的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><span>apt</span><span>-</span><span>get</span><span>-</span><span>-</span><span>install</span><span>-</span><span>suggests</span><span>install</span><span>gcc</span><span>g</span><span>+</span><span>+</span><span>make</span></code>

b. 下载相关软件

<code>wget http://jaist<span>.dl</span><span>.sourceforge</span><span>.net</span>/project/pcre/pcre/<span>8.35</span>/pcre-<span>8.35</span><span>.tar</span><span>.gz</span>
wget http://zlib<span>.net</span>/zlib-<span>1.2</span><span>.8</span><span>.tar</span><span>.gz</span>
wget http://www<span>.openssl</span><span>.org</span>/source/openssl-<span>1.0</span><span>.1</span>g<span>.tar</span><span>.gz</span>
wget http://www<span>.canonware</span><span>.com</span>/download/jemalloc/jemalloc-<span>3.6</span><span>.0</span><span>.tar</span><span>.bz</span>2
wget http://tengine<span>.taobao</span><span>.org</span>/download/tengine-<span>2.0</span><span>.2</span><span>.tar</span><span>.gz</span></code>

c. 安装Pcre

<code>  tar zxvf pcre-<span>8.35</span><span>.tar</span><span>.gz</span>
  cd pcre-<span>8.35</span>
 ./configure --prefix=/usr/local/pcre-<span>8.35</span>
  make && make install</code>

d. 安装Zlib

<code>  tar zxvf zlib-<span>1.2</span><span>.8</span><span>.tar</span><span>.gz</span>
  cd zlib-<span>1.2</span><span>.8</span>
  ./configure --prefix=/usr/local/zlib-<span>1.2</span><span>.8</span>
  make && make install</code>

[ERROR]-1:

<code>./configure: error: <span>the</span> HTTP gzip module requires <span>the</span> zlib library. 
You can either disable <span>the</span> module <span>by</span><span>using</span><span>--without-http_gzip_module </span>
option, <span>or</span> install <span>the</span> zlib library <span>into</span><span>the</span><span>system</span>, <span>or</span> build <span>the</span> zlib library 
statically <span>from</span><span>the</span> source <span>with</span> nginx <span>by</span><span>using</span><span>--with-zlib=<path> opti</path></span></code>

[ERROR]-2:

<code>configure: error: You need a C<span>++ compiler for C+</span>+ support.
make[1]: *** [/usr/local/src/pcre-8.32/Makefile] Error 1
make[1]: Leaving directory <span>`/home/kallen/MyDOC/nginx-1.8.0'</span>
make: *** [build] Error 2</code>

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

<code>[Nginx <span>Configuration</span> Summary]
<span>Configuration</span> summary
+ using PCRE <span>library</span>: /usr/local/src/pcre-<span>8.32</span>
+ OpenSSL <span>library</span><span>is</span><span>not</span> used
+using builtin md5 code
+ sha1 <span>library</span><span>is</span><span>not</span> found
+ using zlib <span>library</span>: /usr/local/zlib
  nginx path prefix:<span>"/usr/local/nginx"</span>
  nginx binary <span>file</span>:<span>"/usr/local/nginx/sbin/nginx"</span>
  nginx <span>configuration</span> prefix:<span>"/usr/local/nginx/conf"</span>
  nginx <span>configuration</span><span>file</span>:<span>"/usr/local/nginx/conf/nginx.conf"</span>
  nginx pid <span>file</span>:<span>"/usr/local/nginx/var/nginx.pid"</span>
  nginx error log <span>file</span>:<span>"/usr/local/nginx/logs/error.log"</span>
  nginx http <span>access</span> log <span>file</span>:<span>"/usr/local/nginx/logs/access.log"</span>
  nginx http client request <span>body</span> temporary files:<span>"/dev/shm/nginx_temp/client_body"</span>
  nginx http proxy temporary files:<span>"/dev/shm/nginx_temp/proxy"</span>
  nginx http fastcgi temporary files:<span>"/dev/shm/nginx_temp/fastcgi"</span>
  nginx http uwsgi temporary files:<span>"uwsgi_temp"</span>
  nginx http scgi temporary files:<span>"scgi_temp"</span></code>

(2) 编写nginx启动脚本

<code>[root<span>@kallen</span> init.d]<span># vi /etc/init.d/nginx</span></code>

写入以下内容:

<code><span>#!/bin/bash</span><span>#        </span><span># Startup script for the Nginx HTTP Server</span><span>#</span><span># Kallen Ding, Apr 23 2015</span>NGINX_PATH=<span>"/usr/local/nginx/"</span>
OPTIONS=<span>"-c <span>${NGINX_PATH}</span>conf/nginx.conf"</span>
prog=nginx
nginx=<span>${NGINX_PATH}</span>sbin/nginx
pidfile=/var/run/nginx.pid

<span># Source function library.</span>
. /etc/rc.d/init.d/functions

<span><span>start</span></span>() {
    <span>echo</span> -n <span>"Starting <span>$prog</span>: "</span>
    daemon --pidfile=<span>${pidfile}</span><span>$nginx</span><span>$OPTIONS</span>
    RETVAL=$?
    <span>echo</span><span>return</span><span>$RETVAL</span>
}

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

<span># See how we were called.</span><span>case</span><span>"<span>$1</span>"</span><span>in</span>
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  reload)
        reload
    ;;
  status)
    status <span>$prog</span>
    RETVAL=$?
    ;;
  *)
    <span>echo</span><span>"Usage: <span>$prog</span> {start|stop|restart|reload|status}"</span>
    RETVAL=<span>3</span><span>esac</span><span>exit</span><span>$RETVAL</span></code>

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

<code>[root<span>@kallen</span> ~]<span># chmod 755 /etc/init.d/nginx</span>
[root<span>@kallen</span> ~]<span># chkconfig --add nginx</span>
[root<span>@kallen</span> ~]<span># chkconfig nginx on</span></code>

以上就介绍了LNMP环境搭建——Nginx篇,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn