实际环境
- 系统环境
macOS Sierra(10.12.5)
- Apache
Apache/2.4.25 (Unix)
- OpenSSL
OpenSSL 1.0.2l
引言
恭喜你,一个HTTP服务器已经搭建成功了!是不是很简单?接下来介绍如何具体定制化配置Apache服务器。
搭建HTTP服务器
-
修改服务器默认根路径
打开配置文件/private/etc/apache2/httpd.conf
,更改系统默认的根路径DocumentRoot
为自定义路径(因为系统默认的根路径要求管理员权限,更改比较繁琐,如果需要用系统默认的根路径,可以跳过此步骤)。
DocumentRoot "/Users/libo/apache_server"
<Directory "/Users/libo/apache_server">
-
设置虚拟主机
通过设置多个虚拟主机可以支持一台物理服务器访问多个域名,就好像有多个服务器一样。打开配置文件/private/etc/apache2/httpd.conf
,去掉#Include /private/etc/apache2/extra/httpd-vhosts.conf
前面的#
,保存并退出。然后打开/private/etc/apache2/extra/httpd-vhosts.conf
,注释掉以下代码:
#<VirtualHost *:80>
# ServerAdmin
# DocumentRoot
# ServerName
# ServerAlias
# ErrorLog
# CustomLog common
#</VirtualHost>
#<VirtualHost *:80>
# ServerAdmin
# DocumentRoot
# ServerName
# ErrorLog
# CustomLog common
#</VirtualHost>
然后加入以下代码:
<VirtualHost *:80>
# ServerAdmin
DocumentRoot "/Users/libo/apache_server/mywebsite"
ServerName
ErrorLog
CustomLog common
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
# ServerAdmin
DocumentRoot "/Users/libo/apache_server/mywebsite2"
ServerName
ErrorLog
CustomLog common
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
注意:两个虚拟主机的根路径必须在服务器根路径下。
-
更改本地DNS配置文件
打开配置文件/private/etc/hosts
,把我们的服务器域名对应到回送地址127.0.0.1
上,这样就可以直接本地进行域名访问。文件前3行配置是系统默认配置,我们平时访问的localhost
就是通过该文件直接解析成对应的IP地址进行本地访问的,其中127.0.0.1
是IPv4
标准,::1
是IPv6
标准。其实我们平时通过域名访问网址时,都会首先访问这个本地的hosts
文件来查找IP地址,如果找不到对应的结果才会访问DNS服务器进行DNS解析来进行后续的步骤。
配置代码如下:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1
127.0.0.1
-
验证
最后我们在配置的服务器根路径下加入测试的HTML文件来进行测试,结果如下:
说明我们的HTTP服务器已经配置成功了!
搭建HTTPS服务器
我们只需要在HTTP服务器的基础上配置HTTPS就可以了。
-
创建自签名SSL/TLS证书
首先使用OpenSSL创建私钥文件
openssl genrsa -out mywebsite.key 2048
然后利用私钥创建自签名证书
openssl req -new -x509 -key mywebsite.key -out mywebsite.cer
需要自己填写一些证书信息,如下图:
SSLCertificateFile "/Users/libo/apache_server/mywebsite/mywebsite.cer"
SSLCertificateFile "/Users/libo/apache_server/mywebsite2/mywebsite2.cer"
SSLCertificateKeyFile "/Users/libo/apache_server/mywebsite/mywebsite.key"
SSLCertificateKeyFile "/Users/libo/apache_server/mywebsite2/mywebsite2.key"
-
修改虚拟主机配置
更改虚拟主机默认端口号为443
,配置服务器证书SSLCertificateFile
和私钥SSLCertificateKeyFile
,如下:
<VirtualHost *:443>
# ServerAdmin
DocumentRoot "/Users/libo/apache_server/mywebsite"
ServerName
ErrorLog
CustomLog common
SSLCertificateFile "/Users/libo/apache_server/mywebsite/mywebsite.cer"
SSLCertificateKeyFile "/Users/libo/apache_server/mywebsite/mywebsite.key"
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:443>
# ServerAdmin
DocumentRoot "/Users/libo/apache_server/mywebsite2"
ServerName
ErrorLog
CustomLog common
SSLCertificateFile "/Users/libo/apache_server/mywebsite2/mywebsite2.cer"
SSLCertificateKeyFile "/Users/libo/apache_server/mywebsite2/mywebsite2.key"
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
-
验证
重启服务器sudo apachectl restart
,浏览器访问,如下图:
可以看到我们的HTTPS服务器已经搭建成功了,尽管不是“安全”的访问!接下来我们要进行一些操作使访问变得“安全”。
-
创建安全的HTTPS访问
所谓创建安全的HTTPS访问,就是让浏览器信任服务器颁发的公钥证书,因为我们的证书没有添加到浏览器信任列表,所以浏览器会提示我们“不安全”。废话不多说,首先创建CA根证书:
创建CA私钥:
openssl genrsa -des3 -out Apache_CA.key 2048
这里使用-des3进行加密,需要4~1023位密码。
创建CA证书:
openssl req -new -x509 -days 365 -key Apache_CA.key -out Apache_CA.cer
这里需要输入刚才设置的CA私钥密码并填写一些证书信息。
创建服务器证书私钥:
openssl genrsa -out mywebsite.key 2048
生成证书请求文件CSR:
openssl req -new -key mywebsite.key -out mywebsite.csr
这里同样需要填写一些证书信息,同样,红框标注选项最好填写虚拟主机域名。
生成证书配置文件v3.ext:
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 =
DNS.1
要写配置证书的服务器域名。
用自己的CA签发证书:
openssl x509 -req -in mywebsite.csr -CA Apache_CA.cer -CAkey Apache_CA.key -CAcreateserial -out mywebsite.cer -days 365 -sha256 -extfile v3.ext
然后需要输入生成Apache_CA.key时设置的密码。
恭喜你,证书已经创建成功!然后我们按照前面提到的步骤替换掉服务器的证书,重启服务器sudo apachectl restart
。
搭建正向代理服务器
在搭建正向代理服务器之前,我们先说下正向代理和反向代理的区别。我们平时访问网上的资源,绝大多数情况是要经过各种(正向或反向或其他)代理的,只是这对用户来说是无感知的。正向代理可以理解为一层跳板,我们访问资源的目的IP地址是服务器,只是经过正向代理这个节点。而反向代理是我们访问资源的目的IP地址就是反向代理服务器,反向代理服务器和最终的服务器进行交互,获取资源。下图可以很清晰的展示这两种关系:
下面我们开始配置正向代理服务器。
-
修改服务器配置
打开配置文件/private/etc/apache2/httpd.conf
,去掉以下module
前面的#
。
#LoadModule proxy_module libexec/apache2/mod_proxy.so
#LoadModule proxy_connect_module libexec/apache2/mod_proxy_connect.so
#LoadModule proxy_ftp_module libexec/apache2/mod_proxy_ftp.so
#LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so
-
修改虚拟主机配置
将虚拟主机端口号改为80
,加入正向代理设置。
<VirtualHost *:80>
# ServerAdmin
DocumentRoot "/Users/libo/apache_server/mywebsite"
ServerName
ErrorLog
CustomLog common
SSLCertificateFile "/Users/libo/apache_server/mywebsite/mywebsite.cer"
SSLCertificateKeyFile "/Users/libo/apache_server/mywebsite/mywebsite.key"
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order deny,allow
Allow from all
</Directory>
#正向代理设置
ProxyRequests On
ProxyVia Full
<Proxy *>
Order deny,allow
#Deny from all
Allow from all
</Proxy>
</VirtualHost>
ProxyVia Full
可以为我们打出最详细的代理服务器信息。
-
创建自动代理配置
为了方便测试,我们创建mywebsite.pac
文件来配置代理。
function FindProxyForURL(url, host) {
if (host == {
return "PROXY 127.0.0.1:80";
}
return 'DIRECT;';
}
-
配置代理配置
打开系统偏好设置->网络->高级->代理,选中自动代理配置,配置mywebsite.pac
文件的路径地址,然后点击好->应用。
通过Via
字段,我们发现这次请求经过了我们的代理服务器,说明我们的配置成功了!
搭建反向代理服务器
<VirtualHost *:80>
# ServerAdmin
DocumentRoot "/Users/libo/apache_server/mywebsite2"
ServerName
ErrorLog
CustomLog common
SSLCertificateFile "/Users/libo/apache_server/mywebsite2/mywebsite2.cer"
SSLCertificateKeyFile "/Users/libo/apache_server/mywebsite2/mywebsite2.key"
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order deny,allow
Allow from all
</Directory>
#反向代理设置
ProxyPass /
ProxyPassReverse /
</VirtualHost>
-
验证
我们在"/Users/libo/apache_server/mywebsite"
路径下创建两个PHP
文件:
redirect.php
<?php
function redirect($url)
{
header("Location: $url");
exit();
}
$url =
redirect($url);
?>
test.php
<?php
phpinfo();
?>