关于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下载的)
配置安装
进入到目录,我们需要在安装的时候将安装目录配置到/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相关组件
错误2:
Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/
安装curl相关组件
错误3:
configure: error: png.h not found.
安装libpng相关组件
错误4:
freetype-config not found.
安装freetype相关组件
错误5:
xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
安装libxslt相关组件
#yum install libxslt-devel
好的,当我们看到下面这句话的时候,说明你的PHP已经配置完成啦!
接下来我们只需要编译安装即可完成PHP的安装
看到这句话,表明安装完成!
为了保险起见,我们make test一把,看看是否真的成功了。
配置相关
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配置
# 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是否启动成功
看到这些,表明你的PHP已经启动成功啦!
重启及关闭
配置Nginx支持PHP
#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 index index.html index.PHP;
}
保存退出。
#/etc/init.d/Nginx restart
如果你没有按照我们在Nginx的方法配置,可以按照以下的方式重启Nginx服务
重启成功!下面我们在/mnt/project目录下添加一个新文件。
插入以下内容
在浏览器中打开http://远程ip/PHPinfo.PHP
补充说明:
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. 遇到问题:“Fatalerror:Uncaughtexception'CacheException'withmessage'Cacheengine_cake_core_isnotproperlyconfigured”,请参考:
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; # } #} }