如何在CentOS 6上通過YUM安裝Nginx和PHP-FPM

來源:互聯網
上載者:User
原文地址:http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on-centos-6-via-yum/


開始安裝Nginx和PHP-FPM之前,你必須卸載系統中以前安裝的Apache和PHP。用root登入輸入下面的命令:

[plain] view plaincopy

  1. # yum remove httpd* php*


增加額外資產庫 預設情況下,CentOS的官方資源是沒有php-fpm的, 但我們可以從Remi的RPM資源中獲得,它依賴於EPEL資源。我們可以這樣增加兩個資產庫:

[plain] view plaincopy

  1. # yum install yum-priorities -y
  2. # rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm
  3. # rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm


輸出範例

[plain] view plaincopy

  1. Retrieving http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
  2. warning: /var/tmp/rpm-tmp.00kiDx: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
  3. Preparing...########################################### [100%]
  4. 1:epel-release ########################################### [100%]


安裝Nginx輸入下列命令

[plain] view plaincopy

  1. # yum install nginx

輸出範例

[plain] view plaincopy

  1. Dependencies Resolved
  2. ================================================================================
  3. Package Arch Version Repository Size
  4. ================================================================================
  5. Installing:
  6. nginx x86_64 0.8.54-1.el6 epel 358 k
  7. Installing for dependencies:
  8. GeoIP x86_64 1.4.8-1.el6 epel 620 k
  9. fontconfig x86_64 2.8.0-3.el6 base 186 k
  10. freetype x86_64 2.3.11-6.el6_1.8 updates 358 k
  11. gd x86_64 2.0.35-10.el6 base 142 k
  12. libX11 x86_64 1.3-2.el6 base 582 k
  13. libX11-common noarch 1.3-2.el6 base 188 k
  14. libXau x86_64 1.0.5-1.el6 base 22 k
  15. libXpm x86_64 3.5.8-2.el6 base 59 k
  16. libjpeg x86_64 6b-46.el6 base 134 k
  17. libpng x86_64 2:1.2.46-1.el6_1 base 180 k
  18. libxcb x86_64 1.5-1.el6 base 100 k
  19. libxslt x86_64 1.1.26-2.el6 base 450 k
  20. perl x86_64 4:5.10.1-119.el6_1.1 base 10 M
  21. perl-Module-Pluggable x86_64 1:3.90-119.el6_1.1 base 37 k
  22. perl-Pod-Escapes x86_64 1:1.04-119.el6_1.1 base 30 k
  23. perl-Pod-Simple x86_64 1:3.13-119.el6_1.1 base 209 k
  24. perl-libs x86_64 4:5.10.1-119.el6_1.1 base 575 k
  25. perl-version x86_64 3:0.77-119.el6_1.1 base 49 k
  26. Transaction Summary
  27. ================================================================================
  28. Install 19 Package(s)
  29. Upgrade 0 Package(s)
  30. Total download size: 14 M
  31. Installed size: 47 M
  32. Is this ok [y/N]: y


如果你想在系統啟動時自動運行nginx,輸入下列命令:

[plain] view plaincopy

  1. # chkconfig --level 345 nginx on


第一次啟動nginx,輸入下列命令:

[plain] view plaincopy

  1. # /etc/init.d/nginx start


輸出範例

[plain] view plaincopy

  1. Starting nginx: [ OK ]


安裝PHP-FPM輸入下列命令:

[plain] view plaincopy

  1. # yum --enablerepo=remi install php php-fpm


輸出範例

[plain] view plaincopy

  1. Dependencies Resolved
  2. ====================================================================================
  3. Package Arch Version Repository Size
  4. ====================================================================================
  5. Installing:
  6. php x86_64 5.3.10-2.el6.remi remi 2.3 M
  7. php-fpm x86_64 5.3.10-2.el6.remi remi 1.1 M
  8. Installing for dependencies:
  9. apr x86_64 1.3.9-3.el6_1.2 base 123 k
  10. apr-util x86_64 1.3.9-3.el6_0.1 base 87 k
  11. apr-util-ldap x86_64 1.3.9-3.el6_0.1 base 15 k
  12. httpd x86_64 2.2.15-15.el6.centos.1 updates 813 k
  13. httpd-tools x86_64 2.2.15-15.el6.centos.1 updates 70 k
  14. libedit x86_64 2.11-4.20080712cvs.1.el6 base 74 k
  15. mailcap noarch 2.1.31-2.el6 base 27 k
  16. php-cli x86_64 5.3.10-2.el6.remi remi 2.2 M
  17. Transaction Summary
  18. ====================================================================================
  19. Install 10 Package(s)
  20. Upgrade 0 Package(s)
  21. Total download size: 6.8 M
  22. Installed size: 21 M
  23. Is this ok [y/N]: y


如果你想在系統啟動時自動運行php-fpm,輸入下列命令:

[plain] view plaincopy

  1. # chkconfig --level 345 php-fpm on


PHP僅安裝了核心模組,你很可能需要安裝其他的模組,比如MySQL、 XML、 GD等等,你可以輸入下列命令:

[plain] view plaincopy

  1. # yum --enablerepo=remi install php-gd php-mysql php-mbstring php-xml php-mcrypt


第一次啟動php-fpm,輸入下列命令:

[plain] view plaincopy

  1. # /etc/init.d/php-fpm restart


輸出範例

[plain] view plaincopy

  1. Starting php-fpm: [ OK ]

配置PHP-FPM和Nginx,讓他們一起工作

nginx的設定檔在/etc/nginx/nginx.conf,輸入下列命令編輯這個檔案:

[plain] view plaincopy

  1. # vi /etc/nginx/nginx.conf


像下面這樣編輯取消注釋:

[plain] view plaincopy

  1. ...
  2. location / {
  3. root /usr/share/nginx/html;
  4. index index.html index.htm index.php;
  5. }
  6. ...
  7. location ~ \.php$ {
  8. root html;
  9. fastcgi_pass 127.0.0.1:9000;
  10. fastcgi_index index.php;
  11. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  12. include fastcgi_params;
  13. }
  14. ...


重啟Nginx會重新讀取設定檔,輸入

[plain] view plaincopy

  1. # /etc/init.d/nginx reload


現在在document root目錄下建立下列PHP檔案

[plain] view plaincopy

  1. # vi /usr/share/nginx/html/info.php


檔案內容如下:

[plain] view plaincopy

  1. phpinfo();
  2. ?>


訪問 http://YOUR-SERVER-IP

Nginx虛擬機器主機設定

設定例子

IP: 192.168.1.113
Domain: domain.local
Hosted at: /home/www/domain.local

輸入下列命令建立名叫“www”的使用者

[plain] view plaincopy

  1. # useradd www

建立必要的目錄

[plain] view plaincopy

  1. # mkdir -p /home/www/domain.local/public_html
  2. # mkdir -p /home/www/domain.local/log
  3. # chown -R www.www /home/www/
  4. # chmod 755 /home/www/


建立虛擬機器主機設定檔

[plain] view plaincopy

  1. # cd /etc/nginx/conf.d/
  2. # cp virtual.conf www.conf


輸入下面命令開啟www.conf檔案

[plain] view plaincopy

  1. # vi /etc/nginx/conf.d/www.conf


增加以下配置

[plain] view plaincopy

  1. server {
  2. server_name domain.local;
  3. root /home/www/domain.local/public_html;
  4. access_log /home/www/domain.local/log/domain.local-access.log;
  5. error_log /home/www/domain.local/log/domain.local-error.log;
  6. location / {
  7. index index.html index.htm index.php;
  8. }
  9. location ~ \.php$ {
  10. include /etc/nginx/fastcgi_params;
  11. fastcgi_pass 127.0.0.1:9000;
  12. fastcgi_index index.php;
  13. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  14. }
  15. }


你可以用下列方法檢查設定檔是否有語法錯誤

[plain] view plaincopy

  1. # /etc/init.d/nginx configtest


輸出範例

[plain] view plaincopy

  1. the configuration file /etc/nginx/nginx.conf syntax is ok
  2. configuration file /etc/nginx/nginx.conf test is successful


現在編輯/etc/php-fpm.d/www.conf檔案,將運行php-fpm進程的使用者改為“www”,輸入

[plain] view plaincopy

  1. # vi /etc/php-fpm.d/www.conf

找到“group of processes”,編輯成下面的樣子:

[plain] view plaincopy

  1. ; Unix user/group of processes
  2. ; Note: The user is mandatory. If the group is not set, the default user's group
  3. ; will be used.
  4. ; RPM: apache Choosed to be able to access some dir as httpd
  5. user = www
  6. ; RPM: Keep a group allowed to write in log dir.
  7. group = www


最後重啟nginx

[plain] view plaincopy

  1. # /etc/init.d/nginx restart
  2. # /etc/init.d/php-fpm restart


From: http://blog.csdn.net/seanchan/article/details/7680354

以上就介紹了如何在CentOS 6上通過YUM安裝Nginx和PHP-FPM,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.