一、安装 MySQL
平时安装MySQL都是源码包安装,但是由于它的编译需要很长的时间,所以安装二进制免编译包。
MySQL官方网站下载: http://dev.mysql.com/downloads/
sohu镜像站下载:http://mirrors.sohu.com/ 选择合适的版本。
1、下载 mysql 包到 /usr/local/src/
2、解压
1
| [iyunv@tpp src]# tar -zxvf /usr/local/src/mysql-5.1.73-linux-i686-glibc23.tar.gz
|
3、把解压后的数据移动到 /usr/local/mysql
1
| [iyunv@tpp src]# mv mysql-5.1.73-linux-i686-glibc23 /usr/local/mysql
|
4、创建 MySQL 用户
1
| [iyunv@tpp src]# useradd -s /sbin/nologin mysql
|
5、初始化数据库
1
2
3
4
| [iyunv@tpp src]# cd /usr/local/mysql
[iyunv@tpp mysql]# mkdir -p /data/mysql //创建datadir,数据库文件会放在这里
[iyunv@tpp mysql]# chown -R mysql /data/mysql //更改权限
[iyunv@tpp mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql //--user定义数据库所属主,--datadir定义数据库安装目录
|
6、拷贝配置文件
1
| [iyunv@tpp mysql]# cp support-files/my-large.cnf /etc/my.cnf
|
7、拷贝启动脚本并修改其属性
1
2
| [iyunv@tpp mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[iyunv@tpp mysql]# chmod 755 /etc/init.d/mysqld
|
8、修改启动脚本
1
| [iyunv@tpp mysql]# vim /etc/init.d/mysqld //进入后修改以下两个地方
|
1
2
| basedir=/use/local/mysql
datadir=/data/mysql
|
9、把启动脚本加入系统服务项,并设定开机启动,启动mysql
1
2
3
| [iyunv@tpp mysql]# chkconfig --add mysqld
[iyunv@tpp mysql]# chkconfig mysqld on
[iyunv@tpp mysql]# service mysqld start
|
若不能启动,可以到 /data/mysql/ 目录下查看错误日志,日志通常是主机名.err。检查MySQL是否启动的命令为:ps aux |gerp mysqld 。
二、安装 Apache sohu镜像站下载地址:http://mirrors.sohu.com/
1、下载 httpd 包到 /usr/local/src/
2、解压
1
| [iyunv@tpp src]# tar -jxvf httpd-2.2.31.tar.bz2
|
3、配置编译参数
1
2
3
4
5
6
7
8
9
| [iyunv@tpp src]# cd httpd-2.2.31
[iyunv@tpp httpd-2.2.31]# ./configure \
--prefix=/usr/local/apache2 \
--with-included-apr \
--enable-so \
--enable-deflate=shared \
--enable-expires=shared \
--enable-rewrite=shared \
--with-pcre
|
这里 --prefix 指定安装到目录, --enable-so 表示启用DSO, --enable-deflate=shared 表示共享的方式编译deflate,后面的参数同理。
可能会出现如下报错:
1
2
| 报错1: error: mod_deflate has been requested but can not be built due to prerequisite failures
解决:yum install -y zlib-devel
|
1
2
| 为了避免在make的时候出现错误,所以最好是提前先安装好一些库文件:
yum install -y pcre pcre-devel apr apr-devel
|
4、编译
1
| [iyunv@tpp httpd-2.2.31]# make
|
5、安装
1
| [iyunv@tpp httpd-2.2.31]# make install
|
以上4、5两步都可以使用 echo $? 来检查是否正确执行,0则表示执行成功。否则需要根据错误提示去解决问题。
三、安装 PHP
PHP官方下载地址:http://www.php.net/downloads.php
sohu镜像站下载地址:http://mirrors.sohu.com/php/php-5.3.27.tar.gz
1、下载 php 包到 /usr/local/src/
2、解压
1
| [iyunv@tpp src]# tar -zxvf php-5.3.27.tar.gz
|
3、配置编译参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| [iyunv@tpp src]# cd php-5.3.27
[iyunv@tpp php-5.3.27]# ./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=/usr/local/mysql \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-bz2 \
--with-openssl \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-sockets \
--enable-exif \
--disable-ipv6
|
注:可能会出现如下报错 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 报错1:configure: error: xml2-config not found. Please check your libxml2 installation.
解决:yum install -y libxml2-devel
报错2:configure: error: Cannot find OpenSSL's <evp.h>
解决:yum install -y openssl openssl-devel
报错3:checking for BZip2 in default path... not found
configure: error: Please reinstall the BZip2 distribution
解决:yum install -y bzip2 bzip2-devel
报错4:configure: error: png.h not found.
解决:yum install -y libpng libpng-devel
报错5:configure: error: freetype.h not found.
解决:yum install -y freetype freetype-devel
报错6:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决:rpm -ivh "http://www.aminglinux.com/bbs/data/attachment/forum/month_1211/epel-release-6-7.noarch.rpm"
yum install -y libmcrypt-devel
|
4、编译 1
| [iyunv@tpp php-5.3.27]# make
|
5、安装
1
| [iyunv@tpp php-5.3.27]# make install
|
6、拷贝配置文件
1
| [iyunv@tpp php-5.3.27]# cp php.ini-production /usr/local/php/etc/php.ini
|
四、Apache 结合 PHP
编辑Apache 的主配置文件为: /usr/local/apache2/conf/httpd.conf
1
| [iyunv@tpp ~]# vim /usr/local/apache2/conf/httpd.conf
|
①首先找到:
1
| AddType application/x-gzip .gz .tgz //在其下面增加如下一行:
|
1
| AddType application/x-httpd-php .php
|
②再找到:
1
2
3
| <IfModule dir_module>
DirectoryIndex index.html
</IfModule>
|
更改为:
1
2
3
| <IfModule dir_module>
DirectoryIndex index.html index.htm index.php
</IfModule>
|
③最后找到:
更改为:
1
| ServerName localhost:80
|
五、测试LAMP是否成功
Apache、MySQL 和 PHP 安装之后,需要先检验 Apache 的配置文件是否正确,从而才能正确解析出php文件。
1、测试配置文件是否正确
①启动 Apache 之前需要先检验配置文件是否正确,如下所示:
1
| [iyunv@tpp ~]# /usr/local/apache2/bin/apachectl -t
|
(如果有错误,继续修改 httpd.conf,如果是正确的则显示为 “Syntax OK”)
②启动 Apache:
1
| [iyunv@tpp ~]# /usr/local/apache2/bin/apachectl start
|
③查看是否启动:
1
2
| [iyunv@tpp ~]# netstat -lnp |grep httpd
tcp 0 0 :::80 :::* LISTEN 7667/httpd
|
(如果显示这一行,则说明 Apache 启动了)
另外我们也可以使用 curl 命令简单测试:
1
2
| [iyunv@tpp ~]# curl localhost
<html><body><h1>It works!</h1></body></html>
|
(如果显示了这一行,也说明测试成功了)
2、测试是否正确解析PHP
首先编写一个脚本,如下所示:
1
| [iyunv@tpp ~]# vim /usr/local/apache2/htdocs/1.php
|
1
2
3
| <?php
echo "php解析正常";
?>
|
保存脚本后继续测试,如下所示:
1
| [iyunv@tpp ~]# curl localhost/1.php
|
如果能够显示以上信息则说明 php 解析正确。
|