目的我們的目標(biāo)是安裝一個(gè)允許我們托管多個(gè)網(wǎng)站的web服務(wù)器 為了這個(gè)任務(wù)所需的工具是: Apache-一個(gè)網(wǎng)站服務(wù)器 Mod_SSL-一個(gè)安全套接字層(SSL)的模塊 OpenSSL-開(kāi)放源代碼工具箱(mod_ssl所需) RSARef-僅對(duì)美國(guó)用戶 MySQL-一個(gè)數(shù)據(jù)庫(kù)服務(wù)器 PHP-一種腳本語(yǔ)言 “條條大路通羅馬”……因此這只是很多能達(dá)到我們要求的配置之一 希望你將在結(jié)束這個(gè)簡(jiǎn)單的指南后能成功地完成下列目標(biāo)。 安裝并設(shè)置MySQL數(shù)據(jù)庫(kù)服務(wù)器 o 知道怎樣檢查MySQL服務(wù)器的狀態(tài) o 知道怎樣使用命令行客戶程序存取MySQL服務(wù)器 o 知道怎樣從web存取你的DB服務(wù)器 安裝并設(shè)置具備SSL的Apache網(wǎng)站服務(wù)器 o 配置一個(gè)簡(jiǎn)單的虛擬網(wǎng)站 o 知道怎樣停止并啟動(dòng)服務(wù)器 o 知道怎樣做一些基本的主機(jī)托管配置 安裝并配置服務(wù)器端腳本的PHP 4.0超文本預(yù)處理器 o 知道怎樣編寫(xiě)簡(jiǎn)單的php代碼 o 知道怎樣使用php連接一個(gè)DB o 創(chuàng)建一個(gè)啟用PHP地簡(jiǎn)單網(wǎng)站與一個(gè)數(shù)據(jù)庫(kù)溝通 創(chuàng)造一些樣本證書(shū)用于Apache SSL o 知道怎樣產(chǎn)生一個(gè)CSR文件 o 知道怎樣加密一個(gè)鍵碼 o 知道怎樣 簽署你自己的證書(shū) 本文將覆蓋大量的信息。本指南作為一個(gè)入門(mén)性地的指南 本文絕非是一個(gè)詳細(xì)全面的文檔,它當(dāng)然將有一些錯(cuò)誤(希望最小) 假設(shè) 本文假設(shè)你已經(jīng)把下列軟件安裝在你的系統(tǒng)上了。 Perl (最好是ver 5+) gzip或gunzip gcc 和 GNU make 如果你沒(méi)有安裝好這些,你將需要采取必要的步驟在解釋本文的任何 你也需要對(duì)UNIX命令、HTML、和SQL的一個(gè)基本了解 工作原理 理解在幕后發(fā)生了什么是有幫助的。這里是一個(gè)過(guò)分簡(jiǎn)化的工作原理 情況是:我們有一個(gè)從一個(gè)數(shù)據(jù)庫(kù)取出一些數(shù)據(jù)的網(wǎng)頁(yè)。John Doe從他的瀏覽器請(qǐng)求該頁(yè),請(qǐng)求被發(fā)送給web服務(wù)器 讓我們一步一步地看: John Doe 從他的瀏覽器中點(diǎn)擊一個(gè)鏈接;他的瀏覽器發(fā)送對(duì)http:/ Apache得到對(duì)test.php的請(qǐng)求,它知道.php文件應(yīng)由PHP預(yù)處理器(mod_php)處理 test.php是包含命令的一個(gè)PHP腳本。這些命令之一是打開(kāi)一個(gè)到一個(gè)數(shù)據(jù)庫(kù)的連接并抓取數(shù)據(jù)。PHP 處理到數(shù)據(jù)庫(kù)的連接,并且解釋SQL調(diào)用從DB中提取數(shù)據(jù)。 服務(wù)器服務(wù)器得到從PHP解釋器來(lái)的連接請(qǐng)求,并且處理這個(gè)請(qǐng)求 數(shù)據(jù)庫(kù)然后將應(yīng)答和結(jié)果回送到PHP解釋器。 Apache回送該結(jié)果到John Doe的瀏覽器,作為對(duì)他請(qǐng)求的應(yīng)答。John Doe現(xiàn)在看見(jiàn)一個(gè)包含從一個(gè)數(shù)據(jù)庫(kù)來(lái)的一些信息的網(wǎng)頁(yè)。 如果這是一個(gè)對(duì)https://www.yoursecures 服務(wù)器看到請(qǐng)求,解密并且認(rèn)證它。它處理文件,加密并且發(fā)送它 再說(shuō)一次,它不是100%的正確,但是它足夠快地讓你知道幕后發(fā) 既然我們對(duì)我們正在試圖達(dá)到的目標(biāo)有了一個(gè)很基本的了解 準(zhǔn)備 Apache (Web服務(wù)器)-http://www. Mod_SSL (安全服務(wù)器層)-http://www. OpenSSL (SSL工具箱)-http://www. PHP (腳本語(yǔ)言)-http://www. MySQL (SQL數(shù)據(jù)庫(kù)服務(wù)器 )-http://www. 下載所有(tar文件)源代碼到一個(gè)臨時(shí)目錄下。保證你把他們放 我們的計(jì)劃 我們的計(jì)劃是首先安裝MySQL服務(wù)器并保證它工作 MySQL源代碼安裝(UNIX) 你必須用來(lái)執(zhí)行安裝MySQL源代碼分發(fā)的基本命令是 通過(guò)使用su成為 root用戶。 $su 直接進(jìn)入你有tar文件的目錄。(使用一個(gè)臨時(shí)目錄。這里使用 /tmp/download/ ) #cd /tmp/download/ 使用下列命令提取文件。 # gunzip -d -c mysql-3.22.xx.tar.gz | tar xvf - 改變到新目錄,它在提取期間創(chuàng)建。 # cd mysql-3.22.xx 現(xiàn)在你可以開(kāi)始“配置”MySQL服務(wù)器。你可以用config # configure --prefix=/usr/local/mysq 在你完成了配置以后,你可以執(zhí)行下列命令make真正的二進(jìn)制代 # make 現(xiàn)在你已準(zhǔn)備好安裝所有的二進(jìn)制代碼。運(yùn)行下列命令在你用con # make install 在你安裝好二進(jìn)制代碼后,現(xiàn)在是創(chuàng)建用于定義權(quán)限的mysql表 # scripts/mysql_install_db # cd /usr/local/mysql/bin # ./safe_mysqld & # ./mysqladmin -u root password "new-password" 注意:/usr/local/mysql是我選擇安裝MySQL 你可以通過(guò)運(yùn)行一些簡(jiǎn)單的測(cè)試來(lái)驗(yàn)證服務(wù)器正在工作以確保MyS # BINDIR/mysqlshow -p +---------------+ | Databases | +---------------+ | mysql | +---------------+ 一旦你安裝好MySQL,它將自動(dòng)地創(chuàng)建2個(gè)數(shù)據(jù)庫(kù) # mysql -u root -p mysql> show databases; +----------------+ | Database | +----------------+ | mysql | | test | +----------------+ mysql> create database test2; Query OK, 1 row affected (0.00 sec) 現(xiàn)在選擇新的數(shù)據(jù)庫(kù)使用,并創(chuàng)建一個(gè)名為tst_tbl的新表, 有下列2個(gè)字段。字段1是是一個(gè)id字段,允許你知道記錄的id mysql> use test2; Database changed mysql> CREATE TABLE books ( id int(3) not null -> auto_increment, name char(50) not null, -> unique(id), primary key(id)); Query OK, 0 rows affected (0.00 sec) 現(xiàn)在我們用下列命令驗(yàn)證一切正確無(wú)誤。 mysql> show tables +---------------------+ | Tables in test2 | +---------------------+ | books | +---------------------+ 1 row in set (0.00 sec) mysql> describe books; +-------+-------------+------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+ | id | int(3) | | PRI | 0 | auto_increment | | name | char(50) | | | | | +-------+-------------+------+ 2 rows in set (0.00 sec) 注意到describe命令基本上“描述”了表的布局 好,該試一些確實(shí)有用的SQL命令,插入并從數(shù)據(jù)庫(kù)中選擇數(shù)據(jù) mysql> INSERT INTO books (name) values("PHP 4 Newbies"); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO books (name) values("Red Hat Linux 6 Server"); Query OK, 1 row affected (0.00 sec) 現(xiàn)在我們可以檢查新紀(jì)錄,發(fā)出一條“選擇所有”命令 mysql> SELECT * from books; +----+------------------------ | id | name | +----+------------------------ | 1 | PHP for Newbies | | 2 | Red Hat Linux 6 Server | +----+------------------------ 2 rows in set (0.00 sec) 很好,MySQL服務(wù)器完全起作用了。我們可以繼續(xù)加入 讓我演示一下如何做一個(gè)快速刪除。這只是讓你知道,記住 mysql> delete from books where id=1; Query OK, 1 row affected (0.00 sec) mysql> select * from books; +----+------------------------ | id | name | +----+------------------------ | 2 | Red Hat Linux 6 Server | +----+------------------------ 1 row in set (0.00 sec) 好了,退出MySQL,繼續(xù)安裝。你可在完成所有安裝并且一切工 PHP安裝(UNIX) 現(xiàn)在安裝PHP語(yǔ)言。你下載了最新的beta版,但是你可能必須 你仍然假定是root,如果不是,su回到root。 PHP要求你已經(jīng)預(yù)先配置好的Apache,以便它能知道所需的 # cd /tmp/DOWNLOAD # gunzip -c apache_1.3.x.tar.gz | tar xf - # cd apache_1.3.x # ./configure # cd .. 好的,現(xiàn)在你可以開(kāi)始PHP的安裝。提取源代碼文件并進(jìn)入其目錄 # gunzip -c php-4.0.x.tar.gz | tar xf - # cd php-4.0.x 如果你正在編譯代碼,configure將永遠(yuǎn)是你的朋友。:- # ./configure --with-mysql=/usr/local/mysql --with-xml --with-apache=../apache_1.3.x --enable-track-vars --with-ldap make并安裝二進(jìn)制代碼。 # make # make install 拷貝ini文件到lib目錄。 # cp php.ini-dist /usr/local/lib/php.ini 你可以編輯PHP文件來(lái)設(shè)置PHP選項(xiàng),如你可以通過(guò)在你的ph max_execution_time = 60; 注意:php3用戶將使用php3.ini,而php4用戶將使 Apache 與 Mod_SSL 該配置并安裝mod_ssl和Apache了。對(duì)此 創(chuàng)建rasref目錄,你將在該目錄提取文件。注意 # mkdir rsaref-2.0 # cd rsaref-2.0 # gzip -d -c ../rsaref20.tar.Z | tar xvf - 現(xiàn)在配置并構(gòu)造OpenSSL庫(kù)。 # cd rsaref-2.0 # cp -rp install/unix local # cd local # make # mv rsaref.a librsaref.a # cd ../.. 安裝OpenSSL。記住,你將用它來(lái)創(chuàng)建臨時(shí)證書(shū)和CSR文件 # cd openssl-0.9.x # ./config -prefix=/usr/local/ssl -L`pwd`/../rsaref-2.0/local/ rsaref -fPIC 現(xiàn)在make、測(cè)試并安裝它。 # 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 .. 現(xiàn)在我們可以把更多的Apache模塊加到Apache源代碼樹(shù) # 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 --enable-module=php4 --prefix=/usr/local/apache --enable-shared=ssl [...你可加入更多的選項(xiàng)...] 生成Apache,然后生成證書(shū),并安裝... # 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 | | rse@ | | www. | +----------------------------- 現(xiàn)在你可以創(chuàng)建一個(gè)定制的證書(shū)。該選項(xiàng)將提示輸入你的地址、公司 # make certificate TYPE=custom 現(xiàn)在安裝Apache... # make install 如果一切正常,你應(yīng)該看到類似于以下的信息: +----------------------------- | 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 | and then you should be able to immediately fire up | | Apache the first time by running: | | | | /usr/local/apache/bin/apachect | Or when you want to run it with SSL enabled use: | | | | /usr/local/apache/bin/apachect | Thanks for using Apache. The Apache Group | | http://www. / | +----------------------------- 現(xiàn)在驗(yàn)證Apache和PHP是否正在工作。然而 > > # And for PHP 4.x, use: > # ---> AddType application/x-httpd-php .php ---> AddType application/x-httpd-php-source .phps > > 現(xiàn)在我們準(zhǔn)備啟動(dòng)Apache服務(wù)器看它是否在工作 # cd /usr/local/apache/bin # ./apachectl configtest Syntax OK # ./apachectl start ./apachectl start: httpd started 測(cè)試我們的工作 Apache 正在工作嗎? 如果它工作正常,當(dāng)你用Netscape連接服務(wù)器時(shí) 注意:你可以用域名或機(jī)器實(shí)際的IP地址與服務(wù)器連接 PHP支持正在工作嗎?? 現(xiàn)在將測(cè)試PHP支持……創(chuàng)建一個(gè)文件(名為:test.php ),它有下列信息。文件需要位于文檔根路徑下,它應(yīng)該缺省設(shè)置為 test.php 文件 < ? phpinfo(); ?> 它將顯示有關(guān)服務(wù)器、php和環(huán)境的信息。下面是輸出頁(yè)面的頂部 很酷吧,PHP起作用了。 SSL 選擇正在工作嗎?? 好了,現(xiàn)在我們準(zhǔn)備測(cè)試SSL了。首先停止服務(wù)器 # /usr/local/apache/bin/apachect # /usr/local/apache/bin/apachect 測(cè)試它是否工作:通過(guò)用一個(gè)Netscape與服務(wù)器連接并且選 如果它起作用了,服務(wù)器將把證書(shū)發(fā)送到瀏覽器以建立一個(gè)安全連接 你在Netscape中將看見(jiàn)啟用了下列選項(xiàng)。這就告訴你一個(gè)安 PHP和MySQL能一起工作嗎?? 現(xiàn)在,我們可以確定php能與MySQL一起工作 記得我們以前創(chuàng)建了書(shū)籍?dāng)?shù)據(jù)庫(kù)。如果你跳過(guò)了以前的內(nèi)容 這個(gè)腳本基本上瀏覽該表并列出所有字段名,它的確很簡(jiǎn)單。 < ? $dbuser = "root"; $dbhost = "localhost"; $dbpass = "password"; $dbname = "test2"; $dbtble = "books"; $mysql_link = mysql_connect($dbhost,$dbuser, $column = mysql_list_fields($dbname, for($i=0; $i< mysql_num_fields($column); $i++ ) { print mysql_field_name($column,$i )."< br> "; } ?> 一個(gè)更復(fù)雜的例子將向你演示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, $column = mysql_list_fields($dbname, $sql = "SELECT * FROM $dbtable"; $result = mysql_db_query($dbname,$sql); ?> < table bgcolor="black"> < tr> < td> < table> < /td> < /tr> < /table> < /body> < /html> 注意,我們竟能在同一文件中同時(shí)有HTML和PHP命令 虛擬主機(jī)的設(shè)置 現(xiàn)在是設(shè)置Apache處理一些虛擬主機(jī)的時(shí)間了 讓我們看一個(gè) 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 webmaster@ # 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//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. # Log files Relative to ServerRoot option ErrorLog logs/-error_log TransferLog logs/-access_log RefererLog logs/-referer_log AgentLog logs/-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// 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 webmaster@ # Where documents are kept in the virtual domain DocumentRoot /virtual//www/html # Name of the server ServerName www. # Log files Relative to ServerRoot option ErrorLog logs/-error_log TransferLog logs/-access_log RefererLog logs/-referer_log AgentLog logs/-agent_log # No CGI‘s for this host # End: virtual host section 使用上述例子在你的服務(wù)器上創(chuàng)建你自己的虛擬主機(jī) SSL虛擬主機(jī) 創(chuàng)建SSL虛擬主機(jī)類似非SSL。除了你需要指定另外的指令 #----------------------------- # SSL Virtual Host Context #----------------------------- # General setup for the virtual host DocumentRoot /usr/local/apache/htdocs ServerAdmin webmaster@secure ServerName www.secure ErrorLoglogs/-error TransferLog logs/-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 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 # 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 "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x "%r" %b" 記住你有很多指令可以指定。我們將在另一篇有關(guān)配置Apache 生成證書(shū) 這是如何生成證書(shū)的按部就班的描述。 為你的Apache服務(wù)器創(chuàng)建一個(gè)RSA私用密鑰 # openssl genrsa -des3 -out server.key 1024 請(qǐng)?jiān)诎踩牡胤絺浞葸@個(gè)server.key文件。記住你輸入的通行短語(yǔ)(pass phrase)!你可以通過(guò)下面的命令看到這個(gè)RSA私用密鑰的 # openssl rsa -noout -text -in server.key 而且你可以為這個(gè)RSA私用密鑰創(chuàng)建一個(gè)加密的PEM版本 # openssl rsa -in server.key -out server.key.unsecure 用服務(wù)器RSA私用密鑰生成一個(gè)證書(shū)簽署請(qǐng)求(CSR # openssl req -new -key server.key -out server.csr 當(dāng)OpenSSL提示你“CommonName”時(shí) # openssl req -noout -text -in server.csr 將CSR發(fā)到一個(gè)CA 現(xiàn)在你必須發(fā)送該CSR到一個(gè)CA以便簽署,然后的結(jié)果才是可以 有兩種選擇: 第一種,你可以通過(guò)一個(gè)商業(yè)性CA如Verisign 或 Thawte簽署證書(shū)。那么你通常要將CSR貼入一個(gè)web表格 Verisign - http://digitalid. Thawte Consulting - http://www./certs CertiSign Certificadora Digital Ltda. - http://www. IKS GmbH - http://www./produkt Uptime Commerce Ltd. - http://www. BelSign NV/SA - http://www. 你自己的CA 第二種,你可以利用自己的CA并由該CA簽署CSR 為你的CA創(chuàng)建一個(gè)RSA私用密鑰( 被Triple-DES加密并且進(jìn)行PEM格式化的): # openssl genrsa -des3 -out ca.key 1024 請(qǐng)?jiān)诎踩牡胤絺浞葸@個(gè)ca.key文件。記住你輸入的通行短語(yǔ)(pass phrase)!你可以通過(guò)下面的命令看到這個(gè)RSA私用密鑰的 # openssl rsa -noout -text -in ca.key 而且你可以為這個(gè)RSA私用密鑰創(chuàng)建一個(gè)加密的PEM版本 # openssl rsa -in ca.key -out ca.key.unsecure 利用CA的RSA密鑰創(chuàng)建一個(gè)自簽署的CA證書(shū)(X509結(jié)構(gòu)) # openssl req -new -x509 -days 365 -key ca.key -out ca.crt 你可以通過(guò)下列命令查看該證書(shū)的細(xì)節(jié): # openssl x509 -noout -text -in ca.crt 準(zhǔn)備一個(gè)簽署所需的腳本,因?yàn)?openssl ca"命令有一些奇怪的要求而且缺省的OpenSSL配置不允許 現(xiàn)在你可以使這個(gè)CA簽署服務(wù)器的CSR,以便創(chuàng)建用于Apac # ./sign.sh server.csr 它簽署服務(wù)器的CSR并且結(jié)果在一個(gè)server.crt文件中 現(xiàn)在你有兩個(gè)文件:server.ket和server.crt SSLCertificateFile /path/to/this/server.crt SSLCertificateKeyFile /path/to/this/server.key server.csr不再需要了。 |
|