Linux下搭建LAMP是经典的不能再经典的中小企业站点构建平台。它的全称是Linux+Apache+Mysql+PHP,常用来搭建动态网站,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。因此在网络上也有比叫有名的LAMP一键安装解决方案。但是对于运维人员来讲,有必要了解其完成的安装过程。本文主要描述基于CentOS 6 下使用yum方式来快速构建LAMP平台(Linux安装忽略)。
一、准备yum源(本文使用了163镜像)
<code><span># mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup</span><span># wget http://mirrors.163.com/.help/CentOS6-Base-163.repo -P /etc/yum.repos.d/</span><span># yum clean all</span><span># yum makecache</span></code>
二、安装apache httpd
详细可以参考:Linux下安装Apache httpd
<code><span>###检查是否已安装httpd</span><span># rpm -qa|grep httpd</span><span> httpd-tools-2.2.15-45.el6.centos.x86_64</span><span> httpd-2.2.15-45.el6.centos.x86_64</span><span># yum -y install httpd ###此时我们看到httpd的小版本从45变成了53</span><span># rpm -qa|grep httpd</span><span> httpd-tools-2.2.15-53.el6.centos.x86_64</span><span> httpd-2.2.15-53.el6.centos.x86_64</span><span>###查询生成的相关配置文件</span><span># rpm -qc httpd|grep conf</span><span> /etc/httpd/conf.d/welcome.conf</span><span> /etc/httpd/conf/httpd.conf</span><span> /etc/httpd/conf/magic</span><span> /etc/sysconfig/htcacheclean</span><span> /etc/sysconfig/httpd</span><span>###启动httpd</span><span># /etc/init.d/httpd start</span><span> Starting httpd: [ OK ]</span><span># netstat -nltp|grep 80</span><span> tcp 0 0 :::80 :::* LISTEN 7621/httpd</span><span>###验证web服务</span><span># curl -I http://localhost</span><span> HTTP/1.1 403 Forbidden</span><span> Date: Tue, 12 Jul 2016 09:25:15 GMT</span><span> Server: Apache/2.2.15 (CentOS)</span><span> Accept-Ranges: bytes</span><span> Content-Length: 4961</span><span> Connection: close</span><span> Content-Type: text/html; charset=UTF-8</span><span>###编写一个php页面测试</span><span># echo "</span><span>> </span><span>> <h1>This is a php test page.</h1></span><span>> <?php </span><span>> phpinfo();</span><span>> ?></span><span>> ">>/var/www/html/index.php </span><span>###测试结果为phpinfo函数没有被解释</span><span># curl http://localhost/index.php</span><span> </span><span> <h1>This is a php test page.</h1></span><span> <?php </span><span> phpinfo();</span><span> ?></span><span> </span></span></span></code>
三、安装php
<code><span>###安装php,同时会安装依赖包</span><span># yum install php</span> Installing: php x86_64 <span>5.3</span><span>.3</span>-<span>47.</span>el6 base <span>1.1</span> M Installing <span>for</span> dependencies: php-cli x86_64 <span>5.3</span><span>.3</span>-<span>47.</span>el6 base <span>2.2</span> M php-common x86_64 <span>5.3</span><span>.3</span>-<span>47.</span>el6 base <span>530</span> k <span>###查看php安装清单 </span><span># rpm -ql php</span> /etc/httpd/conf.d/php.conf /usr/lib64/httpd/modules/libphp5.so /<span>var</span>/lib/php/session /<span>var</span>/www/icons/php.gif <span>###查看php的配置文件 </span><span># grep -vE "^#|^$" /etc/httpd/conf.d/php.conf </span> <ifmodule prefork.c> LoadModule php5_module modules/libphp5.so </ifmodule> <ifmodule worker.c> LoadModule php5_module modules/libphp5-zts.so </ifmodule> AddHandler php5-script .php AddType text/html .php DirectoryIndex index.php <span>###在上面的配置文件中,由于php以模块化方式与httpd结合工作,根据httpd的mpm模式不同,</span><span>###其所需要的php模块格式有所不同; prefork模式使用libphp5模块 worker和event模式则使用libphp5-zts模块</span><span>###重启httpd已使得php模块生效</span><span># /etc/init.d/httpd configtest</span> Syntax OK <span># /etc/init.d/httpd restart</span> Stopping httpd: [ <span> OK </span> ] Starting httpd: [ <span> OK </span> ] <span>###验证php模块已经被加载</span><span># httpd -M |grep php</span> php5_module (shared) <span>###验证php页面 </span><span># curl http://localhost/index.php|more</span> <h1>This is a php test page.</h1> span> PUBLIC <span>"-//W3C//DTD XHTML 1.0 Transitional//EN"</span><span>"DTD/xhtml1-transitional.dtd"</span>> <style type="<span">"text/css"> body {background-color: #ffffff; color: #<span>000000;} ........... <span>###切换为使用worker工作模式<span># cp /etc/sysconfig/httpd /etc/sysconfig/httpd.bk<span># sed -i "s@#HTTPD=/usr/sbin/httpd.worker@HTTPD=/usr/sbin/httpd.worker@g" /etc/sysconfig/httpd<span># grep -vE "^#|^$" /etc/sysconfig/httpd HTTPD=/usr/sbin/httpd.worker <span>###从下面的提示中,我们需要使用php5zts模块<span># /etc/init.d/httpd restart Stopping httpd: [ <span> OK ] Starting httpd: httpd.worker: Syntax error on line <span>221 of /etc/httpd/conf/httpd.conf: Syntax error on line <span>9 of /etc/httpd/conf.d/php.conf: Cannot load /etc/httpd/modules/libphp5-zts.so into server: /etc/httpd/modules/libphp5-zts.so: cannot open shared object file: No such file or directory [FAILED] <span>###安装php-zts模块<span># yum -y install php-zts<span># rpm -ql php-zts /usr/lib64/httpd/modules/libphp5-zts.so <span># ps -ef|grep http ###查看httpd,已经切换为使用worker模式 root <span>10339<span>1<span>0<span>04:<span>35 ? <span>00:<span>00:<span>00 /usr/sbin/httpd.worker apache <span>10341<span>10339<span>0<span>04:<span>35 ? <span>00:<span>00:<span>00 /usr/sbin/httpd.worker apache <span>10342<span>10339<span>0<span>04:<span>35 ? <span>00:<span>00:<span>00 /usr/sbin/httpd.worker apache <span>10343<span>10339<span>0<span>04:<span>35 ? <span>00:<span>00:<span>00 /usr/sbin/httpd.worker apache <span>10344<span>10339<span>0<span>04:<span>35 ? <span>00:<span>00:<span>00 /usr/sbin/httpd.worker</style></code>
四、安装mysql
<code><span># rpm -qa|grep mysql</span> mysql-libs-<span>5.1</span><span>.73</span>-<span>5.</span>el6_6.x86_64 <span># yum install mysql-server </span><span># rpm -qa|grep mysql </span> mysql-<span>5.1</span><span>.73</span>-<span>7.</span>el6.x86_64 mysql-libs-<span>5.1</span><span>.73</span>-<span>7.</span>el6.x86_64 mysql-server-<span>5.1</span><span>.73</span>-<span>7.</span>el6.x86_64 <span>###查看mysql安装产生的文件</span><span># rpm -ql mysql-server</span><span># rpm -ql mysql</span><span># more /etc/my.cnf</span> [mysqld] datadir=/<span>var</span>/lib/mysql socket=/<span>var</span>/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=<span>0</span> [mysqld_safe] log-error=/<span>var</span>/log/mysqld.log pid-file=/<span>var</span>/run/mysqld/mysqld.pid <span># /etc/init.d/mysqld start</span><span># /usr/bin/mysqladmin -u root password '***'</span><span># mysql -uroot -p</span> mysql> show variables like <span>'port'</span>; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | <span>3306</span> | +---------------+-------+ <span>###安装php连接mysql驱动</span><span># yum install php-mysql</span><span>###查看安装完毕后生产的文件</span><span># rpm -ql php-mysql</span> /etc/php.d/mysql.ini ### Author : Leshami /etc/php.d/mysqli.ini ### Blog : http:<span>//blog.csdn.net/leshami</span> /etc/php.d/pdo_mysql.ini /usr/lib64/php/modules/mysql.so /usr/lib64/php/modules/mysqli.so /usr/lib64/php/modules/pdo_mysql.so <span>###测试到mysql的连接</span><span>#vim /var/www/html/connmysql.php</span> <?php $conn = mysql_connect(<span>'127.0.0.1',<span>'root'</span>,<span>'***'</span>); if ($conn) echo <span>"succ"</span>; <span>else</span> echo <span>"failure"</span>; mysql_close(); ?> <span># curl http://localhost/connmysql.php</span> succ</code>
五、小结
1、apache httpd与php之间的衔接是通过模块化的方式来实现。
2、对于perfork模式使用libphp5模块,worker和event模式则使用libphp5-zts模块。
3、php与mysql则通过安装php-mysql包来实现php到mysql的访问。
以上就介绍了 Linux 6 下yum方式安装配置LAMP平台,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。