apache + nginx + fastcgi/php + varnish on Centos 6

作者: jaclon 分类: Linux, PHP 发布时间: 2012-09-26 21:35 ė 6没有评论

概念

nginx作为前端,作负载均衡;varnish是缓存服务器;apache处理动态脚本

 

准备及安装

安装EPEL库

32位执行

rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm

64位执行

rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm

更新软件包

$ yum -y update

关闭selinux
编辑/etc/sysconfig/selinux, 修改SELINUX 从 enforcing 到 disabled

安装本教程所需的软件包

$ yum install httpd php nginx varnish mod_fcgid mysql-server elinks apachetop php-mysql php-gd php-mcrypt php-mbstring php-cli

创建虚拟主机
由于是测试环境,添加一个本地域名,修改/etc/hosts

127.0.0.1 testsite.tld www.testsite.tld

添加虚拟主机配置文件/etc/httpd/conf.d/vh-testsite.tld.conf

<VirtualHost *:8080>
ServerAdmin root@localhost
DocumentRoot /var/www/sites/testsite.tld/public_html
ServerName testsite.tld:8080
ServerAlias www.testsite.tld
### ErrorLog /var/www/sites/testsite.tld/logs/testsite.tld-error_log
### CustomLog /var/www/sites/testsite.tld/logs/testosite.tld-access_log combined
</VirtualHost>

创建站点目录

mkdir -p /var/www/sites/testsite.tld/public_html

修改apache配置文件/etc/httpd/conf/httpd.conf

//监听端口
Listen 8080
NameVirtualHost *:8080

创建一个默认的虚拟主机/etc/httpd/conf.d/vh-00-default.conf

<VirtualHost *:8080>
    ServerName place-your-server-ip-here:8080
    ServerAdmin root@localhost
    DocumentRoot /var/www/html
    ErrorLog /var/log/httpd/error_log
    CustomLog /var/log/httpd/access_log combined
    DirectoryIndex index.html index.htm index.shtml index.php index.php4 index.php3 index.cgi index.phtml
    <IfModule mod_php4.c>
        AddType application/x-httpd-php .php4 .phtml .php
        AddType application/x-httpd-php-source .phps
    </IfModule>
    <Directory "/var/www/html">
        Options Indexes Includes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

测试配置文件是否正确,正确返回“Syntax OK”

$ apachectl -t
  Syntax OK

启动apache服务

$ service httpd start
httpd starten:                                             [  OK  ]

创建一个测试文件/var/www/sites/testsite.tld/public_html/testme.php

<?php
    phpinfo();
 ?>

拷贝测试文件到默认站点

cp /var/www/sites/testsite.tld/public_html/testme.php /var/www/html/

用命令行浏览器测试一下

$ elinks --dump http://testsite.tld:8080/testme.php | grep -e "PHP Version" -e "System" -C 1 -m 2

PHP Version 5.3.3

System               Linux x07 2.6.32-220.17.1.el6.x86_64 #1 SMP Wed May 16
00:01:37 BST 2012 x86_64

fastcgi
先禁用php模块

$ cd /etc/httpd/conf.d/
$ mv php.conf php.conf-deactivated

为站点创建用户

groupadd -g 1000 web_1000
useradd -s /sbin/nologin -g web_1000 -d /var/www/sites/testsite.tld/ web_1000
passwd web_1000

完善之前创建的配置文件/etc/httpd/conf.d/vh-testsite.tld.conf

<VirtualHost *:8080>
    ServerAdmin root@localhost
    DocumentRoot /var/www/sites/testsite.tld/public_html
    ServerName testsite.tld:8080
    ServerAlias www.testsite.tld 
    ### ErrorLog /var/www/sites/testsite.tld/logs/testsite.tld-error_log
    ### CustomLog /var/www/sites/testsite.tld/logs/testosite.tld-access_log combined
    <IfModule mod_fcgid.c>
        SuexecUserGroup web_1000 web_1000
        <Directory /var/www/sites/testsite.tld/public_html/>
            Options +ExecCGI
            AllowOverride All
            AddHandler fcgid-script .php
            FCGIWrapper /var/www/sites/testsite.tld/php-fcgi-starter .php
            Order allow,deny
            Allow from all
        </Directory>
    </IfModule>
</VirtualHost>

创建/var/www/sites/testsite.tld/php-fcgi-starter

#!/bin/sh
PHPRC=/var/www/sites/testsite.tld/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=8
exec /usr/bin/php-cgi

创建/var/www/sites/testsite.tld/php.ini

session.save_path = "/var/www/sites/testsite.tld/session"

作一些调整

$ mkdir /var/www/sites/testsite.tld/session
$ chown -R web_1000.web_1000 /var/www/sites/testsite.tld

设置文件的权限,这个可以等配置确认完成后再做,方便修改

$ chmod 400 /var/www/sites/testsite.tld/php-fcgi-starter /var/www/sites/testsite.tld/php.ini
$ chattr +i  /var/www/sites/testsite.tld/php-fcgi-starter /var/www/sites/testsite.tld/php.ini

测试看看配置是否正确

$ apachectl -t
  Syntax OK

重新启动apache服务

$ service httpd restart
httpd beenden:                                             [  OK  ]
httpd starten:                                             [  OK  ]

访问看看是否正确执行

$ elinks --dump http://testsite.tld:8080/testme.php | grep -e "PHP Version" -e "System" -C 1 -m 2

PHP Version 5.3.3

System               Linux x07 2.6.32-220.17.1.el6.x86_64 #1 SMP Wed May 16
00:01:37 BST 2012 x86_64

看看fastcgi

$ elinks --dump http://testsite.tld:8080/testme.php | grep -e "Server API" 
   Server API      CGI/FastCGI

nginx
配置nginx虚拟目录配置文件/etc/nginx/conf.d/vh-testsite.tld.conf

server {
listen 80; # Default listen port
server_name testsite.tld;
server_name www.testsite.tld;
access_log /var/log/nginx/testsite.tld-access.log;
gzip on; # Turn on gZip
gzip_disable msie6;
gzip_static on;
gzip_comp_level 9;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

location / {
proxy_redirect off; # Do not redirect this proxy - It needs to be pass-through
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Server-Address $server_addr;
proxy_pass_header Set-Cookie;
proxy_pass http://127.0.0.1:8080; # Pass all traffic through to apache httpd
}
}

测试配置文件是否正确

$ nginx -t

启动nginx服务

service nginx start
nginx starten:                                             [  OK  ]

测试一个

$ elinks --dump http://testsite.tld/testme.php | grep -e "PHP Version" -m 1
   PHP Version 5.3.3

varnish
修改varnish配置文件 /etc/varnish/default.vcl

backend default {
  .host = "127.0.0.1";
  .port = "8080";
}

启动varnish

$ service varnish start
Starting varnish HTTP accelerator:                         [  OK  ]

修改nginx代理地址 /etc/nginx/conf.d/vh-testsite.tld.conf

proxy_pass http://127.0.0.1:6081; # Pass all traffic through to varnish

重新启动varnish

$ service nginx restart
nginx beenden:                                             [  OK  ]
nginx starten:                                             [  OK  ]

设置随机启动

$ chkconfig varnish on
$ chkconfig nginx on
$ chkconfig httpd on

测试配置文件

$ elinks --dump http://testsite.tld/testme.php | grep -e "PHP Version" -m 1
   PHP Version 5.3.3

测试一个服务器性能

ab -n 32000 -c 700 -k http://testsite.tld/testme.php

源文:http://www.michael-kress.de/

本文出自 肥蕉博客@IT技术,热门话题,生活随笔,美图欣赏,转载时请注明出处及相应链接。

本文永久链接: http://www.bananawolf.com/html/2012/09/845.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

Ɣ回顶部