手机版
你好,游客 登录 注册
背景:
阅读新闻

CentOS + Nginx + PHP-FPM + MySQL快速完美安装教程

[日期:2010-12-26] 来源:21andy.com  作者:21andy [字体:大 中 小]
看下我的完整安装,只要一句

yum -y install nginx mysql-server php-fpm php-cli php-pdo php-mysql php-mcrypt php-mbstring php-gd php-tidy php-xml php-xmlrpc php-pear php-pecl-memcache php-eaccelerator
安装结果,全自动

Dependencies Resolved

==========================================================
Package Arch Version Repository Size
==========================================================
Installing:
mysql x86_64 5.0.89-1.el5 idcfree.com 3.5 M
mysql-server x86_64 5.0.89-1.el5 idcfree.com 10 M
nginx x86_64 0.8.33-3.el5 idcfree.com 422 k
php-cli x86_64 5.3.1-2.el5 idcfree.com 2.4 M
php-eaccelerator x86_64 2:0.9.6-1.el5 idcfree.com 118 k
php-fpm x86_64 5.3.1-2.el5 idcfree.com 1.2 M
php-gd x86_64 5.3.1-2.el5 idcfree.com 110 k
php-mbstring x86_64 5.3.1-2.el5 idcfree.com 1.1 M
php-mcrypt x86_64 5.3.1-2.el5 idcfree.com 27 k
php-mysql x86_64 5.3.1-2.el5 idcfree.com 84 k
php-pdo x86_64 5.3.1-2.el5 idcfree.com 91 k
php-pear noarch 1:1.9.0-1.el5 idcfree.com 420 k
php-pecl-memcache x86_64 2.2.5-3.el5 idcfree.com 44 k
php-tidy x86_64 5.3.1-2.el5 idcfree.com 31 k
php-xml x86_64 5.3.1-2.el5 idcfree.com 115 k
php-xmlrpc x86_64 5.3.1-2.el5 idcfree.com 48 k
Installing for dependencies:
gmp x86_64 4.1.4-10.el5 base 201 k
libXaw x86_64 1.0.2-8.1 base 329 k
libXmu x86_64 1.0.2-5 base 63 k
libXpm x86_64 3.5.5-3 base 44 k
libedit x86_64 2.11-2.20080712cvs.el5 epel 80 k
libmcrypt x86_64 2.5.8-4.el5.CentOS extras 105 k
libtidy x86_64 0.99.0-14.20070615.el5 epel 140 k
php-common x86_64 5.3.1-2.el5 idcfree.com 554 k
sqlite2 x86_64 2.8.17-5.el5 idcfree.com 165 k
t1lib x86_64 5.1.1-7.el5 epel 208 k
Updating for dependencies:
libevent x86_64 1.4.12-1.el5 idcfree.com 129 k

Transaction Summary
==========================================================
Install 26 Package(s)
Update 1 Package(s)
Remove 0 Package(s)

最后只要 yum -y update 一下,全是最新的
别忘了开机启动

chkconfig --level 345 mysqld on
chkconfig --level 345 php-fpm on
chkconfig --level 345 nginx on

补充:所有的配置文件都在 /etc 目录下,包括 nginx, php-fpm, mysql 的配置文件,请自行查找设置,以下为我的 nginx 的配置范例

先新建一个 /www 目录,网站和日志也全放在这里
别忘了建日志存放目录,你在配置文件中 access_log 用到的 /www/logs

注意:fastcgi_params 要加入这一行
# vim /etc/nginx/fastcgi_params

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# vi /etc/nginx/nginx.conf

user nobody nobody;

worker_processes 8; #这里根据你的CPU和内存配置, 设置2到10都OK

error_log /www/logs/nginx_error.log crit;

pid /usr/local/nginx/logs/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;

   #charse gb2312; # 默认编码,可以不设置

   server_names_hash_bucket_size 128;
   client_header_buffer_size 16k;
   large_client_header_buffers 4 16k;
   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 5;
   gzip_types text/plain text/Javascript application/x-javascript text/css application/xml;
   gzip_vary on;

   #limit_zone crawler $binary_remote_addr 10m;
   server {
   listen 80;
   server_name localhost;
   root /www;
   location /status {
   stub_status on;
   access_log off;
   }
   location / {
   # 这里是把所有不存在的文件和目录,全都转到 index.php 处理
   try_files $uri $uri/ /index.php?q=$uri&$args;
   }
  
   # 这里分开放到 server.conf 是为了再开 server 的时候方便,统一调用,放到/etc/nginx/ 目录下
   include server.conf;

   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 /www/logs/access.log access;
   }

   server {
   listen 80;
   server_name www.idcfree.com idcfree.com *.idcfree.com;
   root /www/idcfree.com;
   if ($host !~* idcfree\.com$) {
   return 444;
   }
   location / {
   try_files $uri $uri/ /index.php?q=$uri&$args;
   }
   include server.conf; # 这里复用了,这段就省了
   access_log /www/logs/idcfree.com_access.log access;
   }
}
# vi /etc/nginx/server.conf

index index.html index.htm index.php;

#limit_conn crawler 20;

location ~ /\.ht {
   deny all;
}

location ~ .*\.(sqlite|sq3)$ {
   deny all;
}

location ~ .*\.php$ {
   fastcgi_pass unix:/tmp/php-cgi.sock;
   #fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   include fastcgi_params;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
   expires 30d;
   access_log off;
}

location ~ .*\.(js|css)?$ {
   expires 30d;
   access_log off;
}
本行以下内容可以略过
php-fpm 也可以用以下设置,但建议用上面的,比较稳定

location ~ \.php$ {
   root /www;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   #下面这一行要加在 /etc/nginx/fastcgi_params 里
   #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include fastcgi_params;
}
如果使用 9000 端口方法,php-fpm 也要相应设置
# vi /etc/php-fpm.conf

<value name="listen_address">127.0.0.1:9000</value>

linux
相关资讯       CentOS  Nginx 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款