neversoft 发表于 2018-11-21 07:24:05

CentOS上如何把Web服务器从Apache换到nginx-zhuzhaogua

  码农日记原创,转载请注明出处并给出原文链接! http://www.androiddev.net/webserver-apache-to-nginx/
  我的网站在阿里云服务器上, 1G内存的配置,但用了一段时间,感觉Apache有些撑不出,想了想,还是换到目前最热门的Nginx吧。 这里把整个过程记录下来,与大家共享。
  Nginx简介:
  Nginx是一个高性能的HTTP服务器和反向代理服务器, 最大的优点是节省资源,适用于处理高并发的请求。
  1. Nginx最初是按照反向代理设计的,和Apache不同, nginx关心如何处理url,而不是文件!
  2.Apache 是个基于进程处理的web服务器,如果同时有多个请求,必须要启动多个进程来处理。 这样在高负载的情况下,资源的消耗和响应的速度都会有很大的问题。 而Nginx是个基于事件(event)的异步处理模式, 下面是Nginx的一个简单的示意图,有一个Master进程,Maste进程负责系统配置,管理socket,以及管理一个或是多个Worker进程。而Worker进程接收和处理来自用户(浏览器)的请求。一般来讲,一个worker进程可以同时处理上千个用户的连接请求。每个worker进程采用异步的,基于event的方式来处理用户的请求。对于HTML的静态页面,Nginx会自行来处理,但对于PHP,JSP, Python等动态页面,Nginx是通过FastCGI(或者SCGI,UWSGI)来把动态页面的请求交给相应的处理程序来处理。
  nginx
  安装Nginx
  需要注意的是,在CentOS的YUM的基础的容器中,并没有nginx和php-fpm的RPM包。这两个RMP包在epel的容器中, 虽然你可以从官网下载RPM包来安装,但我个人建议,如果你的CentOS/Redhat中没有加入YUM的epel容器,还是先把这个yum容器加上去比较好,以后可以省无数的折腾。epel具体的安装方法,我在 Redhat/CentOS 软件安装之RPM和YUM 这篇文章中有介绍。
  yum的容器库中加入了epel容器后,在CentOS上安装Nginx就非常简单,运行下面的命令就可以了。
  1
  yum install nginx
  安装玩以后,会发现Nginx的配置文件放在 /etc/nginx目录下, 一般在缺省的情况下,web的root目录会在/usr/share/ngxin/html中。
  安装完nginx以后,我们要测试一下是否安装成功了.
  如果之前已经安装过Apache的话,先要把Apache的服务停掉。
  1
  /etc/init.d/httpd stop #停掉apache服务
  2
  3
  chkconfig httpd off #开机重启后,apache服务不再启动
  这时候你在浏览器上输入 http://主机ip, 如果能出现nginx的测试页面 “Test page for the nginx http server on EPEL”就说明nginx已经正常运行了。
  安装php-fpm
  PHP-FPM (PHP-FastCGI Process Manager) 是目前最常用的一个PHP FastCGI的实现。通俗的讲,这个模块在Nginx和PHP之间桥梁,使之可以互相通信和交换。
  安装及启动过程如下:
  1
  yum install php-fpm
  2
  3
  /etc/init.d/php-fpm start
  4
  5
  chkconfig php-fpm on
  下一步是确认一下,nginx和php-fpm是否已经正常运行. 执行 netstat -tunlp 命令,会看到大约如下的一个界面。
  可以看到nginx在监听80端口,而php-fpm在监听9000端口。
  Screenshot
  设置Nginx 和 PHP-FPM
  我们假定这个主机上有两个网站,一个是aaa.com, 普通的PHP站点, 一个是bbb.com,为wordpress的博客。 我们就讨论一下在这种情况下,如何设置nginx.
  首先为站点建立相应的目录
  1
  mkdir -p /var/www/aaa/html
  2
  3
  mkdir -p /var/www/bbb/html
  4
  5
  mkdir -p /var/log/nginx/aaa
  6
  7
  mkdir -p /var/log/nginx/bbb
  8
  9
  chown -R nginx:adm /var/www/aaa/html
  10
  11
  chown -R nginx:adm /var/wwww/bbb/html
  12
  13
  chown -R nginx:adm /var/log/nginx/aaa
  14
  15
  chown -R nginx:adm /var/log/nginx/bbb
  为两个网站分别设置虚拟目录(virtual directory)
  为了保证整个配置更加清晰,我们尽量不修改主配置文件/etc/nginx/nginx.conf , 而是在在/etc/nginx/conf.d目录下建立两个文件,一个是aaa.conf, 一个是bbb.conf
  其中aaa.conf的内容如下 (aaa是一个普通的php网站): 参考于: http://wiki.nginx.org/PHPFcgiExample
  查看源代码打印帮助
  1
  server {
  2
  listen 80 default_server; #当输入ip时,会访问aaa.com
  3
  server_name www.aaa.com aaa.com *.aaa.com; #这个应该是最好的写法了
  4
  5
  access_log /var/log/nginx/aaa/access.log; #access_log属于ngx_http_log_module的设置, 缺省level为info
  6
  error_log /var/log/nginx/aaa/error.log; #error_log属于core module, 缺省的level是error
  7
  8
  location / {
  9
  root /var/www/aaa/html;
  10
  index index.php index.html index.htm; #由于是PHP类型的动态页面为主,所以把index.php放在前面效率会更高些
  11
  # try_files $uri $uri/ /index.php?$args; #普通php网站因为没有rewrite的话,这个不需要
  12
  }
  13
  14
  error_page 404 /404.html; #error_page errcode uri (也就是说出现了404错误,会请求/404.html)
  15
  location = /404.html { #这是一个典型的location
  16
  root /var/www/aaa/html;
  17
  }
  18
  19
  # redirect server error pages to the static page /50x.html
  20
  #
  21
  error_page 500 502 503 504 /50x.html;
  22
  location = /50x.html {
  23
  root /var/www/aaa/html;
  24
  }
  25
  26
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  27
  # 因为我们不用Nginx做Apache的反向代理,所以不需要这个
  28
  #location ~ \.php$ {
  29
  # proxy_pass http://127.0.0.1;
  30
  #}
  31
  32
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  33
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  34
  # 这种写法可以防止把恶意程序伪装成.jpg之类的***,(其实有个更简单的方法,就是把php.ini中的cgi.fix_pathinfo=0,但有时候简单的修改cgi.fix_pathinfo会造成有的php脚本出错)
  35
  36
  location ~ [^/]\.php(/
  $) {
  37
  root /var/www/aaa/html;
  38
  fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  39
  40
  if (!-f $document_root$fastcgi_script_name) {
  41
  return 404;
  42
  }
  43
  #try_files$uri =404; #这个try_files说明:对于.php文件,直接执行$uri, 如果找不到这个$uri,直接给出404错误,(和 location / 定义不同!),主要是为了防止 伪装成图片的*** (目前看,最安全的方式,是用上面那一句话,官方推荐的)
  44
  45
  fastcgi_pass 127.0.0.1:9000;
  46
  fastcgi_index index.php;
  47
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  48
  include fastcgi_params;
  49
  }
  50
  51
  # deny access to .htaccess files, if Apache's document root
  52
  # concurs with nginx's one
  53
  #
  54
  location ~ /\.ht {
  55
  deny all;
  56
  }
  设置完aaa.com的环境后,还需要设置bbb.com的 nginx的配置,因为bbb.com是wordpress的站点, 除了和aaa.com相同的设置外,还有些特殊的设置,具体设置请参考 http://codex.wordpress.org/Nginx
  至此,从Apache向Nginx的移植基本完成。

页: [1]
查看完整版本: CentOS上如何把Web服务器从Apache换到nginx-zhuzhaogua