今天开始安装php和nginx,以及squid,目的是访问首先通过squid缓存,未缓存则转到nginx,若为php则转到php解析器进行解析,php采用fastcgi启动方式,并使用php-fpm php fastcgi管理器。

1,安装php及php-fpm:
# yum -y install php php-fpm
设置php-fpm随系统启动:
# chkconfig php-fpm on
修改php-fpm配置文件:
# vim /etc/php-fpm.conf
error_log = /var/log/php-fpm/error.log
# vim /etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000
启动php-fpm:
# service php-fpm restart
Stopping php-fpm:                                          [  OK  ]
Starting php-fpm:                                          [  OK  ]
检查是否启动正确,监听9000端口:
# netstat -an | grep :9000
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN
# lsof -i:9000
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
php-fpm 3204   root    7u  IPv4  40935      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 3205 apache    0u  IPv4  40935      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 3206 apache    0u  IPv4  40935      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 3207 apache    0u  IPv4  40935      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 3208 apache    0u  IPv4  40935      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 3209 apache    0u  IPv4  40935      0t0  TCP localhost:cslistener (LISTEN)
安装php其它组件:
# yum -y install php-gd php-mysql php-mbstring php-xml php-mcrypt

2,安装配置nginx,并使php页面转交给php-fpm处理
安装nginx:
# yum -y install nginx
配置nginx:
# vim /etc/nginx/nginx.conf
# vim /etc/nginx/conf.d/default.conf
server {
        listen             8080;
        server_name    localhost;

        #charset koi8-r;
        access_log    /var/log/nginx/log/host.access.log    main;

        location / {
                #root     /usr/share/nginx/html;
                root     /var/www/html;
                index    index.html index.htm default.htm default.html index.php default.php;
        }

        #error_page    404                            /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page     500 502 503 504    /50x.html;
        location = /50x.html {
                root     /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #        proxy_pass     http://127.0.0.1:8080;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                root                     /var/www/html;
                fastcgi_pass     127.0.0.1:9000;
                fastcgi_index    index.php;
                fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                include                fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #        deny    all;
        #}
}

设置nginx随系统启动:
# chkconfig nginx on
重启nginx:
# service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
在网站目录 /var/www/html 下创建phpinfo.php:
<?php
phpinfo();
?>

关掉防火墙,访问http://192.168.1.18:8080/phpinfo.php,访问应该成功。

3, 安装squid
由于squid缓存文件将放在SSD盘里,因此需要将SSD随系统启动挂载。
首先找出SSD来:
# fdisk -l
   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1       14594   117218304    7  HPFS/NTFS
可知SSD盘符 /dev/sdc1, 且格式为NTFS;
首先格式化SSD为ext linux格式:
# mkfs.ext4 /dev/sdc1
将SSD挂载信息加入到/etc/fstab文件中
# vim /etc/fstab
/dev/sdc1                            /cache                 ext4    defaults,ssd,discard,noatime 2 1
并在当前挂载:
# mkdir /cache
# mount -o discard,noatime -t ext4 /dev/sdc1 /cache
再查看则可看到 /cache 目录:
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sdc1             111G  188M  105G   1% /cache
创建squid需要的目录:
# mkdir -p /cache/spool/squid
关闭selinux,否则squid会报如下错误:
2012/09/30 09:05:46| /cache/spool/squid: (13) Permission denied
FATAL:    Failed to verify one of the swap directories, Check cache.log
                for details.    Run 'squid -z' to create swap directories
                if needed, or if running Squid for the first time.
解决这个问题:
# vim /etc/sysconfig/selinux
SELINUX=disabled
# setenforce 0

现在开始安装配置squid:
安装squid,运行下面命令,将会安装squid3.1:
# yum -y install squid
将squid设置随系统启动:
# chkconfig squid on
安装完后配置squid:
# vim /etc/squid/squid.conf
在文件末尾加上以下配置项,若前面有相同的注释掉原有的:
####### customazation #########
http_port 80 vhost
icp_port 0

cache_peer 192.168.1.18 parent 8080 0 no-query originserver weight=1 name=a
cache_peer_access a allow all

#cache_mem 0MB
memory_pools off
maximum_object_size 10 MB
maximum_object_size_in_memory 100 KB
negative_ttl 0 second
ipcache_size 10240
fqdncache_size 10240
#reply_body_max_size    1024000 allow all
cache_dir aufs /cache/spool/squid 10240 64 256
read_ahead_gap 20 KB

access_log /var/log/squid/access.log
#cache_access_log /var/squid/log/access.log
#logformat abc-squid %ts.%03tu %6tr %>a %Ss/%03Hs %<st %rm %ru %un %Sh/%<A %mt "%{Referer}>h" "%{User-Agent}>h"
cache_store_log none
strip_query_terms off
check_hostnames off
#dns_children 30


#httpd_accel_port 80
#httpd_accel_host virtual
#httpd_accel_uses_host_header on
#httpd_accel_with_proxy off

acl over_conn_limit maxconn 800
http_access deny over_conn_limit

negative_ttl 0 minutes
pipeline_prefetch on
forwarded_for on

refresh_pattern -i /.(htm|html|txt|js)$ 0 66% 1440 reload-into-ims
refresh_pattern -i /.(bmp|gif|jpg|png|swf)$ 0 66% 10080 reload-into-ims

重新启动squid:
# service squid restart
Stopping squid: ................                           [  OK  ]
Starting squid: .                                          [  OK  ]
检查是否启动成功:
# netstat -an | grep :80
tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN
tcp        0      0 :::80                       :::*                        LISTEN
# lsof -i:80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
squid   3277 squid   16u  IPv6  41615      0t0  TCP *:http (LISTEN)

再访问原来的网址,将端口改为80:
http://192.168.1.18/phpinfo.php
访问成功,查看squid日志,可看到通过了squid,并转发到nginx。
至此squid+nginx+php网站的基本配置已完成。