超級伺服器apache+mysql+php+ssl之完整安裝攻略(2)

來源:互聯網
上載者:User
apache|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://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 )."<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 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不再需要了。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.