Unix 服務(wù)器的安裝與配置 (FreeBSD) 之Apache的完全安裝(apache+php4+mod_ssl+mod_perl+mod_fastcgi+mod_gzip)
本文主要討論源代碼方式的安裝。
從互聯(lián)網(wǎng)下載以下幾個(gè)程序(本文采用DSO方式安裝),按如下步驟進(jìn)行處理:
1、apache_1.3.27.tar.gz
2、mod_ssl-2.8.12-1.3.27.tar.gz
3、mod_perl-1.0-current.tar
4、mod_gzip-1.3.26.1a.tar.gz
5、mod_fastcgi-2.4.0.tar.gz
6、php-4.3.1.tar.gz
(另需安裝pth-1.4.1.tar.gz、mm-1.1.3.tar.gz、perl-5.6.1.tar.gz、zlib-1.1.4.tar.gz、libpng-1.2.5.tar.gz、png、gettext、freetype、jpeg、gd、gd2)
安裝pth-1.4.1
server# tar zxvf pth-1.4.1.tar.gz
server# cd pth-1.4.1
server# ./configure --enable-pthread
server# make
server# make test
server# make install
安裝mm-1.1.3
server# tar zxvf mm-1.1.3.tar.gz
server# cd mm-1.1.3
server# ./configure --disable-shared
server# make
server# make test
server# make install
安裝perl-5.6.1
server# tar zxvf perl-5.6.1.tar.gz
server# cd perl-5.6.1
server# sh Configure -de
server# make
server# make test
server# make install
安裝zlib-1.1.4
server# tar zxvf zlib-1.1.4.tar.gz
server# cd zlib-1.1.4
server# ./configure
server# make test
server# make install
安裝libpng-1.2.5 (need zlib-1.1.4、png)
server# pkg_add png-version
server# tar zxvf libpng-1.2.5.tar.gz
server# cd libpng-1.2.5
server# cp scripts/makefile.freebsd makefile
server# make
server# make test
server# make install
安裝apache_1.3.27 + mod_ssl-2.8.12-1.3.27 (need OpenSSL、MM、Prel_5.6.0) + mod_perl-1.0-current
1、解壓縮包(以下文件解壓在同一個(gè)目錄下)
server# tar zxvf apache_1.3.27.tar.gz
server# tar zxvf mod_ssl-2.8.12-1.3.27.tar.gz
server# tar zxvf mod_perl-1.0-current.tar
2、配置mod_ssl-2.8.12-1.3.27
server# cd mod_ssl-2.8.12-1.3.27
server# ./configure --with-apache=../apache_1.3.27 \
--with-mm=../mm-1.1.3 \
--prefix=/usr/local/apache \
--enable-shared=ssl
server# cd ..
3、配置安裝mod_perl-1.27
server# cd mod_perl-1.27
server# perl Makefile.PL EVERYTHING=1 APACHE_SRC=../apache_1.3.27/src USE_APACI=1 PREP_HTTPD=1 DO_HTTPD=1 (Freebsd 4.7下有兩處錯(cuò)誤提示,忽略)
server# make
server# make install
server# cd ..
4、配置安裝apache_1.3.27
server# cd apache_1.3.27
server# ./configure --prefix=/usr/local/apache \
--enable-module=ssl \
--activate-module=src/modules/perl/libperl.a \
--enable-module=most \
--enable-shared=max
server# make
server# make certificate(生成證書,按提示選擇,并記下密碼)
server# make install
安裝mod_gzip-1.3.26
server# tar zxvf mod_gzip-1.3.26.1a.tar.gz
server# cd mod_gzip-1.3.26.1a
server# edit Makefile(將APXS的路徑 APXS?=/usr/local/sbin/apxs 改成apache安裝路徑: APXS?=/usr/local/apache/bin/apxs)
server# make
server# make install
安裝mod_fastcgi-2.4.0
server# tar zxvf mod_fastcgi-2.4.0.tar.gz
server# cd mod_fastcgi-2.4.0
server# /usr/local/apache/bin/apxs -o mod_fastcgi.so -c *.c
server# /usr/local/apache/bin/apxs -i -a -n fastcgi mod_fastcgi.so
安裝php-4.3.1 (need gettext、freetype、jpeg、gd、gd2、png、這里假設(shè)mysql已經(jīng)安裝并運(yùn)行)
server# pkg_add gettest-version
server# pkg_add jpeg-version
server# pkg_add gd-version
server# pkg_add gd2-version
server# tar zxvf php-4.3.1.tar.gz
server# cd php-4.3.1
server# ./configure --with-apxs=/usr/local/apache/bin/apxs \
--with-config-file-path=/usr/local/apache/conf \
--with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/--enable-versioning \
--enable-ftp --enable-bcmath --disable-debug --enable-memory-limit=yes \
--enable-track-vars --enable-sysvsem --enable-sysvshm --with-gettext \
--enable-trans-sid --enable-fastcgi --with-tsrm-pth=pth-config \
--with-freetype-dir=/usr/local --with-jpeg-dir=/usr/local --with-gd=/usr/local \
--enable-gd-native-ttf --with-png-dir=/usr/local --with-zlib=/usr/local \
--with-zlib-dir=/usr/local --with-mm=/usr/local --with-openssl --with-iconv
server# make
server# make install
server# cp php.ini-dist /usr/local/apache/conf/php.ini
配置apache是其支持php,及php.ini文件:
在/use/local/apache/conf下有這兩個(gè)文件:
1、/usr/local/apache/conf/httpd.conf 示例:httpd.conf
2、/usr/local/apache/conf/php.ini 示例:php.ini
在httpd.conf中添加:
AddType application/x-httpd-php .php .php3
AddType application/x-httpd-php-source .phps
配置php.ini:
upload_tmp_dir /tmp
default_charset gb2312
register_globals On
安裝至此完成,可以使用如下命令啟動(dòng)apache:
server# /usr/local/apache/bin/apachectl start
如果要使用https則使用如下命令啟動(dòng):
server# /usr/local/apache/bin/apachectl startssl
(鍵入證書密碼,然后回車)
編寫apache的自啟動(dòng)文件(Freebsd下):
server# edit /usr/local/etc/rc.d/apache-server.sh 示例:apache-server.sh
server# chmod 755 apache-server.sh
重新啟動(dòng)服務(wù)器,Bind9就可以自動(dòng)運(yùn)行!
測試服務(wù)器:
在/usr/local/apache/htdocs下編輯test.php來測試php是否安裝成功 示例:test.php
<?
phpinfo()
?>
在瀏覽器中的地址欄中輸入:http://www.nankai.edu.cn/test.php即可以顯示php的安裝信息。
如果你使用的是apachects startssl命令啟動(dòng)的話,你可以
在瀏覽器中的地址欄中輸入:https://www.nankai.edu.cn/test.php來顯示php的安裝信息。
虛擬服務(wù)器的設(shè)置(摘自化境編程界Apache Server設(shè)置虛擬WEB 作者:不詳)原文,未加修改:
一 、IP型虛擬主機(jī)
IP型虛擬主機(jī)指每一虛擬主機(jī)對(duì)應(yīng)唯一的IP。可通過多個(gè)物理網(wǎng)卡或虛擬網(wǎng)口實(shí)現(xiàn)多IP,Solaris2.5和Windows NT都支持這種方式。
兩種配置多虛擬主機(jī)的方法:
1、為每一虛擬主機(jī)啟動(dòng)一個(gè)httpd進(jìn)程。
下列情況下使用此方法:
1) 需考慮安全隔離問題,如兩個(gè)httpd運(yùn)行于不同的User、Group、Listen、ServerRoot,兩者用戶除通過Web相互瀏覽數(shù)據(jù),無法訪問其他數(shù)據(jù)。
2) 能提供足夠內(nèi)存和文件描述器。
設(shè)置方法:
為每一虛擬主機(jī)建立一個(gè)獨(dú)立的httpd安裝,在每一個(gè)安裝路徑的配置文件httpd.conf里,用Listen指令指定進(jìn)程服務(wù)的IP,如:Listen 10.68.37.10:80
2、為所有虛擬主機(jī)啟動(dòng)一個(gè)httpd進(jìn)程。
下列情況下使用此方法:
1) 允許在虛擬主機(jī)間共享httpd配置。
2) 計(jì)算機(jī)服務(wù)于大量的請(qǐng)求,運(yùn)行多個(gè)進(jìn)程使服務(wù)器性能降低成為重要考慮因素。
設(shè)置方法:
在配置文件httpd.conf里,用VirtualHost指令為每一虛擬主機(jī)設(shè)置ServerAdmin、ServerName、DocumentRoot、 ErrorLog、TransferLog或CustomLog,如:
<VirtualHost www.smallco.com> #此處建議用IP
ServerAdmin webmaster@mail.smallco.com
DocumentRoot /usr/local/etc/httpd/htdocs/smallco
ServerName www.smallco.com #建議此處用域名
ErrorLog /usr/local/etc/httpd/logs/smallco/error_log
TransferLog /usr/local/etc/httpd/logs/smallco/access_log
</VirtualHost>
<VirtualHost www.baygroup.org> #此處建議用IP
ServerAdmin webmaster@mail.baygroup.org
DocumentRoot /groups/baygroup/www
ServerName www.baygroup.org #建議此處用域名
ErrorLog /groups/baygroup/logs/error_log
TransferLog /groups/baygroup/logs/access_log
</VirtualHost>
同時(shí)要做虛擬網(wǎng)口或網(wǎng)卡的配置,在DNS也要做相應(yīng)設(shè)置。
二 、名字型虛擬主機(jī)(Apache1.3以上版本支持)
IP型虛擬主機(jī)雖好,但不是最佳方案。它要求每一虛擬主機(jī)有一專用 IP,在某些機(jī)器上難于實(shí)現(xiàn)。名字型虛擬主機(jī)是指每一虛擬主機(jī)的名字不相同,但I(xiàn)P一樣。它的好處是不限制虛擬主機(jī)數(shù)量,配置、使用簡單,不需另外的軟硬件。缺點(diǎn)是客戶端必須支持該部分協(xié)議,最近版本的瀏覽器都支持,某些老版本瀏覽器不支持。但Apache為此提供了解決方法。
設(shè)置方法:
在配置文件httpd.conf里,用NameVirtualHost指令設(shè)置虛擬主機(jī),如:
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44> #建議此處用IP
ServerName www.domain.tld #建議此處用域名
DocumentRoot /web/domain
</VirtualHost>
同時(shí),在DNS定義www.domain.tld指向111.22.33.44。
注意:當(dāng)在NameVirtualHost指令后使用IP時(shí),任何使用IP的URL請(qǐng)求都是針對(duì)虛擬主機(jī)的,主服務(wù)器從不會(huì)響應(yīng)一個(gè)使用IP的URL 請(qǐng)求。另外,有些服務(wù)器希望以多個(gè)名字被訪問。例如,假設(shè)有某一IP的服務(wù)器,希望以名domain.tld、www2.domain.tld都能被訪問,做法是在VirtualHost指令部分使用ServerAlias指令。如:ServerAlias domain.tld *.domain.tld
另附一些虛擬主機(jī)的設(shè)置實(shí)例。
附:虛擬主機(jī)設(shè)置實(shí)例
IP型的虛擬主機(jī)配置
Setup 1: 服務(wù)器有兩個(gè)IP,
111.22.33.44 server.domain.tld
111.22.33.55 www.otherdomain.tld
www.domain.tld 是server.domain.tld 的別名(CNAME),代表主服務(wù)器。
服務(wù)器配置:
...
Port 80
DocumentRoot /www/domain
ServerName www.domain.tld
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
Setup 2: 基本同Setup1,但不設(shè)置專門的主服務(wù)器。
服務(wù)器配置:
...
Port 80
ServerName server.domain.tld
<VirtualHost 111.22.33.44>
DocumentRoot /www/domain
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
這種設(shè)置只有當(dāng)URL為http://server.domain.tld時(shí)才擊中主服務(wù)器
Setup 3: 服務(wù)器有兩個(gè)IP,
111.22.33.44 server.domain.tld
111.22.33.55 www-cache.domain.tld
www.domain.tld 是server.domain.tld 的別名(CNAME),代表主服務(wù)器。
www-cache.domain.tld是proxy-cache,端口是8080,Web服務(wù)器使用默認(rèn)的80。
服務(wù)器配置:
...
Port 80
Listen 111.22.33.44:80
Listen 111.22.33.55:8080
ServerName server.domain.tld
<VirtualHost 111.22.33.44:80>
DocumentRoot /www/domain
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55:8080>
ServerName www-cache.domain.tld
...
<Directory proxy:>
order deny,allow
deny from all
allow from 111.22.33
</Directory>
</VirtualHost>
名字型虛擬主機(jī)配置
Setup 1: 服務(wù)器有一個(gè)IP,
111.22.33.44 server.domain.tld.
www.domain.tld和www.sub.domain.tld是別名(CNAMEs) 。
服務(wù)器配置:
...
Port 80
ServerName server.domain.tld
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
DocumentRoot /www/domain
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain
ServerName www.sub.domain.tld
...
</VirtualHost>
若使用IP訪問服務(wù)器,由于 www.domain.tld 有最高優(yōu)先級(jí),被認(rèn)為是默認(rèn)服務(wù)器或
第一服務(wù)器。
Setup 2:服務(wù)器有兩個(gè)IP,
111.22.33.44 server1.domain.tld 用于主服務(wù)器
111.22.33.55 server2.domain.tld 用于虛擬主機(jī)
別名www.domain.tld用于主服務(wù)器,
別名www.otherdomain.tld用于一個(gè)虛擬主機(jī),
別名www.sub.domain.tld,*.sub.domain.tld 用于另一虛擬主機(jī),
服務(wù)器配置:
...
Port 80
ServerName www.domain.tld
DocumentRoot /www/domain
NameVirtualHost 111.22.33.55
...
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName www.sub.domain.tld
ServerAlias *.sub.domain.tld
...
</VirtualHost>
混合型(IP/名字)虛擬主機(jī)配置
Setup:服務(wù)器有三個(gè)IP,
111.22.33.44 server.domain.tld 用于名字型虛擬主機(jī)
111.22.33.55 www.otherdomain1.tld 用于IP型虛擬主機(jī)
111.22.33.66 www.otherdomain2.tld 用于IP型虛擬主機(jī)
服務(wù)器配置:
...
Port 80
ServerName server.domain.tld
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
DocumentRoot /www/domain
ServerName www.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain1
ServerName www.sub1.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain2
ServerName www.sub2.domain.tld
...
</VirtualHost>
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain1
ServerName www.otherdomain1.tld
...
</VirtualHost>
<VirtualHost 111.22.33.66>
DocumentRoot /www/otherdomain2
ServerName www.otherdomain2.tld
...
</VirtualHost>
端口型虛擬主機(jī)配置
Setup: 服務(wù)器有一個(gè)IP,
111.22.33.44 www.domain.tld
不需要另外的別名或IP,采用端口型虛擬主機(jī)即可設(shè)置一個(gè)配置有別于主服務(wù)器的虛
擬主機(jī)。
服務(wù)器配置:
...
Listen 80
Listen 8080
ServerName www.domain.tld
DocumentRoot /www/domain
<VirtualHost 111.22.33.44:8080>
DocumentRoot /www/domain2
...
</VirtualHost>