centos6安裝lnmp

CentOS 6 默認倉庫不包含nginx,咱們能夠手動添加nginx的倉庫。php

訪問nginx官網獲取repo文件

咱們須要先訪問nginx的官方網站,獲取官方的倉庫地址。
點擊這裏訪問nginx官方文檔html

依照文檔中的說明,最後的repo文件應該是下面這樣,您能夠直接複製。mysql

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1linux

使用vim將上面的配置保存到/etc/yum.repos.d/nginx.repo文件中。nginx

安裝nginx

安裝好倉庫後能夠直接使用yum安裝nginx。web

yum install -y nginx

啓動nginx

執行service nginx start啓動nginx。sql

啓動成功後執行netstat -tunlp|grep 80就能夠看到nginx已經啓動了80端口的監聽。vim

  1. [root@localhost ~] # netstat -tunlp|grep 80
  2. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1881/nginx

而且經過瀏覽器直接訪問服務器的ip地址,能看到已經出現了nginx的歡迎頁面。centos

nginx歡迎頁面

設置nginx爲開機啓動

執行chkconfig nginx on設置nginx爲開機啓動。瀏覽器

安裝MySQL

CentOS 6的默認倉庫直接包含MySQL,能夠直接經過yum安裝 MySQL Server。

yum install -y mysql mysql-server 

MySQL服務名稱爲mysqld,咱們能夠經過下面命令啓動MySQL服務。

service mysqld start 

同nginx同樣,使用下面命令將mysqld加入開機啓動任務。

chkconfig mysqld on 

啓動成功後執行netstat -tunlp|grep 3306就能夠看到mysqld已經啓動了3306端口的監聽。

  1. [root@localhost ~] # netstat -tunlp|grep 3306
  2. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2094/mysqld

還能夠經過mysql客戶端鏈接到MySQL服務器。

  1. [root@localhost ~]# mysql
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 2
  4. Server version: 5.1.73 Source distribution
  5.  
  6. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  7.  
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11.  
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13.  
  14. mysql>
  15.  

安裝PHP

CentOS 默認倉庫中包含了php套件,咱們能夠直接使用yum安裝。
下面是最小化安裝,咱們使用php-fpm來解析php。

yum install -y php-cli php-fpm 

您能夠隨時使用yum list php-*查看其它php擴展,下面是默認倉庫中包含的全部擴展。

  1. [root@localhost ~]# yum list php-*
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: mirrors.sina.cn
  5. * extras: mirrors.sina.cn
  6. * updates: mirrors.sina.cn
  7. Installed Packages
  8. php-cli.x86_64 5.3.3-48.el6_8 @updates
  9. php-common.x86_64 5.3.3-48.el6_8 @updates
  10. php-fpm.x86_64 5.3.3-48.el6_8 @updates
  11. Available Packages
  12. php.x86_64 5.3.3-48.el6_8 updates
  13. php-bcmath.x86_64 5.3.3-48.el6_8 updates
  14. php-dba.x86_64 5.3.3-48.el6_8 updates
  15. php-devel.x86_64 5.3.3-48.el6_8 updates
  16. php-embedded.x86_64 5.3.3-48.el6_8 updates
  17. php-enchant.x86_64 5.3.3-48.el6_8 updates
  18. php-gd.x86_64 5.3.3-48.el6_8 updates
  19. php-imap.x86_64 5.3.3-48.el6_8 updates
  20. php-intl.x86_64 5.3.3-48.el6_8 updates
  21. php-ldap.x86_64 5.3.3-48.el6_8 updates
  22. php-mbstring.x86_64 5.3.3-48.el6_8 updates
  23. php-mysql.x86_64 5.3.3-48.el6_8 updates
  24. php-odbc.x86_64 5.3.3-48.el6_8 updates
  25. php-pdo.x86_64 5.3.3-48.el6_8 updates
  26. php-pear.noarch 1:1.9.4-5.el6 base
  27. php-pecl-apc.x86_64 3.1.9-2.el6 base
  28. php-pecl-apc-devel.i686 3.1.9-2.el6 base
  29. php-pecl-apc-devel.x86_64 3.1.9-2.el6 base
  30. php-pecl-memcache.x86_64 3.0.5-4.el6 base
  31. php-pgsql.x86_64 5.3.3-48.el6_8 updates
  32. php-process.x86_64 5.3.3-48.el6_8 updates
  33. php-pspell.x86_64 5.3.3-48.el6_8 updates
  34. php-recode.x86_64 5.3.3-48.el6_8 updates
  35. php-snmp.x86_64 5.3.3-48.el6_8 updates
  36. php-soap.x86_64 5.3.3-48.el6_8 updates
  37. php-tidy.x86_64 5.3.3-48.el6_8 updates
  38. php-xml.x86_64 5.3.3-48.el6_8 updates
  39. php-xmlrpc.x86_64 5.3.3-48.el6_8 updates
  40. php-zts.x86_64 5.3.3-48.el6_8 updates

一樣的,咱們須要將php-fpm設置爲開機啓動。

  1. chkconfig php-fpm on
  2. service php-fpm start

啓動完成後,咱們能夠經過netstat -tunlp|grep 9000命令查看到,php-fpm 已經開始監聽9000端口。

  1. [root@localhost ~] # netstat -tunlp|grep 9000
  2. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2147/php-fpm

配置nginx使其支持php程序

接下來咱們演示如何部署web服務的內容。

建立web目錄和文件

咱們假設web目錄爲/var/www,建立並進入這個目錄。

  1. mkdir / var/www
  2. cd / var/www

咱們新建兩個文件,一個html文件,一個php文件,稍後會看到效果。

a.html的內容爲:

<h1>Hello World</h1> 

b.php的內容爲:

  1. <?php
  2. phpinfo();
  3. // 將會打印出全部的PHP信息
  4. ?>

變動nginx配置

咱們使用vim打開nginx第一個站點的配置文件vim /etc/nginx/conf.d/default.conf

將第9行的root變動爲咱們指定的目錄。

修改

  1. location / {
  2. root /usr/share/nginx/html;
  3. index index.html index.htm;
  4. }

變動爲

  1. location / {
  2. root / var/www;
  3. index index.html index.htm;
  4. }

將30-36行的註釋去掉,使其支持php文件,同時還須要修改rootfastcgi_param選項指定咱們的工做目錄。

修改

  1. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  2. #
  3. #location ~ \.php$ {
  4. # root html;
  5. # fastcgi_pass 127.0.0.1:9000;
  6. # fastcgi_index index.php;
  7. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  8. # include fastcgi_params;
  9. #}

變動爲

  1. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  2. #
  3. location ~ \.php$ {
  4. root / var/www;
  5. fastcgi_pass 127.0.0.1:9000;
  6. fastcgi_index index.php;
  7. fastcgi_param SCRIPT_FILENAME / var/www$fastcgi_script_name;
  8. include fastcgi_params;
  9. }

保存後,執行service nginx reload從新載入nginx配置。

此時,咱們能夠經過瀏覽器直接訪問咱們剛纔創建的文件了。

經過瀏覽器訪問

結語

以上,就是lnmp的簡單安裝和配置,它已經能夠解析php程序。生產環境中,每每還要對其配置文件進行各類更改,已對其性能進行優化。

例如,php的session目錄可能默認會沒有寫權限、nginx的鏈接數要更改等各類問題。

熟悉了lnmp的簡單安裝之後,就能夠繼續深刻了解,學習手動編譯指定版本的nginx、php或mysql服務了。

相關文章
相關標籤/搜索