LAMP 环境搭建

Linux + Apache + MySQL + PHP
PHP是一种脚本语言,当前中国乃至世界上使用php语言开发网站非常普遍
Apache是一个web服务软件
MySQL是当前最为流行的小型关系型数据库
LAMP就是一个支持解析php程序的环境
安装MySQL

1.下载mysql到/usr/local/src/:

# cd /usr/local/src/ 
# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.26-linux-glibc2.5-i686.tar.gz

2.解压:

# tar zxvf mysql-5.6.26-linux-glibc2.5-i686.tar.gz

3.把解压完的数据移动到/usr/local/mysql:

# mv mysql-5.6.26-linux-glibc2.5-i686 /usr/local/mysql
4.建立mysql用户:
# useradd -s /sbin/nologin mysql
5.初始化数据库:
# cd /usr/local/mysql
# mkdir -p /data/mysql 
# chown -R mysql:mysql /data/mysql 
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql 
这一步会报错:
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
缺少libaio-devel 库安装就好。
# yum install -y libaio-devel
 --user 定义数据库的所属主,--datadir 定义数据库安装到哪里,建议放到大空间的分区上,这个目录需要自行创建。这一步骤很关键,如果你看到两个 “OK” 说明执行正确,否则请仔细查看错误信息。

6.拷贝配置文件:
# cp support-files/my-default.cnf /etc/my.cnf
7.拷贝启动脚本文件并修改其属性:
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod 755 /etc/init.d/mysqld
8.修改启动脚本,需要修改的地方有 “datadir=/data/mysql” (前面初始化数据库时定义的目录)
# vim /etc/init.d/mysqld   
9.把启动脚本加入系统服务项,并设定开机启动,启动mysql
# chkconfig --add mysqld
# chkconfig mysqld on
# service mysqld start
如果启动不了,请到 /data/mysql/ 下查看错误日志,这个日志通常是主机名.err. 检查mysql是否启动的命令为:
# ps aux |grep mysqld

安装Apache

apache官网下载地址: http://www.apache.org/dyn/closer.cgi 

# cd /usr/local/src/

# wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.17.tar.gz

解压:

# tar zxvf httpd-2.2.16.tar.gz

配置编译参数:

# cd httpd-2.2.16

# ./configure --prefix=/usr/local/apache2 --with-included-apr  --enable-deflate=shared  --enable-expires=shared  --enable-rewrite=shared  --with-pcre 
 --prefix 指定安装到哪里,--enable-so 表示启用DSO , --enable-deflate=shared 表示共享的方式编译deflate,后面的参数同理。如果这一步你出现了这样的错误:

error: mod_deflate has been requested but can not be built due to prerequisite failures

解决办法是:

# yum install -y zlib-devel

为了避免在make的时候出现错误,所以最好是提前先安装好一些库文件:

# yum install -y pcre pcre-devel apr apr-devel

完成后可以:

# echo $?

如果出现 0 表示成功,否则需要根据错误提示去解决问题。

编译:

# make

安装:

# make install

以上两个步骤都可以使用# echo $? 来检查。

想查看详细的编译请看:http://www.php-note.com/article/detail/309

安装PHP

建议你使用5.4或者5.5版本,php官方下载地址: http://www.php.net/downloads.php

下载php:

# cd /usr/local/src/

# wget http://am1.php.net/distributions/php-5.5.30.tar.gz

解压:

# tar zxvf php-5.5.30.tar.gz

配置编译参数:

# cd php-5.5.30

./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;

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: jpeglib.h not found.

解决方法:

# yum install -y libjpeg-turbo-devel

错误5:

configure: error: png.h not found.

解决方法:

# yum install -y libpng libpng-devel

错误6:

configure: error: freetype.h not found.

解决方法:

# yum install -y freetype freetype-devel

错误7:

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解决方法:

# rpm -e epel-release 

# yum install -y epel-release 
# yum install -y libmcrypt-devel

错误8:

Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again

解决方法:

处理很简单,修改文件“/etc/yum.repos.d/epel.repo”, 将baseurl 开头的注释取消, mirrorlist 开头注释掉。即可。

因为centos6.x 默认的yum源没有libmcrypt-devel 这个包,只能借助第三方yum源。

编译:

# make

安装:

# make install

拷贝配置文件:

# cp php.ini-production /usr/local/php/etc/php.ini

Apache结合PHP

Apache主配置文件为:/usr/local/apache2/conf/httpd.conf ,编辑这个文件:

# vim /usr/local/apache2/conf/httpd.conf

首先找到下面这一行:

AddType application/x-gzip .gz .tgz

在该行下面加入一行:

AddType application/x-httpd-php .php

接着找到:

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

修改为:

<IfModule dir_module>
    DirectoryIndex index.html index.htm index.php
</IfModule>

最后找到:

ServerName www.example:80

修改为:

ServerName localhost:80

测试LAMP是否成功

Apache、MySQL和PHP安装之后,需要先检验Apache的配置文件是否正确,从而正确解析php文件。

启动Apache之前需要检验配置文件是否正确:

# /usr/local/apache2/bin/apachectl -t

如果配置正确就会显示 Syntax OK ,否则就要继续修改配置文件httpd.conf

启动Apache:

# /usr/local/apache2/bin/apachectl start

查看是否启动的命令如下:

# netstat -lnp |grep httpd

tcp        0      0 :::80                       :::*                        LISTEN      21806/httpd

如果显示这一行就表示已经启动Apache了。

使用curl测试:

# curl localhost
<html><body><h1>It works!</h1></body></html>

显示了这一行说明测试成功了。

测试是否正确解析PHP

编写一个脚本:

# vim /usr/local/apache2/htdocs/1.php

写入以下内容:

<?php
    echo "1111111111111"
?>

保存脚本继续测试:

# curl localhost/1.php

1111111111111

如果显示表明php解析正确。

初次使用浏览器访问我们的web服务的时候,你可能无法访问,这是因为防火墙的缘故。请运行下面的命令:

# iptables -F

这样就可以清除系统默认的防火墙规则,放行80端口。

LAMP环境是搭建好了,这其实仅仅是安装上了软件而已,而具体的配置还是有很多工作要做的呢?也就是说,你虽然搭建出来了环境,但是如果不会配置细节的东西,相当于没有任何工作经验,所以还是多配置配置apache或者php吧。

 Apache安装扩展模块 - mod_status

#cd /usr/local/src/httpd-2.2.16/modules/generators/

# /usr/local/apache2/bin/apxs -i -a -c -n mod_status mod_status.c

你进入 /usr/local/apache2/conf/httpd.conf 就会出现

LoadModule mod_status_module  modules/mod_status.so

PHP扩展模块安装 - memcache

# /usr/local/src

# wget http://pecl.php.net/get/memcache-2.2.7.tgz

# tar zxvf memcache-2.2.7.tgz

# cd memcache-2.2.7

# /usr/local/php/bin/phpize

执行完上边这步会报错:

Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

安装:

# yum install -y autoconf

然后在:

# /usr/local/php/bin/phpize

# ./configure --with-php-config=/usr/local/php/bin/php-config

# make

# make install 

编译完之后:

# cd /usr/local/src/php-5.3.27

# cp php.ini-development /usr/local/php/etc/php.ini

# vim /usr/local/php/etc/php.ini

在末尾加入一行:

extension = memcache.so

在执行:

# /usr/local/php/bin/php -m

你就会看到 memcache




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值