❶ nginx配置支持php
nginx本身不支持php解析,需要配合php-fpm來配置。
location~.php${
root/var/www;#指定php的根目錄
fastcgi_pass127.0.0.1:9000;#php-fpm的默認埠是9000
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
includefastcgi_params;
}
centos下安裝php-fpm (php及其它組件已經安裝過的情況)
yuminstallphp-fpm
啟動php-fpm 並設置開機啟動 (centos 7)
systemctlstartphp-fpm
systemctlenablephp-fpm
❷ mac配置php-fpm,nginx運行多版本php
1、brew 安裝 php5.6 php5.7 nginx
2、配置php-conf
3、配置虛擬主機
安裝好brew
用brew 命令安裝,如果速度太慢或訪問不了,自行goole brew 換源
brew search php 查看可用的php版本
brew install [email protected] 安裝php5.6
brew install [email protected] 安裝php5.6
brew install nginx 安裝nginx
1、修改php5.6 php-fpm的埠為9056
cd usr/local/etc/php/5.6 # 到php5.6的目錄下
vi php-fpm.conf # 修改文件
listen = 127.0.0.1:9056 # 修改此埠
daemonize = yes # 修改為允許後台啟動php-fpm
2、修改php5.6 php-fpm的埠為9070
cd /usr/local/etc/php/7.1/php-fpm.d # 到php7.1的目錄下
vi www.conf # 修改埠
listen = 127.0.0.1:9056 # 修改此埠
vi php-fpm.conf # 修改文件
daemonize = yes # 修改為允許後台啟動php-fpm
3、啟動php-fpm
cd /usr/local/sbin # 到此目錄,建立兩個軟鏈接指向不同版本的php
切換到root用戶
./php-fpm56
./php-fpm71
啟動後可看到php-fpm的進程,則成功
ps-ef | grep php-fpm
cd /usr/local/etc/nginx/ # 到nginx的目錄下
復制默認的配置文件到server下(此目錄用來存虛擬主機文件)
這里我在server創建了這兩個
vi local.phpinfo56.com.conf # 修改本地域名和nginx代理到php-fpm埠,按照這種方法修改另一個
nginx # 啟動nginx
nginx -s reload # 修改配置文件,重新載入nginx
vi /etc /hosts # 修改host 加上映射關系
cd /usr /local/var/www # 在此目錄下建立一個index.php
echo "<?php phpinfo();" > index.php
在瀏覽器訪問可看到
❸ 雲伺服器如何配置nginx支持php
[root@redhat7 ~]# wget http://am1.php.net/get/php-7.1.2.tar.gz/from/this/mirror
[root@redhat7 ~]# tar xzvf php-7.1.2.tar.gz
[root@redhat7 ~]# cd php-7.1.2/
[root@redhat7 ~]# ./configure --prefix=/usr/local/php --enable-fpm
[root@redhat7 php-7.1.2]# make&&make install
查看是否成功編譯安裝PHP
[root@redhat7 php-7.1.2]# php -v
PHP 7.1.2 (fpm-fcgi) (built: Apr 14 2017 20:21:53)
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
編譯安裝完成後PHP不具備配置文件php.ini,此時只需復制php.ini-proction到 /usr/local/lib/php.ini即可,php.ini文件一般在/usr/local/lib/和/etc目錄下
[root@localhost php-7.1.2]# cp php.ini-proction /usr/local/lib/php.ini
[root@redhat7 php]# /usr/local/php/sbin/php-fpm
[14-Apr-2017 20:59:49] ERROR: failed to open configuration file '/usr/local/php/etc/php-fpm.conf': No such file or directory (2)
[14-Apr-2017 20:59:49] ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'
[14-Apr-2017 20:59:49] ERROR: FPM initialization failed
啟動php-fpm發現缺乏配置文件/usr/local/php/etc/php-fpm.conf
此時只需復制php-fpm的配置文件在安裝php時提供的配置文件的模版/usr/local/php/etc/php-fpm.conf.default到相應/usr/local/php/etc/php-fpm.conf即可
[root@redhat7 etc]# /usr/local/php/sbin/php-fpm
[14-Apr-2017 21:14:32] WARNING: Nothing matches the include pattern '/usr/local/php/etc/php-fpm.d/﹡.conf' from /usr/local/php/etc/php-fpm.conf at line 125.
[14-Apr-2017 21:14:32] ERROR: No pool defined. at least one pool section must be specified in config file
[14-Apr-2017 21:14:32] ERROR: failed to post process the configuration
[14-Apr-2017 21:14:32] ERROR: FPM initialization failed
[root@redhat7 etc]# cp php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@redhat7 etc]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@redhat7 etc]# /etc/init.d/php-fpm
[14-Apr-2017 21:23:02] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[14-Apr-2017 21:23:02] ERROR: FPM initialization failed
[root@redhat7 etc]# netstat -nldp|grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 3721/php-fpm: maste
[root@redhat7 php-7.1.2]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@redhat7 php-7.1.2]# chmod a+x /etc/init.d/php-fpm
[root@redhat7 php-7.1.2]# ll /etc/init.d/php-fpm
-rwxr-xr-x 1 root root 2401 4月 14 21:26 /etc/init.d/php-fpm
[root@redhat7 php-7.1.2]# /etc/init.d/php-fpm start
Starting php-fpm [14-Apr-2017 21:28:09] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[14-Apr-2017 21:28:09] ERROR: FPM initialization failed
failed
[root@redhat7 php-7.1.2]# netstat -nldp |grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 3721/php-fpm: maste
[root@redhat7 php-7.1.2]# kill 3721
[root@redhat7 php-7.1.2]# netstat -nldp |grep 9000
[root@redhat7 php-7.1.2]# /etc/init.d/php-fpm start
Starting php-fpm done
[root@redhat7 php-7.1.2]# service php-fpm status
php-fpm (pid 3927) is running...
[root@redhat7 php-7.1.2]# chkconfig --add php-fpm
[root@redhat7 php-7.1.2]# chkconfig php-fpm --level 345 on
配置nginx支持PHP
修改nginx的配置文件,支持php文件的解析,找到location的添加位置,在後面添加下面這個location
location ~ .php$ {
root /usr/share/nginx/html; #指定php的根目錄
fastcgi_pass 127.0.0.1:9000;#php-fpm的默認埠是9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
❹ 求教docker下部署nginx和php-fpm,但fpm無法被解析
配置如下:
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 172.17.0.178:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}