源码编译LAMP:httpd-2.4.4、mysql-5.6.21、php-5.6.3

此处只是做个编译记录方便以后使用。

系统环境:centos 6.5

编译前准备:关闭selinux、iptables

系统软件全部放在/usr/local/src/下

先安装编译工具及常用库文件:

yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl  cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng* libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libX* libtiff libtiff* make mpfr ncurses* ntp openssl nasm nasm* openssl-devel patch pcre-devel perl php-common php-gd policycoreutils ppl telnet t1lib t1lib* wget zlib-devel

一、先安装apache

1、安装apr

cd /usr/local/src
tar jxvf apr-1.4.6.tar.bz2
cd apr-1.4.6
./configure --prefix=/usr/local/apr
make
make install

2、安装apr-util

cd /usr/local/src
tar jxvf apr-util-1.5.2.tar.bz2
cd apr-util-1.5.2
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
make
make install

3、安装zlib

tar zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/usr/local/zlib
make && make install

4、安装pcre

tar zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure --prefix=/usr/local/pcre 
make && make install

4、安装apache

添加apache运行账号和组
groupadd apache
useradd -g apache apache -s /sbin/nologin
安装一个组件:
yum -y install libc-client-devel
ln -s /usr/lib64/libc-client* /usr/lib/

cd /usr/local/src
tar zxvf httpd-2.4.4.tar.gz 
cd  httpd-2.4.4
./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd --enable-so \
--enable-ssl --enable-cgi --enable-rewrite \
--enable-mime-magic \
--with-zlib=/usr/local/zlib/ \
--with-pcre=/usr/local/pcre/ \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--with-ssl=/etc --enable-modules=most \
--enable-mpms-shared=all --with-mpm=event
make
make install

对apache进行简单配置:

vim /etc/httpd/httpd.conf

找到:#ServerName www.example.com: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支持伪静态

修改User和Group为apache

vim /etc/profile  #添加apache服务系统环境变量

在最后一行添加:

export PATH=$PATH:/usr/local/apache/bin

cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd      #把apache加入到系统启动

vim /etc/init.d/httpd   #编辑文件

在#!/bin/sh下面添加以下两行

#chkconfig:2345 10 90

#description:Activates/Deactivates Apache Web Server

chown  apache.apache  -R /usr/local/apache/htdocs  #更改目录所有者

chmod   700 /usr/local/apache/htdocs #更改apache网站目录权限

chkconfig httpd on    #设置开机启动

/etc/init.d/httpd start

service httpd restart

二、安装mysql

1、安装libmcrypt

cd /usr/local/src
tar zxvf libmcrypt-2.5.8.tar.gz 
cd libmcrypt-2.5.8
./configure 
make && make install

2、安装cmake

cd /usr/local/src
tar zxvf cmake-3.0.2.tar.gz
cd cmake-3.0.2
./configure
make && make install

3、安装mysql

groupadd mysql #添加mysql组
useradd -g mysql mysql -s /sbin/nologin#创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
mkdir -p /data/mysql #创建MySQL数据库存放目录
chown -R mysql:mysql /data/mysql #设置MySQL数据库存放目录权限
mkdir -p /usr/local/mysql #创建MySQL安装目录
cd /usr/local/src #进入软件包存放目录
tar zxvf mysql-5.6.21.tar.gz #解压
cd mysql-5.6.21 #进入目录
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc
make
make install 

编译完成以后设置:

cd /usr/local/mysql #进入MySQL安装目录
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
ln -s /usr/local/mysql/my.cnf /etc/my.cnf #添加到/etc目录的软连接
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld #把Mysql加入系统启动
chmod 755 /etc/init.d/mysqld #增加执行权限
chkconfig mysqld on #加入开机启动
vi /etc/rc.d/init.d/mysqld #编辑
basedir=/usr/local/mysql #MySQL程序安装路径
datadir=/data/mysql #MySQl数据库存放目录
:wq! #保存退出
service mysqld start #启动
vi /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行
export PATH=$PATH:/usr/local/mysql/bin
:wq! #保存退出
source /etc/profile #使配置立刻生效

设置mysql库文件到系统默认位置: 

ln -s /usr/local/mysql/lib/ /usr/lib/mysql

ln -s /usr/local/mysql/lib/ /usr/lib64/mysql/

ln -s /usr/local/mysql/include/mysql /usr/include/mysql 

mkdir /var/lib/mysql #创建目录 

ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock #添加软链接

对mysql进行初始化并设置mysql密码:

mysql_secure_installation   #根据提示选择Y,最后输入两次root密码

三、安装php

 1、安装yasm

cd /usr/local/src
tar zxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure
make
make install

 2、安装libvpx

cd /usr/local/src
tar jxvf libvpx-v1.3.0.tar.bz2
cd libvpx-v1.3.0
./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9
make
make install

3、安装tiff

cd /usr/local/src
tar zxvf tiff-4.0.3.tar.gz
cd tiff-4.0.3
./configure --prefix=/usr/local/tiff --enable-shared
make
make install

4、安装libpng

tar zxvf libpng-1.6.15.tar.gz
cd libpng-1.6.15
./configure --prefix=/usr/local/libpng --enable-shared
make
make install

5、安装freetype

cd /usr/local/src
tar zxvf freetype-2.5.4.tar.gz
cd freetype-2.5.4
./configure --prefix=/usr/local/freetype --enable-shared
make #编译
make install #安装

6、安装jpeg

cd /usr/local/src
tar zxvf jpegsrc.v9a.tar.gz
cd jpeg-9a
./configure --prefix=/usr/local/jpeg --enable-shared
make #编译
make install #安装

7、安装libgd

cd /usr/local/src
tar zxvf libgd-2.1.0.tar.gz
cd libgd-2.1.0
./configure --prefix=/usr/local/libgd \
--enable-shared --with-jpeg=/usr/local/jpeg/ \
--with-png=/usr/local/libpng/ \
--with-freetype=/usr/local/freetype/ \
--with-fontconfig=/usr/local/freetype/ \
--with-xpm=/usr/ --with-tiff=/usr/local/tiff \
--with-vpx=/usr/local/libvpx/ \
--with-zlib=/usr/local/zlib/
make
make install

8、安装t1lib

cd /usr/local/src
tar zxvf t1lib-5.1.2.tar.gz
cd t1lib-5.1.2
./configure --prefix=/usr/local/t1lib --enable-shared
make without_doc
make install

9、安装libiconv

cd /usr/local/src
tar zxvf libiconv-1.14.tar.gz 
cd libiconv-1.14 
./configure --prefix=/usr/local/libiconv
make #编译
make install #安装

10、安装curl

cd /usr/local/src
tar zxvf curl-7.44.0.tar.gz 
cd curl-7.44.0
./configure --prefix=/usr/local/curl --with-gssapi --enable-tls-srp --with-libmetalink
make
make install

11、安装php

如果系统是64位,请执行以下两条命令,否则安装php会出错(32位系统不需要执行)

\cp -frp /usr/lib64/libltdl.so*  /usr/lib/

\cp -frp /usr/lib64/libXpm.so* /usr/lib/

cd /usr/local/src
tar -zvxf php-5.6.3.tar.gz
cd php-5.6.3
export LD_LIBRARY_PATH=/usr/local/libgd/lib
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql=/usr/local/mysql/ \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-mysql-sock=/tmp/mysql.sock \
--with-pdo-mysql=/usr/local/mysql/ \
--with-gd=/usr/local/libgd \
--with-png-dir=/usr/local/libpng/ \
--with-jpeg-dir=/usr/local/jpeg/ \
--with-freetype-dir=/usr/local/freetype/ \
--with-xpm-dir=/usr/ \
--with-vpx-dir=/usr/local/libvpx/ \
--with-zlib-dir=/usr/local/zlib/ \
--with-t1lib=/usr/local/t1lib/ \
--with-iconv=/usr/local/libiconv/ \
--with-curl=/usr/local/curl/ \
--enable-libxml \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-opcache \
--enable-fpm \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-mcrypt \
--enable-ctype \
--with-imap \
--with-imap-ssl \
--enable-pcntl \
--with-kerberos
make
make install 

下面是关于php的后续配置:

cp php.ini-production /usr/local/php/etc/php.ini #复制php配置文件到安装目录

rm -rf /etc/php.ini #删除系统自带配置文件

ln -s /usr/local/php/etc/php.ini /etc/php.ini #添加软链接到 /etc目录

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf #拷贝模板文件为php-fpm配置文件

ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf #添加软连接到 /etc目录

vi /usr/local/php/etc/php-fpm.conf #编辑

user = apache #设置php-fpm运行账号为apache,user和group根据实际使用情况修改

group = apache #设置php-fpm运行组为apache

pid = run/php-fpm.pid #取消前面的分号

:wq! #保存退出

设置 php-fpm开机启动

cp /usr/local/src/php-5.6.3/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm #拷贝php-fpm到启动目录

chmod +x /etc/rc.d/init.d/php-fpm #添加执行权限

chkconfig php-fpm on #设置开机启动

vim /usr/local/php/etc/php.ini #编辑配置文件

找到:disable_functions =

修改为: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,popepassthru,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可以禁止的函数,需要开启哪些函数自行调整。

php.ini里面个别地方需要修改:

找到:;date.timezone =

修改为:date.timezone = PRC #设置时区

找到:expose_php = On

修改为:expose_php = Off #禁止显示php版本的信息

找到:short_open_tag = Off

修改为:short_open_tag = ON #支持php短标签

找到opcache.enable=0

修改为opcache.enable=1 #php支持opcode缓存

找到:;opcache.enable_cli=1 #php支持opcode缓存

修改为:opcache.enable_cli=0

找到:;opcache.memory_consumption=64

修改为:opcache.memory_consumption=128

找到:;opcache.interned_strings_buffer=4

修改为:opcache.interned_strings_buffer=8

找到:;opcache.max_accelerated_files=2000

修改为:opcache.max_accelerated_files=4000

找到:;opcache.revalidate_freq=2

修改为:opcache.revalidate_freq=60

找到:;opcache.fast_shutdown=0

修改为:opcache.fast_shutdown=1

在最后一行添加:zend_extension=opcache.so #开启opcode缓存功能,或者不开启这个换成其他php加速软件

六、配置apache支持php

vim /etc/httpd/httpd.conf

添加如下二行

  AddType application/x-httpd-php  .php

  AddType application/x-httpd-php-source  .phps

定位至DirectoryIndex index.html

  修改为:

   DirectoryIndex  index.php  index.html

重启httpd即可使用

server httpd restart

测试时需要在/usr/local/apache/htdocs/下新建index.php

输入以下内容:<?php phpinfo();?>

然后访问index.php页面出现php相关信息则测试成功。

可以使用如下代码测试连接mysql信息,自己创建php文件写入代码:

<?php

$link = mysql_connect(‘localhost’,’databasename’,password’);

if($link)

echo”Success!!!”;

else

echo”Failure!!!”;

mysql_close();?>