1分鐘完美安裝最新 CentOS + Nginx + PHP-FPM + MySQL

來源: 21Andy.com  發布時間: 2011-02-28 14:58  閱讀: 4090 次  推薦: 0   原文鏈接   [收藏]  
摘要:本文將告訴我們可以快速全自動搞定 CentOS + Nginx + PHP-FPM + MySQL 安裝的辦法.這個可比網上流傳的什么一鍵安裝包要好得多,強烈推薦此法安裝,適合所有菜鳥和高手我服務器上全用的源代碼編譯安裝。

  PHP 5.3.1

  MySQL 5.0.89

  Nginx 0.8.33 或 0.7.65 (可選)

  現在,我們可以快速全自動搞定 CentOS + Nginx + PHP-FPM + MySQL 的安裝了

  這個可比網上流傳的什么一鍵安裝包要好得多,強烈推薦此法安裝,適合所有菜鳥和高手

  我服務器上全用的源代碼編譯安裝,也好不到哪去,還很費勁

  我這個裝完已經包含PHP的一些常用擴展, PDO,eaccelerator,memcache,tidy等等

  先新建一個 repo

 
# vi /etc/yum.repos.d/centos.21andy.com.repo

  放入如下內容

 
[21Andy.com]
name
=21Andy.com Packages for Enterprise Linux 5 - $basearch
baseurl=http://www.21andy.com/centos/5/$basearch/
enabled=1
gpgcheck=0
protect=1

  啟用 EPEL repo

  i386

rpm -ihv
 http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm

  x86_64

rpm -ihv 
http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm

  接著導入key

 
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL

  好了,一鍵安裝

 
# yum install nginx php-fpm mysql-server

  如果 nginx 你要用 0.7.65 最新穩定版,把yum -y install nginx換成yum -y install nginx-stable就可以了

  更完整的安裝

 
yum -y update
yum
-y mysql-server
service mysqld start
mysqladmin
-u root password root
service mysqld stop
yum
-y install nginx 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
# APC 和 eAccelerator 有沖突,2選1
yum -y install php-pecl-apc

  看下我的完整安裝,只要一句

  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 21Andy.com 3.5 M
mysql-server x86_64 5.0.89-1.el5 21Andy.com 10 M
nginx x86_64
0.8.33-3.el5 21Andy.com 422 k
php
-cli x86_64 5.3.1-2.el5 21Andy.com 2.4 M
php
-eaccelerator x86_64 2:0.9.6-1.el5 21Andy.com 118 k
php
-fpm x86_64 5.3.1-2.el5 21Andy.com 1.2 M
php
-gd x86_64 5.3.1-2.el5 21Andy.com 110 k
php
-mbstring x86_64 5.3.1-2.el5 21Andy.com 1.1 M
php
-mcrypt x86_64 5.3.1-2.el5 21Andy.com 27 k
php
-mysql x86_64 5.3.1-2.el5 21Andy.com 84 k
php
-pdo x86_64 5.3.1-2.el5 21Andy.com 91 k
php
-pear noarch 1:1.9.0-1.el5 21Andy.com 420 k
php
-pecl-memcache x86_64 2.2.5-3.el5 21Andy.com 44 k
php
-tidy x86_64 5.3.1-2.el5 21Andy.com 31 k
php
-xml x86_64 5.3.1-2.el5 21Andy.com 115 k
php
-xmlrpc x86_64 5.3.1-2.el5 21Andy.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 21Andy.com 554 k
sqlite2 x86_64
2.8.17-5.el5 21Andy.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 21Andy.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

  來看看我用虛擬機測試的強大結果

  http://www.21andy.com/blog/20100219/1703.html

  補充:所有的配置文件都在 /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

 
1. user nobody nobody;
2.
3. worker_processes 8; #這里根據你的CPU和內存配置, 設置2到10都OK
4.
5. error_log /www/logs/nginx_error.log crit;
6.
7. pid /usr/local/nginx/logs/nginx.pid;
8.
9. #Specifies the value for maximum file descriptors that can be opened by this process.
10. worker_rlimit_nofile 51200;
11.
12. events {
13. use epoll;
14. worker_connections 51200;
15. }
16.
17. http {
18. include mime.types;
19. default_type application/octet-stream;
20.
21. #charse gb2312; # 默認編碼,可以不設置
22.
23. server_names_hash_bucket_size 128;
24. client_header_buffer_size 16k;
25. large_client_header_buffers 4 16k;
26. client_max_body_size 8m;
27.
28. sendfile on;
29. tcp_nopush on;
30.
31. keepalive_timeout 60;
32.
33. tcp_nodelay on;
34.
35. fastcgi_connect_timeout 300;
36. fastcgi_send_timeout 300;
37. fastcgi_read_timeout 300;
38. fastcgi_buffer_size 64k;
39. fastcgi_buffers 4 64k;
40. fastcgi_busy_buffers_size 128k;
41. fastcgi_temp_file_write_size 128k;
42.
43. gzip on;
44. gzip_min_length 1k;
45. gzip_buffers 4 16k;
46. gzip_http_version 1.0;
47. gzip_comp_level 5;
48. gzip_types text/plain text/javascript application/x-javascript text/css application/xml;
49. gzip_vary on;
50.
51. #limit_zone crawler $binary_remote_addr 10m;
52. server {
53. listen 80;
54. server_name localhost;
55. root /www;
56. location /status {
57. stub_status on;
58. access_log off;
59. }
60. location / {
61. # 這里是把所有不存在的文件和目錄,全都轉到 index.php 處理
62. try_files $uri $uri/ /index.php?q=$uri&$args;
63. }
64.
65. # 這里分開放到 server.conf 是為了再開 server 的時候方便,統一調用,放到/etc/nginx/ 目錄下
66. include server.conf;
67.
68. log_format access '$remote_addr - $remote_user [$time_local] "$request" '
69. '$status $body_bytes_sent "$http_referer" '
70. '"$http_user_agent" $http_x_forwarded_for';
71. access_log /www/logs/access.log access;
72. }
73.
74. server {
75. listen 80;
76. server_name www.21andy.com 21andy.com *.21andy.com;
77. root /www/21andy.com;
78. if ($host !~* 21andy\.com$) {
79. return 444;
80. }
81. location / {
82. try_files $uri $uri/ /index.php?q=$uri&$args;
83. }
84. include server.conf; # 這里復用了,這段就省了
85. access_log /www/logs/21andy.com_access.log access;
86. }
87. }

  # vi /etc/nginx/server.conf

 
1. index index.html index.htm index.php;
2.
3. #limit_conn crawler 20;
4.
5. location ~ /\.ht {
6. deny all;
7. }
8.
9. location ~ .*\.(sqlite|sq3)$ {
10. deny all;
11. }
12.
13. location ~ .*\.php$ {
14. fastcgi_pass unix:/tmp/php-cgi.sock;
15. #fastcgi_pass 127.0.0.1:9000;
16. fastcgi_index index.php;
17. include fastcgi_params;
18. }
19.
20. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
21. expires 30d;
22. access_log off;
23. }
24.
25. location ~ .*\.(js|css)?$ {
26. expires 30d;
27. access_log off;
28. }

  本行以下內容可以略過

  php-fpm 也可以用以下設置,但建議用上面的,比較穩定

 
1. location ~ \.php$ {
2. root /www;
3. fastcgi_pass 127.0.0.1:9000;
4. fastcgi_index index.php;
5. #下面這一行要加在 /etc/nginx/fastcgi_params 里
6. #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
7. include fastcgi_params;
8. }

  不管是使用 php-cgi.sock 或 9000 端口方法,nginx 和 php-fpm 都要同時設置成一樣的

nginx 的設置上面已經提過,記得同時修改 php-fpm.conf 相應的地方

  # vi /etc/php-fpm.conf

 
127.0.0.1:9000

  或

 
unix:/tmp/php-cgi.sock
0
0
 
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()