茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1669187 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00278.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [分享]Apache 2.0.53 / SSL, PHP 5.0.3, MySQL 4.1.10 on Linux

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Apache、SSL、MySQL和PHP平滑無縫地安裝
Apache、SSL、MySQL和PHP平滑無縫地安裝
原作者:Israel Denis Jr. 譯:晏子 (2001-04-19 10:00:09)
目的

我們的目標是安裝一個允許我們托管多個網站的web服務器,其中一些是針對電子商務的安全解決方案,而大部分網站是通過連接一個數據庫服務器並且提取其數據的腳本驅動的。

為了這個任務所需的工具是:


Apache-一個網站服務器 
Mod_SSL-一個安全套接字層(SSL)的模塊 
OpenSSL-開放源代碼工具箱(mod_ssl所需) 
RSARef-僅對美國用戶 
MySQL-一個數據庫服務器 
PHP-一種腳本語言 
“條條大路通羅馬”……因此這只是很多能達到我們要求的配置之一。我選擇這樣的配置,是因為它是最簡單和最快的一種。選擇Mod_SSL/OpenSSL的原因是因為我有它的先前經驗,是最快配置和最容易安裝的一種。為了彼此方便地與Apache集成,我選擇了PHP和MySQL。記住,Perl能做到你想要做任何工作,然而,PHP對任何想學習它地程序員來說簡單並且容易。

希望你將在結束這個簡單的指南能成功地完成下列目標。 

安裝並設置MySQL數據庫服務器 
o 知道怎樣檢查MySQL服務器的狀態
o 知道怎樣使用命令行客戶程序存取MySQL服務器 
o 知道怎樣從web存取你的DB服務器 
安裝並設置具備SSL的Apache網站服務器 
o 配置一個簡單的虛擬網站
o 知道怎樣停止並啟動服務器
o 知道怎樣做一些基本的主機托管配置 
安裝並配置服務器端腳本的PHP 4.0超文本預處理器
o 知道怎樣編寫簡單的php代碼
o 知道怎樣使用php連接一個DB
o 創建一個啟用PHP地簡單網站與一個數據庫溝通 
創造一些樣本証書用Apache  SSL
o 知道怎樣產生一個CSR文件
o 知道怎樣加密一個鍵碼
o 知道怎樣 簽署你自己的証書 
本文將覆蓋大量的信息。本指南作為一個入門性地的指南,讓你步入電子商務、網站腳本和安全套接字層 (SSL)的世界,目的是幫助你建立由存儲在數據庫中的動態信息驅動的安全網站。

本文絕非是一個詳細全面的文檔,它當然將有一些錯誤(希望最小),在你閱讀它時請記住這一點。然而,它將激起你的熱情並運行前面提到的產品,希望讓你更好理解這些東西是怎樣工作的。不需要以前的編程知識,但假定你有點計算機知識背景。我的目標是編寫這個文檔以便任何新手能理解我正在談論的東西。如果我達到了,那我做了一件好事。如果你輕而易舉地建立了電子商務站點,比我做的更出色:-) 給我一些解釋。


假設

本文假設你已經把下列軟件安裝在你的系統上了。


Perl (最好是ver 5+) 
gzip或gunzip 
gcc 和 GNU make 

如果你沒有安裝好這些,你將需要採取必要的步驟在解釋本文的任何過程前把他們安裝好。

你也需要對UNIX命令、HTML、和SQL的一個基本了解。你應該有怎樣管理你的Linux機器的一個基本了解。你也需要一個完全正常的Linux機器,你將在它上面安裝軟件。當然你將需要在前面列出的必要的軟件包以編譯源代碼,並且最,保証你還沒有在Linux機器中預裝了MySQL、Apache、或PHP。


工作原理

理解在幕發生了什是有幫助的。這裡是一個過分簡化的工作原理,下圖和隨的解釋目前不是完全正確的,只是它的一個要點概述:

情況是:我們有一個從一個數據庫取出一些數據的網頁。John Doe從他的瀏覽器請求該頁,請求被發送給web服務器,接著調用一個PHP腳本。PHP腳本由PHP預處理器解釋並從數據庫中取出數據,然結果由余下的PHP腳本加工並轉化成HTML,做的HTML被發回用戶的瀏覽器。



 

讓我們一步一步地看: 

John Doe 從他的瀏覽器中點擊一個鏈接;他的瀏覽器發送對http://www.yourserver.com/test.php的請求。 
Apache得到對test.php的請求,它知道.php文件應由PHP預處理器(mod_php)處理,因此它通知 PHP處理它。它知道這些,是因為我們在Apache的配置中指定它。 
test.php是包含命令的一個PHP腳本。這些命令之一是打開一個到一個數據庫的連接並抓取數據。PHP 處理到數據庫的連接,並且解釋SQL調用從DB中提取數據。 
服務器服務器得到從PHP解釋器來的連接請求,並且處理這個請求。請求可能是類似一個簡單的選擇語句,或數據庫表創建等。 
數據庫然將應答和結果回送到PHP解釋器。 
Apache回送該結果到John Doe的瀏覽器,作為對他請求的應答。John Doe現在看見一個包含從一個數據庫來的一些信息的網頁。 
如果這是一個對https://www.yoursecureserver.com/test.php的請求,整個過程與上述類似,除了每個請求和應答在兩端被加密和解密,即,瀏覽器連接Apache,獲得它的加密鍵碼,加密請求並發送它。 
服務器看到請求,解密並且認証它。它處理文件,加密並且發送它。然瀏覽器用服務器的鍵碼解密它。記住既然連接被加密,就是用不同的端口用。端口80用在在非安全連接上,而端口443用在安全連接時。 
再說一次,它不是100%的正確,但是它足夠快地讓你知道幕發生的事情的非常簡單的概述。

既然我們對我們正在試圖達到的目標有了一個很基本的了解,讓我們繼續安裝軟件吧。

準備


Apache (Web服務器)-http://bjv2.readworld.com/?http://www.apache.org 
Mod_SSL (安全服務器層)-http://bjv2.readworld.com/?http://www.modssl.org 
OpenSSL (SSL工具箱)-http://bjv2.readworld.com/?http://www.openssl.org 
PHP (腳本語言)-http://bjv2.readworld.com/?http://www.php.net 
MySQL (SQL數據庫服務器 )-http://bjv2.readworld.com/?http://www.mysql.com 
下載所有(tar文件)源代碼到一個臨時目錄下。保証你把他們放在有很多空間的地方……你應該以root身份下載他們以避免權限問題。

我們的計劃


我們的計劃是首先安裝MySQL服務器並保証它工作,然我們將安裝PHP和Mod_SSL,最我們將安裝Apache網站服務器。在我們安裝了Apache以,我們可以測試PHP和Mod_SSL支持是否起作用了。

MySQL源代碼安裝(UNIX)
你必須用來執行安裝MySQL源代碼分發的基本命令是(自一個沒解開“tar”文件):

通過使用su成為 root用戶。 

$su  

直接進入你有tar文件的目錄。(使用一個臨時目錄。這裡使用 /tmp/download/ )

#cd /tmp/download/ 

使用下列命令提取文件。 

# gunzip -d -c mysql-3.22.xx.tar.gz | tar xvf - 

改變到新目錄,它在提取期間創建。

# cd mysql-3.22.xx 

現在你可以開始“配置”MySQL服務器。你可以用configure指定很多選項,使用configure --help查看所有的選項。我已經選擇--prefix指定到安裝地點的直接路徑。configure將檢查你的編譯器和一些其他東西。如果你有任何錯誤,你可以檢查config.cache文件查看錯誤。 

# configure --prefix=/usr/local/mysq 

在你完成了配置以,你可以執行下列命令make真正的二進制代碼。

# make 

現在你已準備好安裝所有的二進制代碼。運行下列命令在你用configure --prefix選項指定的目錄下安裝二進制代碼。

# make install 

在你安裝好二進制代碼,現在是創建用定義權限的mysql表的時候了。

# scripts/mysql_install_db 
# cd /usr/local/mysql/bin 
# ./safe_mysqld & 
# ./mysqladmin -u root password 'new-password' 

注意:/usr/local/mysql是我選擇安裝MySQL服務器的目錄。你可以通過改變目錄選擇另外的地方。

你可以通過運行一些簡單的測試來驗証服務器正在工作以確保MySQL正在運行。輸出應該類似下面所示的:BINDIR=/usr/local/mysql/bin。BINDIR依賴你在上面的前綴選擇的目錄。

# BINDIR/mysqlshow -p 
+---------------+
| Databases     | 
+---------------+
| mysql         |
+---------------+ 

一旦你安裝好MySQL,它將自動地創建2個數據庫。一個mysql表,它控制在實際的服務器中用戶、主機和 數據庫權限;另一個是一個test數據庫,我們能使用test數據庫。然而,我們想給你一個快速而簡單的MySQL可用的一些命令行選項的概述。這也將保証root被設置了對DB服務器的全部存取權限,即:root有創建數據庫、數據庫表等的許可,因此我們將創建一個test2數據庫,在以我們用它進行我們的測試。在你通過命令行進入MySQL前,你將被提示root用戶的新口令。記住你以前改變了它。

# mysql -u root -p 
mysql> show databases;
+----------------+ 
| Database       | 
+----------------+ 
| mysql          |
| test           |
+----------------+
mysql> create database test2;
Query OK, 1 row affected (0.00 sec) 

現在選擇新的數據庫使用,並創建一個名為tst_tbl的新表, 有下列2個字段。字段1是是一個id字段,允許你知道記錄的id。實質上為了簡化這只是的一個行號。第二個字段是你一個name字段,存儲書名信息。這些字段的格式是:字段1(id)是一個長度為3的整數(int),而字段2(name)是一個長度為50的字符(char)字段。為搜索和索引數據,我們指定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) 

現在我們用下列命令驗証一切正確無誤。

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命令,插入並從數據庫中選擇數據,現在把幾個記錄加到新表中。記住這些是簡單的書名記錄,但是一旦你獲得了SQL足夠的經驗,你可以為一些大的電子商務站點創建確實復雜的數據庫。讓我們創建2本假想的書的2條記錄。第一條記錄是我在將來某天寫的一本書的名字-“PHP 4 Newbies”,另一本是一個很有用的Linux書,“Red Hat Linux 6 Server”, 由Mohammed J. Kabir所。

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) 

現在我們可以檢查新紀錄,發出一條“選擇所有”命令    

mysql> SELECT * from books;
+----+----------------------------------+
| id | name                             | 
+----+----------------------------------+
| 1  | PHP for Newbies                  | 
| 2  | Red Hat Linux 6 Server           |
+----+----------------------------------+
2 rows in set (0.00 sec) 

很好,MySQL服務器完全起作用了。我們可以繼續加入,但是此時沒什意義。注意到當你向數據庫中插入記錄時,你怎樣不必指定id號,這是因為你創建了啟用auto_increment選項的id字段。

讓我演示一下如何做一個快速刪除。這只是讓你知道,記住,你可在MySQL的網站http://bjv2.readworld.com/?http://www.mysql.com上找到所需的有關mysql命令和服務器的所有信息。

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,繼續安裝。你可在完成所有安裝並且一切工作正常玩MySQL也不遲。

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://bjv2.readworld.com/?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                | 
|                               rse@engelschall.com                     | 
|                               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 )."
"; 
} 
?> 

一個更復雜的例子將向你演示PHP某些絕妙的功能。






$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);
?>



        
                while($value = mysql_fetch_array($result))
        {
                print "";
               //This loop goes through the colums and prints
               //each value
                for($i=0; $i< mysql_num_fields($column); $i++ )
                {
                    print "  $value[$i]  ";
                }
        print "
";

        }
        mysql_free_result($result);
        mysql_close();
        ?>
        






 

 

注意,我們竟能在同一文件中同時有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 webmaster@domain1.com

# 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 webmaster@domain2.com

# 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     webmaster@securedomain1.com
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不再需要了。

 


--------------------------------------------------------------------------------

參考資源

http://www.apache.org
http://www.modssl.org/
http://www.openssl.org/
http://www.php.net/
http://www.mysql.com/
http://www.perl.com/
http://www.cpan.org/ 

原文:http://www.devshed.com/Server_Side/PHP/SoothinglySeamless/ (http://www.fanqiang.com)
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[分享]Apache 2.0.53 / SSL, PHP 5.0.3, MySQL 4.1.10 on Linux
Apache 2.0.53 / SSL, PHP 5.0.3, MySQL 4.1.10 on Linux

This article comes with a full reference on how to compile from source latest Apache 2.0.53 server, including support for SSL, PHP 5.0.3 as a module, and MySQL 4.1.10 database on a Linux. It was fully tested under SUSE Linux 9.1, SUSE Linux 9.2 and Fedora Core 3, but shall work with any Linux distribution (only on Debian you will have to change RPMs for proper deb packages).


Today, when almost every Linux distribution comes with a binary form of Apache 2.0.x, PHP 4.3.x and MySQL 4.0.x, it may seem a bit unnecessary to compile these, but, if you want some special configuration, latest components, or simply tune performance of your Apache, PHP and MySQL, compilation from source is the only possibility.

Basic system description:
phpMyAdmin

PHP 5.0.3 will be compiled with support for: bz2, cpdflib, ctype, curllib, dom, ftp, gd2, freetype2, gettext, libiconv, libxml, mbstring, mysql, openssl, pcre, posix, session, SimpleXML, SPL, SQLite, tokenizer, xml, and zlib.


Apache 2.0.53 will be compiled with support for mod_access, mod_auth, mod_auth_digest, mod_deflate, mod_env, mod_headers, mod_setenvif, mod_ssl, mod_mime, mod_imap, mod_alias and mod_rewrite.

Necessary tools:

This article assumes that you have a working copy of gcc, gcc-c++ and many other utilities (make, bison, flex). Most distributions come with these packages by default, but if you do not have them, searching
rpmseek.comwill help you.


This is what you shall have before trying to compile anything:


Compilation options:

Compilation can be customized by passing several parameters to gcc at runtime, for my Pentium-IV/HT/3.2GHz, this is a good starting set of parameters:


 export CFLAGS="-march=pentium4 -mfpmath=sse -msse2 -O2 -pipe -s -fomit-frame-pointer" 


You may get a list of gcc compilation options for your CPU at
gcc.gnu.org.


All these scripts were fully tested under SESE Linux 9.1 with custom-built kernel 2.6.8.1 and Fedora Core 3 with custom-built kernel 2.6.9.1, and gcc version 3.3.3 / 3.4.2, but they shall work with any Linux distro (only on Debian you may need to change rpm packages for deb ones).


This manual assumes that all source files are located (downloaded to) /usr/local/src directory, SSL keys are placed into /home/ssl directory, and web root is located at /home/www directory.

Compile from source (Open) SSL:

Compilation of OpenSSL:

su cd /usr/local/src
tar -zxvf openssl-0.9.7e.tar.gz
cd openssl-0.9.7e
./config --prefix=/usr/local
make
make install

Create a private key and place it into directory /home/ssl:

mkdir /home/ssl
cd /home/ssl
/usr/local/bin/openssl genrsa -des3 -rand   some_big_file_1:some_big_file_2   -out localhost.key 1024

Next, we will create a private key without a pass-phrase, this is less secure, but it allows us to bootup the server without manually entering the pass-phrase every time…

/usr/local/bin/openssl rsa   -in localhost.key   -out localhost.key.unsecure

We will also create a request file that will be emailed to proper certification authority for getting a trusted SSL certificate (if needed) under file localhost.key.csr:

/usr/local/bin/openssl req -new   -key localhost.key   -out localhost.key.csr

While waiting for the certification authority, we can create a temporary self-signed certificate, good for 30 days:

/usr/local/bin/openssl x509 -req   -days 30   -in localhost.key.csr   -signkey localhost.key   -out localhost.cert
chmod 400 localhost.cert
chmod 400 localhost.key
chmod 400 localhost.key.unsecure

Compile MySQL 4.1.10 database from source:


MySQL 4.1.10 has a completely different communication protocol and associated PHP mysqli functions. If your scripts were not designed for MySQL 4.1, you shall rather get
MySQL release 4.0.23, to stay 100% compatible! Compilation options for MYSQL 4.0.23 will be the same, just remove one line with mysqli from PHP ./configure script.


However for any new development, MySQL 4.1.10 is recommended.


Compiling MySQL from source, and creating user / group called mysql:

cd /usr/local/src
tar -zxvf mysql-4.1.10.tar.gz
cd mysql-4.1.10
./configure   --prefix=/usr/local/mysql   --with-unix-sock-path=/tmp/mysql.sock   --with-charset=utf8 make make install

groupadd mysql
useradd -g mysql mysql
cp support-files/my-medium.cnf /etc/my.cnf
cd /usr/local/mysql
bin/mysql_install_db --user=mysql chown -R root  .
chown -R mysql var
chgrp -R mysql .

MySQL configuration file /etc/my.cnf can (for our local testing) look like this:

[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-locking
key_buffer = 16K
max_allowed_packet = 1M
table_cache = 4
sort_buffer_size = 64K
net_buffer_length = 2K
thread_stack = 64K
skip-networking
server-id = 1
[mysqldump]
quick max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 8M
sort_buffer_size = 8M
[myisamchk]
key_buffer = 8M
sort_buffer_size = 8M
[mysqlhotcopy]
interactive-timeout

Compile from source Apache 2.0.53 web server:

Quite a few web-hosting companies still use Apache 1.3.x, but time of Apache 2.0 incompatibilities and problems is long gone, so 2.0 is a better choice now.



And compile it:

cd /usr/local/src
tar -zxvf httpd-2.0.53.tar.gz
cd httpd-2.0.53
./configure --prefix=/usr/local/apache2 --enable-so --enable-auth-digest
--enable-rewrite --enable-setenvif --enable-mime --enable-deflate --enable-ssl
--with-ssl=/usr/local --enable-headers
make
make install

Next we have to modify (alter) main Apache config file located at /usr/local/apache2/conf/httpd.conf (this also assumes your web root is located at /home/www):

DocumentRoot "/home/www"

And we well add support for PHP 5 (as a module):

LoadModule php5_module modules/libphp5.so
DirectoryIndex index.html index.htm index.php
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

We also have to allow / create basic mod_rewrite rules:

<Directory "/home/www">
Options Indexes
Includes FollowSymLinks MultiViews
AllowOverride All  Order allow,deny  Allow from all
</Directory>

And dissalow clients to access .htaccess:

<Files ~ "^.ht">
Order allow,deny
Deny from all
</Files>

Next, if using SSL (on standard port 443), we will have to modify file /usr/local/apache2/conf/ssl.conf as follows (just replace the file content with this):

Listen 443
<VirtualHost _default_:443>
ServerName localhost
SSLEngine on
SSLCertificateFile /home/ssl/localhost.cert
SSLCertificateKeyFile /home/ssl/localhost.key.unsecure
</VirtualHost>

Compile from source PHP 5.0.3:


For compiling PHP, we will need quite a few external libraries, like libcurl, libiconv, libjpeg, libpng, and few others, which we have to download and compile first:



Compile libiconv from source:

cd /usr/local/src
tar -zxvf libiconv-1.9.2.tar.gz
cd libiconv-1.9.2
./configure --prefix=/usr/local
make
make install

Compile libjpeg from source:

cd /usr/local/src
tar -zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b
./configure --prefix=/usr/local
make
make install
make install-lib

Compile libpng from source:

cd /usr/local/src
tar -zxvf libpng-1.2.8.tar.gz
cd libpng-1.2.8
cp scripts/makefile.linux makefile
make
make install

Compile cpdflib from source:

cd /usr/local/src
tar -zxvf clibpdf202r1.tar.gz
cd ClibPDF/source
cp Makefile.Linux makefile
make
make install

Compile curl from source:

cd /usr/local/src
tar -zxvf curl-7.12.1.tar.gz
cd curl-7.12.1
./configure --prefix=/usr/local
make
make install

Compile freetype 2 from source:

cd /usr/local/src
tar -jxvf freetype-2.1.9.tar.bz2
cd freetype-2.1.9
./configure --prefix=/usr/local
make
make install

Next, we will compile PHP, with support for MySQL, iconv, curl, zlib, gd2, mbstring, SSL and many other modules:

cd /usr/local/src
tar -jxvf php-5.0.3.tar.bz2
cd php-5.0.3
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql
--with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock
--with-sqlite --enable-sqlite-utf8 --with-zlib --with-zlib-dir --with-bz2 --with-gd
--enable-gd --enable-gd-native-ttf --with-jpeg-dir=/usr/local --with-png-dir=/usr/local
--with-ttf --with-freetype-dir=/usr/local --with-iconv=/usr/local --with-curl=/usr/local
--enable-track-vars --with-gettext --with-config-file-path=/usr/local/apache2/conf
--enable-trans-id --enable-ftp --with-cpdflib=/usr/local --enable-mbstring --with-openssl=/usr/local
make
make install
cp php.ini-dist /usr/local/apache2/conf/php.ini

Next, we have to modify PHP configuration in file /usr/local/apache2/conf/php.ini, including basic PHP security settings:

mysql.default_socket = /tmp/mysql.sock
short_open_tag = Off
register_globals = Off
allow_url_fopen = Off

How to start Apache and MySQL at bootup?


The last thing left is to create a startup script, whitch will allow to run Apache and MySQL at bootup, automatically, so that we don’t have to do it manually. We will create a new file (for SuSE Linux 9.1, other ditros may vary here) /etc/init.d/web and set “executable” flag to it.

#! /bin/sh
#
# /etc/init.d/web
#
# (c) Radek HULAN
# http://hulan.cz/
#
### BEGIN INIT INFO
# Provides:       apache-mysql
# Default-Start:  5
# Default-Stop:   5
# Description:    Starts Apache2 and MySQL 4
### END INIT INFO
case "$1" in
start)
/usr/local/apache2/bin/apachectl start
/usr/local/mysql/share/mysql/mysql.server start
;;
stop)
/usr/local/apache2/bin/apachectl stop
/usr/local/mysql/share/mysql/mysql.server stop
;;
restart)
/usr/local/apache2/bin/apachectl restart
/usr/local/mysql/share/mysql/mysql.server restart
;;
esac

Next we will run YaST, section “System”, sub-section “Run level editor”, where we will enable service web for runlevel 3 and 5.

Testing the system?

First, start Apache and MySQL servers by entering:

/etc/init.d/web start[/code[code]
<p>Next, create file  /home/www/index.php with the following content:
</p>[code]
<?php phpinfo(); ?>

In your browser, type URL http://localhost/ and https://localhost/, and if everything is installed fine, you will see a lot of information about your new Apache/PHP/MySQL installation.

phpMyAdmin:

We will also need phpMyAdmin to manage MySQL database, by entering http://localhost/db/ into our browser:



Installation of phpMyAdmin into /home/www/db:

mkdir /home/www
cd /home/www
tar -jxvf /usr/local/src/phpMyAdmin-2.6.1.tar.bz2
ln -s phpMyAdmin-2.6.1 db

Next, we will configure phpMyAdmin’s advanced feaures, by modifying file /home/www/db/config.inc.php:

// URL to phpMyAdmin
$cfg['PmaAbsoluteUri'] = 'http://localhost/db/';
//connection settings
$cfg['Servers'][$i]['connect_type'] = 'socket';
$cfg['Servers'][$i]['extension'] = 'mysqli';
// user na password
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';
// PMA settings
$cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';
$cfg['Servers'][$i]['bookmarktable'] = 'pma_bookmark';
$cfg['Servers'][$i]['relation'] = 'pma_relation';
$cfg['Servers'][$i]['table_info'] = 'pma_table_info';
$cfg['Servers'][$i]['table_coords'] = 'pma_table_coords';
$cfg['Servers'][$i]['pdf_pages'] = 'pma_pdf_pages';
$cfg['Servers'][$i]['column_info'] = 'pma_column_info';
$cfg['Servers'][$i]['history'] = 'pma_history';
$cfg['Servers'][$i]['verbose_check'] = FALSE;
// persistent connections
$cfg['PersistentConnections'] = TRUE;
// do not display logo on the left
$cfg['LeftDisplayLogo'] = FALSE;
// show MySQL and PHP info
$cfg['ShowMysqlInfo'] = TRUE;
$cfg['ShowMysqlVars'] = TRUE;
$cfg['ShowPhpInfo'] = TRUE;
// show BLOBs
$cfg['ShowBlob'] = TRUE;

After everything is installed, use phpMyAdmin SQL window to run script /home/www/db/scripts/create_tables_mysql_4_1_2+.sql to create PMA tables, needed by phpMyAdmin.


PHPEclipseDebugging PHP:

There are several tools, like
PHPeclipse, which allow to debug PHP, in a full-featured IDE. In order to use PHPeclipse, you need to install PHP debugger first.



Installation:

cd /usr/local/src
tar -zxvf dbg-2.11.32-src.tar.gz
cd dbg-2.11.32./deferphpize
mkdir /usr/local/modules
cp modules/dbg.so /usr/local/modules
cp modules/dbg.la /usr/local/modules[/code[code]
<p>Next, you will have to modify PHP configuration file located at  /usr/local/apache2/conf/php.ini, add here:
</p>[code];load debugger
extension_dir = "/usr/local/modules" extension=dbg.so
;debugger configuration
[debugger]
debugger.enabled = true
debugger.profiler_enabled = true
debugger.JIT_host = localhost
debugger.JIT_port = 10001
debugger.JIT_enabled = on
;implicint flush - use only when debugging
implicit_flush = On

Do you need mod_perl as well?



Installation and compilation:

cd /usr/local/src
tar zxvf mod_perl-2.0-current.tar.gz
cd mod_perl-1.99_16
perl Makefile.PL MP_APXS=/usr/local/apache2/bin/apxs
make
make install[/code[code]
<p>Next, you have to modify Apache configuration file located at  /usr/local/apache2/conf/httpd.conf to load mod_perl, and set to use perl at directory  /home/www/perl:
</p>[code]
LoadModule perl_module modules/mod_perl.so
PerlModule Apache2
Alias /perl/ /home/www/perl/
<Location /perl/>
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
PerlOptions +ParseHeaders
Options +ExecCGI
</Location>

Testing? Create file /home/www/perl/test.pl, issue chmod 755 test.pl on it, and type http://localhost/perl/test.pl in your browser to test your mod_perl installation.

#!/usr/bin/perl
print "Content-type: text/plain ";
print "mod_perl 2.0 rocks! ";
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]幫 Linux 向企業 CA 申請網頁伺服器憑證
幫 Linux 向企業 CA 申請網頁伺服器憑證(適用 Fedora Core 7)

 一般中大型企業會用 Windows 架設企業內部 CA Server,在 IIS 上申請憑證沒什麼太大問題,而 Linux 要如何向 Windows 的 CA Server 申請網頁伺服器憑證呢?



一、建立申請憑證
 1. 建立私密金鑰 Server.key
   # openssl genrsa –out server.key 1024
  Generating RSA private key, 1024 bit long modulus
  ....................................................................++++++
  ....++++++
  e is 65537 (0x10001)


 2. 建立 Server.csr 憑證申請
   # openssl req –new –key server.key –out server.csr

  You are about to be asked to enter information that will be incorporated
  into your certificate request.
  What you are about to enter is what is called a Distinguished Name or a DN.
  There are quite a few fields but you can leave some blank
  For some fields there will be a default value,
  If you enter '.', the field will be left blank.
  -----
  Country Name (2 letter code) [GB]: TW  <- 國家
  State or Province Name (full name) [Berkshire]: Taiwan  <- 省份
  Locality Name (eg, city) [Newbury]: Taipei  <- 城市
  Organization Name (eg, company) [My Company Ltd]: XYZ  <-公司名稱
  Organizational Unit Name (eg, section) []: MIS  <- 單位名稱

  Common Name (eg, your name or your server's hostname) []: www.abc.com.tw  <- 網站的 FQDN
  Email Address []: service@abc.com.tw  <- 管理者Mail

  Please enter the following 'extra' attributes
  to be sent with your certificate request
  A challenge password []:  <- 直接按"Enter"
  An optional company name []:  <- 直接按"Enter"


 3. 執行後將產生 server.csr 檔案,此檔案要交給企業 CA 或第三方公信單位產生 Server.crt 憑證。


二、申請網頁伺服器憑證
 1. 開啟企業 CA 憑證申請網頁,點選 「要求憑證」


 2. 點選 「進階憑證要求」

 3. 點選 「用 Base-64 編碼的 CMC 或 PKCS #10 檔案來提交憑證......更新要求」

 4. 將 Linux 上產生的 server.csr 內容,貼到 「Base-64 編碼的憑證要求」內,並將憑證範本選擇 「網頁伺服器」
(註:如果看不到網頁伺服器選項,表示使用一般使用者身份申請憑證,請切換使用者身份為 Domain Admin,重新申請),然後點選 「提交」送出憑證申請。

 5. 選擇 DER 編碼或 Base 64 編碼都可以,點選 「下載憑證」,儲存申請好的 certnew.cer 網頁伺服器憑證。


三、安裝憑證
 1. 將申請下來的 certnew.cer 複製到 Linux 上,並將副檔名更改為 certnew.crt。


 2. 將 certnew.crt 搬移到憑證存放路徑下:
   /etc/pki/tls/certs/  <- 各 Linux 版本路徑不同



 3. 將剛才在 Linux 上所產生的 server.key 搬移到金鑰存放路徑下:
   /etc/pki/tls/private/  <- 各 Linux 版本路徑不同


 4. 編輯 /etc/httpd/conf.d/ssl.conf
   SSLCertificateFile /etc/pki/tls/certs/certnew.crt  <- 修改新的憑證名稱
   SSLCertificateKeyFile /etc/pki/tls/private/server.key  <- 修改新的金鑰名稱


 5. 重新啟動服務 service httpd restart
 


原文出處:Gavin's Linux學習手札: 幫 Linux 向企業 CA 申請網頁伺服器憑證(適用 Fedora Core 7) - yam天空部落
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]openssl_簽發憑證方式

OpenSSL 簽發憑證方式

  • OpenSSL 版本 : openssl-0.9.8b-10.el5

1 根憑證 (Root CA) 作法:

1.1 建立根憑證 Private Key 檔 RootCA.key


openssl genrsa -des3 -out RootCA.key 2048
chmod og-rwx RootCA.key

看產生結果

1.2 產生根憑證的申請檔 RootCA.req

openssl req -new -key RootCA.key -out RootCA.req

看產生結果

1.3 產生效期十年的根憑證檔 RootCA.crt


openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey RootCA.key -in RootCA.req -out RootCA.crt
rm -f RootCA.req

看產生結果

2 伺服器憑證 (Server CA) 作法:


2.1 建立伺服器憑證 Private Key 檔 ServerCA.key


openssl genrsa -out ServerCA.key 2048
chmod og-rwx ServerCA.key

看產生結果

2.2 產生伺服器憑證的申請檔 ServerCA.req

openssl req -new -key ServerCA.key -out ServerCA.req

看產生結果

2.3 產生效期兩年的伺服器憑證檔 ServerCA.crt


openssl x509 -req -days 730 -sha1 -extensions v3_req -CA RootCA.crt -CAkey RootCA.key  -CAserial RootCA.srl -CAcreateserial -in ServerCA.req -out ServerCA.crt
rm -f ServerCA.req

看產生結果


3 用戶憑證 (Client CA) 作法:

3.1 建立用戶憑證 Private Key 檔 ClientCA.key


openssl genrsa -des3 -out ClientCA.key 2048
chmod og-rwx ClientCA.key

看產生結果

3.2 產生用戶憑證的申請檔 ClientCA.req

openssl req -new -key ClientCA.key -out ClientCA.req

看產生結果

3.3 產生效期兩年的用戶憑證檔 ClientCA.crt


openssl x509 -req -days 730 -sha1 -extensions v3_req -CA RootCA.crt -CAkey RootCA.key  -CAserial RootCA.srl -CAcreateserial -in ClientCA.req -out ClientCA.crt
rm -f ClientCA.req

看產生結果

3.4 將 ClientCA.crt 與 ClientCA.key 轉成 ClientCA.pfx

openssl pkcs12 -export -in ClientCA.crt -inkey ClientCA.key -out ClientCA.pfx

看產生結果


  • 看到 apache 的 ssl_request_log 發現 IE7 使用 TLSv1 RC4-MD5, Firefox3 使用 SSLv3 DHE-RSA-AES256-SHA, cURL 7.15.5 使用 TLSv1 DHE-RSA-AES256-SHA



[root@pd920 httpd]# cat ssl_request_log
[20/Aug/2008:16:28:12 +0800] 192.168.11.248 TLSv1 RC4-MD5 "GET /t.txt HTTP/1.1" 25 <-- IE7
[20/Aug/2008:16:46:14 +0800] 192.168.11.248 SSLv3 DHE-RSA-AES256-SHA "GET /t.txt HTTP/1.1" 25 <-- Firefox3
[21/Aug/2008:12:18:55 +0800] 127.0.0.1 TLSv1 DHE-RSA-AES256-SHA "GET /t.txt HTTP/1.1" 5 <-- cURL 7.15.5
[root@pd920 httpd]#

參考網址


原文出處: OpenSSL 簽發憑證方式 [蔡宗融個人網站]
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]常用的 OpenSSL 命令備忘表

SSL證書安裝小技巧:常用的OpenSSL命令備忘表

2018-05-07  由 GDCA數安時代 發表于 程式開發

下列是常用的OpenSSL命令列表

作為一個開源工具,OpenSSL可用於Windows,Linux,macOS,Solaris,QNX以及大多數主要作業系統。

以C程式語言編寫的核心庫,OpenSSL命令可用於執行從CSR生成到轉換證書格式等數百種功能。但對於只想安裝SSL證書的人來說,只有少數命令是非常必要的。下面是安裝SSL證書時常要用到的OpenSSL命令及其應用程式。

用於生成私鑰的OpenSSL命令

openssl genrsa -out yourdomain.key 2048

OpenSSL命令檢查您的私鑰

openssl rsa -in privateKey.key -check

OpenSSL命令來生成CSR

如果已生成私鑰:

openssl req -new -key yourdomain.key -out yourdomain.csr

一旦執行這個命令,會被要求更多的細節。輸入如下信息:

國家名稱:您的組織合法所在的2位國家代碼。州/省:寫出貴組織合法所在州的全名。城市:寫出您的組織合法所在城市的全名。組織名稱:編寫組織的合法名稱。組織單位:部門名稱(不強制,按Enter鍵跳過)通用名稱:合格域名(例如,www.yourdomainname.com。)電子郵件:已通過認證的電子郵件ID(不強制,按Enter鍵跳過)

如果還沒有生成私鑰:

該命令將一次生成CSR和私鑰。


openssl req -new \

-newkey rsa:2048 -nodes -keyout yourdomain.key \

-out yourdomain.csr \

-subj 「/C=US/ST=Florida/L=Saint Petersburg/O=Your Company, Inc./OU=IT/CN=yourdomain.com」

國家名稱:您的組織合法所在的2位國家代碼。州/省:寫出貴組織合法所在州的全名。城市:寫出您的組織合法所在城市的全名。組織名稱:編寫組織的合法名稱。組織單位:部門的名稱通用名稱:您的完全限定域名

OpenSSL命令檢查CSR

openssl req -text -noout -verify -in CSR.csr

用於轉換證書和密鑰文件的OpenSSL命令

OpenSSL命令轉換PEM文件:

將PEM轉換為DER

openssl x509 -outform der -in certificate.pem -out certificate.der

轉換PEM到P7B

openssl crl2pkcs7 -nocrl -certfile certificate.cer -out certificate.p7b -certfile CACert.cert

將PEM轉換為PFX

openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt

OpenSSL命令來轉換DER文件

將DER轉換為PEM

要轉換證書文件:


openssl x509 -inform DER -in yourdomain.der -outform PEM -out yourdomain.crt

要轉換私鑰文件:

openssl rsa -inform DER -in yourdomain_key.der -outform PEM -out yourdomain.key

OpenSSL命令轉換P7B文件

將P7B轉換為PEM

openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer

將P7B轉換為PFX

openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer

openssl pkcs12 -export -in certificate.cer -inkey privateKey.key -out certificate.pfx -certfile CACert.cer

OpenSSL命令轉換PKCS#12(.pfx)文件

將PFX轉換為PEM

要轉換證書文件:

openssl pkcs12 -in certificate.pfx -out certificate.cer -nodes

要轉換私鑰文件:

openssl pkcs12 -in yourdomain.pfx -nocerts -out yourdomain.key -nodes

OpenSSL命令檢查證書

openssl x509 -in certificate.crt -text -noout

用於檢查PKCS#12文件(.pfx文件)的OpenSSL命令


openssl pkcs12 -info -in keyStore.p12

————————————————————————

SSL證書是HTTP明文協議升級HTTPS加密協議的重要渠道,是網絡安全傳輸的加密通道。關於更多SSL證書的資訊,請關注數安時代(GDCA)。GDCA致力於網絡信息安全,已通過WebTrust 的國際認證,是全球可信任的證書籤發機構。GDCA專業技術團隊將根據用戶具體情況為其提供最優的產品選擇建議,並針對不同的應用或伺服器要求提供專業對應的HTTPS解決方案。

文章來源於https://www.trustauth.cn/wiki/25709.html


原文出處:SSL證書安裝小技巧:常用的OpenSSL命令備忘表 - 每日頭條
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|