原文出處:
https://blog.csdn.net/hemin1003/article/details/77434862
關于php-fpm
nginx本身不能處理PHP,它隻是個web伺服器,當接收到請求後,如果是php請求,則發給php解釋器處理,并把結果傳回給用戶端。
nginx一般是把請求發fastcgi管理程序處理,fascgi管理程序選擇cgi子程序處理結果并傳回被nginx。
PHP-FPM是一個PHP FastCGI管理器,是隻用于PHP的。
PHP在 5.3.3 之後已經講php-fpm寫入php源碼核心了。是以已經不需要另外下載下傳了。
擷取PHP下載下傳位址
打開php的官網:http://php.NET/,檢視php的版本清單
右擊,複制連結位址,在遠端主機登入,下載下傳該軟體(我選的是Australia的主機mirror下載下傳的)
# wget http://au1.php.net/get/php-5.6.30.tar.gz/from/this/mirror
下載下傳下來的是一個mirror檔案,改成我們需要的檔案名
#mv mirror php-5.6.30.tar.gz
#tar zxvf php-5.6.30.tar.gz
#cd php-5.6.30
配置安裝
進入到目錄,我們需要在安裝的時候将安裝目錄配置到/usr/local/php/裡
#./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-MySQL --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip
配置的過程中可能會報如下錯誤
錯誤1:
xml2-config not found. Please check your libxml2 installation.
解決辦法
安裝libxml2相關元件
#yum install libxml2
#yum install libxml2-devel -y
錯誤2:
Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/
安裝curl相關元件
#yum install curl curl-devel
錯誤3:
configure: error: png.h not found.
安裝libpng相關元件
#yum install libpng
#yum install libpng-devel
錯誤4:
freetype-config not found.
安裝freetype相關元件
#yum install freetype-devel
錯誤5:
xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
安裝libxslt相關元件
#yum install libxslt-devel
好的,當我們看到下面這句話的時候,說明你的php已經配置完成啦!
接下來我們隻需要編譯安裝即可完成php的安裝
#make && make install
看到這句話,表明安裝完成!
為了保險起見,我們make test一把,看看是否真的成功了。
配置相關
php.ini配置
首先我們需要配置的是php.ini這個檔案
安裝目錄有2個檔案:php.ini-development和php.ini-production
php.ini-production 線上版本使用
php.ini-development 開發版本使用
我們選擇production進行配置
# cp -a php.ini-production /usr/local/php/etc/php.ini //拷貝安裝包裡的php配置檔案到安裝目錄下
# rm -rf /etc/php.ini //删除預設的php配置檔案
# ln -s /usr/local/php/etc/php.ini /etc/php.ini //建立軟連結
php-fpm配置
拷貝php-fpm啟動配置檔案
# cp -a ./sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf //拷貝安裝包裡的php-fpm配置檔案到安裝目錄下
# cp -a ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm //拷貝啟動檔案
啟動
service php-fpm start
檢視php是否啟動成功
#ps aux | grep php
看到這些,表明你的php已經啟動成功啦!
重新開機及關閉
service php-fpm restart service php-fpm stop
配置Nginx支援PHP
進入nginx主目錄,需要修改nginx.conf
#vim nginx.conf
代開下面代碼,讓Nginx支援PHP,在server代碼段裡。
修改完,這段代碼變為,紅色部分為我們主機目錄為/mnt/project,需要修改fastcgi_param SCRIPT_FILENAME指向對應目錄即可:
location ~ \.php$ {
root /mnt/project; //項目根目錄
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /mnt/project$fastcgi_script_name; 在$符前面加上項目根目錄
include fastcgi_params;
}
設定主目錄設定為/mnt/project。
location / {
root /mnt/project; //項目根目錄
index index.html index.php;
}
儲存退出。
根據Nginx章的解釋,我們重新開機Nginx服務。
#/etc/init.d/nginx restart
如果你沒有按照我們在Nginx的方法配置,可以按照以下的方式重新開機Nginx服務
# /usr/local/nginx/sbin/nginx -s reload
重新開機成功!下面我們在/mnt/project目錄下添加一個新檔案。
#vim /mnt/project/phpinfo.php
插入以下内容
<?php
phpinfo();
?>
在浏覽器中打開http://遠端ip/phpinfo.php
看到這個頁面,恭喜你,你的PHP已經安裝配置完成了。
補充說明:
1. 關于網址重寫功能以及樣式顯示問題,請參考:
https://book.cakephp.org/2.0/zh/installation/url-rewriting.html
https://yq.aliyun.com/ziliao/105485
2. 遇到問題:"It is not safe to rely on the system's timezone settings........",請參考:
http://php.net/manual/en/function.date-default-timezone-set.php
http://www.jb51.net/article/73059.htm
3. 遇到問題:“ php-fpm Permission denied”,請參考:
http://blog.csdn.net/k178441367/article/details/51613042,或者賦予php-fpm可執行權限
4. 遇到問題:“Fatal error: Uncaught exception 'CacheException' with message 'Cache engine _cake_core_ is not properly configured”,請參考:
https://stackoverflow.com/questions/42002751/uncaught-exception-cacheexception-with-message-cache-engine-cake-core-is
https://stackoverflow.com/questions/29408227/uncaught-exception-cacheexception-with-message-cache-engine-cake-core-is-no
http://bbs.csdn.net/topics/391014151
最後
附上我的nginx配置檔案内容:cakephp項目源碼下載下傳(github的一個添删查改功能,cakephp官方教程位址)
-
#user nobody;
-
worker_processes 1;
-
#error_log logs/error.log;
-
#error_log logs/error.log notice;
-
#error_log logs/error.log info;
-
#pid logs/nginx.pid;
-
events {
-
worker_connections 1024;
-
}
-
http {
-
include mime.types;
-
default_type application/octet-stream;
-
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
-
# '$status $body_bytes_sent "$http_referer" '
-
# '"$http_user_agent" "$http_x_forwarded_for"';
-
#access_log logs/access.log main;
-
sendfile on;
-
#tcp_nopush on;
-
#keepalive_timeout 0;
-
keepalive_timeout 65;
-
#gzip on;
-
server {
-
listen 88;
-
server_name localhost;
-
root /mnt/project/cakephp/app/webroot;
-
index index.php;
-
#charset koi8-r;
-
#access_log logs/host.access.log main;
-
location / {
-
try_files $uri $uri/ /index.php?$uri&$args;
-
}
-
#error_page 404 /404.html;
-
# redirect server error pages to the static page /50x.html
-
#
-
error_page 500 502 503 504 /50x.html;
-
location = /50x.html {
-
root html;
-
}
-
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
-
#
-
#location ~ \.php$ {
-
# proxy_pass http://127.0.0.1;
-
#}
-
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
-
#
-
location ~ \.php$ {
-
root /mnt/project/cakephp/app/webroot;
-
try_files $uri =404;
-
fastcgi_pass 127.0.0.1:9000;
-
fastcgi_index index.php;
-
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
-
include fastcgi_params;
-
fastcgi_buffer_size 128k;
-
fastcgi_buffers 256 4k;
-
fastcgi_busy_buffers_size 256k;
-
fastcgi_temp_file_write_size 256k;
-
}
-
# deny access to .htaccess files, if Apache's document root
-
# concurs with nginx's one
-
#
-
#location ~ /\.ht {
-
# deny all;
-
#}
-
}
-
# another virtual host using mix of IP-, name-, and port-based configuration
-
#
-
#server {
-
# listen 8000;
-
# listen somename:8080;
-
# server_name somename alias another.alias;
-
# location / {
-
# root html;
-
# index index.html index.htm;
-
# }
-
#}
-
# HTTPS server
-
#
-
#server {
-
# listen 443 ssl;
-
# server_name localhost;
-
# ssl_certificate cert.pem;
-
# ssl_certificate_key cert.key;
-
# ssl_session_cache shared:SSL:1m;
-
# ssl_session_timeout 5m;
-
# ssl_ciphers HIGH:!aNULL:!MD5;
-
# ssl_prefer_server_ciphers on;
-
# location / {
-
# root html;
-
# index index.html index.htm;
-
# }
-
#}
-
}
推薦一個很不錯的視訊教程店,裡面東西還是挺不錯的,店主是個實在人:
https://shop60974085.taobao.com/