很多站长朋友们都不太清楚phpnginx套装,今天小编就来给大家整理phpnginx套装,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 Nginx+php(FastCGI)高性能服务器怎么搭建呢? 2、 php和nginx安装后怎么配置 3、 如何搭建Nginx+PHP环境 4、 如何正确配置 Nginx 和 PHP 5、 如何正确配置Nginx + PHP 6、 如何正确配置Nginx+PHP Nginx+php(FastCGI)高性能服务器怎么搭建呢?Nginx+php(FastCGI)+Memcached+Mysql+APC Nginx高性能web服务器安装 APC模块安装 linux apc 配置安装
参考资料 问题补充:
不是 主要是百度不收录 他们说经常在百度知道什么的提问题,然后发表一些好文章附带自己的网站,这样会收录快一些,谢谢一楼!谢谢大家的支持!
最佳答案 Nginx+php(FastCGI)+Memcached+Mysql+APC Nginx高性能web服务器安装 APC模块安装 linux apc 配置 fastCGI安装配置3前言*
Nginx+php(FastCGI)+Memcached+Mysql+APC 是目前主流的高性能服务器搭建方式!适合大中型网站,小型站长也可以采用这种组合!
Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括国内最大的电子地图MapBar、新浪博客、新浪播客、网易新闻等门户网站频道,六间房、56测试数据等视频分享网站,Discuz!官方论坛、水木社区等知名论坛,豆瓣、YUPOO相册、海内SNS、迅雷在线等新兴Web 2.0网站,更多的网站都在使用Nginx配置。
【Nginx+php(FastCGI)+Memcached+Mysql+APC Nginx高性能web服务器安装 APC模块安装 linux apc 配置 fastCGI安装配置】
下载所需的安装包:这里采用源码包编译安装:本博客集成下载
下载到 /usr/src下
另外还有两个包mysql-5.1.41.tar.gz、php-5.3.5.tar.gz 【其他相似版本也可以!】可以在官网下载。
一、正式安装Nginx、【安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等】
(1)首先安装pcre:
cd /usr/src tar xzf pcre-8.01.tar.gz cd pcre-8.01 ./configure –prefix=/usr/local/pcre make make install
(2)、然后再安装nginx :
useradd www cd /usr/src tar xzf nginx-0.7.61.tar.gz cd nginx-0.7.61 ./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-openssl=/usr/ –with-pcre=/usr/src/pcre-8.01 –user=www –group=www make make install
【nginx注意* –with-pcre=/usr/src/pcre-8.01指向的是源码包解压的路径,而不是安装的路径,否则会报
make[1]: *** [/usr/local/pcre/Makefile] Error 127 错误】
二、接下来安装mysql
cd /usr/src tar xzf mysql-5.1.41.tar.gz cd mysql-5.1.41 ./configure –prefix=/usr/local/mysql/ –enable-assembler –with-extra-charsets=complex –enable-thread-safe-client –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile –with-plugins=innobase make make install
(2)、mysql安装完毕,创建mysql用户和组并初始化数据库,并启动数据库。
cd /usr/local/mysql useradd mysql chown -R mysql:mysql /usr/local/mysql /usr/local/mysql/bin/mysql_install_db –user=mysql chown -R mysql:mysql var/ ./bin/mysqld_safe –user=mysql
即可。【如果mysql启动报错,请检查 /usr/local/mysql/var 目录,mysql是否有权限】
三、安装 php :
cd /usr/src tar xzf php-5.3.5.tar.gz cd php-5.3.5 ./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-mysql=/usr/local/mysql –with-mysqli=/u
sr/local/mysql/bin/mysql_config –with-iconv-dir=/usr/local –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-li
bxml-dir=/usr –enable-xml –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem
–enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fpm –enable-sockets make make install
安装完毕!【注意这个参数在此可以不加–enable-fastcgi;其他之前版本需要加上,以上安装根据自己的选择添加,如果报错,根据具体报错找原因】
四、整合Nginx和php(FastCGI)安装完php-5.3.5后支持fastCGI
(1)、配置nginx ,拷贝nginx配置文件:
user www www;
worker_processes 8;
error_log /usr/local/logs/nginx/error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server
{
listen 80;
server_name wgkgood.gicp.net;
index index.php index.htm index.html;
root /usr/webapps/www;
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;
access_log /usr/local/logs/nginx/access.log access;
}
}
Nginx配置完毕!启动nginx ;/usr/local/nginx/sbin/nginx 即可,重启nginx命令如下/usr/local/nginx/sbin/nginx –s reload
此配置文件仅供参考,感谢张宴老师!
(2)、配置fcgi.conf文件如下
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
五、配置php配置文件:
cd /usr/local/php5/etc/ cp
php-fpm.conf.default php-fpm.conf 然后根据提示修改php-fpm.conf里面的选项。
配置完毕后,启动php-fpm
cp /usr/src/php-5.3.5/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 然后启动 /etc/init.d/php-fpm start 即可。
六、安装apc配置:
cd /usr/src tar xzf APC-3.1.4.tgz cd APC-3.1.4
/usr/local/php5/bin/phpize 【增加扩展模块】
./configure –enable-apc –enable-apc-mmap –with-php-config=/usr/local/php5/bin/php-config
make make install
安装完后会生成一个apc.so在/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/里面。
七、安装memcached,使fastcGI支持memcached
首先安装libevent,
cd /usr/src tar xzf libevent-1.4.12-stable.tar.gz cd libevent-1.4.12-stable ./configure –prefix=/usr/local/libevent make make install
然后安装memcached
tar xzf memcache-2.2.5.tar.gz cd memcache-2.2.5 /usr/local/php5/bin/phpize ./configure –prefix=/usr/local/memcached –with-libevent=/usr/local/libevent –with-php-config=/usr/local/php5/bin/php-config make make install
安装完后,会在/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/里生成一个memcache.so 这个模块:
八、接下来修改php.ini
默认的php.ini在/usr/local/php5/lib/php.ini 你也可以指定:
extension_dir = “./”
修改为
extension_dir=”/usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626〃
把下面这些添加到最后:
extension = apc.so
extension=memcache.so //这里引用缓存模块
[APC]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64M
apc.optimization = 1
apc.num_files_hint = 0
apc.ttl=7200
apc.user_ttl=7200
apc.gc_ttl = 3600
apc.cache_by_default = on
安装到此已经完成!
重新启动nginx和php-fpm ,用测试页面访问。
此文章仅供参考!有不妥之处欢迎指正!共同学习!
本博客采用的nginx+fastcGI+mysql+memcached+apc完美架构整合!
感谢张宴老师
php和nginx安装后怎么配置先安装编译依赖的一些组件
复制代码 代码如下:
yum install pcre pcre-devel openssl openssl-devel -y
1、解压程序包
复制代码 代码如下:
tar xf nginx-1.10.0.tar.gz
cd nginx-1.10.0
2、预编译配置参数
复制代码 代码如下:
./configure --user=www \
--group=www \
--prefix=/data/server/nginx \
--with-http_stub_status_module \
--without-http-cache \
--with-http_ssl_module \
--with-http_gzip_static_module
3、执行编译
复制代码 代码如下:
make make install
4、替换配置文件
nginx.conf
user www www;
worker_processes 1;
error_log /u01/data/log/nginx/error.log crit;
pid /u01/data/server/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
log_format main '$remote_addr - "$request_time" [$time_local] "$request" '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
log_format '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for "$request_time"';
include /u01/alidata/server/nginx/conf/vhosts/*.conf;
如何搭建Nginx+PHP环境1、首先需要准备的应用程序包。
nginx:nginx/Windows-1.0.4
php:php-5.2.16-nts-Win32-VC6-x86.zip (nginx下php是以FastCGI的方式运行,所以我们下载非线程安全也就是nts的php包)
(还会用到)RunHiddenConsole:RunHiddenConsole.zip
2、安装与配置。
1)php的安装与配置。
直接解压下载好的php包,到D盘wnmp目录(D:\wnmp),这里把解压出来的文件夹重命名成php5。进入文件夹修改php.ini-recommended文件为php.ini,并用Editplus或者Notepad++打开来。找到
extension_dir = "./ext"
更改为
extension_dir = "D:/wnmp/php5/ext"
往下看,再找到
;extension=php_mysql.dll
;extension=php_mysqli.dll
前面指定了php的ext路径后,只要把需要的扩展包前面所对应的“;”去掉,就可以了。这里打开php_mysql.dll和php_mysqli.dll,让php支持mysql。当然不要忘掉很重要的一步就是,把php5目录下的libmysql.dll文件复制到C:\Windows目录下,也可以在系统变量里面指定路径,当然这里我选择了更为方便的方法^_^。
到这里,php已经可以支持mysql了。
接下来我们来配置php,让php能够与nginx结合。找到
;cgi.fix_pathinfo=1
我们去掉这里的封号。
cgi.fix_pathinfo=1
这一步非常重要,这里是php的CGI的设置。
2)nginx的安装与配置。
把下载好的nginx-1.0.4的包同样解压到D盘的wnmp目录下,并重命名为nginx。接下来,我们来配置nginx,让它能够和php协同工作。进入nginx的conf目录,打开nginx的配置文件nginx.conf,找到
location / {
root html;#这里是站点的根目录
index index.html index.htm;
}
将root html;改为root D:/wnmp/www;
再往下,找到
复制代码
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
复制代码
先将前面的“#”去掉,同样将root html;改为root D:/wnmp/www;。再把标记为红色的/scripts改为“$document_root”,这里的“$document_root”就是指前面“root”所指的站点路径,这是改完后的:
复制代码
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root D:/wnmp/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
复制代码
保存配置文件,就可以了。
nginx+php的环境就初步配置好了,来跑跑看。我们可以输入命令
来启动php,并手动启动nginx,当然也可以利用脚本来实现。
首先把下载好的RunHiddenConsole.zip包解压到nginx目录内,RunHiddenConsole.exe的作用是在执行完命令行脚本后可以自动关闭脚本,而从脚本中开启的进程不被关闭。然后来创建脚本,命名为“start_nginx.bat”,我们在Notepad++里来编辑它
复制代码
@echo off
REM Windows 下无效
REM set PHP_FCGI_CHILDREN=5
REM 每个进程处理的最大请求数,或设置为 Windows 环境变量
set PHP_FCGI_MAX_REQUESTS=1000
echo Starting PHP FastCGI...
RunHiddenConsole D:/wnmp/php5/php-cgi.exe -b 127.0.0.1:9000 -c D:/wnmp/php5/php.ini
echo Starting nginx...
RunHiddenConsole D:/wnmp/nginx/nginx.exe -p D:/wnmp/nginx
复制代码
再另外创建一个名为stop_nginx.bat的脚本用来关闭nginx
@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit
做好后,是这样的
这样,我们的服务脚本也都创建完毕了。双击start_nginx.bat看看进程管理器是不是有两个nginx.exe的进程和一个php-cgi.exe的进程呢?
这样nginx服务就启动了,而且php也以fastCGI的方式运行了。
到站点目录下,新建一个phpinfo.php的文件,在里面编辑
<?php
phpinfo();
?>
保存后,打开浏览器输入“”,如果看到
就说明,nginx+php的环境已经配置好了,呵呵~
如何正确配置 Nginx 和 PHP前提:NGINX、PHP、PHP-FPM...安装完成
基本配置
配置PHP-FPM:
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice
[www]
listen = /tmp/php-cgi.sock
#listen = 127.0.0.1:9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 60
pm.start_servers = 30
pm.min_spare_servers = 30
pm.max_spare_servers = 60
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log
配置NGINX:
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
这是我复制我自己本地的配置。如果要全面一点的,你可以下载个lnmp一件安装包,安装之后有各种配置,挺全的。
如何正确配置Nginx + PHP对很多人而言,配置Nginx+PHP无外乎就是搜索一篇教程,然后拷贝粘贴。听上去似乎也没什么问题,可惜实际上网络上很多资料本身年久失修,漏洞百出,如果大家不求甚解,一味的拷贝粘贴,早晚有一天会为此付出代价。
假设我们用PHP实现了一个前端控制器,或者直白点说就是统一入口:把PHP请求都发送到同一个文件上,然后在此文件里通过解析「REQUEST_URI」实现路由。
此时很多教程会教大家这样配置Nginx+PHP:
server {
listen 80;
server_name foo测试数据;
root /path;
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite . /index.php last;
}
}
location ~ .php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
这里面有很多错误,或者说至少是坏味道的地方,大家看看能发现几个。
…
我们有必要先了解一下Nginx配置文件里指令的继承关系:Nginx配置文件分为好多块,常见的从外到内依次是「http」、「server」、「location」等等,缺省的继承关系是从外到内,也就是说内层块会自动获取外层块的值作为缺省值(有例外,详见参考)。
参考:UNDERSTANDING THE NGINX CONFIGURATION INHERITANCE MODEL
…
让我们先从「index」指令入手吧,在问题配置中它是在「location」中定义的:
location / {
index index.html index.htm index.php;
}
一旦未来需要加入新的「location」,必然会出现重复定义的「index」指令,这是因为多个「location」是平级的关系,不存在继承,此时应该在「server」里定义「index」,借助继承关系,「index」指令在所有的「location」中都能生效。
参考:Nginx Pitfalls
…
接下来看看「if」指令,说它是大家误解最深的Nginx指令毫不为过:
if (!-e $request_filename) {
rewrite . /index.php last;
}
很多人喜欢用「if」指令做一系列的检查,不过这实际上是「try_files」指令的职责:
try_files $uri $uri/ /index.php;
除此以外,初学者往往会认为「if」指令是内核级的指令,但是实际上它是rewrite模块的一部分,加上Nginx配置实际上是声明式的,而非过程式的,所以当其和非rewrite模块的指令混用时,结果可能会非你所愿。
参考:IfIsEvil and How nginx “location if” works
…
下面看看「fastcgi_params」配置文件:
include fastcgi_params;
Nginx有两份fastcgi配置文件,分别是「fastcgi_params」和「fastcgi.conf」,它们没有太大的差异,唯一的区别是后者比前者多了一行「SCRIPT_FILENAME」的定义:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
注意:$document_root 和 $fastcgi_script_name 之间没有 /。
原本Nginx只有「fastcgi_params」,后来发现很多人在定义「SCRIPT_FILENAME」时使用了硬编码的方式,于是为了规范用法便引入了「fastcgi.conf」。
不过这样的话就产生一个疑问:为什么一定要引入一个新的配置文件,而不是修改旧的配置文件?这是因为「fastcgi_param」指令是数组型的,和普通指令相同的是:内层替换外层;和普通指令不同的是:当在同级多次使用的时候,是新增而不是替换。换句话说,如果在同级定义两次「SCRIPT_FILENAME」,那么它们都会被发送到后端,这可能会导致一些潜在的问题,为了避免此类情况,便引入了一个新的配置文件。
参考:FASTCGI_PARAMS VERSUS FASTCGI.CONF – NGINX CONFIG HISTORY
此外,我们还需要考虑一个安全问题:在PHP开启「cgi.fix_pathinfo」的情况下,PHP可能会把错误的文件类型当作PHP文件来解析。如果Nginx和PHP安装在同一台服务器上的话,那么最简单的解决方法是用「try_files」指令做一次过滤:
try_files $uri =404;
参考:Nginx文件类型错误解析漏洞
…
依照前面的分析,给出一份改良后的版本,是不是比开始的版本清爽了很多:
server {
listen 80
server_name foo测试数据;
root /path;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php;
}
location ~ .php$ {
try_files $uri =404;
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
}
实际上还有一些瑕疵,主要是「try_files」和「fastcgi_split_path_info」不够兼容,虽然能够解决,但方案比较丑陋,具体就不多说了,有兴趣的可以参考问题描述。
补充:因为「location」已经做了限定,所以「fastcgi_index」其实也没有必要.
如何正确配置Nginx+PHP1.先要正确保证nginx和php都是启动开启的!使用ps命令查看两个服务的进程,然后查看端口是否开启,web:80 php:9000 命令:lsof -i:80
2.在nginx.conf中加入下面一段配置
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
/usr/local/nginx/html/$fastcgi_script_name; 这里我写的是php的绝对路径,你可以修改成相应的。
关于phpnginx套装的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。