目标:

以源码编译的方式安装一个lamp webserver服务器,有下列php扩展或功能,memcache, xcache, pdo, zip, p_w_picpathmagick等

软件环境版本

ubuntu 14.0.4

apache 2.2

mysql 5

php 5

软件下载地址

/home/eric/下载/software/dev/server/lamp/

安装目标目录

/usr/local/

最终安装后要形成的路径

apache: /usr/local/apache

mysql:  /usr/local/mysql

php:    /usr/local/php

下载相关软件

# cd /home/eric/下载/software/dev/server/lamp

# wget http://apache.freelamp.com/httpd/httpd-2.2.14.tar.gz

# wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.44.tar.gz/from/http://mysql.cs.pu.edu.tw/

# wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download

# wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download

# wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz

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

# wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download

# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz

# wget ftp://ftp.kddlabs.co.jp/graphics/ImageMagick/ImageMagick.tar.gz

# wget http://pecl.php.net/get/imagick-2.3.0.tgz

# wget http://xcache.lighttpd.net/pub/Releases/1.3.0/xcache-1.3.0.tar.gz

0, 先安装基本支持库

这个适用于debian/ubuntu, centos用yum安装,得自己查找相应的包名。也可以一个一个自己下载回来手工编译,这些库将在安装php及其他软件时被使用到。

# apt-get install libjpeg62 libjpeg62-dev libpng12-0 libpng-12-0-dev libfeetype6 libfreetype6-dev libxml2 libxml2-dev zlib1g zlib1g-dev libcurl3 libcur3-dev libncurses5-dev libperl-dev bison

!! 如果没装libperl-dev,在安装ImageMagick时会遇到麻烦。如果缺少bison,则会一切安装顺利,但当apache load libphp5.so时会报错#ff0000 symbol: zend_parse_parameters。

1, 安装mysql

# cd /home/eric/下载/software/dev/server/lamp/

# tar -zxvf mysql-5.1.44.tar.gz

或者使用下面解压

gunzip < mysql-5.1.44.tar.gz | tar -xvf -

为运行mysql建个新组和一个新用户名

# useradd  mysql

# cd mysql-5.1.44

下面进行配置,mysql需要libncurses5-dev否则会出现 error: no curses/termcap library,先要确认自己系统里安装过libncurses5-dev,如果没有的话configure时会出错,我电脑上就没有,先用apt-get安装之,centos系统则用yum,包名可以自己查询一下。参见步骤0 - 安装基本支持库

# ./configure --prefix=/usr/local/mysql --enable-assembler --with-charset=utf8 --with-extra-charsets=complex --enable-local-infile --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-thread-safe-client --with-plugins=innobase

# make

# make install

# cd support-files

# cp my-medium.cnf /usr/local/mysql/etc/my.cnf

# cd /usr/local/mysql

#bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

在这里如果不指定basedir,datadir则会报错找不到mysqld

调整权限

# chown -R root .

# chown -R mysql lib data etc

# chgrp -R mysql .

启动mysql

/usr/local/mysql/bin/mysqld_safe --user=mysql --defaults-file=/usr/local/mysql/etc/my.cnf &

链接上mysql

mysql -uroot -S /tmp/mysql.sock

停止mysql

/usr/local/mysql/bin/mysqladmin -uxxx -pxxx -S /tmp/mysql.sock shutdown

遇到的问题,若不用mysql -S /tmp/mysql.sock指定,则mysql客户端会去读/etc/mysql/my.cnf配置文件,目前尚不知道该mysql client是在何时安装的。而且位于新力得包管理器里,可能是在安装mysql workbench时由于依赖关系被安装上去的。

2, 安装apache

# cd /home/eric/下载/software/dev/server/lamp/

# tar zxvf httpd-2.2.14.tar.gz

# cd lamp/httpd-2.2.14

# ./configure --prefix=/usr/local/apache --enable-so --enable-mods-shared=all

# make

# make install

# cd /usr/local/apache

修改apache配置文件

# vi conf/httpd.conf

搜索LoadModule,在下面加上

LoadMOdule rewrite_module modules/mod_rewrite.so

LoadModule alias_module modules/mod_alias.so

LoadModule autoindex_module modules/mod_autoindex.so

LoadModule deflate_module modules/mod_deflate.so

LoadModule dir_module modules/mod_dir.so

LoadModule userdir_module modules/mod_userdir.so

LoadModule env_module modules/mod_env.so

LoadModule expires_module modules/mod_expires.so

LoadModule mime_module modules/mod_mime.so

LoadModule vhost_alias_module modules/mod_vhost_alias.so

LoadModule authz_host_module modules/mod_authz_host.so

LoadModule log_config_module modules/mod_log_config.so

<Directory />

</Directory>

<Directory "/usr/local/apache/htdocs">

</Directory>

俩节相应的部分改为 (如有必要,比如不使用虚拟主机来跑网站,要设置虚拟主机可以修改/etc/hosts来增加一点虚拟的域名)

AllowOverride All

Order allow,deny

Allow from all

Shift + G 转到配置文件末尾

取消掉对Virtual hosts和various default settings俩项include的注释

启动apache

/usr/local/apache/bin/apachectl -k start

关闭apache

/usr/local/apache/bin/apachectl -k stop

重启apache

/usr/local/apache/bin/apachectl -k restart

3, 安装php和一些扩展

3.1 安装编译php5.3所需的基本支持库

# cd /home/eric/下载/software/dev/server/lamp/

# tar zxvf libiconv-1.13.1.tar.gz

# cd libiconv-1.13.1

# ./configure --prefix=/usr/local

# make

# make install

# cd ..

# tar zxvf libmcrypt-2.5.8.tar.gz 

# cd libmcrypt-2.5.8

# ./configure

# make

# make install

# /sbin/ldconfig

# cd libltdl

# ./configure --enable-ltdl-install

# make

# make install

# cd ../..

# tar zxvf mhash-0.9.9.9.tar.gz

# cd mhash-0.9.9.9

# ./configure 

# make

# make install

# cd ..

# ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la

# ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so

# ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4

# ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8

# ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a

# ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la

# ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so

# ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2

# ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

# tar zxvf mcrypt-2.6.8.tar.gz

# cd mcrypt-2.6.8/

# /sbin/ldconfig

# ./configure

# make

# make install

# cd ../

没有先安装libperl-dev的话,这里make install会遇到麻烦,而在centos服务器上似乎无此问题

# tar zxvf ImageMagick.tar.gz

# cd ImageMagick-6.6.0-0

# ./configure 

# make

# make install

# cd ..

3.2 安装php

# tar zxvf php-5.3.1.tar.gz

# cd php-5.3.1

./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap

# make ZEND_EXTRA_LIBS='-liconv'

# make install

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

# cd ..

3.3 安装php扩展模块

# tar zxvf memcache-2.2.5.tgz

# cd memcache-2.2.5/

# /usr/local/webserver/php/bin/phpize

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

# make

# make install

# cd ../

# tar zxvf PDO_MYSQL-1.0.2.tgz

# cd PDO_MYSQL-1.0.2/

# /usr/local/php/bin/phpize

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

# make

# make install

# cd ../

tar zxvf imagick-2.3.0.tgz

cd imagick-2.3.0/

/usr/local/php/bin/phpize

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

make

make install

cd ../

安装xcache

# tar zxvf xcache-1.3.0.tar.gz

# /usr/local/php/bin/phpize

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

# make

# make install

# cat xcache.ini >> /usr/local/php/etc/php.ini

开始修改php.ini,要确保 xcache.so 加载位置在任何其他的zend_extension=***语句之前。

查找 extension_dir

将它修改为 

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"

并在此行下面添加一下几行

extension = "memcache.so"

extension = "pdo_mysql.so"

extension = "imagick.so"

将[xcache-common]节里的

zend_extension = /usr/local/lib/php/extensions/non-debug-non-zts-xxx/xcache.so 注释掉

在其下面添加一行

zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xcache.so

4, 整合apache与php

# vi /usr/local/apache/conf/httpd.conf 

找到

     <IfModule dir_module>

         DirectoryIndex index.html

     </IfModule>

将其改为

     <IfModule dir_module>

         DirectoryIndex index.php index.html index.htm

     </IfModule>

找到 Addtype application/x-gzip .tgz

在它下面添加一行

Addtype application/x-httpd-php .php

5, 创建一个测试用虚拟主机

# vi /etc/hosts

添加俩行

127.0.0.1 test.local

127.0.0.1 www.test.local

下面为apache添加一个虚拟主机

# cd /usr/local/apache/conf/extra/

# vi httpd-vhosts.conf

将配置文件中原先俩个用来演示的配置注释掉,添加如下指令

<VirtualHost *:80>

    ServerAdmin greathqy@gmail.com

    DocumentRoot "/home/eric/projects/php/testing"

    ServerName www.test.local

    ErrorLog "logs/testing-error_log"

    CustomLog "logs/testing-access_log" common

    <Directory "/home/eric/projects/php/testing">

        Options Indexes FollowSymLinks

        AllowOverride All

        Order deny,allow

        Allow from all

    </Directory>

</VirtualHost>

保存并关闭

6, 启动apache mysql

# /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf &

# /usr/local/apache/bin/apachectl -k start 

转到 /home/eric/projects/php/testing

创建一个新文件 test.php

输入

<?php 

phpinfo();

?>

在浏览器输入 http://www.test.local/test.php

呈现出phpinfo页面,可以看到curl, gd, iconv, imagick, mbstring, memcache, mcrypt, mysql, mysqli, pdo mysql, xcache, zip等扩展和支持都已经完成了。

可以尝试用php链接下mysql试试,将test.php修改为


<?php
//phpinfo();

$handle = mysql_connect("localhost", "root", "") or die("unable connect to mysql");

if ($handle) {
  
mysql_select_db("test", $handle) or die("can't select db");
}

echo "connected to mysql <Br />";

$sql = "CREATE TABLE IF NOT EXISTS `user` (`username` varchar(50) NOT NULL, `password` varchar(32) NOT NULL) ENGINE=MYISAM DEFAULT CHARSET=utf8";

mysql_query($sql, $handle) or die("can't query database");

echo "data table created";

echo mysql_info();


?>

保存刷新test.php,可以看到输出:

connected to mysql 

data table created

 

到这里为止一个适用于开发环境的lamp算是配置成功了。