farmer111 发表于 2018-11-11 07:40:41

Nginx +PHP部署一

  Nginx+PHP部署一
  Alvin.zeng
  目录
  一、安装PHP3
  1、Yum安装需要的包3
  2、编译gd3
  3、编译PHP5.3.33
  4、编译eaccelerator3
  5、配置PHP4
  6、启动PHP5
  二、安装Ngnix6
  1、创建用户,建立网站数据目录6
  2、编译pcre-8.10稳定版6
  3、编译nginx-0.8.51 稳定版6
  4、创建日志目录6
  5、编辑Nginx 配置文件7
  6、新建fcgi配置文件10
  7、优化Linux 内核参数10
  8、启动Nginx11
  9、每写每天定时切割Nginx日志的脚本11

  一、安装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
http://blog.51cto.com/e/u/themes/default/images/spacer.gif
  #:cp php.ini /etc/
  将PHP这两个功能打开
  extension = "xhprof.so"
  extension = "eaccelerator.so"
  
  xhprof.output_dir=/tmp/xhprof
  
  ;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配置文件
http://blog.51cto.com/e/u/themes/default/images/spacer.gif
  # cat /usr/local/php5.3/etc/php-fpm.conf | grep -v ";"
  
  
  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 242820 22:43 pts/1    00:00:00 grep php
  root   27635   10 20:54 ?      00:00:00 ./php-fpm
  php      27636 276350 20:54 ?      00:00:00 ./php-fpm
  php      27637 276350 20:54 ?      00:00:00 ./php-fpm
  php      27638 276350 20:54 ?      00:00:00 ./php-fpm
  php      27639 276350 20:54 ?      00:00:00 ./php-fpm
  php      27640 276350 20:54 ?      00:00:00 ./php-fpm
  php      27641 276350 20:54 ?      00:00:00 ./php-fpm
  php      27642 276350 20:54 ?      00:00:00 ./php-fpm
  php      27643 276350 20:54 ?      00:00:00 ./php-fpm
  php      27644 276350 20:54 ?      00:00:00 ./php-fpm
  php      27645 276350 20:54 ?      00:00:00 ./php-fpm
  php      27646 276350 20:54 ?      00:00:00 ./php-fpm
  php      27647 276350 20:54 ?      00:00:00 ./php-fpm
  php      27648 276350 20:54 ?      00:00:00 ./php-fpm
  php      27649 276350 20:54 ?      00:00:00 ./php-fpm
  php      27650 276350 20:54 ?      00:00:00 ./php-fpm
  php      27651 276350 20:54 ?      00:00:00 ./php-fpm
  php      27652 276350 20:54 ?      00:00:00 ./php-fpm
  php      27653 276350 20:54 ?      00:00:00 ./php-fpm
  php      27654 276350 20:54 ?      00:00:00 ./php-fpm
  php      27655 276350 20:54 ?      00:00:00 ./php-fpm
二、安装Nginx
1、创建用户,建立网站数据目录
  #:groupaddwww
  #: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 配置文件
http://blog.51cto.com/e/u/themes/default/images/spacer.gif
  #:vi /usr/local/nginx/confuserwww www;
  worker_processes 8;                  #:启动8个进程
  error_log/nginxlog/logs/nginx_error.logcrit;#:日志文件路径
  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_typeapplication/octet-stream;
  #charsetgb2312;
  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_length1k;
  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_zonecrawler$binary_remote_addr10m;
  server
  {
  listen       80;                           #:监听端口80
  server_namewww.yong.com;               #:网站域名
  index index.html index.htm index.php;
  root/data0/htdocs/blog;                   #:网站数据聚路径
  #limit_conn   crawler20;
  location ~ .*\.(php|php5)?$
  {
  #fastcgi_passunix:/tmp/php-cgi.sock;
  fastcgi_pass127.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_formataccess'$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.logaccess;
  }
  server
  {
  listen       80;                           #:监听端口
  server_namewww.zeng.com;               #:网站域名
  index index.html index.htm index.php;         #:主页格式
  root/data0/htdocs/www;                   #:网站数据存放路径
  location ~ .*\.(php|php5)?$
  {
  #fastcgi_passunix:/tmp/php-cgi.sock;
  fastcgi_pass127.0.0.1:9000;
  fastcgi_index index.php;
  include fcgi.conf;
  }
  log_formatwwwlogs'$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.logwwwlogs;   #:数据日志存放路径
  }
  server
  {
  listen80;
  server_namestatus.blog.s135.com;
  location / {
  stub_status on;
  access_log   off;
  }
  }
  }
6、新建fcgi配置文件
http://blog.51cto.com/e/u/themes/default/images/spacer.gif
  #vi /usr/local/nginx/conf/fcgi.conf
  fastcgi_paramGATEWAY_INTERFACECGI/1.1;
  fastcgi_paramSERVER_SOFTWARE    nginx;
  fastcgi_paramQUERY_STRING       $query_string;
  fastcgi_paramREQUEST_METHOD   $request_method;
  fastcgi_paramCONTENT_TYPE       $content_type;
  fastcgi_paramCONTENT_LENGTH   $content_length;
  fastcgi_paramSCRIPT_FILENAME    $document_root$fastcgi_script_name;
  fastcgi_paramSCRIPT_NAME      $fastcgi_script_name;
  fastcgi_paramREQUEST_URI      $request_uri;
  fastcgi_paramDOCUMENT_URI       $document_uri;
  fastcgi_paramDOCUMENT_ROOT      $document_root;
  fastcgi_paramSERVER_PROTOCOL    $server_protocol;
  fastcgi_paramREMOTE_ADDR      $remote_addr;
  fastcgi_paramREMOTE_PORT      $remote_port;
  fastcgi_paramSERVER_ADDR      $server_addr;
  fastcgi_paramSERVER_PORT      $server_port;
  fastcgi_paramSERVER_NAME      $server_name;
  # PHP only, required if PHP was built with --enable-force-cgi-redirect
  fastcgi_paramREDIRECT_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 = 102465535
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

页: [1]
查看完整版本: Nginx +PHP部署一