centos6实现LNMP

来源:互联网 发布:徐老师淘宝店叫什么 编辑:程序博客网 时间:2024/06/05 23:31
nginx


client 192.168.50.10          //使用客户端测试网站时记得做解析

nginx/php server 192.168.50.20

mysql server 192.168.50.30


nginx功能
1. 高性能的HTTP Server,为了解决C10k的问题,支持高达20000并发访问
2. 反向代理服务器,给网站加速
3. 做为前端一个负载均衡器


nginx安装
yum -y install gcc gcc-c++ make openssl-devel pcre-devel
pcre-devel支持地址重写

安装包位置 mkdir /soft
cd /soft
tar xf nginx-1.8.0.tar.gz
cd nginx-1.8.0

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_sub_module --with-http_ssl_module --with-pcre

make
make install


关于编译安装参数详解

/configure --help

 

--help 显示本提示信息

 

--prefix=PATH 设定安装目录

 

--sbin-path=PATH 设定程序文件目录

 

--conf-path=PATH 设定配置文件(nginx.conf)目录

 

--error-log-path=PATH 设定错误日志目录

 

--pid-path=PATH 设定pid文件(nginx.pid)目录

 

--lock-path=PATH 设定lock文件(nginx.lock)目录

 

--user=USER 设定程序运行的用户环境(www)

 

--group=GROUP 设定程序运行的组环境(www)

 

--builddir=DIR 设定程序编译目录

 

--with-rtsig_module 允许rtsig模块

rtsig模块是一种实时信号,在Linux 2.2.19 默认情况下,实时信号连接数不超过1024,但是对于高负载是肯定不够的。因此通过调整内核参数/proc/sys/kernel/rtsig-max达到效果。但是linux 2.6.6-mm2开始,这个参数不再可用,并为每个进程有一个独立的信号队列,数字是由RLIMIT_SIGPENDING确定。当队列变得满载时,nginx开始抛弃连接并使用poll方法,直到负载恢复正常。

 

--with-select_module 允许select模块(一种轮询模式,不推荐用在高载环境)

 

--without-select_module 不使用select模块

标准连接模式。默认情况下自动编译方式。您可以启用或禁用通过使用-select_module和不带- select_module配置参数这个模块。

 

--with-poll_module 允许poll模块(一种轮询模式,不推荐用在高载环境)

标准连接模式。默认情况下自动编译方式。

 

--without-poll_module 不使用poll模块

 

--with-http_ssl_module 允许ngx_http_ssl_module模块(Apache对应:mod_ssl)

 

--with-http_realip_module 允许ngx_http_realip_module模块(mod_rpaf)

此模块支持显示真实来源IP地址,主要用于NGINX做前端负载均衡服务器使用。

 

--with-http_addition_module 允许ngx_http_addition_module模块(mod_layout)

游戏服务器不必安装,门户网站可以安装,有利于被搜索引擎收录页面信息。

 

--with-http_xslt_module 允许ngx_http_xslt_module模块

这个模块是一个过滤器,它可以通过XSLT模板转换XML应答。0.7.8后面版本才可以使用。

 

--with-http_sub_module 允许ngx_http_sub_module模块

这个模块可以能够在nginx的应答中搜索并替换文本。

 

--with-http_dav_module 允许ngx_http_dav_module模块(mod_dav)

为文件和目录指定权限,限制不同类型的用户对于页面有不同的操作权限

 

--with-http_flv_module 允许ngx_http_flv_module模块(mod_flvx)

这个模块支持对FLV(flash)文件的拖动播放。

 

--with-http_gzip_static_module 允许ngx_http_gzip_static_module模块(mod_dflate)。

这个模块在一个预压缩文件传送到开启Gzip压缩的客户端之前检查是否已经存在以“.gz”结尾的压缩文件,这样可以防止文件被重复压缩。

 

--with-http_random_index_module 允许ngx_http_random_index_module模块(mod_autoindex),从目录中选择一个随机主页

 

--with-http_stub_status_module 允许ngx_http_stub_status_module模块(mod_status)

这个模块可以取得一些nginx的运行状态,如果是工业状况,可以直接取消。

 

--without-http_charset_module 不使用ngx_http_charset_module模块

这个模块将在应答头中为"Content-Type"字段添加字符编码

 

--without-http_gzip_module 不使用ngx_http_gzip_module模块,文件压缩模式。

 

--without-http_ssi_module 不使用ngx_http_ssi_module模块,此模块处理服务器端包含文件(ssi)的处理.

 

--without-http_userid_module 不使用ngx_http_userid_module模块

The module ngx_http_userid_module gives out cookies for identification of clients

 

--without-http_access_module 不使用ngx_http_access_module模块

 

--without-http_auth_basic_module 不使用ngx_http_auth_basic_module模块

 

--without-http_autoindex_module 不使用ngx_http_autoindex_module模块

 

--without-http_geo_module 不使用ngx_http_geo_module模块

这个模块基于客户端的IP地址创建一些ngx_http_geoip_module变量,并与MaxMindGeoIP文件进行匹配,该模块仅用于0.7.63和0.8.6版本之后。但效果不太理想,对于城市的IP记录并不是特别准确,不过对于网站的来源访问区域的分析大致有一定参考性。

 

--without-http_map_module 不使用ngx_http_map_module模块

这个模块允许你分类或者同时映射多个值到多个不同值并储存到一个变量中,可用于页面跳转其他域名使用。

 

--without-http_referer_module 不使用ngx_http_referer_module模块

当一个请求头的Referer字段中包含一些非正确的字段,这个模块可以禁止这个请求访问站点。可以禁止盗链的情况发生。

 

--without-http_rewrite_module 不使用ngx_http_rewrite_module模块,跳转模块

 

--without-http_proxy_module 不使用ngx_http_proxy_module模块,代理模块

 

--without-http_fastcgi_module 不使用ngx_http_fastcgi_module模块

 

--without-http_memcached_module 不使用ngx_http_memcached_module模块

 

--without-http_limit_zone_module 不使用ngx_http_limit_zone_module模块

此模块可以限制并发连接,达到减少攻击的效果

 

 

--without-http_empty_gif_module 不使用ngx_http_empty_gif_module模块

这个模块在内存中保存一个能够很快传递的1×1透明GIF

 

--without-http_browser_module 不使用ngx_http_browser_module模块

识别客户端浏览器版本来制定计划。可以实现更加来源浏览器的版本访问不同页面的效果。

 

--without-http_upstream_ip_hash_module,不使用ngx_http_upstream_ip_hash_module模块

 

--with-http_perl_module 允许ngx_http_perl_module模块,这个模块允许nginx使用SSI调用perl或直接执行perl 

 

--with-perl_modules_path=PATH 设置perl模块路径

 

--with-perl=PATH 设置perl库文件路径

 

--http-log-path=PATH 设置access log文件路径

 

--http-client-body-temp-path=PATH 设置客户端请求临时文件路径

 

--http-proxy-temp-path=PATH 设置http proxy临时文件路径

 

--http-fastcgi-temp-path=PATH 设置http fastcgi临时文件路径

 

--without-http 不使用HTTP server功能,如果只是做代理服务器,可以不提供http服务

 

--with-mail 允许POP3/IMAP4/SMTP代理模块

 

--with-mail_ssl_module 允许ngx_mail_ssl_module模块

这个模块使得POP3/IMAP/SMTP可以使用SSL/TLS.配置已经定义了HTTP SSL模块,但是不支持客户端证书检测

 

--without-mail_pop3_module 不允许ngx_mail_pop3_module模块

 

--without-mail_imap_module 不允许ngx_mail_imap_module模块

 

--without-mail_smtp_module 不允许ngx_mail_smtp_module模块

 

--with-google_perftools_module 允许ngx_google_perftools_module模块(调试用)

这个模块可以启用google性能分析工具套件,模块在版本0.6.29增加。

 

--with-cpp_test_module 允许ngx_cpp_test_module模块

 

--add-module=PATH 允许使用外部模块,以及路径

 

--with-cc=PATH 设置C编译器路径

 

--with-cpp=PATH 设置C预处理路径

 

--with-cc-opt=OPTIONS 设置C编译器参数

 

--with-ld-opt=OPTIONS 设置连接文件参数

 

--with-cpu-opt=CPU 为指定CPU优化,可选参数有:

                                   pentium, pentiumpro, pentium3, pentium4,

                                   athlon, opteron, sparc32, sparc64, ppc64

 

--without-pcre 不使用pcre库文件,pcre是包括 perl 兼容的正规表达式库

 

--with-pcre=DIR 设定PCRE库路径

 

--with-pcre-opt=OPTIONS 设置PCRE运行参数

 

--with-md5=DIR 设定md5库文件路径

 

--with-md5-opt=OPTIONS 设置md5运行参数

 

--with-md5-asm 使用md5源文件编译

 

--with-sha1=DIR 设定sha1库文件路径

sha1:安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准(Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。当接收到消息的时候,这个消息摘要可以用来验证数据的完整性。在传输的过程中,数据很可能会发生变化,那么这时候就会产生不同的消息摘要。

 

--with-sha1-opt=OPTIONS 设置sha1运行参数

 

--with-sha1-asm 使用sha1源文件编译

 

--with-zlib=DIR 设定zlib库文件路径

很多程序中的压缩或者解压缩函数都会用到这个库


--with-zlib-opt=OPTIONS 设置zlib运行参数


--with-zlib-asm=CPU 使zlib对特定的CPU进行优化,可选参数:

                                   pentium, pentiumpro


--with-openssl=DIR 设定OpenSSL库文件路径
Openssl作为一个基于密码学的安全开发包,OpenSSL提供的功能相当强大和全面,囊括了主要的密码算法、常用的密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用。


--with-openssl-opt=OPTIONS 设置OpenSSL运行参数


--with-debug 允许调试日志




查看安装后目录结构
# tree /usr/local/nginx/
/usr/local/nginx/
|-- conf
|   |-- fastcgi.conf
|   |-- fastcgi.conf.default
|   |-- fastcgi_params
|   |-- fastcgi_params.default
|   |-- koi-utf
|   |-- koi-win
|   |-- mime.types
|   |-- mime.types.default
|   |-- nginx.conf
|   |-- nginx.conf.default
|   |-- scgi_params
|   |-- scgi_params.default
|   |-- uwsgi_params
|   |-- uwsgi_params.default
|   `-- win-utf
|-- html
|   |-- 50x.html
|   `-- index.html
|-- logs
`-- sbin
    `-- nginx
    
启动nginx

/usr/local/nginx/sbin/nginx


停止nginx

/usr/local/nginx/sbin/nginx -s stop


重启nginx
/usr/local/nginx/sbin/nginx -s reload

查看运行状态
netstat -tnlp |grep :80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      10627/nginx

添加至开机运行
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

测试
curl http://
curl http://本机ip       出现nginx网站即成功



Nginx LNMP环境

yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
libxml2 libxml2-devel libcurl libcurl-devel libxslt-devel openssl-devel

tar xf php-5.5.7.tar.gz

./configure \
--prefix=/usr/local/php \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-jpeg-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysql \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip

make && make install 

PHP编译安装详解

--prefix=/usr/local/php     指定 php 安装目录

--with-apxs2=/usr/local/apache/bin/apxs      整合 apache,apxs功能是使用mod_so中的LoadModule指令,加载指定模块到 apache,要求 apache 要打开SO模块

--with-config-file-path=/usr/local/php/etc            指定php.ini位置

--with-MySQL=/usr/local/mysql            mysql安装目录,对mysql的支持

--with-mysqli=/usr/local/mysql/bin/mysql_config            mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。 

--enable-safe-mode   打开安全模式 

--enable-ftp   打开ftp的支持 

--enable-zip   打开对zip的支持 

--with-bz2    打开对bz2文件的支持        

--with-jpeg-dir   打开对jpeg图片的支持 

--with-png-dir   打开对png图片的支持 

--with-freetype-dir   打开对freetype字体库的支持 

--without-iconv   关闭iconv函数,种字符集间的转换 

--with-libXML-dir   打开libxml2库的支持 

--with-XMLrpc    打开xml-rpc的c语言 

--with-zlib-dir   打开zlib库的支持 

--with-gd    打开gd库的支持 

--enable-gd-native-ttf   支持TrueType字符串函数库 

--with-curl    打开curl浏览工具的支持 

--with-curlwrappers    运用curl工具打开url流 

--with-ttf     打开freetype1.*的支持,可以不加了 

--with-xsl     打开XSLT 文件支持,扩展了libXML2库 ,需要libxslt软件 

--with-gettext     打开gnu 的gettext 支持,编码库用到 

--with-pear    打开pear命令的支持,PHP扩展用的 

--enable-calendar    打开日历扩展功能 

--enable-mbstring    多字节,字符串的支持 

--enable-bcmath    打开图片大小调整,用到zabbix监控的时候用到了这个模块

--enable-sockets     打开 sockets 支持

--enable-exif    图片的元数据支持 

--enable-magic-quotes    魔术引用的支持 

--disable-rpath    关闭额外的运行库文件 

--disable-debug    关闭调试模式 

--with-mime-magic=/usr/share/file/magic.mime      魔术头文件位置
 

CGI方式安装才用的参数

--enable-fpm                      

打上PHP-fpm 补丁后才有这个参数,CGI方式安装的启动程序

 

--enable-fastCGI                  

支持fastcgi方式启动PHP

 

--enable-force-CGI-redirect        

重定向方式启动PHP

 

--with-ncurses                    

支持ncurses 屏幕绘制以及基于文本终端的图形互动功能的动态库

--enable-pcntl                     freeTDS需要用到的,可能是链接mssql 才用到
 

mhash和mcrypt算法的扩展

--with-mcrypt                     算法

--with-mhash                      算法

以上函数库需要安装

--with-gmp  应该是支持一种规范

--enable-inline-optimization  优化线程

--with-openssl                     openssl的支持,加密传输时用到的

--enable-dbase                     建立DBA 作为共享模块

--with-pcre-dir=/usr/local/bin/pcre-config      perl的正则库案安装位置

--disable-dmalloc

--with-gdbm                     dba的gdbm支持

--enable-sigchild

--enable-sysvsem

--enable-sysvshm

--enable-zend-multibyte         支持zend的多字节

--enable-mbregex

--enable-wddx

--enable-shmop

--enable-soap



php-fpm配置文件(影响php处理php程序的性能,例如php进程数、最大连接数配置等,运维人员关注)
[root@centos php-5.5.7]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

php置文件(影响php代码,例如允许客户端最大上传文件的大小,php所支持的扩展功能例如是否可以连接MySQL、Memcache,程序员关注)
[root@centos php-5.5.7]# cp php.ini-production /usr/local/php/lib/php.ini

添加到init启动[可选]
[root@centos php-5.5.7]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@centos php-5.5.7]# chmod a+x /etc/rc.d/init.d/php-fpm
[root@centos php-5.5.7]# chkconfig --add php-fpm
[root@centos php-5.5.7]# chkconfig php-fpm on
[root@centos php-5.5.7]# service php-fpm start
Starting php-fpm  done

netstat -tnlp |grep :80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4408/nginx          
netstat -tnlp |grep :9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*                   LISTEN      1475/php-fpm  

二、整合Nginx和PHP
1. Nginx启用Fastcgi
[root@centos php-5.5.7]# vim /usr/local/nginx/conf/nginx.conf

方法一:使用TCP连接
#user  nobody;         //默认连接nginx的用户
worker_processes  1;          //工作进程数量,理论上与cpu数量相同,也可以auto

events {
    worker_connections  1024;        //最大连接数
}


http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request"'           //启用日志功能
            '$status $body_bytes_sent "$http_referer"'
            '"$http_user_agent" "$http_x_forwarded_for"';


    server {        
        listen 80;                //端口
        server_name www.b.org b.org;         //站点名称,域名
        index index.html index.php;        //可以读取的站点文件类型
        root b.org;        //站点文件存放位置,这里的b.org全目录是/usr/local/nginx/b.org
 
        error_log /usr/local/nginx/logs/b.org/err.log error;        //错误日志存放位置
        access_log /usr/local/nginx/logs/b.org/access.log main;        //访问日志存放位置

        location ~ \.php$ {              //链接php
            fastcgi_pass 127.0.0.1:9000;        //php位置:端口
            fastcgi_index index.php;           //php默认首页
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;                 //脚本文件请求路径
            include fastcgi_params;
        }
    }
}



方法二:使用socket连接
[root@centos ~]# vim /usr/local/php/etc/php-fpm.conf
;listen = 127.0.0.1:9000
listen = /dev/shm/php-fpm.sock
[root@centos ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@centos ~]# ll /dev/shm/php-fpm.sock
srw-rw-rw-. 1 root root 0 Sep 18 04:55 /dev/shm/php-fpm.sock

[root@centos ~]# vim /usr/local/nginx/conf/nginx.conf
去掉以下行的注释:
location ~ \.php$ {
            root                   html;
            fastcgi_pass       unix:/dev/shm/php-fpm.sock;
            fastcgi_index      index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include                fastcgi_params;
        }


2. 最终确认并重启
[root@centos ~]# /usr/local/nginx/sbin/nginx -s reload
[root@centos ~]# /etc/init.d/php-fpm restart
[root@centos ~]# netstat -tnlp |grep :80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*        LISTEN      17947/nginx         
[root@centos ~]# netstat -tnlp |grep :9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*        LISTEN      17961/php-fpm       


三、安装MySQL

mysql编译安装

1. 安装相应工具
[root@centos2 ~]# yum -y groupinstall 'Development tools'
[root@centos2 ~]# yum -y install cmake bison-devel ncurses-devel

2. 创建mysql用户
[root@centos2 ~]# useradd -u 2000 -s /sbin/nologin mysql

3. 解压mysql
[root@centos2 ~]# cd /usr/local/src/lamp
[root@centos2 lamp]# ls mysql*
[root@centos2 lamp]# tar -zxvf mysql-5.6.28.tar.gz

3. 编译前配置
[root@centos2 mysql-5.6.28]# cmake \
-DCMAKE_INSTALL_PREFIX=/yingyong/mysql \
-DMYSQL_DATADIR=/yingyong/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

-DCMAKE_INSTALL_PREFIX=/yingyong/mysql #安装路径
-DMYSQL_DATADIR=/yingyong/mysql/data #数据文件路径
-DSYSCONFDIR=/etc #配置文件my.cnf路径
-DWITH_MYISAM_STORAGE_ENGINE=1 #支持MyIASM引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 #支持InnoDB引擎
-DWITH_MEMORY_STORAGE_ENGINE=1 #支持Memory引擎
-DENABLED_LOCAL_INFILE=1 #允许从本地导入数据
-DWITH_PARTITION_STORAGE_ENGINE=1 #安装支持数据库分区
-DEXTRA_CHARSETS=all #安装所有的字符集

4. 编译并安装
[root@centos2 mysql-5.6.28]# make && make install

5. 复制启动脚本
[root@centos2 mysql-5.6.28]# cp support-files/mysql.server /etc/init.d/mysqld
[root@centos2 mysql-5.6.28]# chmod 755 /etc/init.d/mysqld
[root@centos2 mysql-5.6.28]# chkconfig --add mysqld

6. 复制配置文件
[root@centos2 mysql-5.6.28]# cp support-files/my-default.cnf /etc/my.cnf

7. 修改权限
[root@centos2 mysql-5.6.28]#chown -R root:mysql /yingyong/mysql
[root@centos2 mysql-5.6.28]#chown -R mysql /yingyong/mysql/data

8. 初始化数据库
[root@centos2 mysql-5.6.28]# cd /yingyong/mysql/
[root@centos2 mysql]# ./scripts/mysql_install_db --user=mysql

9. 启动服务
[root@centos2 mysql]# service mysqld start
[root@centos2 mysql]# chkconfig mysqld on

10. 发布库文件路径
[root@centos2 lib]# echo /yingyong/mysql/lib/ >> /etc/ld.so.conf
[root@centos2 lib]# ldconfig

11. 发布命令文件路径
# vi /etc/profile
在export PATH行之前,添加
PATH=/yingyong/mysql/bin:$PATH
# . /etc/profile


12.建立php用于连接mysql数据库的用户并授权允许远程登陆

root@((none))> grant all on bbs.* to phptest@'192.168.50.20' identified by '123';
Query OK, 0 rows affected (0.00 sec)


12.刷新

root@((none))> flush privileges;
Query OK, 0 rows affected (0.00 sec)



五、php程序测试
1. 测试php文件能否执行
[root@centos ~]# cd /usr/local/nginx/b.org/
[root@centos html]# rm -rf *
[root@centos html]# vim index.php
<?php
phpinfo();
?>

2. 测试php连接MySQL是否正常
[root@centos html]# vim index.php
<?php
$link=mysql_connect('192.168.50.30','phptest','123');
if ($link)
              echo "Successfuly";
else
              echo "Faile";
mysql_close();
?>

//测试访问本机ip/index.php和ip/index2.php



Nginx基本配置

注意:一个nginx.conf里只能有一个http,多个server。

vim /usr/local/nginx/conf/nginx.conf
worker_processes  2;             //启动的进程数(建议逻辑CPU数或auto)

events {
    worker_connections  10240;              //每个进程最大连接数
}

http {

server {
        listen       80;                                //监听的端口        
        server_name  www.a.com;    //站点名
        location / {
            root   html;                                //root指令指定网站主目录(相对于nginx的安装目录)
            index  index.html index.htm;    //默认主页
        }
    }
}

语法检查:
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重新加载nginx:
[root@tianyun ~]# /usr/local/nginx/sbin/nginx -s reload



Nginx虚拟主机

  基于域名:
    建立在nginx服务器本机上的多站点,通过不同域名访问
      1、建立站点目录及站点
        mkdir -p /nginx/youku
        echo "www.youku.com" > /nginx/youku/index.html
      2、修改配置文件
        vim /usr/local/nginx/conf/nginx.conf
        server {
          server_name www.youku.com youku.com;
          location / {
            root /nginx/youku;
            index index.html index.htm;
            }
        }

  基于IP:
    实现多个IP对应同一站点
          在eth0网卡设备上添加两个lP别名10.0.0.189和10.0.0.190,通过ifconfig和route命令来进行:

          [root@centos nginx]# ifconfig eth0:1 10.0.0.189 broadcast 10.255.255.255 netmask 255.0.0.0 up

          [root@centos nginx]# route add -host 10.0.0.189 dev eth0:1    

          [root@centos nginx]# ifconfig eth0:2 10.0.0.190 broadcast 10.255.255.255 netmask 255.0.0.0 up

          [root@centos nginx]# route add -host 10.0.0.190 dev eth0:2  


    从另外一台服务器Ping 10.0.0.189和10.0.0.190两个IP,如果能够Ping通,则证明配置无误。但是,通过ifconfig和route配置的IP别名在服 务器重启后会消失,不过可以将这两条ifconng和route命令添加到/etc/rc.local文件中,让系统开机时自动运行,以下是相关命令:
vi /etc/rc.local
在文件末尾增加以下内容,然后保存即可

          ifconfig eth0:1 10.0.0.189 broadcast 10.255.255.255 netmask 255.0.0.0 up

          route add -host 10.0.0.189 dev eth0:1    

          ifconfig eth0:2 10.0.0.190 broadcast 10.255.255.255 netmask 255.0.0.0 up

          route add -host 10.0.0.190 dev eth0:2  
      1、建立站点目录及站点(同上)
      2、修改配置文件
           vim /usr/local/nginx/conf/nginx.conf
            
             #第一个虚拟主机
                server {
                listen       10.0.0.133:80;               #监听的IP和端口
                server_name  10.0.0.133;              #主机名称

                access_log  logs/host1.access.log  main;                #访问日志文件存放路径

                location /
                           {
                        root /usr/local/nginx/html/host1;              #HTML网页文件存放的目录
                        index  index.html index.htm;                    #默认首页文件,顺序从左到右,如果找不到index.html文件,则查找index.htm文件作为首页文件
        }
}
             #第二个虚拟主机
                server {
                listen       10.0.0.189:80;
                server_name  10.0.0.189;

                access_log  logs/host2.access.log  main;

                location /
                           {
                        root /usr/local/nginx/html/host2;
                        index  index.html index.htm;
        }
}
             #第三个虚拟主机
                server {
                listen       10.0.0.190:80;
                server_name  10.0.0.190;

                access_log  logs/host3.access.log  main;

                location /
                          {
            root /usr/local/nginx/html/host3;
            index  index.html index.htm;
        }
}
  配置修改后重启nginx服务生效。
 
 
配置DNS服务器解析nginx (令起服务器)
  vim /etc/named.conf 下面两项改成any
    listen-on port 53 { any; };
    allow-query     { any; };
 
  vim /etc/named.rfc1912.zones
    zone "youku.com" IN {
    type master;
    file "youku.com.zone";
    };

  cp -a /var/named/named.localhost /var/named/youku.com.zone   //-a选项保留原有属性,注意文件属性必须是root:named
  vim /var/named/youku.com.zone
$TTL 86400
@    IN SOA     dns.c.com. root. (
                    12    ; serial
                    28800    ; refresh
                    14400    ; retry
                    3600000    ; expire
                    86400 )    ; minimum
@    IN NS    dns.c.com.
dns     A     192.168.50.30             //dns本机IP
www    A    192.168.50.3               //nginx服务器      //注意!顶头的五行没有空格!!

客户端配置指向dns
  vim /etc/resolv.conf
    nameserver 192.168.50.30       //指向dns服务器,指向后无法解析重启网卡
  重启后resolv.conf配置丢失解决
    chkconfig NetworkManager off
    service NetworkManager stop
    service network start
    service network restart
    

nginx访问控制

  一、基于用户的访问控制
1. 建立口令文件
[root@centos ~]# htpasswd -cm /usr/local/nginx/passwd user10  //-c新建一个加密文件 //-m使用md5加密方式  
[root@centos ~]# htpasswd -m /usr/local/nginx/passwd user20
[root@centos ~]# cat /usr/local/nginx/passwd
user10:$apr1$Cw6eF/..$MNBh6rvkvsfH9gDZ/kEhg/
user20:$apr1$tb6B8...$y28sfvudhfb4V8xPlvvi//

2. 实现认证
[root@centos ~]# vim /usr/local/nginx/conf/nginx.conf
server {
        server_name  www.baidu.com baidu.com;
        location / {
            root   /nginx/baidu;
            index  index.html index.htm;
            auth_basic "nginx access test!";                     //添加行
            auth_basic_user_file /usr/local/nginx/passwd;           //添加行
        }
    }

3. 访问测试
[root@centos ~]# /usr/local/nginx/sbin/nginx -s reload
[root@centos ~]# links www.baidu.com    //测试使用user10或者user20都可以访问

如果其他网站也要添加则新建加密文件即可
htpasswd -cm /usr/local/nginx/新加密文件名称 用户   //回车输入密码,按上述添加至指定位置即可


二、基于主机的访问控制
server {
        listen       192.168.10.133:80;
        server_name  www.baidu.com baidu.com;
        location / {
            root   /nginx/baidu;
            index  index.html index.htm;
            allow 192.168.10.240;             //允许
            allow 192.168.228.0/24;             //允许
            deny all;               //其余全部拒绝
        }
    }
    
    
    
    
nginx查看状态

  条件:
    编译安装时必须有--with-http_stub_status_module

  配置:
    vim /usr/local/nginx/conf/nginx.conf
    添加:
    server {
    listen 80;
    server_name www.a.com a.com;
    location / {
        root /nginx/a;
        index index.html index.htm;
        auth_basic "password";
        auth_basic_user_file /usr/local/nginx/a;
    location /nginx_status {         //添加行
        stub_status on;                //添加行
        allow all;                    //添加行
          }
        }
     }
    保存重启nginx
    通过网页访问http://www.a.com/nginx_status  (需在图像化下显示)
    Active connections: 22                     
    server accepts handled requests    
    27 27 434                                     
    Reading: 2 Writing: 1 Waiting: 19
 
    Active connections: 22    当前活动的连接数

    server accepts handled requests    
    27 27 434
    27        从启动到现在一共处理的连接数
    27        从启动到现在成功创建的握手的次数
    434         总共处理的请求数(requests)
    失败连接=(总连接数-成功连接数)

    connection   连接数,tcp连接
    request            http请求,GET/POST/DELETE

    Reading: 2 Writing: 1 Waiting: 19
    Reading: 2     读取客户端Header的信息数   请求头
    Writing: 1        返回给客户端的header的信息数        响应头
    Waiting: 19    等待的请求数


    
upstream模块实现负载均衡
  vim /usr/local/nginx/conf/nginx.conf
  添加:
    http {        //在http下添加下述模块
    upstream tianyun_lb {       //模块名称
    ip_hash;        //添加此行则访问时固定访问一个后端服务器,解决session问题,去掉此条会采用轮询访问后端服务器,比如客户端访问一个带账                          号密码的页面,如不不加此行,则会在访问时轮询跳转重复要求输入账号密码
        server 192.168.50.66:80;            //后端服务器
        server 192.168.50.50:80;            //后端服务器
        }
    
    server {
            listen       80;
            server_name  www.tianyun.com;       //站点名称
    
            location / {
                root   html;
                index index.php index.html index.htm;
                proxy_pass http://tianyun_lb;           //引用upstream模块
                proxy_set_header Host $host;         //表明请求的主机名
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                  root   html;
              }
          }
        }
    }
    
    
php状态功能

  vim /usr/local/php/etc/php-fpm.conf
    去掉;pm.status_path = /status的;
  vim /usr/local/nginx/conf/nginx.conf
    server {
    server_name www.c.com c.com;
    location / {
        root /nginx/c;
        index index.html index.html;
      }
    location ~ ^/status$           //从此行以下为添加行
        {
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
          include fastcgi_params;
      }
    }
  检查nginx配置,重启nginx
  重启php:  service php-fpm restart
  测试:http://www.c.com/status
  显示以下内容为成功:
    pool:                 www       //php运行的组
    process manager:      dynamic          //php-fpm运行的方式
    start time:           24/Feb/2017:17:16:53 +0800         开始时间
    start since:          126
    accepted conn:        1            //接受链接
    listen queue:         0            //监听队列
    max listen queue:     0            //最大监听队列
    listen queue len:     128        //监听队列len
    idle processes:       1            //空闲进程
    active processes:     1            //活动进程
    total processes:      2            //总进程
    max active processes: 1            //最大活动进程
    max children reached: 0            //最大的子进程达到
    slow requests:        0