wsaer 发表于 2018-11-14 11:15:04

nginx和lnmp架构

  nginx的概述及常见Web服务器的介绍:
  Unix和Linux平台下的常用Web服务器有Apache、 Nginx、 Lighttpd、 Tomcat、 IBM WebSphere等。其中目前应用最广泛的Web服务器是Apache。 Windows平台下最常用的服务器则是微软公司的IIS(Internet Information Server)。
  1)Apache服务器
  Apache 起初由 Illinois 大学 Urbana-Champaign 的国家高级计算程序中心开发。此后Apache 被开放源代码团体的成员不断的发展和加强。1996年4月以来,Apache一直是Internet上最流行的HTTP服务器,1999年5月它在 57% 的网页服务器上运行,到了2005年7月这个比例上升到69%。Apache是目前世界上用的最多的Web服务器,它有优势主要在于源代码开放、有一支开放的开发队伍、支持跨平台的应用(可以运行在几乎所有的Unix、Linux、Windows系统平台之上)。Apache的模块支持非常丰富,以至于它提供了非常完善的功能。
  Apache的官方网站: http://www.apache.org
  2)Lighttpd 服务器
  Lighttpd是一个德国人领导的开源软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的Web server环境。具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点。Lighttpd是众多OpenSource轻量级的Web server中较为优秀的一个。支持FastCGI, CGI, Auth, 输出压缩(output compress), URL重写, Alias等重要功能,而Apache之所以流行,很大程度也是因为功能丰富,在Lighttpd 上很多功能都有相应的实现了,这点对于Apache的用户是非常重要的,因为迁移到Lighttpd就必须面对这些问题。
  Lighttpd的官方网站: http://www.lighttpd.net
  3)Tomcat服务器
  Tomcat是一个免费的开源的Serlvet容器,它是Apache基金会的Jakarta项目中的一个核心项目,由Apache、Sun和其它一些公司及个人共同开发而成。由于有了Sun的参与和支持,最新的Servlet和Jsp规范总能在Tomcat中得到体现。Tomcat即是一个Jsp和Servlet的运行平台。同时Tomcat又不仅仅是一个Servlet容器,它也具有传统的Web服务器的功能:处理Html页面,但是与Apache相比,它的处理静态Html的能力就不如Apache,我们可以将Tomcat和Apache集成到一块,让Apache处理静态Html,而Tomcat处理Jsp和Servlet。这种集成只需要修改一下Apache和Tomcat的配置文件即可。
  Tomcat的官方网站: http://tomcat.apache.org
  4)IBM WebSphere (weblogic/jboss)
  WebSphere Application Server 是 一 种功能完善、开放的Web应用程序服务器,是IBM电子商务计划的核心部分。基于Java和Servlets的Web应用程序运行环境,包含了为Web站点提供服务所需的一切,运 行 时 可 以 协 同 并 扩 展Apache、Netscape、 IIS 和IBM 的HTTPWeb服务器,因此可以成为强大的Web应用服务器。
  WebSphere的官方网站: http://www.ibm.com
  5)Windows IIS
  IIS 是Internet Information Server的缩写,它是微软公司主推的服务器。IIS与Window NT Server完全集成在一起,因而用户能够利用Windows NT Server和NTFS(NT File System,NT的文件系统)内置的安全特性,建立强大,灵活而安全的Internet和Intranet站点。 IIS支持HTTP(Hypertext Transfer Protocol,超文本传输协议),FTP(File Transfer Protocol,文件传输协议)以及SMTP协议,通过使用CGI和ISAPI,IIS可以得到高度的扩展。
  IIS的官方网站: http://www.iis.net
  6)Nginx的介绍
  Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP/FTP 代理服务器。Nginx是由俄罗斯人 Igor Sysoev为俄罗斯访问量第二的 Rambler.ru站点开发的,它已经在该站点运行超过两年半了。Igor Sysoev在建立的项目时,使用基于BSD许可。自Nginx 发布四年来,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
  在俄罗斯许多大网站都已经使用它, 且一直表现不凡。截至2007年4月,俄罗斯大约有20%左右的虚拟主机是由nignx服务或代理的。Google在线安全博客中统计Nginx服务或代理了大约所有Internet虚拟主机的4%。而Netcraft的统计显示,Nginx服务的主机在过去的一年里以四倍的速度增长并且在这几年里,它的排名还在不断上升,下图为Netcraft截止至2012年1月的统计。 (http://news.netcraft.com/)
  nginx安装和启动
  安装pcre
  安装openssl
  安装zlib
  安装nginx
  yum -y install gcc openssl-devel pcre-devel zlib-devel
  yum -y install nginx
  了解软件目录结构
  # find /opt/nginx/ -maxdepth 1 -type d
  /opt/nginx/
  /opt/nginx/conf配置文件
  /opt/nginx/logs日志
  /opt/nginx/html页面家目录
  /opt/nginx/sbin二进制命令
  # find /opt/nginx/
  /opt/nginx/
  /opt/nginx/conf
  /opt/nginx/conf/win-utf
  /opt/nginx/conf/uwsgi_params.default--模板
  /opt/nginx/conf/fastcgi_params.default
  /opt/nginx/conf/mime.types---识别类型
  /opt/nginx/conf/nginx.conf---主配置文件
  /opt/nginx/conf/uwsgi_params--内部变量
  /opt/nginx/conf/scgi_params.default
  /opt/nginx/conf/fastcgi_params   --PHP相关
  /opt/nginx/conf/scgi_params--程序
  /opt/nginx/conf/koi-utf---编码
  /opt/nginx/conf/fastcgi.conf ---fastcgi配置文件
  /opt/nginx/conf/mime.types.default
  /opt/nginx/conf/fastcgi.conf.default
  /opt/nginx/conf/koi-win
  /opt/nginx/conf/nginx.conf.default
  /opt/nginx/logs
  /opt/nginx/html
  /opt/nginx/html/index.html---首页
  /opt/nginx/html/50x.html    ---错误代码
  /opt/nginx/sbin
  /opt/nginx/sbin/nginx--二进制命令
  nginx启动
  注意 关闭占用80端口的程序
  # /opt/nginx/sbin/nginx
  # netstat -tnlp |grep nginx
  # ps aux |grep nginx
  平滑方式关闭(正常退出服务)
  # pkill -15 nginx
  直接关闭
  # pkill -9 nginx
  完善环境变量
  # echo "export PATH=$PATH:/opt/nginx/sbin" >> ~/.bashrc
  # which nginx
  /opt/nginx/sbin/nginx
  定制启动脚本
  # cp /root/lnmp/nginx.init.d /etc/rc.d/init.d/nginx
  # chmod +x /etc/rc.d/init.d/nginx
  # chkconfig nginx on
  # service nginx restart
  nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
  Stopping nginx:                                          
  Starting nginx:                                          
  检测配置文件的语法
  # nginx -t
  nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
  # cat /opt/nginx/conf/nginx.conf |grep -v '^$'|grep -v '.*#'
  usernginx nginx;
  worker_processes2;
  error_loglogs/error.loginfo;
  pid      logs/nginx.pid;
  events {
  use epoll;
  worker_connections2048;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_user_agent" "$www.abc.com" >> /etc/hosts
  elinks http://www.abc.com
  Nginx 网站服务配置示例
  添加 nginx 状态统计
  Nginx下的虚拟主机配置
  访问控制
  +++nginx 正则表达式+++
  location的匹配顺序:
  1、=   严格匹配这个查询,如果匹配到,则停止搜索
  2、~    匹配正则表达式,区分大小写
  3、~*   匹配正则表达式,不区分大小写
  4、!~*取反
  使用正则表达式,区分大小写
  # vim /opt/nginx/conf/nginx.conf
  location ~ ^/a\.html$ {
  root /test1;
  indexindex.html;
  }
  # mkdir /test1
  # echo 'this is test1 dir Test page' > /test1/a.html
  # elinks -dump http://192.168.0.1/a.html
  this is test1 dir Test page
  使用正则表达式,不区分大小写
  location ~* ^/Status {
  stub_status on;
  }
  # elinks -dump http://192.168.0.1/status
  # elinks -dump http://192.168.0.1/STATUS
  精确匹配
  # vim /opt/nginx/conf/nginx.conf
  location = /a.html {
  root /test2;
  index index.html;
  }
  # mkdir /test2
  # echo 'this is test2 dir Test page' > /test2/a.html
  结论
  等号(=)与( / )的对比,结论:等号的优先级别比( / )的高,(其实等号的优先级别是最高的)
  正则表达式的优先级别仅次于等号
  如果正则表达式之间有冲突,则按照最详细的正则表达式优先匹配
  +++Nginx下的虚拟主机配置+++
  基于端口
  基于IP
  基于域名
  -----------------基于端口---------------------
  server {
  listen       80;
  #server_namewww.abc.com;
  charset utf-8;
  access_loglogs/www.abc.access.logmain;
  location / {
  root   /www1;
  indexindex.html index.htm;
  }
  location ~* ^/Status {
  stub_status on;
  }
  }
  server {
  listen       8000;
  #server_namewww.abc.com;
  charset utf-8;
  access_loglogs/www.abc.access.logmain;
  location / {
  root   /www2;
  indexindex.html index.htm;
  }
  location ~* ^/Status {
  stub_status on;
  }
  }
  }
  # mkdir /www{1..2} -p
  # echo www1 > /www1/index.html
  # echo www2 > /www2/index.html
  # service nginx restart
  # netstat -tnlp |grep nginx
  tcp      0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7201/nginx
  tcp      0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      7201/nginx
  # elinks -dump http://192.168.0.1:80
  # elinks -dump http://192.168.0.1:8000
  -----------------基于IP地址---------------------
  # ifconfig eth0 192.168.0.X1 up
  # ifconfig eth0:0 192.168.0.X2 up
  # vim /opt/nginx/conf/nginx.conf
  listen       192.168.0.X1:80;
  listen       192.168.0.X2:80;
  # service nginx restart
  # netstat -tnlp |grep nginx
  tcp      0      0 192.168.0.10:80             0.0.0.0:*                   LISTEN      7283/nginx
  tcp      0      0 192.168.0.1:80            0.0.0.0:*                   LISTEN      7283/nginx
  测试:
  # elinks -dump http://192.168.0.1
  www1
  # elinks -dump http://192.168.0.10
  www2
  # mkdir /www{1..2} -p
  # echo www1 > /www1/index.html
  # echo www2 > /www2/index.html
  -------------基于域名------------------------
  www.abc.com 192.168.0.1
  www.bcd.com 192.168.0.1
  ---------------
  listen       80;
  server_namewww.abc.com;
  listen       80;
  server_namewww.bcd.com;
  --------------
  测试过程
  # echo "192.168.0.1 > /etc/hosts
  # echo "192.168.0.1 > /etc/hosts
  # elinks -dump http://www.abc.com
  www1
  # elinks -dump http://www.bcd.com
  www2
  访问控制
  目录的访问控制:
  1、根据账号密码进行访问控制
  2、根据客户端的源地址进行访问控制
  3、结合正则表达式和文件类型做访问控制
  # vim /opt/nginx/conf/nginx.conf
  location ~* ^/Status {
  stub_status on;
  auth_basic "status login";
  auth_basic_user_file"/opt/nginx/conf/.htpasswd";
  }
  设置用户与密码
  # vim /opt/nginx/conf/.htpasswd
  # grub-md5-crypt
  Password:
  Retype password:
  $1$3eyLv$FRqUzfIiHgGJb00B0l0M4/
  # vim /opt/nginx/conf/.htpasswd
  user01:$1$3eyLv$FRqUzfIiHgGJb00B0l0M4/
  # service nginx restart
  # elinkshttp://192.168.0.1/status
  ++++根据客户端的源地址进行访问控制++++
  location ~* ^/abc/save\.txt$ {
  root /www1;
  index index.html;
  allow 192.168.0.0/24;
  }
  # mkdir /www1/abc
  # echo "&*&*(" > /www1/abc/save.txt
  # elinkshttp://www.abc.com/abc/save.txt
  允许某一个网段/拒绝某一个IP地址
  location ~* ^/abc/save\.txt$ {
  root /www1;
  index index.html;
  deny 192.168.0.254;
  allow 192.168.0.0/24;
  }
  # elinks -dumphttp://www.abc.com/abc/save.txt
  403 Forbidden
  结合正则表达式和文件类型做访问控制
  server {
  listen       80;
  server_namewww.bcd.com;
  charset utf-8;
  access_loglogs/www.abc.access.logmain;
  location ~* \.(txt|doc) {
  root /www2;
  index index.html;
  deny all;
  }
  }
  # echo deny_test1 > /www2/a1.txt
  # echo deny_test2 > /www2/a1.doc
  # elinks -dumphttp://www.bcd.com/a1.txt
  # elinks -dumphttp://www.bcd.com/a2.doc
  拒绝访问某一个目录
  location ~* ^/install {
  root /www2;
  index index.html;
  deny all;
  }
  # mkdir install
  # cp /etc/passwd install/
  # elinks -dumphttp://www.bcd.com/install/passwd
  403 Forbidden
  禁止单个目录
  location= /searchword/cron/ {
  deny all;
  }
  配置Nginx实现https
  443 --- https
  httpd ssl(openssl)
  证书的颁发机构 CA
  /\
  私钥   证书(公钥)
  # cd /etc/pki/tls/certs/
  # make server.key--生成私钥
  # openssl rsa -in server.key -out server.key --去除私钥的加密密码
  # make server.csr--生成证书颁发机构(CA)
  # openssl x509 -in server.csr -req-signkey server.key -days 365 -out server.crt--生成证书
  1 CA机构 获取私钥与公钥
  2 发布页面的时候填写私钥与公钥
  3 对外发布https:443
  # vim /opt/nginx/conf/nginx.conf
  server {
  listen       443;
  server_namewww.cde.com;
  ssl                  on;
  ssl_certificate      /etc/pki/tls/certs/server.crt;
  ssl_certificate_key/etc/pki/tls/certs/server.key;
  ssl_session_timeout5m;
  ssl_protocolsSSLv2 SSLv3 TLSv1;
  ssl_ciphersHIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers   on;
  location / {
  root   /www3;
  indexindex.html index.htm;
  }
  }
  # service nginx restart
  # mkdir /www3
  # echo www3 > /www3/index.html
  测试
  # echo "192.168.0.1 > /etc/hosts
  https://www.cde.com/
  整合LNMP
  ++++++++++++++++++编译php++++++++++++++++++++
  # yum -y install libxml2-devel
  # tar xf /root/lnmp/php-5.5.3.tar.bz2 -C /usr/local/src/
  # cd /usr/local/src/php-5.5.3/
  # ./configure --prefix=/opt/php5 --with-config-file-path=/opt/php5--enable-fpm --with-mysql
  --prefix=/opt/php5
  --with-config-file-path=/opt/php5
  --enable-fpm (--enable-fastcgi旧版本的选项新的版本选项 --enable-fpm)
  --with-mysql
  #make && make install
  配置PHP的fpm方式
  1) 生成php的主配置文件
  # cp php.ini-production /opt/php5/php.ini
  fpm默认配置文件与二进制命令php-fpm
  # find /opt/php5/|grep fpm |tail -2
  /opt/php5/etc/php-fpm.conf.default
  /opt/php5/sbin/php-fpm
  2) 生成php-fpm的配置文件
  # cd /opt/php5/etc/
  # cp php-fpm.conf.default php-fpm.conf
  3)启动与关闭
  启动
  # /opt/php5/sbin/php-fpm -c /opt/php5/etc/php-fpm.conf
  # netstat -tnlp |grep php
  tcp      0      0 127.0.0.1:9000            0.0.0.0:*                   LISTEN      19345/php-fpm
  # ps aux |grep php
  关闭
  # pkill -9 php
  4) 设置php-fpm
  # vim /opt/php5/etc/php-fpm.conf
  
  user = nobody
  group = nobody
  listen = 192.168.0.1:9000
  pm = dynamic ---动态
  pm.max_children = 10最大开启10个
  pm.start_servers = 4默认启动开启4个
  pm.min_spare_servers = 2 空闲最小开启1个
  pm.max_spare_servers = 8 空闲最大开启2个
  # echo "/opt/php5/sbin/php-fpm -c /opt/php5/etc/php-fpm.conf" >> /etc/rc.local
  # source/etc/rc.local
  # ps aux |grep php |grep pool |wc -l--四个进程
  # netstat -tnlp |grep php
  tcp      0      0 192.168.0.1:9000            0.0.0.0:*                   LISTEN      19438/php-fpm
  整合lamp
  上传论坛 BBS discuz
  # mkdir /webserver/bbs01
  # unzip Discuz_X3.1_SC_UTF8.zip
  # mv upload/* /webserver/bbs01/
  发布页面
  # vim /opt/nginx/conf/nginx.conf
  usernginx nginx;
  worker_processes2;
  error_loglogs/error.loginfo;
  pid      logs/nginx.pid;
  events {
  use epoll;
  worker_connections1024;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_user_agent" "$ create database bbs01 default charset utf8;

  mysql> grant all on bbs01.* to 'runbbs01'@'localhost'>  # cd /webserver/bbs01/
  # chmod 777 data/ uc_client/ uc_server/ config/ -R
  http://bbs01.uplooking.com/install

页: [1]
查看完整版本: nginx和lnmp架构