欢迎莅临阿Q的项目

专业WP商业设计开发销售中心

CentOS 6.2 Minimal 编译安装LAMP实录

花了两天的时间来实践编译安装LAMP最新版,过程十分顺利……

安装好Centos 6.2 Minimal后,第一件事导入Fedora EPEL repo安装源、安装yum优先级控制软件并升级内核:

[syntaxHighLighter brush="shell"]rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-7.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
yum -y install yum-priorities
yum update[/syntaxHighLighter]

---------------------------------------------------------------------------

編譯前準備工作

卸载yum或rpm安装的amp软件(CentOS Minimal不需要)
[syntaxHighLighter brush="shell"]rpm -e httpd
rpm -e mysql
rpm -e php
yum -y remove httpd
yum -y remove php
yum -y remove mysql-server mysql
yum -y remove php-mysql[/syntaxHighLighter]

安裝編譯工具

  1. 安装编译工具gcc gcc-c++make automake autoconf kernel-devel
  2. 安装PHP所需依赖,如libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel等

[syntaxHighLighter brush="shell"]yum -y install gcc gcc-c++ make automake autoconf kernel-devel ncurses-devel libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel pcre-devel libtool-libs freetype-devel gd zlib-devel file bison patch mlocate flex diffutils readline-devel glibc-devel glib2-devel bzip2-devel gettext-devel libcap-devel libmcrypt-devel[/syntaxHighLighter]

安裝下載工具
[syntaxHighLighter brush="shell"]yum -y install wget[/syntaxHighLighter]

下载所需的源码
下载apache(http://httpd.apache.org)
[syntaxHighLighter brush="shell"]wget http://labs.renren.com/apache-mirror//httpd/httpd-2.4.2.tar.gz[/syntaxHighLighter]
下载MySQL(http://mysql.mirror.kangaroot.net/Downloads/MySQL-5.5/)
[syntaxHighLighter brush="shell"]wget http://mysql.mirror.kangaroot.net/Downloads/MySQL-5.5/mysql-5.5.25.tar.gz[/syntaxHighLighter]
下载php(http://www.php.net)
[syntaxHighLighter brush="shell"]wget http://cn.php.net/get/php-5.4.4.tar.gz/from/this/mirror[/syntaxHighLighter]
下载cmake(MySQL编译工具)
[syntaxHighLighter brush="shell"]wget http://www.cmake.org/files/v2.8/cmake-2.8.8.tar.gz[/syntaxHighLighter]
下载libmcrypt(PHPlibmcrypt模块)
[syntaxHighLighter brush="shell"]wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz[/syntaxHighLighter]
下载apr(Apache库文件)
[syntaxHighLighter brush="shell"]wget http://mirror.bit.edu.cn/apache//apr/apr-1.4.6.tar.gz[/syntaxHighLighter]
下载apr-util(Apache库文件)
[syntaxHighLighter brush="shell"]wget http://labs.mop.com/apache-mirror//apr/apr-util-1.4.1.tar.gz[/syntaxHighLighter]
下載APR iconv(Apache库文件)
[syntaxHighLighter brush="shell"]wget http://labs.mop.com/apache-mirror//apr/apr-iconv-1.2.1.tar.gz[/syntaxHighLighter]

開始編譯安裝

安装libmcrypt
[syntaxHighLighter brush="shell"]cd /usr/local/src
tar zxf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure && make && make install[/syntaxHighLighter]

安装cmake
[syntaxHighLighter brush="shell"]cd /usr/local/src
tar zxf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure && make && make install[/syntaxHighLighter]

安装Apr
[syntaxHighLighter brush="shell"]cd /usr/local/src
tar zxf apr-1.4.6.tar.gz
cd apr-1.4.6
./configure --prefix=/usr/local/apr && make && make install[/syntaxHighLighter]

安装Apr-util
[syntaxHighLighter brush="shell"]cd /usr/local/src
tar zxf apr-util-1.4.1.tar.gz
cd apr-util-1.4.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config && make && make install[/syntaxHighLighter]

安装Apr-iconv
[syntaxHighLighter brush="shell"]cd /usr/local/src
tar zxf apr-iconv-1.2.1.tar.gz
cd apr-iconv-1.2.1
./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr/bin/apr-1-config && make && make install[/syntaxHighLighter]

安装Mysql

(準備工作)
[syntaxHighLighter brush="shell"]#添加mysql组并创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
groupadd mysql && useradd -g mysql mysql -s /bin/false
#创建MySQL数据库存放目录并设置MySQL数据库目录权限
mkdir -p /var/data/mysql && chown -R mysql:mysql /var/data/mysql
#创建MySQL安装目录
mkdir -p /usr/local/mysql
#進入源碼目錄解壓mysql源代碼并進入源碼目錄
cd /usr/local/src && tar zxvf mysql-5.5.25.tar.gz && cd mysql-5.5.25
#配置、編譯和安裝
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/var/data/mysql -DSYSCONFDIR=/etc && make && make install[/syntaxHighLighter]
(配置工作)
[syntaxHighLighter brush="shell"]cd /usr/local/mysql
cp ./support-files/my-huge.cnf /etc/my.cnf #拷贝配置文件(注意:/etc目录下面默认有一个my.cnf,直接覆盖即可)
vi /etc/my.cnf #编辑配置文件,在 [mysqld] 部分增加[/syntaxHighLighter]
datadir = /var/data/mysql #添加MySQL数据库路径
[syntaxHighLighter brush="shell"]./scripts/mysql_install_db --user=mysql #生成mysql系统数据库
cp ./support-files/mysql.server /etc/init.d/mysqld #把Mysql加入系统服務
chmod 755 /etc/init.d/mysqld #增加执行权限[/syntaxHighLighter]

(開啟自啟動)
[syntaxHighLighter brush="shell"]chkconfig mysqld on #加入开机启动
vi /etc/init.d/mysqld #编辑Mysql服务参数[/syntaxHighLighter]
basedir = /usr/local/mysql #MySQL程序安装路径
datadir = /var/data/mysql #MySQl数据库存放目录
[syntaxHighLighter brush="shell"]service mysqld start #启动Mysql服务
vi /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行[/syntaxHighLighter]
export PATH=$PATH:/usr/local/mysql/bin

[syntaxHighLighter brush="shell"]#下面这两行把myslq的库文件链接到系统默认的位置,这样你在编译类似PHP等软件时可以不用指定mysql的库文件地址
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql && ln -s /usr/local/mysql/include/mysql /usr/include/mysql[/syntaxHighLighter]

[syntaxHighLighter brush="shell"]shutdown -r now #需要重启系统,等待系统重新启动之后继续在终端命令行下面操作[/syntaxHighLighter]

[syntaxHighLighter brush="shell"]mysql_secure_installation #设置Mysql密码[/syntaxHighLighter]
根据提示按Y,回车输入2次密码

或者直接修改密码
[syntaxHighLighter brush="shell"]/usr/local/mysql/bin/mysqladmin -u root -p password "123456" #修改密码[/syntaxHighLighter]

[syntaxHighLighter brush="shell"]service mysqld restart #重启[/syntaxHighLighter]

安装Apache2

[syntaxHighLighter brush="shell"]cd /usr/local/src && tar -zvxf httpd-2.4.2.tar.gz
cd httpd-2.4.2
mkdir -p /usr/local/apache2 #创建安装目录

./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-apr-iconv=/usr/local/apr-iconv --with-ssl --enable-ssl --enable-so --enable-deflate=shared --enable-expires=shared --enable-headers=shared --enable-rewrite=shared --enable-static-support && make && make install #配置、編譯和安装[/syntaxHighLighter]
编译参数解释:
--prefix=/usr/local/apache:指定安装目录
--with-apr=/usr/local/apr #指定apr目錄
--with-apr-util=/usr/local/apr-util #指定apr-util目錄
--with-apr-iconv=/usr/local/apr-iconv #指定apr-iconv目錄
--enable-so:允许运行时加载DSO模块
--enable-deflate=shared:将deflate模块编译为DSO
--enable-expires=shared:将expires模块编译为DSO
--enable-headers=shared:将headers模块编译为DSO
--enable-rewrite=shared:将rewrite模块编译为DSO
--enable-static-support:使用静态连接(默认为动态连接)编译所有二进制支持程序

更详细的编译参数解释:http://lamp.linux.gov.cn/Apache/ApacheMenu/programs/configure.html
[syntaxHighLighter brush="shell"]/usr/local/apache2/bin/apachectl -k start #启动[/syntaxHighLighter]

(配置工作)
vi /usr/local/apache2/conf/httpd.conf #编辑配置文件
找到:#ServerName www.example.com:80
修改为:ServerName www.localhost.com.cn:80
找到:DirectoryIndex index.html
修改为:DirectoryIndex index.html index.php
找到:Options Indexes FollowSymLinks
修改为:Options FollowSymLinks #不显示目录结构
找到:AllowOverride None
修改为:AllowOverride All #开启apache支持伪静态,有两处都做修改
LoadModule rewrite_module modules/mod_rewrite.so #取消前面的注释,开启apache支持伪静态

[syntaxHighLighter brush="shell"]vi /etc/profile #添加apache服务系统环境变量
#在最后添加下面这一行[/syntaxHighLighter]
export PATH=$PATH:/usr/local/apache2/bin

[syntaxHighLighter brush="shell"]cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd #把apache加入到系统服務
vi /etc/init.d/httpd[/syntaxHighLighter]
在#!/bin/sh下面添加以下两行
#chkconfig:2345 10 90
#descrption:Activates/Deactivates Apache Web Server

[syntaxHighLighter brush="shell"]chown daemon.daemon -R /var/www #更改目录所有者
chmod 700 /var/www -R #更改apache网站目录权限
chkconfig httpd on #设置开机启动
service httpd restart[/syntaxHighLighter]

安装php

[syntaxHighLighter brush="shell"]cd /usr/local/src
tar -zvxf php-5.4.4.tar.gz
cd php-5.4.4
mkdir -p /usr/local/php5 #建立php安装目录
./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-gd --with-iconv --with-freetype --with-jpeg --with-png --with-zlib--with-libxml --enable-xml --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --with-mime-magic --enable-suhosin --enable-session --with-mcrypt && make && make install #配置、编译和安装
mkdir /usr/local/php5/etc
cp php.ini-production /usr/local/php5/etc/php.ini #复制php配置文件到安装目录
rm -rf /etc/php.ini #删除系统自带的配置文件
ln -s /usr/local/php5/etc/php.ini /etc/php.ini #创建配置文件软链接[/syntaxHighLighter]

(配置工作)
[syntaxHighLighter brush="shell"]vi /usr/local/php5/etc/php.ini #编辑[/syntaxHighLighter]
[syntaxHighLighter brush="shell"]disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popep,assthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname #列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
date.timezone = PRC
expose_php = OFF #禁止显示php版本的信息
display_errors = OFF #关闭错误提示[/syntaxHighLighter]

配置apache支持php

vi /usr/local/apache2/conf/httpd.conf #编辑apache配置文件
在LoadModule php5_module modules/libphp5.so这一行下面添加:
AddType application/x-httpd-php .php (注意:php .php这个点前面有一个空格)
[syntaxHighLighter brush="shell"]service httpd restart && service mysqld restart #重启apache和mysql[/syntaxHighLighter]

----------------------------------------------------------------------------------------

配置防火牆,允許防火牆通過21(FTP)、80(WEB)、3306(MYSQL)端口
[syntaxHighLighter brush="shell"]vi /etc/sysconfig/iptables[/syntaxHighLighter]

[syntaxHighLighter brush="shell"]#########################################################

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
#########################################################[/syntaxHighLighter]

[syntaxHighLighter brush="shell"]service iptables restart //最后重启防火墙使配置生效[/syntaxHighLighter]

如果喜欢本文,请分享给朋友们

CentOS 6.2 Minimal 编译安装LAMP实录4 篇评论

  1. 1.博主写的非常好.给我很大的帮助.
    2.Fedora EPEL repo 源更新了
    3就是想跟博主说下,显示哪些命令行的,能不能有换行的效果
    4.安装Apache2,后面好像少了 make && make install 吧只有配置,没有编译安装,还有就是不能编译安装到apache2中.权限755.能够执行但就是看到文件夹中有文件.

    • 阿Q管理员

      1、很高兴能帮到你。
      2、谢谢提供更新信息。
      3、这个文章相当于安装手册,是方便我下次安装的,所以很多命令我用&&连起来了。
      4、谢谢提醒,已更新了命令行字符串。关于不能编译到指定目录中的问题,请问包括创建目录和执行安装命令都是用root用户操作的吗?建议删除安装目录重建并再次执行安装命令试试。

  2. 博主真的是人才!!!佩服!!不过你这样很麻烦!直接EPEL开启源用yum配置即可!

    • 阿Q管理员

      谢谢,确实很麻烦,只是想学习一下编译安装。工作中我可不会这么浪费时间。