httpd.2.4虚拟主机配置测试

测试目标:

三个虚拟主机,要求如下

vhost1: phpMyAdmin, 同时提供https服务;

vhost2: wordpress

配置过程:

一、配置vhost1

1、首先配置vhost1,先搭建私有CA

在172.16.20.242上搭建私有CA:
(1) 创建私钥,公钥无需处理
[[email protected] ~]# cd /etc/pki/CA/
[[email protected] CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048) 
Generating RSA private key, 2048 bit long modulus
.....+++
.......+++
e is 65537 (0x10001)

(2) 生成自签证书,填写相关证书信息
[[email protected] CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
.....+++
.......+++
e is 65537 (0x10001)
[[email protected] CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655
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) [XX]:CN
State or Province Name (full name) []:Hubei
Locality Name (eg, city) [Default City]:Hubei
Organization Name (eg, company) [Default Company Ltd]:Gump Ltd
Organizational Unit Name (eg, section) []:Ops   
Common Name (eg, your name or your server's hostname) []:ca.gump.com
Email Address []:[email protected]
[[email protected] CA]# 
// 需要注意的是,证书格式必须为pem格式

(3)创建签署证书环境
[[email protected] CA]# touch /etc/pki/CA/index.txt
[[email protected] CA]# touch /etc/pki/CA/serial
[[email protected] CA]# echo 01 > /etc/pki/CA/serial

2、在web主机上生成证书请求,并发送证书请求到CA主机

在172.16.20.244生成证书请求:
(1)生成密钥,并保存到应用此证书的服务的配置文件目录下
[[email protected] ~]#  mkdir /etc/httpd/ssl
[[email protected] ~]#  cd /etc/httpd/ssl
[[email protected] ssl]# (umask 077;openssl genrsa -out httpd.key 2048)
Generating RSA private key, 2048 bit long modulus
..........+++
....................................................................+++
e is 65537 (0x10001)
[[email protected] ssl]# ls
httpd.key

(2) 生成证书签署请求,填写相关信息需要注意的是,除了主机地址和邮箱地址,其它需要保持一致
[[email protected] ssl]# openssl req -new -key httpd.key -out httpd.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) [XX]:CN
State or Province Name (full name) []:Hubei
Locality Name (eg, city) [Default City]:Hubei
Organization Name (eg, company) [Default Company Ltd]:Gump Ltd
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:web.gump.com
Email Address []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[[email protected] ssl]# ls
httpd.csr  httpd.key
[[email protected] ssl]# scp httpd.csr [email protected]:/tmp/
[email protected]'s password: 
httpd.csr                                                          100% 1050     1.0KB/s   00:00    
[[email protected] ssl]#

3、签署证书请求,将证书请求发送回web主机

(1)签署证书请求
[[email protected] CA]# openssl ca -in /tmp/httpd.csr -out /tmp/web.gump.com.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Aug 23 10:55:56 2017 GMT
            Not After : Aug 23 10:55:56 2018 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Hubei
            organizationName          = Gump Ltd
            organizationalUnitName    = Ops
            commonName                = web.gump.com
            emailAddress              = [email protected]
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                7A:D2:B5:60:3D:13:27:33:C4:F5:02:DC:AC:44:BB:0F:F9:32:00:71
            X509v3 Authority Key Identifier: 
                keyid:5A:9A:54:2F:9C:91:3E:D6:BE:CC:22:68:50:C6:83:EB:23:AD:AC:AF

Certificate is to be certified until Aug 23 10:55:56 2018 GMT (365 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[[email protected] CA]# 
(2)将证书传回请求者
[[email protected] CA]# scp /tmp/web.gump.com.crt [email protected]:/etc/httpd/ssl
The authenticity of host '172.16.20.244 (172.16.20.244)' can't be established.
RSA key fingerprint is 5a:10:33:a2:bf:5b:06:82:25:01:fb:c2:74:93:34:95.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.20.244' (RSA) to the list of known hosts.
[email protected]'s password: 
web.gump.com.crt                                                   100% 4595     4.5KB/s   00:00    
[[email protected] CA]#

4、配置httpd支持使用ssl

(1)查看当前web主机是否已安装mod_ssl模块,如果没有安装,则必须先安装mod_ssl模块
(2)配置ssl配置文件
[[email protected] ssl]# vim /etc/httpd/conf.d/ssl.conf
修改如下选项:
<VirtuaHost 172.16.20.244:443>
// 因为ssl会话是基于IP地址建立的,若有多个IP地址,则需指定地址,若只有一个地址,则无需修
改,保持"*"即可;
DocumentRoot "/www/htdocs"
// 此目录为虚拟主机vhost1的中心目录,即网页文件存放位置
ServerName web.gump.com:443
// 当前主机名
ErrorLog /logs/pma_error_log
// 错误日志存放位置
Transferlog logs/pma_access_log
// 访问日志存放目录
SSLCertificateFile /etc/httpd/ssl/web.gump.com.crt
// 服务器证书存放目录
SSlCertificateKeyFile /etc/httpd/ssl/httpd.key
// 证书私钥存放目录
(3)配置phpMyadmin网页文件
[[email protected]  ~]#    mkdir -pv /www/htdocs/vhosts{1,2,3}
[[email protected] ~]#  unzip phpMyAdmin-4.0.10.20-all-languages.zip
[[email protected] ~]#  cp phpMyAdmin-4.0.10.20-all-languages /www/htdocs/vhosts1/
[[email protected]  ~]#    ln -sv phpMyAdmin-4.0.10.20-all-languages pma
(4)配置httpd.conf
[[email protected] conf]# vim httpd.conf
ServerName Localhost:80
DocumentRoot "/www/htdocs"
<Directory "/www/htdocs"> 
// Directory 指定的目录要和DocumentRoot一致
(5)配置虚拟主机配置文件
[[email protected] ~]#  vim /etc/httpd/conf.d/httpd-vhost1.conf
<VirtualHost 172.16.20.244:80>
  ServerAdmin web.gump.com
  DocumentRoot "/www/htdocs"
  <Directory "/www/htdocs/vhosts1/pma">
    Options None
    AllowOverride None
    Require all granted
  </Directory>
</VirtualHost>
[[email protected] ~]#   systemctl reload httpd.service

查看配置效果

二、配置虚拟主机2

1、配置虚拟主机2的配置文件
[[email protected] ~]#   vim /etc/httpd/conf.d/httpd-vhost2.conf
<VirtualHost 172.16.20.245:80>
  ServerAdmin web2.gump.com
  DocumentRoot "/www/htdocs"
  <Directory "/www/htdocs/vhosts2">
    Options None
    AllowOverride None
    Require all granted
  </Directory>
</VirtualHost>
2、为虚拟主机2配置IP地址
由于是虚拟机,没有多张网卡使用ip命令添加地址达到多IP效果
[[email protected] ~]#   ip addr add 172.16.20.245/24 dev ens33
[[email protected] ~]#   ip addr show dev ens33
[[email protected] ~]# ip add show dev ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:44:e2:e3 brd ff:ff:ff:ff:ff:ff
    inet 172.16.20.244/24 brd 172.16.20.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 172.16.20.245/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::d846:2237:6188:97fe/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::a0de:8503:69c8:5595/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::9a1a:88f0:c9cf:41bd/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever
3、配置虚拟主机2的网页文件
[[email protected] ~]#  unzip wordpress-4.7.4-zh_CH.zip
[[email protected] ~]#  cp wordpress /www/htdocs/vhosts2/
4、配置虚拟主机2的wordpress的配置文件
[[email protected] ~]# mysql
MariaDB [(none)]> CREATE DATABASE mydb1;
MariaDB [(none)]> exit
// 连接wordpress必须要配置正确的数据库及用户名密码,所以需要实现创建好数据库
[[email protected] ~]#   cd /www/htdocs/vhost2/wordpress
[[email protected] ~]#   cp wp-config-sample.php wp-config.php
[[email protected] ~]#   vim wp-config.php
define('DB_NAME','mydb1');  // 数据库为事先创建好的mydb1
define('DB_USER','root');   // 用户名为root
define('DB_PASSWORD','');   // root密码默认为空

5、查看配置效果

写的比较潦草,如有遗漏错误和争议之处,欢迎大家的批评指正和讨论,谢谢。

时间: 2024-07-31 18:09:34

httpd.2.4虚拟主机配置测试的相关文章

httpd服务之虚拟主机、访问控制、https配置详解

前言 上文讲解了http协议及httpd的一些特性,是学习web服务需要掌握的一些基础知识,接下来让我们进一步了解httpd相关功能的配置,本文讲解的是虚拟主机,访问控制及https等功能的配置. httpd之虚拟主机 虚拟主机共分为三种模式:基于IP.基于端口.基于主机名(FQDN) 实验环境介绍 开始之前,先介绍一下httpd在CentOS6.6版本及文件: 版本:      httpd-2.2.15(CentOS7升级为2.4系列) 配置文件:         /etc/httpd/con

Apache虚拟主机配置

在一个Apache服务器上可以配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录.Apache虚拟主机配置有3中方法:基于IP配置.基于域名配置和基于端口配置,这里介绍基于域名配置和基于端口配置,基于IP配置方法类似. 1. Apache基于域名配置虚拟主机: 打开Apache安装目录下的配置文件conf/extra/httpd-vhosts.conf,添加如下配置信息: <VirtualHost _default_:80> DocumentRoot &quo

lamp centos虚拟主机配置

1.基于不同端口的虚拟主机配置 [[email protected]~]# vi /etc/httpd/conf/httpd.conf Listen 80      #设置监听不同的虚拟主机需要使用的端口 Listen 8080 Listen 8088 <Virtualhost*:80>                      #三个不同端口的主机 ServerName www.80.com DocumentRoot /var/www/html/80 </Virtualhost>

CentOS 7运维管理笔记(7)----Apache基于域名的虚拟主机配置

使用基于域名的虚拟主机配置是比较流行的方式,可以在同一个IP上配置多个域名并且都通过80端口访问. (1) 在网卡 eth0的第五个接口上配置 192.168.1.215 这个地址: (2) 配置/etc/hosts文件,192.168.1.215 对应的域名如下: 做ping测试,保证ip是导通的: (3) 建立虚拟主机存放网页的根目录,并创建首页文件的 index.html 文件 (4)修改 /usr/local/apache2/conf/httpd.conf 文件,使得服务器开始Liste

CentOS 7运维管理笔记(6)----Apache 基于端口的虚拟主机配置

如果一台服务器只有一个IP或需要通过不同的端口访问不同的虚拟主机,可以使用基于端口的虚拟主机配置. (1) 在虚拟机的CentOS7服务器上配置 eth0:4 为192.168.1.214: (2) 配置 /etc/hosts文件以方便测试: 进行ping测试,看ip地址有没有导通: (3) 建立虚拟主机存放网页的根目录,并建立首页文件 index.html (4) 修改 /usr/local/apache2/conf/httpd.conf 文件,在文件末尾添加以下内容: Listen 192.

【I am a coder】Linux虚拟主机配置终极篇

Linux虚拟主机配置 一.概述 虚拟主机(Virtual Host),又称虚拟服务器.主机空间或是网页空间,是一种网络技术,可以让多个主机名称,在一个单一的服务器上运作,而且可以分开支持每个单一的主机名称.虚拟主机可以运行多个网站或服务.虚拟并非指不存在,而是指空间是由实体的服务器延伸而来,其硬件系统可以是基于服务器群,或者单个服务器.其技术是互联网服务器采用的节省服务器硬件成本的技术,虚拟主机技术主要应用于HTTP.FTP.EMAIL等多项服务,将一台服务器的某项或者全部服务内容逻辑划分为多

CentOS 7运维管理笔记(6)----Apache 基于 IP 的虚拟主机配置

Apache 配置虚拟主机支持3种方式:基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置.本篇随笔记录自己基于IP的虚拟主机配置. 如果同一台服务器有多个IP,可以使用基于IP的虚拟主机配置,将不同的服务绑定在不同的IP上. (1)绑定IP: 在虚拟机中搭建的CentOS 7 服务器的IP被自己设置为了静态IP 192.168.1.210,现在使用ifconfig在同一个网络接口上绑定192.168.1.211~213这三个IP: ifconfig eth0:1 192.1

apache-详细配置文件介绍+多种方式虚拟主机配置

grep -v "#" /etc/httpd/conf/httpd.conf ServerTokens OS    返回Server :Apache/2.0.41(unix) servertokens 指令 说明:配置HTTP服务器回应头,此指令控制了server回送给客户端的回应头域是否包含关于服务器OS类型和编译的模块描述信息 语法: servertokens  major|minor|minimal|productonly|os|full apache 启动后有9个进程,一个主进

apache2.2 虚拟主机配置详解

一.修改httpd.conf 打开appserv的安装目录,找到httpd.conf文件,分别去掉下面两行文字前面的#号. 1 #LoadModule vhost_alias_module modules/mod_vhost_alias.so 去掉#意思是启用apache的虚拟主机功能. 1 #Include conf/extra/httpd-vhosts.conf 去掉这一行的#意思是从conf/extra/httpd-vhosts.conf这个文件导入虚拟主机配置 二.修改httpd-vho