前言:在这里将介绍如何在CentOS下搭建LAMP环境(全部使用源码编译安装),用于web服务器开发。
•LAMP: Linux + Apache + PHP + Mysql。
•系统: CentOS 6.5,32位。
CentOS安装
我选取了32位的CentOS 6.5这个Linux系统,安装过程也很简单,不再过多叙述,只稍微介绍一下我的分区情况:
分区名 大小
/boot 200MB
/swap 2GB(不大于物理分区即可)
/ 20GB
/home 剩余空间
Apache编译安装
我选择了httpd 2.2这个版本,可以参考官方说明文档。
安装依赖:
安装依赖需要用到的gcc、g++编译器:
########gcc、g++
sudo yum install gcc
sudo yum install gcc-c++ 下面两个是apache的优化工具:
########apr
wget http://labfile.oss.aliyuncs.com/apr-1.5.1.tar.gz
tar -zxvf apr-1.5.1.tar.gz
cd apr-1.5.1/
./configure --prefix=/opt/apr
Sudo make && make install
########apr-util
cd Downloads/
wget http://labfile.oss.aliyuncs.com/apr-util-1.5.4.tar.gz
tar -zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/opt/apr-util --with-apr=/opt/apr
sudo make && make install
Perl库,兼容正则表达式库:
cd Downloads/
wget http://labfile.oss.aliyuncs.com/pcre-8.36.tar.gz
tar -zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure --prefix=/opt/pcre
sudo make && make install
安装apache:
cd Downloads/
wget http://labfile.oss.aliyuncs.com/httpd-2.2.29.tar.gz
tar -zxvf httpd-2.2.29.tar.gz
cd httpd-2.2.29
./configure --prefix=/opt/apache --with-apr=/opt/apr --with-apr-util=/opt/apr-util --with-pcre=/opt/pcre
sudo make && make install
sudo sed -i 's/^#ServerName.*/ServerName localhost/' /opt/apache/conf/httpd.conf
测试:
启动:
Sudo /opt/apache/bin/apachectl start获取服务器回应(安装apache最后一步已经将服务器设为localhost):
curl -I 127.0.0.1若安装成功则如下:
关闭:
sudo /opt/apache/bin/apachectl stop
MySQL编译安装
安装依赖:
注意: MySQL5.5之后的版本使用cmake进行编译。
########若先前编译过,则需要删除刚才编译生成的CMakeCache.txt文件
rm -f CMakeCache.txt
sudo yum -y install gcc gcc-c++ autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* cmake cmake-gui libaio-devel bison-devel make
编译安装(时间较长):
Cd Downloads/
Wget http://labfile.oss.aliyuncs.com/mysql-5.6.24.tar.gz
Tar -zxvf mysql-5.6.24.tar.gz
cd mysql-5.6.24
cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_DATADIR=/opt/mysql/data -DWITH_INNOBASE_STORAGE_ENGINE=1
sudo make
sudo make install
创建mysql用户:
id mysql > /dev/null 2>&1 || sudo useradd -r -s /sbin/nologin -M -d /opt/mysql/data mysql
初始化数据目录:
sudo chown -R mysql:mysql /opt/mysql/data
sudo /opt/mysql/scripts/mysql_install_db --basedir=/opt/mysql --datadir=/opt/mysql/data --user=mysql --defaults-file=/opt/mysql/support-files/my-default.cnf
修改配置文件:
sudo sh -c 'cat >> /opt/mysql/my.cnf <<EOF
basedir = /opt/mysql
datadir = /opt/mysql/data
log-error = /opt/mysql/data/error.log
pid-file = /opt/mysql/data/mysql.pid
port = 3306
EOF'
清除残留文件:
sudo sh -c 'for file in /etc/mysql/my.cnf /etc/my.cnf ; do [ -f $file ] && rm -f $file ; done'
启动:
sudo /opt/mysql/support-files/mysql.server start
成功则显示:
测试:
/opt/mysql/bin/mysqladmin ping
成功则显示:
查看数据库:
/opt/mysql/bin/mysql -u root -e 'show databases;'
关闭:
sudo /opt/mysql/support-files/mysql.server stop
成功则显示:
PHP编译安装
安装依赖:
cd Downloads/
sudo yum install zlib1g-dev libbz2-dev libcurl4-gnutls-dev libjpeg-dev libpng12-dev libfreetype6-dev libmcrypt-dev libxml2-dev
sudo yum install bzip2-devel -y
sudo yum -y install curl-devel
sudo yum install openssl openssl-devel -y
sudo yum install libjpeg libpng freetype libjpeg-devel libpng-devel freetype-devel -y
sudo yum install libjpeg libjpeg-devel libpng libpng-devel
*以上不一定要全部安装
########安装Libmcrypt
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
sudo make
sudo make install
说明:libmcript默认安装在 /usr/local
########安装mhash
cd ..
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
sudo make
sudo make install
#########安装mcrypt
cd ..
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
LD_LIBRARY_PATH=/usr/local/lib ./configure
sudo make
sudo make install
编译安装:
cd Downloads/
wget http://labfile.oss.aliyuncs.com/php-5.5.21.tar.gz
tar -zxvf php-5.5.21.tar.gz
cd php-5.5.21
./configure --prefix=/opt/php --with-bz2 --with-curl --with-jpeg-dir --with-gd --enable-shared --enable-mbstring --with-mcrypt --with-mysql=/opt/mysql --with-pdo-mysql=/opt/mysql --with-mysqli=/opt/mysql/bin/mysql_config --enable-fpm --enable-phar --enable-bcmath --with-zlib --enable-zip --enable-ftp --with-gettext --enable-sockets --with-freetype-dir --with-config-file-path=/opt/php/etc/php.ini --with-config-file-scan-dir=/opt/php/etc/conf.d --with-apxs2=/opt/apache/bin/apxs
sudo make
sudo make install
sudo cp php.ini-production /opt/php/etc/php.ini
*错误里面含有mysql,那是因为mysql-devel 没有安装。解决方法:yum install mysql-devel
检查php是否支持mysql:
/opt/php/bin/php -m | grep mysql
若支持则显示:
修改apache配置文件,添加php的支持配置:
sudo sh -c 'cat >> /opt/apache/conf/httpd.conf <<EOF
<IfModule mime_module>
AddType application/x-httpd-php .php
</IfModule>
EOF'
若关闭则开启apache,否则重新启动apache:
sudo /opt/apache/bin/apachectl -k restart测试:
sudo sh -c 'echo "<?php echo phpinfo(); ?>" > /opt/apache/htdocs/test.php'
在浏览器输入:
配置成功则显示php信息:
*按以上步棸,注意php.ini文件所在的位置应该是/opt/php/etc/php.ini/php.ini。