LAMP编译安装

Posted by Mathew on 2016-08-23

LAMP:Linux + Apache + Mysql + PHP

首先要安装开发包组 “Developemnt Tools” 和 “Server Platform Development”

编译安装apache

解决依赖关系

httpd-2.4.9需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包。这里选择使用编译源代码的方式进行。

(1) 编译安装apr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[root@daniel ~]# tar xf apr-1.4.6.tar.bz2 
[root@daniel ~]# cd apr-1.4.6
[root@daniel apr-1.4.6]# ./configure --prefix=/usr/local/apr
# 编译过程省略
checking for nl_langinfo... yes
setting have_unicode_fs to "0"
setting apr_has_xthread_files to "0"
setting apr_procattr_user_set_requires_password to "0"
setting apr_thread_func to ""
setting apr_has_user to "1"

Restore user-defined environment settings...
restoring CPPFLAGS to ""
setting EXTRA_CPPFLAGS to "-DLINUX=2 -D_REENTRANT -D_GNU_SOURCE"
restoring CFLAGS to ""
setting EXTRA_CFLAGS to "-g -O2 -pthread"
restoring LDFLAGS to ""
setting EXTRA_LDFLAGS to ""
restoring LIBS to ""
setting EXTRA_LIBS to "-lrt -lcrypt -lpthread"
restoring INCLUDES to ""
setting EXTRA_INCLUDES to ""
configure: creating ./config.status
config.status: creating Makefile
config.status: creating include/apr.h
config.status: creating build/apr_rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating apr-1-config
config.status: creating apr.pc
config.status: creating test/Makefile
config.status: creating test/internal/Makefile
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
rm: cannot remove `libtoolT': No such file or directory
config.status: executing default commands
[root@daniel apr-1.4.6]# make && make install
....省略
Libraries have been installed in:
/usr/local/apr/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 apr.exp /usr/local/apr/lib/apr.exp
/usr/bin/install -c -m 644 apr.pc /usr/local/apr/lib/pkgconfig/apr-1.pc
for f in libtool shlibtool; do \
if test -f ${f}; then /usr/bin/install -c -m 755 ${f} /usr/local/apr/build-1; fi; \
done
/usr/bin/install -c -m 755 /root/apr-1.4.6/build/mkdir.sh /usr/local/apr/build-1
for f in make_exports.awk make_var_export.awk; do \
/usr/bin/install -c -m 644 /root/apr-1.4.6/build/${f} /usr/local/apr/build-1; \
done
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr/build-1/apr_rules.mk
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr/bin/apr-1-config

(2) 编译安装apr-util

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@daniel ~]# tar xf apr-util-1.4.1.tar.bz2 
[root@daniel ~]# cd apr-util-1.4.1
[root@daniel apr-util-1.4.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@daniel apr-util-1.4.1]# make && make install
...省略
Libraries have been installed in:
/usr/local/apr-util/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util/lib
/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util/bin/apu-1-config
[root@daniel apr-util-1.4.1]#

(3) httpd-2.4.9编译过程也要依赖于pcre-devel软件包,需要事先安装。此软件包系统光盘自带,因此,找到并安装即可。

编译安装httpd-2.4.9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@daniel ~]# tar xf httpd-2.4.6.tar.bz2 
[root@daniel ~]# cd httpd-2.4.6
[root@daniel httpd-2.4.6]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event
[root@daniel httpd-2.4.6]# make && make install
... 省略

Installing configuration files
mkdir /etc/httpd24
mkdir /etc/httpd24/extra
mkdir /etc/httpd24/original
mkdir /etc/httpd24/original/extra
Installing HTML documents
mkdir /usr/local/apache/htdocs
Installing error documents
mkdir /usr/local/apache/error
Installing icons
mkdir /usr/local/apache/icons
mkdir /usr/local/apache/logs
Installing CGIs
mkdir /usr/local/apache/cgi-bin
Installing header files
mkdir /usr/local/apache/include
Installing build system files
mkdir /usr/local/apache/build
Installing man pages and online manual
mkdir /usr/local/apache/man
mkdir /usr/local/apache/man/man1
mkdir /usr/local/apache/man/man8
mkdir /usr/local/apache/manual
make[1]: Leaving directory `/root/httpd-2.4.6'
[root@daniel httpd-2.4.6]#

修改httpd的主配置文件,设置其Pid文件的路径

编辑/etc/httpd/httpd.conf,添加如下行即可:
PidFile “/var/run/httpd.pid”

提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0

start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac

exit $RETVAL

而后为此脚本赋予执行权限:

1
# chmod +x /etc/rc.d/init.d/httpd

加入服务列表:

1
# chkconfig --add httpd

启动测试

1
2
3
4
5
6
7
8
9
10
[root@daniel ~]# service httpd start 
Starting httpd: [ OK ]
[root@daniel ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
[root@daniel ~]#

访问页面测试

安装mysql-5.5.33

新建用户以安全方式运行进程:

1
2
3
4
5
6
[root@daniel ~]# groupadd -r mysql 
[root@daniel ~]# useradd -g mysql -s /sbin/nologin -M -d /mydata/data mysql
[root@daniel ~]# mkdir -pv /mydata/data
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
[root@daniel ~]# chown -R mysql:mysql /mydata/data

安装并初始化mysql-5.5.33

这里要注意,mysql的编译安装和大多数软件./configure && make && make install的过程不太一样,需要先执行cmake 然后make && make install
注意安装cmake工具,yum install cmake 即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@daniel ~]# tar xf mysql-5.5.33.tar.gz -C /usr/local
[root@daniel ~]# cd /usr/local/
[root@daniel local]# ln -sv mysql-5.5.33 mysql
[root@daniel local]# cd mysql
[root@daniel mysql]# chown -R mysql:mysql .
[root@daniel mysql]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

...过程省略...

CMake Warning (dev) in sql/CMakeLists.txt:
Policy CMP0022 is not set: INTERFACE_LINK_LIBRARIES defines the link
interface. Run "cmake --help-policy CMP0022" for policy details. Use the
cmake_policy command to set the policy and suppress this warning.

Target "mysqld" has an INTERFACE_LINK_LIBRARIES property which differs from
its LINK_INTERFACE_LIBRARIES properties.

INTERFACE_LINK_LIBRARIES:

-lpthread;sql;mysys

LINK_INTERFACE_LIBRARIES:

rt

This warning is for project developers. Use -Wno-dev to suppress it.

-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:

MYSQL_USER


-- Build files have been written to: /usr/local/mysql-5.5.33
[root@daniel mysql]# make && make install
这是一个非常漫长的过程,大概需要15分钟左右,看你机器的配置,在make和make install的时候可以看到进度百分比,感觉这一点比configure方式要人性化。

编辑选项

指定安装文件的安装路径时常用的选项

1
2
3
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql  # 指定安装路径(默认的就是/usr/local/mysql)
-DMYSQL_DATADIR=/data/mysql # mysql的数据文件路径
-DSYSCONFDIR=/etc # 配置文件目录

编译过程中启用其他存储引擎

1
2
3
-DWITH_INNOBASE_STORAGE_ENGINE=1      # 使用INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 # 常应用于日志记录和聚合分析,不支持索引
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 # 黑洞存储引擎

编译过程中取消一些存储引擎

1
2
3
4
5
-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
示例如下:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1

编译进过程中功能启用的指令介绍

1
2
3
4
-DWITH_READLINE=1       # 支持批量导入mysql数据
-DWITH_SSL=system # mysql支持ssl会话,实现基于ssl的数据复
-DWITH_ZLIB=system # 压缩库
-DWITH_LIBWRAP=0 # 是否可以基于WRAP实现访问控制

其他功能指令

1
2
3
4
5
6
7
8
-DMYSQL_TCP_PORT=3306             # 默认端口
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock # 默认套接字文件路径
-DENABLED_LOCAL_INFILE=1 # 是否启用LOCAL_INFILE功能
-DEXTRA_CHARSETS=all # 是否支持额外的字符集
-DDEFAULT_CHARSET=utf8 # 默认编码机制
-DDEFAULT_COLLATION=utf8_general_ci # 设定默认语言的排序规则
-DWITH_DEBUG=0 # DEBUG功能设置
-DENABLE_PROFILING=1 # 性能分析功能是否启用

参数编辑参考

Mysql安装完成之后的目录结构

1
2
3
4
5
6
7
8
9
10
bin    	Binary files for the server binaries and client programs such as the mysql command-lien client, mysqldump, and mysql admin
data Databases and database data and index files
include Header files
lib Compiled libraries
man Man pages
mysql-test Test suite for testing MySQL Server
scripts Contains the script used to initialize a new installation
share Language support for messages such as errors
sql-bench Benchmarking test program files
support-files Example configurations and scripts for log rotation and staring MySQL Server

Mysql服务启动脚本及配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# mysql 安装目录
# cd /usr/local/mysql

# 拷贝配置文件
[root@daniel mysql]# cp support-files/my-medium.cnf /etc/my.cnf

# 拷贝服务启动脚本
[root@daniel mysql]# cp support-files/mysql.server /etc/init.d/mysqld
# 给脚本赋予可执行权限
[root@daniel mysql]# chmod +x /etc/init.d/mysqld
# 加入SysV
[root@daniel mysql]# chkconfig --add mysqld
# 开机启动
[root@daniel mysql]# chkconfig mysqld on

初始化配置表(用户表、权限表等)

mysql_install_db — Initialize MySQL Data Directory
mysql_install_db initializes the MySQL data directory and creates the system tables that it contains, if they do not exist. mysql_install_db is a shell script and is available only on Unix platforms. (As of MySQL 5.6, mysql_install_db is a Perl script and can be used on any system with Perl installed.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[root@daniel mysql]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data --user=mysql
--basedir:该参数指定了安装 MySQL 的安装路径,填写全路径可以解决相对路径所造成的问题。
--datadir:该参数指定了 MySQL 的数据库文件放在什么路径下。数据库文件即我们常说的 Mysql data 文件

o start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h mysql password 'new-password'

Alternatively you can run:

/usr/local/mysql/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd . ; /usr/local/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

New default config file was created as /usr/local/mysql/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

注:在启动MySQL服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到则会搜索"$basedir/my.cnf",在本例中就是 /usr/local/mysql/my.cnf,这是新版MySQL的配置文件的默认位置!

注意:在CentOS 6.4版操作系统的最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MySQL的正确配置,造成无法启动。

在使用"yum update"更新系统后,需要检查下/etc目录下是否会多出一个my.cnf,如果多出,将它重命名成别的。否则,MySQL将使用这个配置文件启动,可能造成无法正常启动等问题。

修改配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
user = mysql
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /mydata/data
pid-file = /mydata/data/mysqld.pid
ft_min_word_len = 4
max_allowed_packet = 32M
character-set-server = utf8
skip-name-resolve

启动mysql服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@daniel mysql]# service mysqld start
Starting MySQL... SUCCESS!

# 做一些基础安全配置,对MySQL进行安全设置(包括数据库密码以及相关的库权限设置)
[root@daniel mysql]# bin/mysql_secure_installation
# 根据提示选择 [Y/n]

<-------此处省略---------> #这里是运行后相关的提示信息
Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] y #(设置MySQL管理员root密码)
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
Remove anonymous users? [Y/n] y #(移除匿名用户,选择Y)
... Success!
<-------此处省略--------->
Disallow root login remotely? [Y/n] y #(移除管理员远程访问权限,选择Y)
... Success!
<-------此处省略--------->
Remove test database and access to it? [Y/n] y #(移除test测试数据库,选择Y)
<-------此处省略--------->
Reload privilege tables now? [Y/n] y #(刷新数据库权限,选择Y)
... Success!

测试

测试是否启动成功,查看3306端口

1
2
3
4
5
6
7
8
[root@daniel bin]# ss -tnl 
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*

登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 登录mysql
[root@daniel ~]# /usr/local/mysql/bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.38-log Source distribution

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

额外工作

(1) 将mysql的bin目录加入PATH 环境变量

1
2
3
4
5
[root@daniel ~]# vim /etc/profile.d/mysql.sh 
[root@daniel ~]# . /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@daniel ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin

(2) 将mysql的man手册加入到man的path

1
2
3
[root@daniel mysql]# vim /etc/man.config
# 新增一行
MANPATH /usr/local/mysql/man

(3) mysql的库文件

1
2
3
[root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@localhost mysql]# ldconfig -v

(4) mysql的头文件

1
2
[root@daniel ~]# ln -sv /usr/local/mysql/include /usr/include/mysql
`/usr/include/mysql' -> `/usr/local/mysql/include'

编译安装php-5.4.26

编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
[root@daniel ~]# tar xf php-5.4.26.tar.bz2 
[root@daniel ~]# cd php-5.4.26
[root@daniel php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
...过程省略
Configuring extensions
checking size of long... (cached) 8
checking size of int... (cached) 4
checking for int32_t... yes
checking for uint32_t... yes
checking for sys/types.h... (cached) yes
checking for inttypes.h... (cached) yes
checking for stdint.h... (cached) yes
checking for string.h... (cached) yes
checking for stdlib.h... (cached) yes
checking for strtoll... yes
checking for atoll... yes
checking for strftime... (cached) yes
checking which regex library to use... php
checking whether to enable LIBXML support... yes
checking libxml2 install dir... /usr
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.
注意:上面报了一个错,xml2-config not found,笔者的机子最初是最小化安装配置了epel源,可能有些包不完整。 安装便是。

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands


说明:
1、这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
2、如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。
[root@daniel php-5.4.26]# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
[root@daniel php-5.4.26]# make
...省略
directorygraphiterator.inc
pharcommand.inc
clicommand.inc
directorytreeiterator.inc
invertedregexiterator.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

[root@daniel php-5.4.26]# make test
[root@daniel php-5.4.26]# make install

为php提供配置文件
[root@daniel php-5.4.26]# cp php.ini-production /etc/php.ini

编辑apache配置文件httpd.conf,以支持php

1
2
3
4
5
6
7
8
# vim /etc/httpd/httpd.conf
1、添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

2、定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html

而后重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。

测试

编辑httpd的主页文件,这里的主页文件是在/usr/local/apache/htdocs/里面的index.html文件。备份后,修改名字为index.php.编辑index.php文件.

1
2
3
4
5
6
7
8
9
10
11
[root@daniel htdocs]# cat index.php 
<?php
$link = mysql_connect('127.0.0.1','root','');
if ($link)
echo "Success...";
else
echo "Failure...";

mysql_close();
?>
[root@daniel htdocs]#

至此,LAMP组合已然安装完成。

总结

LAMP是比较流行的web组合,淘宝网最初的模型就是LAMP,编译安装时候尤其要注意Mysql的编译安装跟一般程序包的编译安装不太一样,其次就是在程序包编译过程中会报错,报错很正常,要注意查看日志,一般都是所依赖的包版本较旧或者缺失,相应安装即可。

基于LAMP还可以做很多,比如搭建LAMP+Wordpress,Discuzz等。 同时php属于动态标记语言,如果不做优化,基于这种架构的网站访问起来速度极慢,后续有时间还会加上如何利用CGI优化LAMP,提高动态网站的访问速度。