目的我們的目標是安裝一個允許我們托管多個網(wǎng)站的web服務(wù)器 為了這個任務(wù)所需的工具是: Apache-一個網(wǎng)站服務(wù)器 Mod_SSL-一個安全套接字層(SSL)的模塊 OpenSSL-開放源代碼工具箱(mod_ssl所需) RSARef-僅對美國用戶 MySQL-一個數(shù)據(jù)庫服務(wù)器 PHP-一種腳本語言 “條條大路通羅馬”……因此這只是很多能達到我們要求的配置之一 希望你將在結(jié)束這個簡單的指南后能成功地完成下列目標。 安裝并設(shè)置MySQL數(shù)據(jù)庫服務(wù)器 o 知道怎樣檢查MySQL服務(wù)器的狀態(tài) o 知道怎樣使用命令行客戶程序存取MySQL服務(wù)器 o 知道怎樣從web存取你的DB服務(wù)器 安裝并設(shè)置具備SSL的Apache網(wǎng)站服務(wù)器 o 配置一個簡單的虛擬網(wǎng)站 o 知道怎樣停止并啟動服務(wù)器 o 知道怎樣做一些基本的主機托管配置 安裝并配置服務(wù)器端腳本的PHP 4.0超文本預(yù)處理器 o 知道怎樣編寫簡單的php代碼 o 知道怎樣使用php連接一個DB o 創(chuàng)建一個啟用PHP地簡單網(wǎng)站與一個數(shù)據(jù)庫溝通 創(chuàng)造一些樣本證書用于Apache SSL o 知道怎樣產(chǎn)生一個CSR文件 o 知道怎樣加密一個鍵碼 o 知道怎樣 簽署你自己的證書 本文將覆蓋大量的信息。本指南作為一個入門性地的指南 本文絕非是一個詳細全面的文檔,它當(dāng)然將有一些錯誤(希望最小) 假設(shè) 本文假設(shè)你已經(jīng)把下列軟件安裝在你的系統(tǒng)上了。 Perl (最好是ver 5+) gzip或gunzip gcc 和 GNU make 如果你沒有安裝好這些,你將需要采取必要的步驟在解釋本文的任何 你也需要對UNIX命令、HTML、和SQL的一個基本了解 工作原理 理解在幕后發(fā)生了什么是有幫助的。這里是一個過分簡化的工作原理 情況是:我們有一個從一個數(shù)據(jù)庫取出一些數(shù)據(jù)的網(wǎng)頁。John Doe從他的瀏覽器請求該頁,請求被發(fā)送給web服務(wù)器 讓我們一步一步地看: John Doe 從他的瀏覽器中點擊一個鏈接;他的瀏覽器發(fā)送對http:/ Apache得到對test.php的請求,它知道.php文件應(yīng)由PHP預(yù)處理器(mod_php)處理 test.php是包含命令的一個PHP腳本。這些命令之一是打開一個到一個數(shù)據(jù)庫的連接并抓取數(shù)據(jù)。PHP 處理到數(shù)據(jù)庫的連接,并且解釋SQL調(diào)用從DB中提取數(shù)據(jù)。 服務(wù)器服務(wù)器得到從PHP解釋器來的連接請求,并且處理這個請求 數(shù)據(jù)庫然后將應(yīng)答和結(jié)果回送到PHP解釋器。 Apache回送該結(jié)果到John Doe的瀏覽器,作為對他請求的應(yīng)答。John Doe現(xiàn)在看見一個包含從一個數(shù)據(jù)庫來的一些信息的網(wǎng)頁。 如果這是一個對https://www.yoursecures 服務(wù)器看到請求,解密并且認證它。它處理文件,加密并且發(fā)送它 再說一次,它不是100%的正確,但是它足夠快地讓你知道幕后發(fā) 既然我們對我們正在試圖達到的目標有了一個很基本的了解 準備 Apache (Web服務(wù)器)-http://www. Mod_SSL (安全服務(wù)器層)-http://www. OpenSSL (SSL工具箱)-http://www. PHP (腳本語言)-http://www. MySQL (SQL數(shù)據(jù)庫服務(wù)器 )-http://www. 下載所有(tar文件)源代碼到一個臨時目錄下。保證你把他們放 我們的計劃 我們的計劃是首先安裝MySQL服務(wù)器并保證它工作 MySQL源代碼安裝(UNIX) 你必須用來執(zhí)行安裝MySQL源代碼分發(fā)的基本命令是 通過使用su成為 root用戶。 $su 直接進入你有tar文件的目錄。(使用一個臨時目錄。這里使用 /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)在你可以開始“配置”MySQL服務(wù)器。你可以用config # configure --prefix=/usr/local/mysq 在你完成了配置以后,你可以執(zhí)行下列命令make真正的二進制代 # make 現(xiàn)在你已準備好安裝所有的二進制代碼。運行下列命令在你用con # make install 在你安裝好二進制代碼后,現(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 你可以通過運行一些簡單的測試來驗證服務(wù)器正在工作以確保MyS # BINDIR/mysqlshow -p +---------------+ | Databases | +---------------+ | mysql | +---------------+ 一旦你安裝好MySQL,它將自動地創(chuàng)建2個數(shù)據(jù)庫 # 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ù)庫使用,并創(chuàng)建一個名為tst_tbl的新表, 有下列2個字段。字段1是是一個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)在我們用下列命令驗證一切正確無誤。 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命令基本上“描述”了表的布局 好,該試一些確實有用的SQL命令,插入并從數(shù)據(jù)庫中選擇數(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)在我們可以檢查新紀錄,發(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ù)加入 讓我演示一下如何做一個快速刪除。這只是讓你知道,記住 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語言。你下載了最新的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)在你可以開始PHP的安裝。提取源代碼文件并進入其目錄 # gunzip -c php-4.0.x.tar.gz | tar xf - # cd php-4.0.x 如果你正在編譯代碼,configure將永遠是你的朋友。:- # ./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文件來設(shè)置PHP選項,如你可以通過在你的ph max_execution_time = 60; 注意:php3用戶將使用php3.ini,而php4用戶將使 Apache 與 Mod_SSL 該配置并安裝mod_ssl和Apache了。對此 創(chuàng)建rasref目錄,你將在該目錄提取文件。注意 # mkdir rsaref-2.0 # cd rsaref-2.0 # gzip -d -c ../rsaref20.tar.Z | tar xvf - 現(xiàn)在配置并構(gòu)造OpenSSL庫。 # cd rsaref-2.0 # cp -rp install/unix local # cd local # make # mv rsaref.a librsaref.a # cd ../.. 安裝OpenSSL。記住,你將用它來創(chuàng)建臨時證書和CSR文件 # cd openssl-0.9.x # ./config -prefix=/usr/local/ssl -L`pwd`/../rsaref-2.0/local/ rsaref -fPIC 現(xiàn)在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 .. 現(xiàn)在我們可以把更多的Apache模塊加到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 --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 | | rse@ | | www. | +----------------------------- 現(xiàn)在你可以創(chuà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)在驗證Apache和PHP是否正在工作。然而 > > # And for PHP 4.x, use: > # ---> AddType application/x-httpd-php .php ---> AddType application/x-httpd-php-source .phps > > 現(xiàn)在我們準備啟動Apache服務(wù)器看它是否在工作 # cd /usr/local/apache/bin # ./apachectl configtest Syntax OK # ./apachectl start ./apachectl start: httpd started 測試我們的工作 Apache 正在工作嗎? 如果它工作正常,當(dāng)你用Netscape連接服務(wù)器時 注意:你可以用域名或機器實際的IP地址與服務(wù)器連接 PHP支持正在工作嗎?? 現(xiàn)在將測試PHP支持……創(chuàng)建一個文件(名為:test.php ),它有下列信息。文件需要位于文檔根路徑下,它應(yīng)該缺省設(shè)置為 test.php 文件 < ? phpinfo(); ?> 它將顯示有關(guān)服務(wù)器、php和環(huán)境的信息。下面是輸出頁面的頂部 很酷吧,PHP起作用了。 SSL 選擇正在工作嗎?? 好了,現(xiàn)在我們準備測試SSL了。首先停止服務(wù)器 # /usr/local/apache/bin/apachect # /usr/local/apache/bin/apachect 測試它是否工作:通過用一個Netscape與服務(wù)器連接并且選 如果它起作用了,服務(wù)器將把證書發(fā)送到瀏覽器以建立一個安全連接 你在Netscape中將看見啟用了下列選項。這就告訴你一個安 PHP和MySQL能一起工作嗎?? 現(xiàn)在,我們可以確定php能與MySQL一起工作 記得我們以前創(chuàng)建了書籍?dāng)?shù)據(jù)庫。如果你跳過了以前的內(nèi)容 這個腳本基本上瀏覽該表并列出所有字段名,它的確很簡單。 < ? $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> "; } ?> 一個更復(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> 注意,我們竟能在同一文件中同時有HTML和PHP命令 虛擬主機的設(shè)置 現(xiàn)在是設(shè)置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 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)建你自己的虛擬主機 SSL虛擬主機 創(chuàng)建SSL虛擬主機類似非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 生成證書 這是如何生成證書的按部就班的描述。 為你的Apache服務(wù)器創(chuàng)建一個RSA私用密鑰 # openssl genrsa -des3 -out server.key 1024 請在安全的地方備份這個server.key文件。記住你輸入的通行短語(pass phrase)!你可以通過下面的命令看到這個RSA私用密鑰的 # openssl rsa -noout -text -in server.key 而且你可以為這個RSA私用密鑰創(chuàng)建一個加密的PEM版本 # openssl rsa -in server.key -out server.key.unsecure 用服務(wù)器RSA私用密鑰生成一個證書簽署請求(CSR # openssl req -new -key server.key -out server.csr 當(dāng)OpenSSL提示你“CommonName”時 # openssl req -noout -text -in server.csr 將CSR發(fā)到一個CA 現(xiàn)在你必須發(fā)送該CSR到一個CA以便簽署,然后的結(jié)果才是可以 有兩種選擇: 第一種,你可以通過一個商業(yè)性CA如Verisign 或 Thawte簽署證書。那么你通常要將CSR貼入一個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)建一個RSA私用密鑰( 被Triple-DES加密并且進行PEM格式化的): # openssl genrsa -des3 -out ca.key 1024 請在安全的地方備份這個ca.key文件。記住你輸入的通行短語(pass phrase)!你可以通過下面的命令看到這個RSA私用密鑰的 # openssl rsa -noout -text -in ca.key 而且你可以為這個RSA私用密鑰創(chuàng)建一個加密的PEM版本 # openssl rsa -in ca.key -out ca.key.unsecure 利用CA的RSA密鑰創(chuàng)建一個自簽署的CA證書(X509結(jié)構(gòu)) # openssl req -new -x509 -days 365 -key ca.key -out ca.crt 你可以通過下列命令查看該證書的細節(jié): # openssl x509 -noout -text -in ca.crt 準備一個簽署所需的腳本,因為"openssl ca"命令有一些奇怪的要求而且缺省的OpenSSL配置不允許 現(xiàn)在你可以使這個CA簽署服務(wù)器的CSR,以便創(chuàng)建用于Apac # ./sign.sh server.csr 它簽署服務(wù)器的CSR并且結(jié)果在一個server.crt文件中 現(xiàn)在你有兩個文件:server.ket和server.crt SSLCertificateFile /path/to/this/server.crt SSLCertificateKeyFile /path/to/this/server.key server.csr不再需要了。 |
|