使用Linux系统架设网站,LAMP是非常流行的一种解决方案。LAMP组合有着极高的性能,完善的支持体系,灵活的配置方法,但是Lamp架构其本身也越来越臃肿,在Lamp安装完成后最好进行针对硬件的优化,以获得更好的性能。这里主要是通过在VMware workstation中安装CentOS6.5 (最小化安装)来完成这次实验。    

    由于这里采用yum方式安装,前提是我们必须配置好yum源。为了加快下载速度,建议使用网易的yum源。

参考:CentOS配置网易163yum源 http://skypegnu1.blog.51cto.com/8991766/1424904

    通过yum这种方式对于初学者来说,非常方便,但是可定制性不强,而且软件版本较低。一般用于实验和学习环境。


一、安装Apache

#通过yum安装httpd
[root@localhost ~]# yum -y install httpd
#开机自启动
[root@localhost ~]# chkconfig httpd on
#启动httpd 服务
[root@localhost ~]# service httpd start
#查看运行状态
[root@localhost ~]# netstat -tulpn | grep 80
tcp        0      0 :::80       :::*        LISTEN      1452/httpd

###安装apache一些扩展, 非必须
root@localhost ~]# yum -y install httpd-manual mod_ssl mod_perl mod_auth_mysql

现在直接在浏览器键入 http://localhost  或 http://your-ip  ,应该会看到Apache的测试页面

这里需要注意iptables 和 SELinux 的设置哦。

# service iptables stop

# setenforce 0


CentOS6.5使用yum快速搭建LAMP(Linux+Apache+MySQL+PHP)环境_mysql

二、安装MySQL

# mysql           客户端程序
# mysql-server    服务端程序
# mysql-devel     开发涉及的库文件

[root@localhost ~]# yum -y install mysql mysql-server mysql-devel

#开机启动
[root@localhost ~]# chkconfig mysqld on
#启动mysqld服务
[root@localhost ~]# service mysqld start

#进行一些安全性配置,根据提示,一步一步设置就好,包括如下设置:
1、是否设定root密码
2、是否移除 anonymous(匿名)账号,为了安全请务必移除,否则不用账号密码就可以登录数据库。
3、是否允许root用户远程登录,根据需求设定
4、是否移除 test 库
5、是否重新载入 权限表
[root@localhost ~]# /usr/bin/mysql_secure_installation

[root@localhost ~]# netstat -tulpn | grep -i mysql
tcp    0   0 0.0.0.0:3306  0.0.0.0:*     LISTEN   1723/mysqld

OK, 我们看到mysqld已经启动,监听在3306端口上。


三、安装PHP

    安装相关模块:为了让PHP支持MySQL,我们必须安装 php-mysql 软件包;也可使用以下命令搜索可用的php模块

[root@localhost ~]# yum -y install php php-mysql
# 安装php常用扩展模块如GD图形库、mbstring库等):
[root@localhost ~]# yum search php
[root@localhost ~]# yum -y install gd php-gd gd-devel php-xml php-common php-mbstring php-ldap php-pear php-xmlrpc php-imap

### 安装php之后,必须重启httpd服务才会生效,这一步很重要
[root@localhost ~]# service httpd restart

    这时,apache已经能够解析php脚本了,由于apache默认的网站根目录位于: /var/www/html , 因此,我们提供index.php 页面,进行测试

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vi index.php
<?php
	phpinfo();
?>

然后,我们再次在浏览器访问: http://localhost  或 http://本机IP  ,应该会看到如下页面

CentOS6.5使用yum快速搭建LAMP(Linux+Apache+MySQL+PHP)环境_mysql_02


php安装完成之后,会在 /etc/httpd/conf.d 目录下创建 php.conf 文件。

#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
<IfModule prefork.c>
  LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>

#
# Cause the PHP interpreter to handle files with a .php extension.
#
AddHandler php5-script .php
AddType text/html .php

#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

#
# Uncomment the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps


四、介绍相关配置与环境

Apache主配置文件:/etc/httpd/conf/httpd.conf

Apache主配置目录,可以将不同类型的配置分门别类放入这个目录中:/etc/httpd/conf.d/

Apache网站根目录:/var/www/html/

Apache日志文件目录:/var/log/httpd

MySQL的my.cnf配置文件:/etc/my.cnf

MySQL数据库文件位置:/usr/lib/mysql


五、安装配置phpMyAdmin

    安装好MySQL,Apache及PHP后,为了可视化的管理MySQL数据库,我们可以安装phpMyAdmin。

到其官网下载最新版本:http://www.phpmyadmin.net/home_page/

    在windows下,其实Navicat也是一个非常好用的MySQL可视化工具,推荐使用。

MySQL管理工具Navicat

官网地址https://www.navicat.com

MySQL管理工具HeidiSQL

官网地址:http://www.heidisql.com/


phpMyAdmin官方给出的安装步骤:

Quick Install

  1. Choose an appropriate distribution kit from the phpmyadmin.net Downloads page. Some kits contain only the English messages, others contain all languages.     We’ll assume you chose a kit whose name looks like phpMyAdmin-x.x.x -all-languages.tar.gz.

  2. Untar or unzip the distribution (be sure to unzip the subdirectories): tar -xzvf phpMyAdmin_x.x.x-all-languages.tar.gz in your webserver’s document root. If you don’t have direct access to your document root, put the files in a directory on your local machine, and, after step 4, transfer the directory on your web server using, for example, ftp.

  3. Ensure that all the scripts have the appropriate owner (if PHP is running in safe mode, having some scripts with an owner different from the owner of other scripts will be a problem). See 4.2 What’s the     preferred way of making phpMyAdmin secure against evil access?and 1.26 I just     installed phpMyAdmin in my document root of IIS but I get the error “No     input file specified” when trying to run phpMyAdmin. for suggestions.

  4. Now you must configure your installation. There are two methods that can be     used. Traditionally, users have hand-edited a copy ofconfig.inc.php, but     now a wizard-style setup script is provided for those who prefer a graphical installation. Creating a config.inc.php is still a quick way to get started and needed for some advanced features.


配置config.inc.php

进入/libraries目录,找到 config.default.php文件copy

到上级目录,并命名为config.inc.php ;

CentOS6.5使用yum快速搭建LAMP(Linux+Apache+MySQL+PHP)环境_phpmyadmin_03

打开浏览器,输入 http://ip/phpmyadmin/ , 好了, 具体步骤看这里:

[root@localhost ~]# tar xf phpMyAdmin-4.2.6-all-languages.tar.gz
[root@localhost ~]# mv phpMyAdmin-4.2.6-all-languages /var/www/html/phpmyadmin
[root@localhost ~]# cd /var/www/html/phpmyadmin
[root@localhost ~]# cp libraries/config.default.php config.inc.php

[root@localhost ~]# vi config.inc.php
$cfg['PmaAbsoluteUri'] = '';                //这里填写phpMyAdmin的访问网址,保持默认
$cfg['Servers'][$i]['host'] = 'localhost'; // MySQL hostname or IP address,保持默认
$cfg['Servers'][$i]['port'] = '';          // MySQL port - leave blank for default port
$cfg['Servers'][$i]['user'] = 'root';      // 填写MySQL访问phpMyAdmin使用的MySQL用户名,默认为root。
fg['Servers'][$i]['password'] = '';        // 填写对应上述MySQL用户名的密码。


# 然后重启,httpd服务
[root@localhost ~]# service httpd restart

在浏览器键入 http://localhost/phpmyadmin 即可访问。

如果遇到访问权限的问题, 请检查SElinux。

CentOS6.5使用yum快速搭建LAMP(Linux+Apache+MySQL+PHP)环境_LAMP_04

晕死,竟然报错了。Since phpMyAdmin-4.2.3, it enforces the minimum PHP (5.3) and MySQL (5.5) versions, but RHEL/CentOS 6 ships MySQL-5.1.x 所以,出了问题。


算了, 还是不要下载最新的版本吧,phpMyAdmin-4.1.14.2-all-languages

同上面的操作一样,删除原来的phpmyadmin目录,然后重新按照上述步骤操作一次。OK, 重新访问一次:

CentOS6.5使用yum快速搭建LAMP(Linux+Apache+MySQL+PHP)环境_php_05

    如果我们把phpmyadmin 目录放到其他目录下,我们还需要修改apache的配置文件 /etc/httpd/httpd.conf, 添加一个 alias /phpmyadmin  /other/directory/phpmyadmin

    


是不是很简单,其实有更简单的。来,我们来看看 xampp 这个玩意儿。

六、xampp

    XAMPP的前身叫LAMPP,是一个易于部署、安装使用简单、集成度高的LAMP环境套件。目前这个套件已经有Windows、Linux、MacOS X等平台的版本。XAMPP已经包含了大多数常用的php模块,并且针对各种不同的网站有着良好的兼容性。

    XAMPP的初衷是为开发人员准备的,因此,它默认状态下并不含健全的安全设置。所以也不建议大家采用XAMPP作为生产环境的组件。

XAMPP的官方网站:

http://www.apachefriends.org

XAMPP for Linux下载页面:

http://www.apachefriends.org/zh_cn/xampp-linux.html


安装方法:

    首先下载 XAMPP 完整安装包:

# wget http://sourceforge.net/projects/xampp/files/XAMPP%20Linux/5.6.8/xampp-linux-x64-5.6.8-0-installer.run/download

# mv download xampp-linux-x64-5.6.8-0-installer.run

CentOS6.5使用yum快速搭建LAMP(Linux+Apache+MySQL+PHP)环境_xampp_06

好了,可以坐等安装,这一步需要一定的时间,需要耐心等待,就这样几步XAMPP已经安装完成,是不是感到不可思议?是不是简单到令人发指。。


那么,如何运行/停止呢?

CentOS6.5使用yum快速搭建LAMP(Linux+Apache+MySQL+PHP)环境_LAMP_07

CentOS6.5使用yum快速搭建LAMP(Linux+Apache+MySQL+PHP)环境_xampp_08

CentOS6.5使用yum快速搭建LAMP(Linux+Apache+MySQL+PHP)环境_phpmyadmin_09

如果遇到错误怎么办?

CentOS6.5使用yum快速搭建LAMP(Linux+Apache+MySQL+PHP)环境_mysql_10


如何卸载Xampp?
    rm -rf /opt/lampp/lampp


附录:

程序在那里?

在典型的Unix系统里并没有所谓的系统设定或管理接口,而仅有所谓的设定档案,下表是包含在XAMPP中的相关软件设定档案概要。

重要档案和目录

/opt/lampp/bin/

        XAMPP指令的家目录。例如 /opt/lampp/bin/mysql 用来执行MySQL。

/opt/lampp/htdocs/

        Apache 网站根目录。

/opt/lampp/etc/httpd.conf

    Apache设定档案。

/opt/lampp/etc/my.cnf

    MySQL设定档案。

/opt/lampp/etc/php.ini

    PHP设定档案。

/opt/lampp/etc/proftpd.conf

    ProFTPD设定档案。(从 0.9.5版后才有)

/opt/lampp/phpmyadmin/config.inc.php

    phpMyAdmin设定档案。