目的我们的目标是安装一个允许我们托管多个网站的web服务器 为了这个任务所需的工具是: Apache-一个网站服务器 Mod_SSL-一个安全套接字层(SSL)的模块 OpenSSL-开放源代码工具箱(mod_ssl所需) RSARef-仅对美国用户 MySQL-一个数据库服务器 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 知道怎样 签署你自己的证书 本文将覆盖大量的信息。本指南作为一个入门性地的指南 本文绝非是一个详细全面的文档,它当然将有一些错误(希望最小) 假设 本文假设你已经把下列软件安装在你的系统上了。 Perl (最好是ver 5+) gzip或gunzip gcc 和 GNU make 如果你没有安装好这些,你将需要采取必要的步骤在解释本文的任何 你也需要对UNIX命令、HTML、和SQL的一个基本了解 工作原理 理解在幕后发生了什么是有帮助的。这里是一个过分简化的工作原理 情况是:我们有一个从一个数据库取出一些数据的网页。John Doe从他的浏览器请求该页,请求被发送给web服务器 让我们一步一步地看: John Doe 从他的浏览器中点击一个链接;他的浏览器发送对http:/ Apache得到对test.php的请求,它知道.php文件应由PHP预处理器(mod_php)处理 test.php是包含命令的一个PHP脚本。这些命令之一是打开一个到一个数据库的连接并抓取数据。PHP 处理到数据库的连接,并且解释SQL调用从DB中提取数据。 服务器服务器得到从PHP解释器来的连接请求,并且处理这个请求 数据库然后将应答和结果回送到PHP解释器。 Apache回送该结果到John Doe的浏览器,作为对他请求的应答。John Doe现在看见一个包含从一个数据库来的一些信息的网页。 如果这是一个对https://www.yoursecures 服务器看到请求,解密并且认证它。它处理文件,加密并且发送它 再说一次,它不是100%的正确,但是它足够快地让你知道幕后发 既然我们对我们正在试图达到的目标有了一个很基本的了解 准备 Apache (Web服务器)-http://www. Mod_SSL (安全服务器层)-http://www. OpenSSL (SSL工具箱)-http://www. PHP (脚本语言)-http://www. MySQL (SQL数据库服务器 )-http://www. 下载所有(tar文件)源代码到一个临时目录下。保证你把他们放 我们的计划 我们的计划是首先安装MySQL服务器并保证它工作 MySQL源代码安装(UNIX) 你必须用来执行安装MySQL源代码分发的基本命令是 通过使用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服务器。你可以用config # configure --prefix=/usr/local/mysq 在你完成了配置以后,你可以执行下列命令make真正的二进制代 # make 现在你已准备好安装所有的二进制代码。运行下列命令在你用con # make install 在你安装好二进制代码后,现在是创建用于定义权限的mysql表 # scripts/mysql_install_db # cd /usr/local/mysql/bin # ./safe_mysqld & # ./mysqladmin -u root password "new-password" 注意:/usr/local/mysql是我选择安装MySQL 你可以通过运行一些简单的测试来验证服务器正在工作以确保MyS # BINDIR/mysqlshow -p +---------------+ | Databases | +---------------+ | mysql | +---------------+ 一旦你安装好MySQL,它将自动地创建2个数据库 # 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 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命令,插入并从数据库中选择数据 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服务器完全起作用了。我们可以继续加入 让我演示一下如何做一个快速删除。这只是让你知道,记住 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,继续安装。你可在完成所有安装并且一切工 PHP安装(UNIX) 现在安装PHP语言。你下载了最新的beta版,但是你可能必须 你仍然假定是root,如果不是,su回到root。 PHP要求你已经预先配置好的Apache,以便它能知道所需的 # cd /tmp/DOWNLOAD # gunzip -c apache_1.3.x.tar.gz | tar xf - # cd apache_1.3.x # ./configure # cd .. 好的,现在你可以开始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文件来设置PHP选项,如你可以通过在你的ph max_execution_time = 60; 注意:php3用户将使用php3.ini,而php4用户将使 Apache 与 Mod_SSL 该配置并安装mod_ssl和Apache了。对此 创建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文件 # 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源代码树 # 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. | +----------------------------- 现在你可以创建一个定制的证书。该选项将提示输入你的地址、公司 # 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 | 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. / | +----------------------------- 现在验证Apache和PHP是否正在工作。然而 > > # And for PHP 4.x, use: > # ---> AddType application/x-httpd-php .php ---> AddType application/x-httpd-php-source .phps > > 现在我们准备启动Apache服务器看它是否在工作 # cd /usr/local/apache/bin # ./apachectl configtest Syntax OK # ./apachectl start ./apachectl start: httpd started 测试我们的工作 Apache 正在工作吗? 如果它工作正常,当你用Netscape连接服务器时 注意:你可以用域名或机器实际的IP地址与服务器连接 PHP支持正在工作吗?? 现在将测试PHP支持……创建一个文件(名为:test.php ),它有下列信息。文件需要位于文档根路径下,它应该缺省设置为 test.php 文件 < ? phpinfo(); ?> 它将显示有关服务器、php和环境的信息。下面是输出页面的顶部 很酷吧,PHP起作用了。 SSL 选择正在工作吗?? 好了,现在我们准备测试SSL了。首先停止服务器 # /usr/local/apache/bin/apachect # /usr/local/apache/bin/apachect 测试它是否工作:通过用一个Netscape与服务器连接并且选 如果它起作用了,服务器将把证书发送到浏览器以建立一个安全连接 你在Netscape中将看见启用了下列选项。这就告诉你一个安 PHP和MySQL能一起工作吗?? 现在,我们可以确定php能与MySQL一起工作 记得我们以前创建了书籍数据库。如果你跳过了以前的内容 这个脚本基本上浏览该表并列出所有字段名,它的确很简单。 < ? $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> "; } ?> 一个更复杂的例子将向你演示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命令 虚拟主机的设置 现在是设置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 使用上述例子在你的服务器上创建你自己的虚拟主机 SSL虚拟主机 创建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" 记住你有很多指令可以指定。我们将在另一篇有关配置Apache 生成证书 这是如何生成证书的按部就班的描述。 为你的Apache服务器创建一个RSA私用密钥 # 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 # openssl req -new -key server.key -out server.csr 当OpenSSL提示你“CommonName”时 # openssl req -noout -text -in server.csr 将CSR发到一个CA 现在你必须发送该CSR到一个CA以便签署,然后的结果才是可以 有两种选择: 第一种,你可以通过一个商业性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创建一个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结构) # openssl req -new -x509 -days 365 -key ca.key -out ca.crt 你可以通过下列命令查看该证书的细节: # openssl x509 -noout -text -in ca.crt 准备一个签署所需的脚本,因为"openssl ca"命令有一些奇怪的要求而且缺省的OpenSSL配置不允许 现在你可以使这个CA签署服务器的CSR,以便创建用于Apac # ./sign.sh server.csr 它签署服务器的CSR并且结果在一个server.crt文件中 现在你有两个文件:server.ket和server.crt SSLCertificateFile /path/to/this/server.crt SSLCertificateKeyFile /path/to/this/server.key server.csr不再需要了。 |
|