什麼是CGI
CGI全稱"通用網關接口"(Common Gateway Interface),用於HTTP服務器與其它機器上的程序服務通訊交流的一種工具,CGI程序須運行在網絡服務器上。 php
傳統CGI接口方式的主要缺點是性能較差,由於每次HTTP服務器遇到動態程序時都須要重啓解析器來執行解析,而後結果被返回給HTTP服務器。這在處理高併發訪問幾乎是不可用的,所以就誕生了FastCGI。另外傳統的CGI接口方式安全性也不好。 html
什麼是FastCGI
FastCGI是一個可伸縮地、高速地在HTTP服務器和動態腳本語言間通訊的接口(FastCGI接口在Linux下是socket(能夠是文件socket,也能夠是ip socket)),主要優勢是把動態語言和HTTP服務器分離開來。多數流行的HTTP服務器都支持FastCGI,包括Apache、Nginx和lightpd。 mysql
同時,FastCGI也被許多腳本語言所支持,比較流行的腳本語言之一爲PHP。FastCGI接口方式採用C/S架構,能夠將HTTP服務器和腳本解析服務器分開,同時在腳本解析服務器上啓動一個或多個腳本解析守護進程。當HTTP服務器每次遇到動態程序時,能夠將其直接交付給FastCGI進程執行,而後將獲得的結構返回給瀏覽器。這種方式可讓HTTP服務器專注地處理靜態請求或者將動態腳本服務器的結果返回給客戶端,這在很大程度上提升了整個應用系統的性能。 nginx
FastCGI的重要特色: 程序員
一、FastCGI是HTTP服務器和動態腳本語言間通訊的接口或者工具。 sql
二、FastCGI優勢是把動態語言解析和HTTP服務器分離開來。 數據庫
三、Nginx、Apache、Lighttpd以及多數動態語言都支持FastCGI。 api
四、FastCGI接口方式採用C/S架構,分爲客戶端(HTTP服務器)和服務端(動態語言解析服務器)。 瀏覽器
五、PHP動態語言服務端能夠啓動多個FastCGI的守護進程。 安全
六、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 函數庫,所以須要安裝下面的庫。
-
[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庫
-
[root@lnmp tools]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
-
[root@lnmp tools]# tar zxf libiconv-1.14.tar.gz
-
[root@lnmp tools]# cd libiconv-1.14
-
[root@lnmp libiconv-1.14]# ./configure --prefix=/usr/local/libiconv
-
[root@lnmp libiconv-1.14]# make && make install
安裝libmcrypt庫
-
[root@lnmp tools]# wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
-
[root@lnmp libmcrypt-2.5.8]# ./configure
-
[root@lnmp libmcrypt-2.5.8]# make && make install
快速安裝方法:
安裝epel源
-
[root@lnmp ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
安裝PHP依賴包
-
[root@lnmp ~]# yum -y install libxml2-devel gd-devel libcurl-devel libxslt-devel libmcrypt-devel mhash mcrypt
安裝libicov
同上。
安裝libmcrypt
-
[root@lnmp yum.repos.d]# yum install libmcrypt-devel -y
-
[root@lnmp yum.repos.d]# rpm -qa libmcrypt-devel
-
libmcrypt-devel-2.5.8-9.el6.x86_64
安裝mhash加密擴展庫
Mhash是基於離散數學原理的不可逆向的php加密方式擴展庫,其在默認狀況下不開啓。mhash能夠用於建立校驗數值、消息摘要、消息認證碼,以及無需原文的關鍵信息保持(如密碼)等。
-
[root@lnmp ~]# yum install mhash mhash-devel -y
-
[root@lnmp ~]# rpm -qa mhash mhash-devel
-
mhash-devel-0.9.9.9-3.el6.x86_64
-
mhash-0.9.9.9-3.el6.x86_64
安裝mcrypt加密擴展庫
PHP程序員在編寫代碼程序時,除了要保證代碼的高性能以外,還有一點是很是重要的,那就是程序的安全性保障。PHP除了自帶的幾種加密函數外,還有功能更全面的PHP加密擴展庫mcrypt和mhash。
-
[root@lnmp ~]# yum install mcrypt -y
-
[root@lnmp ~]# rpm -qa mcrypt
-
mcrypt-2.6.8-10.el6.x86_64
安裝PHP
lnmp服務器
-
[root@lnmp tools]# tar xf php-5.3.28.tar.gz
-
[root@lnmp tools]# ls -ld php-5.3.28
-
drwxr-xr-x 13 nginx games 4096 Dec 11 2013 php-5.3.28
-
[root@lnmp tools]# cd php-5.3.28
編譯安裝
-
###################輸入如下內容
-
./configure \
-
--prefix=/application/php5.3.28 \
-
--with-mysql=/application/mysql \ #使用php自帶mysql參數:--with-mysql=mysqlnd
-
--with-iconv-dir=/usr/local/libiconv \
-
--with-freetype-dir \
-
--with-jpeg-dir \
-
--with-png-dir \
-
--with-zlib \
-
--with-libxml-dir=/usr \
-
--enable-xml \
-
--disable-rpath \
-
--enable-safe-mode \
-
--enable-bcmath \
-
--enable-shmop \
-
--enable-sysvsem \
-
--enable-inline-optimization \
-
--with-curl \
-
--with-curlwrappers \
-
--enable-mbregex \
-
--enable-fpm \
-
--enable-mbstring \
-
--with-mcrypt \
-
--with-gd \
-
--enable-gd-native-ttf \
-
--with-openssl \
-
--with-mhash \
-
--enable-pcntl \
-
--enable-sockets \
-
--with-xmlrpc \
-
--enable-zip \
-
--enable-soap \
-
--enable-short-tags \
-
--enable-zend-multibyte \
-
--enable-static \
-
--with-xsl \
-
--with-fpm-user=nginx \
-
--with-fpm-group=nginx \
-
--enable-ftp
-
####################編譯結果
-
creating main/internal_functions.c
-
creating main/internal_functions_cli.c
-
+--------------------------------------------------------------------+
-
| License: |
-
| This software is subject to the PHP License, available in this |
-
| distribution in the file LICENSE. By continuing this installation |
-
| process, you are bound by the terms of this license agreement. |
-
| If you do not agree with the terms of this license, you must abort |
-
| the installation process at this point. |
-
+--------------------------------------------------------------------+
-
-
Thank you for using PHP.
-
-
[root@lnmp php-5.3.28]# echo $?
-
0
-
[root@lnmp php-5.3.28]# make
-
/usr/bin/ld: cannot find -lltdl
-
collect2: ld returned 1 exit status
-
make: *** [sapi/fpm/php-fpm] Error 1
-
[root@lnmp php-5.3.28]# echo $?
-
2
cannot find –lltdl解決辦法,須要安裝libltdl。
-
[root@lnmp tools]# cd libmcrypt-2.5.8/libltdl/
-
[root@lnmp libltdl]# ./configure -enable-ltdl-install
-
[root@lnmp libltdl]# make && make install
從新回到PHP的安裝目錄下進行查詢編譯PHP。
-
[root@lnmp php-5.3.28]# make
-
clicommand.inc
-
pharcommand.inc
-
directorytreeiterator.inc
-
invertedregexiterator.inc
-
phar.inc
-
-
Build complete.
-
Don't forget to run 'make test'.
-
-
[root@lnmp php-5.3.28]# echo $?
-
0
-
[root@lnmp php-5.3.28]# make install
建立軟鏈接
-
/home/oldboy/tools/php-5.3.28/build/shtool install -c ext/phar/phar.phar /application/php5.3.28/bin
-
ln -s -f /application/php5.3.28/bin/phar.phar /application/php5.3.28/bin/phar
-
Installing PDO headers: /application/php5.3.28/include/php/ext/pdo/
-
[root@lnmp php-5.3.28]# echo $?
-
0
-
[root@lnmp php-5.3.28]# ln -s /application/php5.3.28/ /application/php
mysql服務器
-
[root@mysql tools]# tar xf php-5.5.38.tar.gz
-
[root@mysql tools]# ls -ld php-5.5.38
-
drwxr-xr-x 14 1000 1000 4096 Jul 20 2016 php-5.5.38
-
[root@mysql tools]# cd php-5.5.38
編譯安裝
-
#############輸入如下內容
-
./configure \
-
--prefix=/application/php5.5.38 \
-
--with-mysql=mysqlnd \
-
--with-iconv-dir=/usr/local/libiconv \
-
--with-freetype-dir \
-
--with-jpeg-dir \
-
--with-png-dir \
-
--with-zlib \
-
--with-libxml-dir=/usr \
-
--enable-xml \
-
--disable-rpath \
-
--enable-bcmath \
-
--enable-shmop \
-
--enable-sysvsem \
-
--enable-inline-optimization \
-
--with-curl \
-
--enable-mbregex \
-
--enable-fpm \
-
--enable-mbstring \
-
--with-mcrypt \
-
--with-gd \
-
--enable-gd-native-ttf \
-
--with-openssl \
-
--with-mhash \
-
--enable-pcntl \
-
--enable-sockets \
-
--with-xmlrpc \
-
--enable-soap \
-
--enable-short-tags \
-
--enable-static \
-
--with-xsl \
-
--with-fpm-user=nginx \
-
--with-fpm-group=nginx \
-
--enable-ftp
-
############編譯結果
-
-
Thank you for using PHP.
-
-
config.status: creating php5.spec
-
config.status: executing default commands
-
[root@mysql php-5.5.38]# echo $?
-
0
-
[root@mysql php-5.5.38]# make
-
invertedregexiterator.inc
-
clicommand.inc
-
directorytreeiterator.inc
-
directorygraphiterator.inc
-
pharcommand.inc
-
phar.inc
-
-
Build complete.
-
Don't forget to run 'make test'.
-
-
[root@mysql php-5.5.38]# echo $?
-
0
-
[root@mysql php-5.5.38]# make install
-
ln -s -f phar.phar /application/php5.5.38/bin/phar
-
Installing PDO headers: /application/php5.5.38/include/php/ext/pdo/
-
[root@mysql php-5.5.38]# echo $?
-
0
建立軟鏈接
-
[root@mysql php-5.5.38]# ln -s /application/php5.5.38/ /application/php
-
[root@mysql php-5.5.38]# ls -ld /application/php
-
lrwxrwxrwx 1 root root 23 Feb 26 16:08 /application/php -> /application/php5.5.38/
配置PHP引擎配置文件php.ini
-
[root@lnmp php-5.3.28]# cp php.ini-production /application/php/lib/php.ini
配置PHP服務(FastCGI模式)配置文件php-fpm.conf
-
[root@lnmp php-5.3.28]# cd /application/php/etc/
-
[root@lnmp etc]# ls
-
pear.conf php-fpm.conf.default
-
[root@lnmp etc]# cp php-fpm.conf.default php-fpm.conf
-
[root@lnmp etc]# ls
-
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
-
[root@lnmp etc]# /application/php/sbin/php-fpm
-
[root@lnmp etc]# ps -ef|grep php-fpm
-
root 1695 1 0 17:32 ? 00:00:00 php-fpm: master process (/application/php5.3.28/etc/php-fpm.conf)
-
nginx 1696 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1697 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1698 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1699 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1700 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1701 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1702 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1703 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1704 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1705 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1706 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1707 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1708 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1709 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1710 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
nginx 1711 1695 0 17:32 ? 00:00:00 php-fpm: pool www
-
root 1713 1360 0 17:33 pts/0 00:00:00 grep php-fpm
-
[root@lnmp etc]# ps -ef|grep php-fpm|wc -l
-
18
查看端口
-
[root@lnmp etc]# netstat -lntup|grep php-fpm
-
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1695/php-fpm
-
[root@lnmp etc]# ss -lntup|grep php-fpm
-
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
-
[root@lnmp ~]# cd /application/nginx/conf/extra/
-
[root@lnmp extra]# cat blog.conf
-
server {
-
listen 80;
-
server_name blog.etiantian.org;
-
root html/blog;
-
location / {
-
index index.html index.htm;
-
}
-
-
location ~ .*\.(php|php5)?$ {
-
fastcgi_pass 127.0.0.1:9000;
-
fastcgi_index index.php;
-
include fastcgi.conf;
-
}
-
}
-
[root@lnmp extra]# ../../sbin/nginx -t
-
nginx: the configuration file /application/nginx-1.6.1/conf/nginx.conf syntax is ok
-
nginx: configuration file /application/nginx-1.6.1/conf/nginx.conf test is successful
-
[root@lnmp extra]# ../../sbin/nginx -s reload
-
[root@lnmp extra]# cd /application/nginx/html/blog/
-
[root@lnmp blog]# cat phpinfo.php
-
<?php
-
phpinfo();
-
?>
瀏覽器訪問
配置PHP與MySQL
-
[root@lnmp blog]# pwd
-
/application/nginx/html/blog
-
[root@lnmp blog]# cat test_mysql.php
-
<?php
-
//$link_id=mysql_connect('主機名','用戶','密碼');
-
$link_id=mysql_connect('localhost','root','system');
-
//$link_id=mysql_connect('localhost','test','');
-
if($link_id){
-
echo "mysql successful by oldboy!";
-
}else{
-
echo mysql_error();
-
}
-
//php單行註釋
-
/*php多行註釋*、
-
?>
-
[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二次開發的。
-
[root@lnmp blog]# pwd
-
/application/nginx/html/blog
-
[root@lnmp blog]# wget https://cn.wordpress.org/wordpress-4.7.2-zh_CN.tar.gz
建立一個專用的數據庫WordPress用於存放blog數據。
-
[root@lnmp blog]# mysql -uroot -psystem
-
mysql> create database wordpress;
-
Query OK, 1 row affected (0.00 sec)
-
-
mysql> show databases;
-
+--------------------+
-
| Database |
-
+--------------------+
-
| information_schema |
-
| mysql |
-
| performance_schema |
-
| wordpress |
-
+--------------------+
-
4 rows in set (0.07 sec)
建立一個管理的數據庫WordPress的用戶wordpress。
-
mysql> grant all on wordpress.* to wordpress@'localhost' identified by 'system';
-
Query OK, 0 rows affected (0.00 sec)
-
-
mysql> flush privileges;
-
Query OK, 0 rows affected (0.01 sec)
-
mysql> select user,host from mysql.user where user='wordpress';
-
+-----------+-----------+
-
| user | host |
-
+-----------+-----------+
-
| wordpress | localhost |
-
+-----------+-----------+
-
1 row in set (0.00 sec)
部署WordPress
-
[root@lnmp blog]# tar xf wordpress-4.7.2-zh_CN.tar.gz
-
[root@lnmp blog]# mv wordpress/* .
-
[root@lnmp blog]# rm -fr wordpress
-
[root@lnmp blog]# cd ../
-
[root@lnmp html]# ll -ld blog/
-
drwxr-xr-x 5 root root 4096 Feb 27 10:10 blog/
-
[root@lnmp html]# find ./blog/ -type f|xargs chmod 644
-
[root@lnmp html]# find ./blog/ -type d|xargs chmod 755
-
權限設置
默認目錄權限:dir 755 root root
默認文件權限:file 644 root root
用戶目錄權限:dir nginx nginx 755
用戶文件權限:file nginx nginx 644
-
[root@lnmp html]# chown -R root.root blog/
-
[root@lnmp html]# find ./blog/ -type f|xargs chmod 644
-
[root@lnmp html]# find ./blog/ -type d|xargs chmod 755
-
[root@lnmp html]# mkdir blog/wp-content/uploads
-
[root@lnmp html]# chown -R nginx.nginx blog/wp-content/uploads/
瀏覽器訪問http://blog.etiantian.org/index.php
根據提示建立wp-config.php,而後點擊"進行安裝"。
數據庫中查看安裝結果
-
[root@lnmp blog]# mysql -uroot -psystem
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
-
mysql> use wordpress
-
Database changed
-
mysql> show tables;
-
Empty set (0.01 sec)
-
-
mysql> show tables;
-
+-----------------------+
-
| Tables_in_wordpress |
-
+-----------------------+
-
| ol_commentmeta |
-
| ol_comments |
-
| ol_links |
-
| ol_options |
-
| ol_postmeta |
-
| ol_posts |
-
| ol_term_relationships |
-
| ol_term_taxonomy |
-
| ol_termmeta |
-
| ol_terms |
-
| ol_usermeta |
-
| ol_users |
-
+-----------------------+
-
12 rows in set (0.00 sec)
-
-
mysql> select * from ol_users;
-
+----+------------+------------------------------------+---------------+---------------+----------+---------------------+---------------------+-------------+--------------+
-
| ID | user_login | user_pass | user_nicename | user_email | user_url | user_registered | user_activation_key | user_status | display_name |
-
+----+------------+------------------------------------+---------------+---------------+----------+---------------------+---------------------+-------------+--------------+
-
| 1 | root | $P$BbYLtgyYZ9xItWMJXp4rYB/ggABd9t/ | root | admin@163.com | | 2017-02-27 03:37:27 | | 0 | root |
-
+----+------------+------------------------------------+---------------+---------------+----------+---------------------+---------------------+-------------+--------------+
-
1 row in set (0.00 sec)