超級服務器apache+mysql+php+ssl之完全安裝攻略(2)
2024-08-27 18:28:40
供稿:網友
php安裝(unix)
現在安裝php語言。你下載了最新的beta版,但是你可能必須下載非beta版本。記住beta版本需要gnu make。
你仍然假定是root,如果不是,su回到root。
php要求你已經預先配置好的apache,以便它能知道所需的東西在哪兒。在以后你安裝apache服務器時,你將會回到這里。改變到你有源代碼的目錄。
# cd /tmp/download
# gunzip -c apache_1.3.x.tar.gz | tar xf -
# cd apache_1.3.x
# ./configure
# cd ..
好的,現在你可以開始php的安裝。提取源代碼文件并進入其目錄。如果你下載了版本3,在數字和命令上有一個改變,不大的改變。
# gunzip -c php-4.0.x.tar.gz | tar xf -
# cd php-4.0.x
如果你正在編譯代碼,configure將永遠是你的朋友。:-) 因此,configure有很多選項。使用configure --help確定你想要增加哪些。我只是需要mysql和ldap,并且當然apache。
# ./configure --with-mysql=/usr/local/mysql
--with-xml
--with-apache=../apache_1.3.x
--enable-track-vars
--with-ldap
make并安裝二進制代碼。
# make
# make install
拷貝ini文件到lib目錄。
# cp php.ini-dist /usr/local/lib/php.ini
你可以編輯php文件來設置php選項,如你可以通過在你的php.ini文件中插入下列行,增加php的max_execution_time。
max_execution_time = 60;
注意:php3用戶將使用php3.ini,而php4用戶將使用php.ini文件。
apache 與 mod_ssl
該配置并安裝mod_ssl和apache了。對此,你將需要有rsaref-2.0文件。在http://ftpsearch.lycos.com/上搜索“rsaref20.tar.z”。如果你不喜歡lycos,你可以選擇其他搜索引擎來搜索文件。當然只有你在美國才需要這個文件。(管它呢,你也可從別處下載,首先在http://ftpsearch.ntnu.no/查找“rsaref20.tar.z”,好多啊!。)
創建rasref目錄,你將在該目錄提取文件。注意。這假定你下載了一個臨時目錄,而你就在此目錄。
# mkdir rsaref-2.0
# cd rsaref-2.0
# gzip -d -c ../rsaref20.tar.z | tar xvf -
現在配置并構造openssl庫。
# cd rsaref-2.0
# cp -rp install/unix local
# cd local
# make
# mv rsaref.a librsaref.a
# cd ../..
安裝openssl。記住,你將用它來創建臨時證書和csr文件。--prefix選項指定主安裝目錄。
# cd openssl-0.9.x
# ./config -prefix=/usr/local/ssl
-l`pwd`/../rsaref-2.0/local/ rsaref -fpic
現在make、測試并安裝它。
# make
# make test
# make install
# cd ..
我們將配置mod_ssl模塊,然后用apache配置指定它為一個可裝載的模塊。
# cd mod_ssl-2.5.x-1.3.x
# ./configure
--with-apache=../apache_1.3.x
# cd ..
現在我們可以把更多的apache模塊加到apache源代碼樹中。可選的--enable-shared=ssl選項使得mod_ssl構造成為一個dso“libssl.so”。關于在apache支持dso的更多信息,閱讀apache源代碼樹中的install和 htdocs/manual/dso.html文檔。我強烈建議isp和軟件包裝維護者為了最靈活地使用mod_ssl而使用dso工具,但是注意,dso不是在所有平臺上的apache都支持。
# cd apache_1.3.x
# ssl_base=../openssl-0.9.x
rsa_base=../rsaref-2.0/local
./configure --enable-module=ssl
--activate-module=src/modules/php4/libphp4.a
--enable-module=php4 --prefix=/usr/local/apache
--enable-shared=ssl
[...你可加入更多的選項...]
生成apache,然后生成證書,并安裝...
# make
如果你已正確地完成,你將得到類似于以下的信息:
+-----------------------------------------------------------------------+
| before you install the package you now should prepare the ssl |
| certificate system by running the "make certificate" command. |
| for different situations the following variants are provided: |
| |
| % make certificate type=dummy (dummy self-signed snake oil cert) |
| % make certificate type=test (test cert signed by snake oil ca) |
| % make certificate type=custom (custom cert signed by own ca) |
| % make certificate type=existing (existing cert) |
| crt=/path/to/your.crt [key=/path/to/your.key] |
| |
| use type=dummy when you’re a vendor package maintainer, |
| the type=test when you’re an admin but want to do tests only, |
| the type=custom when you’re an admin willing to run a real server |
| and type=existing when you’re an admin who upgrades a server. |
| (the default is type=test) |
| |
| additionally add algo=rsa (default) or algo=dsa to select |
| the signature algorithm used for the generated certificate. |
| |
| use "make certificate view=1" to display the generated data. |
| |
| thanks for using apache & mod_ssl. ralf s. engelschall |
| [email protected] |
| www.engelschall.com |
+-----------------------------------------------------------------------+
現在你可以創建一個定制的證書。該選項將提示輸入你的地址、公司、和其他一些東西。關于證書,請參閱本文的結尾。
# make certificate type=custom
現在安裝apache...
# make install
如果一切正常,你應該看到類似于以下的信息:
+----------------------------------------------------------------------------------+
| you now have successfully built and installed the |
| apache 1.3 http server. to verify that apache actually |
| works correctly you now should first check the |
| (initially created or preserved) configuration files |
| |
| /usr/local/apache/conf/httpd.conf |
| and then you should be able to immediately fire up |
| apache the first time by running: |
| |
| /usr/local/apache/bin/apachectl start |
| or when you want to run it with ssl enabled use: |
| |
| /usr/local/apache/bin/apachectl startssl |
| thanks for using apache. the apache group |
| http://www.apache.org / |
+----------------------------------------------------------------------------------+
現在驗證apache和php是否正在工作。然而,我們需要編輯srm.conf和httpd.conf保證我們把php類型加到了配置中。查看httpd.conf并去掉下列行的注釋。如果你精確地遵循了本文的指令,你的httpd.conf文件將位于/usr/local/apache/conf目錄。文件有一行針對php4的addtype加了注釋,現在就去掉注釋。httpd.conf 文件--片斷
>
> # and for php 4.x, use:
> #
---> addtype application/x-httpd-php .php
---> addtype application/x-httpd-php-source .phps
>
>
現在我們準備啟動apache服務器看它是否在工作。首先我們將啟動不支持ssl的服務器看它是否啟動了。我們將檢查對php的支持,然后我們將停止服務器并且啟動啟用了ssl支持的服務器并檢查我們是否一切正常。configtest 將檢查所有配置是否正確設置。
# cd /usr/local/apache/bin
# ./apachectl configtest
syntax ok
# ./apachectl start
./apachectl start: httpd started
測試我們的工作
apache 正在工作嗎?
如果它工作正常,當你用netscape連接服務器時,你將看見一幅類似于這幅屏幕捕獲的屏幕。這是基本上 是apache缺省安裝的頁面。
注意:你可以用域名或機器實際的ip地址與服務器連接。檢查這兩種情形,確保一切工作正常。
php支持正在工作嗎??
現在將測試php支持……創建一個文件(名為:test.php ),它有下列信息。文件需要位于文檔根路徑下,它應該缺省設置為/usr/local/apache/htdocs。注意這依賴于我們以前選擇的前綴,然而,這可在 httpd.conf中改變。設置多個虛擬主機將在另一篇文章加少,請留意,因為它將涉及安裝apache和它的指令的一些很基本的選項。
test.php 文件
<?
phpinfo();
?>
它將顯示有關服務器、php和環境的信息。下面是輸出頁面的頂部的屏幕抓取。
很酷吧,php起作用了。
ssl 選擇正在工作嗎??
好了,現在我們準備測試ssl了。首先停止服務器,并以啟用ssl的選項重啟它。
# /usr/local/apache/bin/apachectl stop
# /usr/local/apache/bin/apachectl startssl
測試它是否工作:通過用一個netscape與服務器連接并且選擇https協議,即:https://youserver.yourdomain.com 或 http://yoursever.yourdomain.com:443 ,也可以再試一下你的服務器的 ip地址,即:https://xxx.xxx.xxx.xxx和 http://xxx.xxx.xxx.xxx:443 。
如果它起作用了,服務器將把證書發送到瀏覽器以建立一個安全連接。這將讓瀏覽器提示你接受自己簽署的證書。,如果它是來自verisign或thawte的一張證書,那么瀏覽器將不提示你,因為證書來自一個可信的證書授權機構(ca)。在我們的情況中,我們創建并簽署我們自己的證書……我們不想馬上買一個。首先,我們想要保證我們能使一切正常。
你在netscape中將看見啟用了下列選項。這就告訴你一個安全的連接已經建立起來了。
php和mysql能一起工作嗎??
現在,我們可以確定php能與mysql一起工作,通過創建一個簡單的腳本,對“test2”數據庫做一些插入和數據刪除操作。只是一個簡單的腳本以測試它是否工作了。在另一篇文章中我們將討論php腳本連接一個 mysql數據庫。還記得我們已經創建立了數據庫和一張表。我們可以現在完成它,但是我選擇不。我想要再檢查一次root有權限創建立數據庫和表,然而,php提供了對mysql的提供,因此我能很容易地編寫代碼以創建一個測試數據庫和若干條記錄。
記得我們以前創建了書籍數據庫。如果你跳過了以前的內容,這部分將不工作。我們創建了有一個“books”表的test2數據庫,并且為一本書插入了一條記錄。
這個腳本基本上瀏覽該表并列出所有字段名,它的確很簡單。
<?
$dbuser = "root";
$dbhost = "localhost";
$dbpass = "password";
$dbname = "test2";
$dbtble = "books";
$mysql_link = mysql_connect($dbhost,$dbuser,$dbpass);
$column = mysql_list_fields($dbname,$dbtble,$mysql_link);
for($i=0; $i< mysql_num_fields($column); $i++ )
{
print mysql_field_name($column,$i )."<br>";
}
?>
一個更復雜的例子將向你演示php某些絕妙的功能。
<html>
<head>
<title>example 2 -- more details</title>
</head>
<body bgcolor="white">
<?
$dbuser = "root";
$dbhost = "localhost";
$dbpass = "password";
$dbname = "test2";
$dbtable = "books";
//------ database connection --------//
$mysql_link = mysql_connect($dbhost,$dbuser,$dbpass);
$column = mysql_list_fields($dbname,$dbtable,$mysql_link);
$sql = "select * from $dbtable";
$result = mysql_db_query($dbname,$sql);
?>
<table bgcolor="black">
<tr><td>
<table>
</td></tr>
</table>
</body>
</html>
注意,我們竟能在同一文件中同時有html和php命令。這就是php腳本的奇妙之處。
虛擬主機的設置
現在是設置apache處理一些虛擬主機的時間了。由于apache提供的靈活性,虛擬主機可很簡單地做到。首先你需要一個dns服務器把虛擬主機的域名指向web服務器的ip地址。在dns使用一個cname記錄把 your_virtual_domain.com指向服務器的ip。其次你需要修改apache的配置文件httpd.conf以增加新的虛擬域名。記住,這只是一個很基本的例子,你有勇氣讀一下apache的指令。
讓我們看一個 httpd.conf 的例子。
httpd.conf 片斷
#--------------------------------------------------------#
# virtual host section non-ssl
#--------------------------------------------------------#
# virtualhost directive allows you to specify another virtual
# domain on your server. most apache options can be specified
# within this section.
# mail to this address on errors
serveradmin [email protected]
# where documents are kept in the virtual domain
# this is an absolute path. so you may want to put
# in a location where the owner can get to it.
documentroot /home/vhosts/domain1.com/www/
# since we will use php to create basically
# all our file we put a directive to the index file.
directoryindex index.php
# name of the server
servername www.domain1.com
# log files relative to serverroot option
errorlog logs/domain1.com-error_log
transferlog logs/domain1.com-access_log
refererlog logs/domain1.com-referer_log
agentlog logs/domain1.com-agent_log
# use cgi scripts in this domain. in the next case you
# can see that it does not have cgi scripts. please
# read up on the security issues relating to cgi-scripting.
scriptalias /cgi-bin/ /var/www/cgi-bin/domain1.com/
addhandler cgi-script .cgi
addhandler cgi-script .pl
# this is another domain. note that you could host
# multiple domains this way...
# mail to this address on errors
serveradmin [email protected]
# where documents are kept in the virtual domain
documentroot /virtual/domain2.com/www/html
# name of the server
servername www.domain2.com
# log files relative to serverroot option
errorlog logs/domain2.com-error_log
transferlog logs/domain2.com-access_log
refererlog logs/domain2.com-referer_log
agentlog logs/domain2.com-agent_log
# no cgi’s for this host
# end: virtual host section
使用上述例子在你的服務器上創建你自己的虛擬主機。如果你想從apache網站上閱讀每一條指令,它的網址是:http://www.apache.org。
ssl虛擬主機
創建ssl虛擬主機類似非ssl。除了你需要指定另外的指令,還有,你需要增加一個dns記錄并且修改 httpd.conf。這里有一個例子。
#--------------------------------------------#
# ssl virtual host context
#--------------------------------------------#
# general setup for the virtual host
documentroot /usr/local/apache/htdocs
serveradmin [email protected]
servername www.securedomain1.com
errorloglogs/domain1.com-error_log
transferlog logs/domain1.com-transfer_log
# ssl engine switch:
# enable/disable ssl for this virtual host.
sslengine on
# server certificate:
# point sslcertificatefile at a pem encoded certificate. if
# the certificate is encrypted, then you will be prompted for a
# pass phrase. note that a kill -hup will prompt again. a test
# certificate can be generated with `make certificate’ under
# built time. keep in mind that if you’ve both a rsa and a dsa
# certificate you can configure both in parallel (to also allow
# the use of dsa ciphers, etc.)
# note that i keep my certificate files located in a central
# location. you could change this if you are an isp, or asp.
sslcertificatefile /usr/local/apache/conf/ssl.crt/server.crt
# server private key:
# if the key is not combined with the certificate, use this
# directive to point at the key file. keep in mind that if
# you’ve both a rsa and a dsa private key you can configure
# both in parallel (to also allow the use of dsa ciphers, etc.)
sslcertificatekeyfile /usr/local/apache/conf/ssl.key/server.key
# per-server logging:
# the home of a custom ssl log file. use this when you want a
# compact non-error ssl logfile on a virtual host basis.
customlog /usr/local/apache/logs/ssl_request_log
"%t %h %{ssl_protocol}x %{ssl_cipher}x "%r" %b"
記住你有很多指令可以指定。我們將在另一篇有關配置apache的文章中討論,本文只是一個入門性指南。
生成證書
這是如何生成證書的按部就班的描述。
為你的apache服務器創建一個rsa私用密鑰(被triple-des加密并且進行pem格式化):
# openssl genrsa -des3 -out server.key 1024
請在安全的地方備份這個server.key文件。記住你輸入的通行短語(pass phrase)!你可以通過下面的命令看到這個rsa私用密鑰的細節。
# openssl rsa -noout -text -in server.key
而且你可以為這個rsa私用密鑰創建一個加密的pem版本(不推薦),通過下列命令:
# openssl rsa -in server.key -out server.key.unsecure
用服務器rsa私用密鑰生成一個證書簽署請求(csr-certificate signing request)(輸出將是pem格式的):
# openssl req -new -key server.key -out server.csr
當openssl提示你“commonname”時,確保你輸入了服務器的fqdn("fully qualified domain name") ,即,當你為一個以后用https://www.foo.dom/訪問的網站生成一個csr時,這里輸入"www.foo.dom"。你可借助下列命令查看該csr的細節:
# openssl req -noout -text -in server.csr
將csr發到一個ca
現在你必須發送該csr到一個ca以便簽署,然后的結果才是可以用于apache的一個真正的證書。
有兩種選擇:
第一種,你可以通過一個商業性ca如verisign 或 thawte簽署證書。那么你通常要將csr貼入一個web表格,支付簽署費用并等待簽署的證書,然后你可以把它存在一個server.crt文件中。關于商業性ca的更多信息,請參見下列鏈接:
verisign - http://digitalid.verisign.com/server/apachenotice.htm
thawte consulting - http://www.thawte.com/certs/server/request.html
certisign certificadora digital ltda. - http://www.certisign.com.br
iks gmbh - http://www.iks-jena.de/produkte/ca /
uptime commerce ltd. - http://www.uptimecommerce.com
belsign nv/sa - http://www.belsign.be
你自己的ca
第二種,你可以利用自己的ca并由該ca簽署csr。你可以創建自己的認證中心來簽署證書。最簡單的方法是利用openssl提供的ca.sh或 ca.pl腳本。比較復雜而且是手工的方法是:
為你的ca創建一個rsa私用密鑰( 被triple-des加密并且進行pem格式化的):
# openssl genrsa -des3 -out ca.key 1024
請在安全的地方備份這個ca.key文件。記住你輸入的通行短語(pass phrase)!你可以通過下面的命令看到這個rsa私用密鑰的細節。
# openssl rsa -noout -text -in ca.key
而且你可以為這個rsa私用密鑰創建一個加密的pem版本(不推薦),通過下列命令:
# openssl rsa -in ca.key -out ca.key.unsecure
利用ca的rsa密鑰創建一個自簽署的ca證書(x509結構)(輸出將是pen格式的):
# openssl req -new -x509 -days 365 -key ca.key -out ca.crt
你可以通過下列命令查看該證書的細節:
# openssl x509 -noout -text -in ca.crt
準備一個簽署所需的腳本,因為"openssl ca"命令有一些奇怪的要求而且缺省的openssl配置不允許簡單地直接使用"openssl ca"命令,所以一個名為sign.sh的腳本隨mod_ssl分發一道發布(子目錄pkg.contrib/)。 使用該腳本進行簽署。
現在你可以使這個ca簽署服務器的csr,以便創建用于apache服務器內部的真正的ssl證書(假定你手頭已經有一個server.csr):
# ./sign.sh server.csr
它簽署服務器的csr并且結果在一個server.crt文件中。
現在你有兩個文件:server.ket和server.crt。在你的apache的httpd.conf文件中,如下使用它們:
sslcertificatefile /path/to/this/server.crt
sslcertificatekeyfile /path/to/this/server.key
server.csr不再需要了。
注冊會員,創建你的web開發資料庫,