Nginx +PHP部署一
Alvin.zeng<孤独0-1>
目录
一、安装PHP
1、Yum安装需要的包
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers mysql-devel libevent-devel
2、编译gd和libiconv
官方网站GD
http://www.libgd.org/releases/gd-2.0.35.tar.bz2
#:tar –xvf gd-2.0.35.tar.bz2–C /opt/
#:cd /opt/gd-2.0.35
#:./configure --prefix=/usr/local/gd2
#: make && make install
需要make 两次,第一次会出错,
编译libiconv
#: tar –xvf libiconv-1.13.1.tar.gz
#: cd libiconv-1.13.1
#: ./configure --prefix=/usr/local/libiconv
#: make && make install
3、编译PHP5.3.3
官方网站
http://cn.php.net/distributions/php-5.3.3.tar.gz
#:rpm -vih libmcrypt-2.5.8-4.el5.centos.x86_64.rpm
#:rpm -vih libmcrypt-devel-2.5.8-4.el5.centos.x86_64.rpm
#: tar –xvf php-5.3.3.tar.gz–C /opt/
#:cd /opt/php-5.3.3
#:./configure --prefix=/usr/local/php5.3 --enable-fpm --with-mysql --with-iconv=/usr/local/libiconv/ --with-pdo-mysql --with-mysqli --with-fpm-user --with-config-file-path=/etc --disable-sqlite3 --enable-soap --with-zlib --enable-xml --with-gd=/usr/local/gd2 --enable-mbstring --enable-mcrypt --enable-mbstring --enable-gd-native-ttf --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --with-mcrypt
#: make && make install
错误:configure: error: mysql configure failed. Please check config.log for more information.
解决:export LDFLAGS=-L/usr/lib64/mysql/ 指定一下64位环境变量,默认是找32位的。
4、编译eaccelerator 和编译xhprof 和Client-phpredis
官方网站
http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2
#:tar –xvf eaccelerator-0.9.6.1.tar.bz2–C /opt/
#: cd /opt/ eaccelerator-0.9.6.1
#:/usr/local/php5.3/bin/phpize运此命令生成configure 文件
#:./configure --prefix=/usr/local/eaccelerator --with-php-config=/usr/local/php5.3/bin/php-config --enable-eaccelerator
#: make && make install
官方网站
http://pecl.php.net/get/xhprof
#:tar –xvf xhprof-0.9.2.tar.gz –C /opt/
#:cd /opt/xhprof-0.9.2/extension
#:/usr/local/php5.3/bin/phpize运此命令生成configure 文件
#: ./configure --with-php-config=/usr/local/php5.3/bin/php-config
#: make && make install
官方网站,
http://download.github.com/owlient-phpredis-2.0.8-0-g0c0409a.tar.gz
#:tar –xvf owlient-phpredis-2.0.8-0-g0c0409a.tar.gz /opt/
#: cd owlient-phpredis-2.0.8-0-g0c0409
#:/usr/local/php5.3/bin/phpize运此命令生成configure 文件
#: ./configure --with-php-config=/usr/local/php5.3/bin/php-config
#: make && make install
5、配置PHP
(1)、拷贝库
#:cd /usr/local/php5.3/lib/php/extensions/no-debug-non-zts-20090626/
#:cp eaccelerator.soxhprof.so /usr/local/php5.3/lib/php/extensions/
(2)、创建PHP用户
#:groupadd php
#:useradd –g php php
#:chgrp –R php /usr/local/php5.3
(3)、拷贝PHP.ini
#:cp php.ini /etc/
将PHP这两个功能打开
extension = "xhprof.so"
extension = "eaccelerator.so"
[xhprof]
xhprof.output_dir=/tmp/xhprof
[eAccelerator]
;extension="eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
(4)、拷贝fpm配置文件
#:cp /usr/local/php5.3/etc/php-fpm.conf.default/usr/local/php5.3/etc/php-fpm.conf
(5)、修改fpm配置文件
# cat /usr/local/php5.3/etc/php-fpm.conf | grep -v ";"
[global]
[www]
listen = 127.0.0.1:9000
user = php
group = php
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
6、启动PHP
#:cd /usr/local/php5.3/sbin/
# ./php-fpm
#: ps –ef | grep php
# ps -ef | grep php
root 14104 24282 0 22:43 pts/1 00:00:00 grep php
root 27635 1 0 20:54 ? 00:00:00 ./php-fpm
php 27636 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27637 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27638 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27639 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27640 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27641 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27642 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27643 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27644 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27645 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27646 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27647 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27648 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27649 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27650 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27651 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27652 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27653 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27654 27635 0 20:54 ? 00:00:00 ./php-fpm
php 27655 27635 0 20:54 ? 00:00:00 ./php-fpm
二、安装Nginx
1、创建用户,建立网站数据目录
#:groupadd www
#:useradd –g www www
#:mkdir –p /data/htdocs/zeng
#:mkdir –p /data/htdocs/yong
#:chown + w /data/htdocs/zeng
#:chwon +w /data/htdocs/yong
#:chown –R www:www /data/htdocs/zeng
#:chwon –R www:www /data/htdocs/yong
2、编译pcre-8.10稳定版
官方网站
下载ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.10.tar.gz
#:tar –xvf pcre-8.10 –C /opt/
#:cd /opt/pcre-8.10
#:./configure 注意不要指定路经,否则下面Nginx会编译出错
#:make && make install
3、编译nginx-0.8.51 稳定版
官方网站下载
下载http://nginx.org/download/nginx-0.8.52.tar.gz
#:tar –xvf nginx-0.8.52.tar.gz –C /opt/
#:cd /opt/ nginx-0.8.52
#:./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#:make && make install
4、创建日志目录
#:mkdir –p /nginxlog/logs
#:chmod +w /nginxlog/logs
#:chown –R www:www /nginxlog/logs
5、编辑Nginx 配置文件
#:vi /usr/local/nginx/confuser www www;
worker_processes 8; #:启动8个进程
error_log /nginxlog/logs/nginx_error.log crit; #:日志文件路径
pid /usr/local/nginx/nginx.pid; #:pid文件路经
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;#:连接数量65535
events
{
use epoll;
worker_connections 65535; #:连接数量65535
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server
{
listen 80; #:监听端口80
server_name www.yong.com; #:网站域名
index index.html index.htm index.php;
root /data0/htdocs/blog; #:网站数据聚路径
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data1/logs/access.log access;
}
server
{
listen 80; #:监听端口
server_name www.zeng.com; #:网站域名
index index.html index.htm index.php; #:主页格式
root /data0/htdocs/www; #:网站数据存放路径
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
log_format wwwlogs '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data1/logs/wwwlogs.log wwwlogs; #:数据日志存放路径
}
server
{
listen 80;
server_name status.blog.s135.com;
location / {
stub_status on;
access_log off;
}
}
}
6、新建fcgi配置文件
#vi /usr/local/nginx/conf/fcgi.conf
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
7、优化Linux 内核参数
#:vi /etc/sysctl.conf 在文件末尾增加
#:/sbin/sysctl –p 可立即生效
# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535
8、启动Nginx
检查配置文件是否正确,
#:/usr/local/nginx/sbin/nginx –t
启动
#:/usr/local/nginx/sbin/nginx
8.0X版本,重起
#:/usr/local/nginx/sbin/nginx–s reload
9、每写每天定时切割Nginx日志的脚本
#:vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/webserver/nginx/logs/"
Cutlog(){
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`
}
Main()
{
Cutlog
}
#:Main
Main
设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e
00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh