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服务了。

相关文章
相关标签/搜索