LINUX的LAMP環境搭配

在ubuntu linux下以編譯方式安裝LAMP(apache mysql php)環境

最近轉向到了使用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 php

找到
     <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/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算是配置成功了。html

相關文章
相關標籤/搜索