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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM