最近转向到了使用 ubuntu 做桌面,安装好系统以来一直都没配置 lamp 开发环境。由于很久以来没有自己编译安装过 apache php mysql 了有些手生,决定再实践一下。最近一次安装 web 环境是在 centos 下装 nginx php + fastcgi。虽然以前在 centos freebsd 下都装过 amp 环境,但大多是依照网络上的阿哥的步骤一步步来,早在好几年前那些阿哥的文章就靠不住了,都是抄来抄去的东西,最可恨的是他们也不查验下是否真的能安装就堂而皇之的抄去博客里放着。以前我就深受其害,可能要找 3,5 套文章方案才能最终成功,这次本人决定尽量不按别人的博客的步骤来,通过读./configure –help 的输出参数,结合一些博客中的经验来安装,做到知其然,知其所以然!
目标:
以源码编译的方式安装一个 lamp 服务器,有下列 php 扩展或功能,memcache, xcache, pdo, zip, imagemagick 等
软件环境版本
ubuntu 9.10
apache 2.2.14
mysql 5.1.14
php 5.3.1
软件下载地址
/home/eric/ 下载 /software/dev/server/lamp/
安装目标目录
/usr/local/webserver
最终安装后要形成的路径
apache: /usr/local/webserver/apache
mysql: /usr/local/webserver/mysql
php: /usr/local/webserver/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 建个新组和一个新用户名
# groupadd mysql
# useradd -g mysql 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/webserver/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/webserver/mysql/etc/my.cnf
# cd /usr/local/webserver/mysql
#bin/mysql_install_db –user=mysql –basedir=/usr/local/webserver/mysql –datadir=/usr/local/webserver/mysql/data
在这里如果不指定 basedir,datadir 则会报错找不到 mysqld
调整权限
# chown -R root .
# chown -R mysql lib data etc
# chgrp -R mysql .
启动 mysql
/usr/local/webserver/mysql/bin/mysqld_safe –user=mysql –defaults-file=/usr/local/webserver/mysql/etc/my.cnf &
链接上 mysql
mysql -uroot -S /tmp/mysql.sock
停止 mysql
/usr/local/webserver/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/webserver/apache –enable-so –enable-mods-shared=all
# make
# make install
# cd /usr/local/webserver/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/webserver/apache/htdocs”>
</Directory>
俩节相应的部分改为 (如有必要,比如不使用虚拟主机来跑网站,要设置虚拟主机可以修改 /etc/hosts 来增加一点虚拟的域名)
AllowOverride All
Order allow,deny
Allow from all
Shift + G 转到配置文件末尾
取消掉对 Virtual hosts 和 various default settings 俩项 include 的注释
启动 apache
/usr/local/webserver/apache/bin/apachectl -k start
关闭 apache
/usr/local/webserver/apache/bin/apachectl -k stop
重启 apache
/usr/local/webserver/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/webserver/php –with-apxs2=/usr/local/webserver/apache/bin/apxs –with-config-file-path=/usr/local/webserver/php/etc –with-mysql=/usr/local/webserver/mysql –with-mysqli=/usr/local/webserver/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/webserver/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/webserver/php/bin/php-config
# make
# make install
# cd ../
# tar zxvf PDO_MYSQL-1.0.2.tgz
# cd PDO_MYSQL-1.0.2/
# /usr/local/webserver/php/bin/phpize
# ./configure –with-php-config=/usr/local/webserver/php/bin/php-config –with-pdo-mysql=/usr/local/webserver/mysql
# make
# make install
# cd ../
tar zxvf imagick-2.3.0.tgz
cd imagick-2.3.0/
/usr/local/webserver/php/bin/phpize
./configure –with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../
安装 xcache
# tar zxvf xcache-1.3.0.tar.gz
# /usr/local/webserver/php/bin/phpize
# ./configure –enable-xcache –enable-xcache-coverager –with-php-config=/usr/local/webserver/php/bin/php-config
# make
# make install
# cat xcache.ini >> /usr/local/webserver/php/etc/php.ini
开始修改 php.ini,要确保 xcache.so 加载位置在任何其他的 zend_extension=*** 语句之前。
查找 extension_dir
将它修改为
extension_dir = “/usr/local/webserver/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/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/xcache.so
4, 整合 apache 与 php
# vi /usr/local/webserver/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
添加俩行
feanlau.com test.local
feanlau.com www.test.local
下面为 apache 添加一个虚拟主机
# cd /usr/local/webserver/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/webserver/mysql/bin/mysqld_safe –defaults-file=/usr/local/webserver/mysql/etc/my.cnf &
# /usr/local/webserver/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 算是配置成功了。