1分钟完美安装最新CentOS+Nginx+PHP-FPM+MySQL

开发 后端 前端
本文将告诉我们可以快速全自动搞定 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

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

放入如下内容

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

启用 EPEL repo

i386

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

x86_64

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

接着导入key

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

好了,一键安装

  1. # yum install nginx php-fpm mysql-server  

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

更完整的安装

  1. yum -y update  
  2. yum -y mysql-server  
  3. service mysqld start  
  4. mysqladmin -u root password root  
  5. service mysqld stop  
  6. yum -y install nginx php-fpm   
  7. php-cli php-pdo php-mysql php-mcrypt php-mbstring   
  8. php-gd php-tidy php-xml php-xmlrpc   
  9. php-pear php-pecl-memcache php-eaccelerator  
  10. # APC 和 eAccelerator 有冲突,2选1  
  11. 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

 安装结果,全自动

  1. Dependencies Resolved 
  2. ==========================================================  
  3. Package Arch Version Repository Size 
  4. ==========================================================  
  5. Installing:  
  6. mysql x86_64 5.0.89-1.el5 21Andy.com 3.5 M  
  7. mysql-server x86_64 5.0.89-1.el5 21Andy.com 10 M  
  8. nginx x86_64 0.8.33-3.el5 21Andy.com 422 k  
  9. php-cli x86_64 5.3.1-2.el5 21Andy.com 2.4 M  
  10. php-eaccelerator x86_64 2:0.9.6-1.el5 21Andy.com 118 k  
  11. php-fpm x86_64 5.3.1-2.el5 21Andy.com 1.2 M  
  12. php-gd x86_64 5.3.1-2.el5 21Andy.com 110 k  
  13. php-mbstring x86_64 5.3.1-2.el5 21Andy.com 1.1 M  
  14. php-mcrypt x86_64 5.3.1-2.el5 21Andy.com 27 k  
  15. php-mysql x86_64 5.3.1-2.el5 21Andy.com 84 k  
  16. php-pdo x86_64 5.3.1-2.el5 21Andy.com 91 k  
  17. php-pear noarch 1:1.9.0-1.el5 21Andy.com 420 k  
  18. php-pecl-memcache x86_64 2.2.5-3.el5 21Andy.com 44 k  
  19. php-tidy x86_64 5.3.1-2.el5 21Andy.com 31 k  
  20. php-xml x86_64 5.3.1-2.el5 21Andy.com 115 k  
  21. php-xmlrpc x86_64 5.3.1-2.el5 21Andy.com 48 k  
  22. Installing for dependencies:  
  23. gmp x86_64 4.1.4-10.el5 base 201 k  
  24. libXaw x86_64 1.0.2-8.1 base 329 k  
  25. libXmu x86_64 1.0.2-5 base 63 k  
  26. libXpm x86_64 3.5.5-3 base 44 k  
  27. libedit x86_64 2.11-2.20080712cvs.el5 epel 80 k  
  28. libmcrypt x86_64 2.5.8-4.el5.centos extras 105 k  
  29. libtidy x86_64 0.99.0-14.20070615.el5 epel 140 k  
  30. php-common x86_64 5.3.1-2.el5 21Andy.com 554 k  
  31. sqlite2 x86_64 2.8.17-5.el5 21Andy.com 165 k  
  32. t1lib x86_64 5.1.1-7.el5 epel 208 k  
  33. Updating for dependencies:  
  34. libevent x86_64 1.4.12-1.el5 21Andy.com 129 k  
  35. Transaction Summary 
  36. ==========================================================  
  37. Install 26 Package(s)  
  38. Update 1 Package(s)  
  39. Remove 0 Package(s  

最后只要 yum -y update 一下,全是最新的

别忘了开机启动

  1. chkconfig --level 345 mysqld on  
  2. chkconfig --level 345 php-fpm on  
  3. 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

  1. 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.     }  

# 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;  

本行以下内容可以略过

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;  

不管是使用 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

原文地址:http://www.21andy.com/blog/20100219/1701.html

【编辑推荐】

  1. 解析PHP中的XML数据
  2. PHP单元测试利器:PHPUnit深入理解
  3. PHP单元测试利器:PHPUnit深入用法
  4. 初探PHP单元测试利器:PHPUnit
  5. PHP企业级应用之常见缓存技术深入解读
责任编辑:陈贻新 来源: 21Andy.com
相关推荐

2019-11-12 14:05:11

U盘LinuxRufus

2009-11-16 09:53:56

PHP上传类

2015-11-23 17:34:33

秒借

2018-03-12 21:31:24

区块链

2017-12-20 09:42:39

PythonNginx日志

2019-03-04 15:36:18

Nginx版本运维

2017-03-16 08:46:57

延时消息环形队列数据结构

2017-03-09 13:26:33

2016-09-13 20:58:41

MySQ基础入门Sql

2018-03-12 14:37:50

区块链比特币架构

2018-03-23 11:56:09

相似性推荐推荐算法推荐

2009-11-16 11:30:55

PHP上传文件代码

2009-11-17 12:47:05

PHP配置

2014-11-28 11:20:54

LEMPNginxMariaDB

2017-12-28 10:22:28

NginxFastCGIPHP-fpm

2023-07-12 16:13:01

2018-02-02 10:24:37

Nginx入门指南

2018-01-16 10:11:11

Nginx访问日志

2017-02-22 14:46:04

Leader-Foll线程

2018-03-23 11:23:14

点赞
收藏

51CTO技术栈公众号