nginxphp服务器搭建
A. 如何正确配置Nginx+php
其实没多复杂
1. 将nginx和php都装好了
2. 然后配置nginx,将php请求分发给php-fpm处理
linux下的配置文件一般在/usr/local/nginx/conf/nginx.conf
找到下面字样,并取消注释,且注意这个$document_root这个地方(原本应为$script***的,改成$document_root)
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
3.启动nginx和php-fpm,然后写个phpinfo脚本测试下成功与否就可以了
B. Thinkphp5项目在nginx服务器部署
1,切换到nginx的配置目录,找到nginx.conf文件
cd /usr/local/nginx/conf
vim nginx.conf
2,如果是单项目部署的话,只需要在nginx.conf文件里面加上以下
server{
listen 80;
# 域名,本地测试可以使用127.0.0.1或localhost
server_name www.zhangc.cn;
# php项目根目录
root /home/data-www/blog;
location /{
# 定义首页索引文件的名称
index index.php index.html index.htm;
# 影藏入口文件
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
try_files $uri $uri/ /index.php?$query_string;
}
# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
# Fastcgi服务器和程序(PHP)沟通的协议
.location ~ .*\.php${
# 设置监听端口
fastcgi_pass 127.0.0.1:9000;
# 设置nginx的默认首页文件
fastcgi_index index.php;
# 设置脚本文件请求的路径
fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;
# 引入fastcgi的配置文件
include fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
try_files $fastcgi_script_name =404;
}
}
3,如果多项目部署,就需要配置vhost
第一步:编辑nginx.conf文件,在最后加上 include vhost/*.conf;
第二步:进入vhost文件夹,创建 域名.conf 文件,如创建一个:quanma.meyat.com.conf
第三步:编辑quanma.meyat.com.conf文件,内容如下:
server
{
listen 80;
server_name quanma.meyat.com;
index index.html index.htm index.php default.html default.htm default.php;
root /data/wwwroot/default/quanma/public/;
#error_page 404 /404.html;
location / {
index index.html index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
try_files $uri $uri/ /index.php?$query_string;
}
location ~ [^/]\.php(/|$)
{
# comment try_files $uri =404; to enable pathinfo
#try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
try_files $fastcgi_script_name =404;
#include fastcgi.conf;
#include pathinfo.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
# Disallow access to .ht, .svn, .bzr, .git, .hg, .cvs directories
location ~ /\.(ht|svn|bzr|git|hg|cvs) {
deny all;
}
#access_log /date/nginx/bmp.com.conf/access.log main;
}
C. 云服务器如何配置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;
}
D. 如何正确配置Nginx+PHP
1、Nginx安装
安装编译软件库
[root@leixuesong ~]# yum -y install gcc gcc-c++ autoconf automake openssl openssl-devel pcre-devel zlib-devel
下载nginx 1.6.2源码包
[root@leixuesong ~]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
解压nginx
[root@leixuesong ~]# tar zxvf nginx-1.6.2.tar.gz
进入解压文件夹
[root@leixuesong ~]# cd nginx-1.6.2
编译设置模块
[root@leixuesong nginx-1.6.2]# ./configure –with-http_stub_status_mocule –prefix=/opt/nginx
安装
[root@leixuesong nginx-1.6.2]# make && make install
nginx编译安装性能优化
1、在nginx 源码文件找到auto/cc/gcc文件找到如下几行
#debug
[root@leixuesong ~]# CFLASS=”$CFLAGS -g”
2、特定CPU优化CPU类型编译
–with-cc-opt=”-O3″
–with-cpu-opt=CPU类型
查看CPU类型
[root@leixuesong ~]# cat /proc/cpuinfo | grep “model name”
nginx重新启动
[root@leixuesong ~]# /opt/nginx/sbin/nginx -s reload
nginx启动
[root@leixuesong ~]# /opt/nginx/sbin/nginx
nginx检测配置
[root@leixuesong ~]# /opt/nginx/sbin/nginx -t
2、PHP的安装和配置
安装php依赖软件库
[root@leixuesong ~]# yum install -y gcc-c++ libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel mysql mysql-devel
下载php5.6源码包
[root@leixuesong ~]# wget http://am1.php.net/distributions/php-5.6.3.tar.gz
解压
[root@leixuesong ~]# tar -zxvf php-5.6.3.tar.gz
[root@leixuesong ~]# cd php-5.6.3
编译设置模块
[root@leixuesong php-5.6.3]# ./configure –prefix=/usr/local/php –with-mysql=mysqlnd–with-mysqli=mysqlnd –enable-fpm –with-pear –with-curl –with-gd –with-jpeg-dir –with-png-dir –with-freetype-dir –with-iconv –with-mhash –with-zlib –enable-mbstring –disable-debug
make && make install
复制php配置文件
[root@leixuesong php-5.6.3]# cp php.ini-proction /opt/php/lib/php.ini
启动php-fpm
[root@leixuesong php-5.6.3]# /opt/php/sbin/php-fpm
检测配置是否正确
[root@leixuesong php-5.6.3]# /opt/php/sbin/php-fpm -t
在centos上成功编译安装nginx 1.6、php 5.6并成功启动nginx和php-fpm后,访问php提示”File not found.”,同时在错误日志中看到:
2013/10/22 20:05:49 [error] 12691#0: *6 FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream, client: 192.168.168.1, server: localhost, request: “GET / HTTP/1.1″, upstream: “fastcgi://127.0.0.1:9000″, host: “192.168.168.133”:
在Nginx配置文件中找到定义调用脚本文件的地方,如:
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
修改成如下方式($document_root):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
nginx gzip设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css applocation/xml;
gzip_vary on;
来源:CentOS编译安装Nginx1.6、PHP5.6 - http://www.leixuesong.cn/84
E. windows7配置Nginx+php+mysql的详细操作是怎样
在Windows下配置搭建PHP环境的步骤:
一、准备工作-下载所需软件
Apache httpd-2.2.22-win32-x86-openssl-0.9.8t.msi
PHP php-5.3.10-Win32-VC9-x86.zip
MySQL mysql-5.5.20-win32.msi
二、安装软件
1、安装Apache: 双击安装,与安装其他Windows软件没有什么区别,在填Server Infomation时,并没有特殊规定,只要输入的信息符合格式即可。
安装完成之后,在浏览器输入http://localhost,如果显示It Works!,表示Apache安装成功。
2、安装PHP:将php-5.3.10-Win32-VC9-x86.zip解压到一个目录即可。
3、安装MySQL:双击答桐安装,如果需要更改安装目录,则在Choose Setup Type选择Custom
安装完成后开肆举迅始配置MySQL,全部保持默认选项即可,但最好把MySQL默认编码改为utf8,在Modify Security Settings选项中设置密码,输入两次密码即可完成,最后点Execute完成配置。
三、整合Apache+PHP+MySQL
Apache : 首先修改Apache的配置文件,让Apache支持解析裂此PHP文件。Apache配置文件在Apache安装目录的conf目录下的httpd.conf。
1. 让Apache可以解析php文件,在配置文件中找到
#LoadMole vhost_alias_mole moles/mod_vhost_alias.so
在下一行添加 (绿色的位置是根据PHP的所在目录而定的)
LoadMole php5_mole "D:/Develop/PHP/php5apache2_2.dll"PHPIniDir "D:/Develop/PHP"AddType application/x-httpd-php .php .html .htm
2. 在配置文件中找到
DirectoryIndex index.html
改为
DirectoryIndex index.php index.html
3. 修改Apache站点目录,在配置文件中找到(Apache安装的目录不同,显示的值不一样)
DocumentRoot "D:/Develop/Apache2.2/htdocs"
改为
DocumentRoot "D:/Workspace/PHP"
再找到
<Directory "D:/Develop/Apache2.2/htdocs">
改为
<Directory "D:/Workspace/PHP">
PHP : 把php.ini-development改名为php.ini,作为PHP的配置文件。修改php.ini
1. 设置PHP扩展包的具体目录,找到
; On windows:; extension_dir = "ext"
改为 (值是ext文件夹的目录)
; On windows: extension_dir = "D:/Develop/PHP/ext"
2. 开启相应的库功能,找到需要开启的库的所在行
;extension=php_curl.dll
;extension=php_gd2.dll
;extension=php_mbstring.dll
;extension=php_mysql.dll
;extension=php_xmlrpc.dll
去掉前面的分号(注释),即改为
extension=php_curl.dll
extension=php_gd2.dll
extension=php_mbstring.dll
extension=php_mysql.dll
extension=php_xmlrpc.dll
3. 设置时区,找到
;date.timezone =
改为
date.timezone = Asia/Shanghai
配置完成,检测一下配置是否成功。重启Apache,在站点目录下新建文件index.php,输入内容。
F. 怎么用nginx在windows上搭建web服务器
怎么用nginx在windows上搭建web服务器解决方法
所需工具:
一台联网的电脑
phpStudy 2014版
windows 2003 server sp2服务器一台
1.phpstudy安装在第四步时选择Nginx&php
G. 如何正确配置Nginx + PHP
先上配置的过程,下面是解释。