zoukankan      html  css  js  c++  java
  • FastCGI与PHP

    什么是CGI

    CGI全称"通用网关接口"(Common Gateway Interface),用于HTTP服务器与其它机器上的程序服务通信交流的一种工具,CGI程序须运行在网络服务器上。

    传统CGI接口方式的主要缺点是性能较差,因为每次HTTP服务器遇到动态程序时都需要重启解析器来执行解析,然后结果被返回给HTTP服务器。这在处理高并发访问几乎是不可用的,因此就诞生了FastCGI。另外传统的CGI接口方式安全性也很差。

    什么是FastCGI

    FastCGI是一个可伸缩地、高速地在HTTP服务器和动态脚本语言间通信的接口(FastCGI接口在Linux下是socket(可以是文件socket,也可以是ip socket)),主要优点是把动态语言和HTTP服务器分离开来。多数流行的HTTP服务器都支持FastCGI,包括Apache、Nginx和lightpd。

    同时,FastCGI也被许多脚本语言所支持,比较流行的脚本语言之一为PHP。FastCGI接口方式采用C/S架构,可以将HTTP服务器和脚本解析服务器分开,同时在脚本解析服务器上启动一个或多个脚本解析守护进程。当HTTP服务器每次遇到动态程序时,可以将其直接交付给FastCGI进程执行,然后将得到的结构返回给浏览器。这种方式可以让HTTP服务器专一地处理静态请求或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了整个应用系统的性能。

    FastCGI的重要特点:

    1、FastCGI是HTTP服务器和动态脚本语言间通信的接口或者工具。

    2、FastCGI优点是把动态语言解析和HTTP服务器分离开来。

    3、Nginx、Apache、Lighttpd以及多数动态语言都支持FastCGI。

    4、FastCGI接口方式采用C/S架构,分为客户端(HTTP服务器)和服务端(动态语言解析服务器)。

    5、PHP动态语言服务端可以启动多个FastCGI的守护进程。

    6、HTTP服务器通过FastCGI客户端和动态语言FastCGI服务端通信。

    Nginx FastCGI的运行原理

    Nginx不支持对外部动态程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper,这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端,这就是Nginx+FastCGI的整个运作过程。

    FastCGI的主要优点是把动态语言和HTTP服务器分离开来,是Nginx专一处理静态请求和向后转发动态请求,而PHP/PHP-FPM服务器专一解析PHP动态请求。

    检查安装PHP所需lib库

    php程序在开发及运行时会调用一些zlib、gb 函数库,因此需要安装下面的库。

    1. [root@lnmp tools]# yum install zlib-devel libxml2-devel libjpeg-devel libiconv-devel freetype-devel libpng-devel gd-devel curl-devel libxslt-devel -y

    安装yum无法安装的libiconv库

    1. [root@lnmp tools]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
    2. [root@lnmp tools]# tar zxf libiconv-1.14.tar.gz
    3. [root@lnmp tools]# cd libiconv-1.14
    4. [root@lnmp libiconv-1.14]# ./configure --prefix=/usr/local/libiconv
    5. [root@lnmp libiconv-1.14]# make && make install

    安装libmcrypt库

    1. [root@lnmp tools]# wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
    2. [root@lnmp libmcrypt-2.5.8]# ./configure
    3. [root@lnmp libmcrypt-2.5.8]# make && make install

    快速安装方法:

    安装epel源

    1. [root@lnmp ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

    安装PHP依赖包

    1. [root@lnmp ~]# yum -y install libxml2-devel gd-devel libcurl-devel libxslt-devel libmcrypt-devel mhash mcrypt

    安装libicov

    同上。

    安装libmcrypt

    1. [root@lnmp yum.repos.d]# yum install libmcrypt-devel -y
    2. [root@lnmp yum.repos.d]# rpm -qa libmcrypt-devel
    3. libmcrypt-devel-2.5.8-9.el6.x86_64

    安装mhash加密扩展库

    Mhash是基于离散数学原理的不可逆向的php加密方式扩展库,其在默认情况下不开启。mhash可以用于创建校验数值、消息摘要、消息认证码,以及无需原文的关键信息保持(如密码)等。

    1. [root@lnmp ~]# yum install mhash mhash-devel -y
    2. [root@lnmp ~]# rpm -qa mhash mhash-devel
    3. mhash-devel-0.9.9.9-3.el6.x86_64
    4. mhash-0.9.9.9-3.el6.x86_64

    安装mcrypt加密扩展库

    PHP程序员在编写代码程序时,除了要保证代码的高性能之外,还有一点是非常重要的,那就是程序的安全性保障。PHP除了自带的几种加密函数外,还有功能更全面的PHP加密扩展库mcrypt和mhash。

    1. [root@lnmp ~]# yum install mcrypt -y
    2. [root@lnmp ~]# rpm -qa mcrypt
    3. mcrypt-2.6.8-10.el6.x86_64

    安装PHP

    lnmp服务器

    1. [root@lnmp tools]# tar xf php-5.3.28.tar.gz
    2. [root@lnmp tools]# ls -ld php-5.3.28
    3. drwxr-xr-x 13 nginx games 4096 Dec 11 2013 php-5.3.28
    4. [root@lnmp tools]# cd php-5.3.28

    编译安装

    1. ###################输入以下内容
    2. ./configure
    3. --prefix=/application/php5.3.28
    4. --with-mysql=/application/mysql #使用php自带mysql参数:--with-mysql=mysqlnd
    5. --with-iconv-dir=/usr/local/libiconv
    6. --with-freetype-dir
    7. --with-jpeg-dir
    8. --with-png-dir
    9. --with-zlib
    10. --with-libxml-dir=/usr
    11. --enable-xml
    12. --disable-rpath
    13. --enable-safe-mode
    14. --enable-bcmath
    15. --enable-shmop
    16. --enable-sysvsem
    17. --enable-inline-optimization
    18. --with-curl
    19. --with-curlwrappers
    20. --enable-mbregex
    21. --enable-fpm
    22. --enable-mbstring
    23. --with-mcrypt
    24. --with-gd
    25. --enable-gd-native-ttf
    26. --with-openssl
    27. --with-mhash
    28. --enable-pcntl
    29. --enable-sockets
    30. --with-xmlrpc
    31. --enable-zip
    32. --enable-soap
    33. --enable-short-tags
    34. --enable-zend-multibyte
    35. --enable-static
    36. --with-xsl
    37. --with-fpm-user=nginx
    38. --with-fpm-group=nginx
    39. --enable-ftp
    40. ####################编译结果
    41. creating main/internal_functions.c
    42. creating main/internal_functions_cli.c
    43. +--------------------------------------------------------------------+
    44. | License: |
    45. | This software is subject to the PHP License, available in this |
    46. | distribution in the file LICENSE. By continuing this installation |
    47. | process, you are bound by the terms of this license agreement. |
    48. | If you do not agree with the terms of this license, you must abort |
    49. | the installation process at this point. |
    50. +--------------------------------------------------------------------+
    51.  
    52. Thank you for using PHP.
    53.  
    54. [root@lnmp php-5.3.28]# echo $?
    55. 0

     

    1. [root@lnmp php-5.3.28]# make
    2. /usr/bin/ld: cannot find -lltdl
    3. collect2: ld returned 1 exit status
    4. make: *** [sapi/fpm/php-fpm] Error 1
    5. [root@lnmp php-5.3.28]# echo $?
    6. 2

    cannot find –lltdl解决办法,需要安装libltdl。

    1. [root@lnmp tools]# cd libmcrypt-2.5.8/libltdl/
    2. [root@lnmp libltdl]# ./configure -enable-ltdl-install
    3. [root@lnmp libltdl]# make && make install

    重新回到PHP的安装目录下进行查询编译PHP。

    1. [root@lnmp php-5.3.28]# make
    2. clicommand.inc
    3. pharcommand.inc
    4. directorytreeiterator.inc
    5. invertedregexiterator.inc
    6. phar.inc
    7.  
    8. Build complete.
    9. Don't forget to run 'make test'.
    10.  
    11. [root@lnmp php-5.3.28]# echo $?
    12. 0
    13. [root@lnmp php-5.3.28]# make install

    创建软连接

    1. /home/oldboy/tools/php-5.3.28/build/shtool install -c ext/phar/phar.phar /application/php5.3.28/bin
    2. ln -s -f /application/php5.3.28/bin/phar.phar /application/php5.3.28/bin/phar
    3. Installing PDO headers: /application/php5.3.28/include/php/ext/pdo/
    4. [root@lnmp php-5.3.28]# echo $?
    5. 0
    6. [root@lnmp php-5.3.28]# ln -s /application/php5.3.28/ /application/php

    mysql服务器

    1. [root@mysql tools]# tar xf php-5.5.38.tar.gz
    2. [root@mysql tools]# ls -ld php-5.5.38
    3. drwxr-xr-x 14 1000 1000 4096 Jul 20 2016 php-5.5.38
    4. [root@mysql tools]# cd php-5.5.38

    编译安装

    1. #############输入以下内容
    2. ./configure
    3. --prefix=/application/php5.5.38
    4. --with-mysql=mysqlnd
    5. --with-iconv-dir=/usr/local/libiconv
    6. --with-freetype-dir
    7. --with-jpeg-dir
    8. --with-png-dir
    9. --with-zlib
    10. --with-libxml-dir=/usr
    11. --enable-xml
    12. --disable-rpath
    13. --enable-bcmath
    14. --enable-shmop
    15. --enable-sysvsem
    16. --enable-inline-optimization
    17. --with-curl
    18. --enable-mbregex
    19. --enable-fpm
    20. --enable-mbstring
    21. --with-mcrypt
    22. --with-gd
    23. --enable-gd-native-ttf
    24. --with-openssl
    25. --with-mhash
    26. --enable-pcntl
    27. --enable-sockets
    28. --with-xmlrpc
    29. --enable-soap
    30. --enable-short-tags
    31. --enable-static
    32. --with-xsl
    33. --with-fpm-user=nginx
    34. --with-fpm-group=nginx
    35. --enable-ftp
    36. ############编译结果
    37.  
    38. Thank you for using PHP.
    39.  
    40. config.status: creating php5.spec
    41. config.status: executing default commands
    42. [root@mysql php-5.5.38]# echo $?
    43. 0

     

    1. [root@mysql php-5.5.38]# make
    2. invertedregexiterator.inc
    3. clicommand.inc
    4. directorytreeiterator.inc
    5. directorygraphiterator.inc
    6. pharcommand.inc
    7. phar.inc
    8.  
    9. Build complete.
    10. Don't forget to run 'make test'.
    11.  
    12. [root@mysql php-5.5.38]# echo $?
    13. 0
    14. [root@mysql php-5.5.38]# make install
    15. ln -s -f phar.phar /application/php5.5.38/bin/phar
    16. Installing PDO headers: /application/php5.5.38/include/php/ext/pdo/
    17. [root@mysql php-5.5.38]# echo $?
    18. 0

    创建软连接

    1. [root@mysql php-5.5.38]# ln -s /application/php5.5.38/ /application/php
    2. [root@mysql php-5.5.38]# ls -ld /application/php
    3. lrwxrwxrwx 1 root root 23 Feb 26 16:08 /application/php -> /application/php5.5.38/

    配置PHP引擎配置文件php.ini

    1. [root@lnmp php-5.3.28]# cp php.ini-production /application/php/lib/php.ini

    配置PHP服务(FastCGI模式)配置文件php-fpm.conf

    1. [root@lnmp php-5.3.28]# cd /application/php/etc/
    2. [root@lnmp etc]# ls
    3. pear.conf php-fpm.conf.default
    4. [root@lnmp etc]# cp php-fpm.conf.default php-fpm.conf
    5. [root@lnmp etc]# ls
    6. pear.conf php-fpm.conf php-fpm.conf.default

    指定pid路径:pid = /app/logs/php-fpm.pid

    进程管理的错误日志路径:error_log = /app/logs/php-fpm.log

    错误日志级别:log_level = error

    主进程文件描述符:rlimit_files = 32768

    模型:events.mechanism = epoll

    用户和组:

    listen.owner = nginx

    listen.group = nginx

    最大进程数:pm.max_children = 1024

    开始启动进程数:pm.start_servers = 16

    最小空闲进程数:pm.min_spare_servers = 5

    最大空闲进程数:pm.max_spare_servers = 20

    超时时间:pm.process_idle_timeout = 15s;

    每个进程最大请求:pm.max_requests = 2048

    慢查询:slowlog = /app/logs/$pool.log.slow

    超时时间:request_slowlog_timeout = 10

    邮箱地址:php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f xxxxxxx@xxx.com

    启动PHP

    1. [root@lnmp etc]# /application/php/sbin/php-fpm
    2. [root@lnmp etc]# ps -ef|grep php-fpm
    3. root 1695 1 0 17:32 ? 00:00:00 php-fpm: master process (/application/php5.3.28/etc/php-fpm.conf)
    4. nginx 1696 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    5. nginx 1697 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    6. nginx 1698 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    7. nginx 1699 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    8. nginx 1700 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    9. nginx 1701 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    10. nginx 1702 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    11. nginx 1703 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    12. nginx 1704 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    13. nginx 1705 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    14. nginx 1706 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    15. nginx 1707 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    16. nginx 1708 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    17. nginx 1709 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    18. nginx 1710 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    19. nginx 1711 1695 0 17:32 ? 00:00:00 php-fpm: pool www
    20. root 1713 1360 0 17:33 pts/0 00:00:00 grep php-fpm
    21. [root@lnmp etc]# ps -ef|grep php-fpm|wc -l
    22. 18

    查看端口

    1. [root@lnmp etc]# netstat -lntup|grep php-fpm
    2. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1695/php-fpm
    3. [root@lnmp etc]# ss -lntup|grep php-fpm
    4. tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",1695,7),("php-fpm",1696,0),("php-fpm",1697,0),("php-fpm",1698,0),("php-fpm",1699,0),("php-fpm",1700,0),("php-fpm",1701,0),("php-fpm",1702,0),("php-fpm",1703,0),("php-fpm",1704,0),("php-fpm",1705,0),("php-fpm",1706,0),("php-fpm",1707,0),("php-fpm",1708,0),("php-fpm",1709,0),("php-fpm",1710,0),("php-fpm",1711,0))

    配置PHP与Nginx

    1. [root@lnmp ~]# cd /application/nginx/conf/extra/
    2. [root@lnmp extra]# cat blog.conf
    3. server {
    4.   listen 80;
    5.   server_name blog.etiantian.org;
    6.   root html/blog;
    7.   location / {
    8.     index index.html index.htm;
    9.   }
    10.  
    11.   location ~ .*.(php|php5)?$ {
    12.     fastcgi_pass 127.0.0.1:9000;
    13.     fastcgi_index index.php;
    14.     include fastcgi.conf;
    15.   }
    16. }
    17. [root@lnmp extra]# ../../sbin/nginx -t
    18. nginx: the configuration file /application/nginx-1.6.1/conf/nginx.conf syntax is ok
    19. nginx: configuration file /application/nginx-1.6.1/conf/nginx.conf test is successful
    20. [root@lnmp extra]# ../../sbin/nginx -s reload
    21. [root@lnmp extra]# cd /application/nginx/html/blog/
    22. [root@lnmp blog]# cat phpinfo.php
    23. <?php
    24. phpinfo();
    25. ?>

    浏览器访问

    配置PHP与MySQL

    1. [root@lnmp blog]# pwd
    2. /application/nginx/html/blog
    3. [root@lnmp blog]# cat test_mysql.php
    4. <?php
    5. //$link_id=mysql_connect('主机名','用户','密码');
    6.   $link_id=mysql_connect('localhost','root','system');
    7. //$link_id=mysql_connect('localhost','test','');
    8.   if($link_id){
    9.     echo "mysql successful by oldboy!";
    10.   }else{
    11.     echo mysql_error();
    12.   }
    13. //php单行注释
    14. /*php多行注释*、
    15. ?>

    1. [root@lnmp blog]# rm -f phpinfo.php test_mysql.php

    部署一个BLOG程序服务

    https://cn.wordpress.org/

    WordPress是一套利用PHP语言和MySQL数据库开发的开源免费的Blog(博客、网站)程序,用户可以在支持PHP环境和MySQL数据库的服务器上建立Blog站点。

    WordPress是一个功能非常强大的博客系统,插件众多,易于扩展功能。安装和使用都非常方便。目前WordPress已经成为主流的Blog搭建平台,很多发布平台也是根据WordPress二次开发的。

    1. [root@lnmp blog]# pwd
    2. /application/nginx/html/blog
    3. [root@lnmp blog]# wget https://cn.wordpress.org/wordpress-4.7.2-zh_CN.tar.gz

    创建一个专用的数据库WordPress用于存放blog数据。

    1. [root@lnmp blog]# mysql -uroot -psystem
    2. mysql> create database wordpress;
    3. Query OK, 1 row affected (0.00 sec)
    4.  
    5. mysql> show databases;
    6. +--------------------+
    7. | Database |
    8. +--------------------+
    9. | information_schema |
    10. | mysql |
    11. | performance_schema |
    12. | wordpress |
    13. +--------------------+
    14. 4 rows in set (0.07 sec)

    创建一个管理的数据库WordPress的用户wordpress。

    1. mysql> grant all on wordpress.* to wordpress@'localhost' identified by 'system';
    2. Query OK, 0 rows affected (0.00 sec)
    3.  
    4. mysql> flush privileges;
    5. Query OK, 0 rows affected (0.01 sec)
    6. mysql> select user,host from mysql.user where user='wordpress';
    7. +-----------+-----------+
    8. | user | host |
    9. +-----------+-----------+
    10. | wordpress | localhost |
    11. +-----------+-----------+
    12. 1 row in set (0.00 sec)

    部署WordPress

    1. [root@lnmp blog]# tar xf wordpress-4.7.2-zh_CN.tar.gz
    2. [root@lnmp blog]# mv wordpress/* .
    3. [root@lnmp blog]# rm -fr wordpress
    4. [root@lnmp blog]# cd ../
    5. [root@lnmp html]# ll -ld blog/
    6. drwxr-xr-x 5 root root 4096 Feb 27 10:10 blog/
    7. [root@lnmp html]# find ./blog/ -type f|xargs chmod 644
    8. [root@lnmp html]# find ./blog/ -type d|xargs chmod 755
    9.  

    权限设置

    默认目录权限:dir 755 root root

    默认文件权限:file 644 root root

    用户目录权限:dir nginx nginx 755

    用户文件权限:file nginx nginx 644

    1. [root@lnmp html]# chown -R root.root blog/
    2. [root@lnmp html]# find ./blog/ -type f|xargs chmod 644
    3. [root@lnmp html]# find ./blog/ -type d|xargs chmod 755
    4. [root@lnmp html]# mkdir blog/wp-content/uploads
    5. [root@lnmp html]# chown -R nginx.nginx blog/wp-content/uploads/

    浏览器访问http://blog.etiantian.org/index.php

    根据提示创建wp-config.php,然后点击"进行安装"。

    数据库中查看安装结果

    1. [root@lnmp blog]# mysql -uroot -psystem
    2. Welcome to the MySQL monitor. Commands end with ; or g.
    3.  
    4. mysql> use wordpress
    5. Database changed
    6. mysql> show tables;
    7. Empty set (0.01 sec)
    8.  
    9. mysql> show tables;
    10. +-----------------------+
    11. | Tables_in_wordpress |
    12. +-----------------------+
    13. | ol_commentmeta |
    14. | ol_comments |
    15. | ol_links |
    16. | ol_options |
    17. | ol_postmeta |
    18. | ol_posts |
    19. | ol_term_relationships |
    20. | ol_term_taxonomy |
    21. | ol_termmeta |
    22. | ol_terms |
    23. | ol_usermeta |
    24. | ol_users |
    25. +-----------------------+
    26. 12 rows in set (0.00 sec)
    27.  
    28. mysql> select * from ol_users;
    29. +----+------------+------------------------------------+---------------+---------------+----------+---------------------+---------------------+-------------+--------------+
    30. | ID | user_login | user_pass | user_nicename | user_email | user_url | user_registered | user_activation_key | user_status | display_name |
    31. +----+------------+------------------------------------+---------------+---------------+----------+---------------------+---------------------+-------------+--------------+
    32. | 1 | root | $P$BbYLtgyYZ9xItWMJXp4rYB/ggABd9t/ | root | admin@163.com | | 2017-02-27 03:37:27 | | 0 | root |
    33. +----+------------+------------------------------------+---------------+---------------+----------+---------------------+---------------------+-------------+--------------+
    34. 1 row in set (0.00 sec)

     

  • 相关阅读:
    sqlserver优化查询
    DateADD日期Sql
    sql 数据字段类型
    sql Server 发送邮件 错误类型及原因
    EassyUI内置方法与属性
    这货不是 for循环
    1.switch选择结构 3.多重if选择结构和switch比对 4.总结选择结构 5.使用hasNextInt()解决用户从控制台输入为非整型问题
    1.基本选择结构if 2.逻辑运算符 3.if-else 4.多重if选择结构 5.嵌套if选择结构
    编写java程序步骤
    css3
  • 原文地址:https://www.cnblogs.com/yinshoucheng-golden/p/6474034.html
Copyright © 2011-2022 走看看