共计 7426 个字符,预计需要花费 19 分钟才能阅读完成。
目录:
一、CentOS6 使用 httpd-2.2 基于域名构建 httpd 服务
二、centos7 使用 httpd2.4 基于域名构建 httpd 服务
三、centos6 编译安装 httpd-2.4 基于域名构建 httpd 服务
一、centos6 使用 httpd-2.2 基于域名构建 httpd 服务:
1、安装 http 服务:
yum -y install httpd2、编辑主配置文件开启 Name VirtualHost
NameVirtualHost 192.168.1.100:803、创建 DocumentRoot 及编辑网页内容
[root@linuxidc ~]# mkdir -p /data/vhost/www1
[root@linuxidc ~]# mkdir -p /data/vhost/www2
[root@linuxidc www1]# echo “www1” >index.html
[root@linuxidc www2]# echo “www2” >index.html4、建立基于 www1 域名的虚拟主机
要求:
## 定义访问日志和错误日志
## 定义 192.168.1.0 网段禁止访问
## 访问 www1.magedu.com/server-status 输出状态页面,并且仅 root 用户可以访问
具体配置如下:
[root@linuxidc ~]# vim /etc/httpd/conf.d/vhost1.conf
<VirtualHost 192.168.1.100:80>
DocumentRoot /data/vhost/www1
ServerName www1.magedu.com
CustomLog /var/log/httpd/www1/aceess_log common
ErrorLog /var/log/httpd/www1/err_log## 日志文件路径需要自己创建即可,否则启服务失败
<Directory “/data/vhost/www1”>
options None
AllowOverride None
Order allow,deny
deny from 192.168.1.0 ### 现在 192.168.1.0 网段中的任何主机都不可访问 www1
</Directory>
<Location /server-status> ### 设置访问 www1.magedu.com/server-status 的状态信息
SetHandler server-status
Order allow,deny
Allow from 192.168.1
AuthType Basic
AuthName “admin”
AuthUserFile “/etc/httpd/conf/.htpasswd”### 用户认证文件
Require valid-user
</Location>
</VirtualHost>
[root@linuxidc conf]# htpasswd -c -m /etc/httpd/.htpasswd aa ## 建立认证用户
ok 重启或重加载测试
5、建立基于 www2 域名的虚拟主机
要求:
### 定义访问日志和错误日志
### 访问此站点为 https 安全站点
具体配置如下:
<VirtualHost 192.168.1.100:80>
DocumentRoot /data/vhost/www2
ServerName www2.magedu.com
ErrorLog /var/log/httpd/www2/error_log ## 定义错误日志
CustomLog /var/log/httpd/www2/access_log common ## 定义访问日志
</VirtualHost> 将此站点构建成 HTTPS 安全访问:
建立 CA:
1)生成私钥文件:
[root@linuxidc tls]# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)2)生成自签证书
[root@linuxidc CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3655
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:magedu.com
Organizational Unit Name (eg, section) []:yunwei
Common Name (eg, your name or your server’s hostname) []:linuxidc
Email Address []:admin@163.com 3)为 CA 提供文件
[root@linuxidc CA]# touch {serial,index.txt}
[root@linuxidc CA]# echo 01 > serial
http 服务器进行配置如下:
1)生成私钥
[root@linuxidc ~]# mkdir /etc/httpd/ssl
[root@linuxidc ~]# cd /etc/httpd/ssl
[root@linuxidc ssl]# (umask 077; openssl genrsa -out /etc/httpd/ssl/httpd.key 2048)2)生成证书请求:
[root@linuxidc ssl]# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr -days 365
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:magedu.com
Organizational Unit Name (eg, section) []:yunwei
Common Name (eg, your name or your server’s hostname) []:linuxidc
Email Address []:admin@163.com
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:3)将此请求证书传给 CA 签署
[root@linuxidc ssl]# scp httpd.csr 192.168.1.100:/tmp4)CA 签署证书并发给请求者
[root@linuxidc tmp]# openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365[root@linuxidc certs]# scp httpd.crt root@192.168.1.100:/etc/httpd/ssl/5)http 要支持 ssl 就需要安装 mod_ssl 模块
[root@linuxidc ~]# yum -y install mod_ssl6)配置 /etc/httpd/conf.d/ssl.conf 文件
<VirtualHost 192.168.1.100:443>
DocumentRoot “/data/vhost/www2”
ServerName www2.magedu.com
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key7)重启服务测试即可
[root@linuxidc ~]# httpd -t
Syntax OK
[root@linuxidc ~]# service httpd reload
Reloading httpd:
———————————— 分隔线 —————————————
二、centos7 使用 httpd2.4 基于域名构建 httpd 服务
centos7 上的 httpd-2.4 基本同 centos6 上的 httpd2.2 一样,所有有的地方就不详细的说明和操作了。
1、安装 httpd 服务
yum -y install httpd
2、创建网页及储存路径
mkdir -p /data/vhost/www{1,2}
echo “centos7 www1” > /data/vhost/www1/index.html
echo “centos7 www2” > /data/vhost/www2/index.html
3、建立虚拟主机 www1 并且做相应的限制等
[root@linuxidc ~]# vim /etc/httpd/conf.d/vhost1.conf
<VirtualHost 192.168.1.104:80>
DocumentRoot /data/vhost/www1
ServerName www1.magedu.com
CustomLog /var/log/httpd/www1/acess_log common
ErrorLog /var/log/httpd/www1/err_log
<Directory “/data/vhost/www1”>
Options None
AllowOverride None
## 定义访问权限:
<RequireAll>
Require all granted
Require not ip 192.168
</RequireAll>
</Directory>
#### 定义状态页面并且认证
<Location “/server-status”>
SetHandler server-status
Require all granted
AuthType Basic
AuthName “admin”
AuthUserFile “/etc/httpd/.htpasswd”
Require valid-user
</Location>
</VirtualHost>4、建立虚拟主机 www2
[root@linuxidc ~]# vim /etc/httpd/conf.d/vhost2.conf
<VirtualHost 192.168.1.104:80>
DocumentRoot /data/vhost/www2
ServerName www2.magedu.com
CustomLog /var/log/httpd/www2/acess_log common
ErrorLog /var/log/httpd/www2/err_log
<Directory “/data/vhost/www2”>
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
———————————- 分隔线 —————————————–
三、centos6 编译安装 httpd-2.4 基于域名构建 httpd 服务
由于 httpd-2.4 所依赖的 apr 和 apr-until 需要 1.4 版本以上。而 centos 默认自带的版本比较低,所以我们在 centos6 使用 httpd-2.4 的时候需要我们手动进行编译安装才可以:
准备工具:apr-1.4.6.tar
apr-util-1.4.1.tar
httpd-2.4.6.tar
1、安装开发包组件
[root@linuxidc ~]# yum -y install prce-devel[root@linuxidc ~]# yum groupinstall Development tools Server Platform Development2、安装 apr
[root@linuxidc ~]# tar xf apr-1.4.6.tar.bz2
[root@linuxidc ~]# cd apr-1.4.6
[root@linuxidc apr-1.4.6]# ./configure –prefix=/usr/local/apr
[root@linuxidc apr-1.4.6]# make && make install3、安装 apr-until
[root@linuxidc ~]# tar xf apr-util-1.4.1.tar.bz2
[root@linuxidc ~]# cd apr-util-1.4.1
[root@linuxidc apr-util-1.4.1]# ./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr
[root@linuxidc apr-util-1.4.1]# make && make install4、编译安装 httpd2.4
[root@linuxidc ~]# tar xf httpd-2.4.6.tar.bz2
[root@linuxidc ~]# cd httpd-2.4.6
[root@linuxidc httpd-2.4.6]# ./configure –prefix=/usr/local/apache24 –sysconfdir=
/etc/httpd24 –enable-so –enable–ssl –enable-cgi –enable-rewrite –with-zlib
–with-pcre –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util
–enable-modules=most –enable-mpms-shared=all –with-mpm=prefork
[root@linuxidc ~]# make && make install5、启动服务查查看下监听端口
[root@linuxidc ~]# /usr/local/apache24/bin/apachectl start
[root@linuxidc ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::53263 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 :::80 :::*6、开启虚拟主机模块编辑配置文件去除 #即可
[root@linuxidc ~]# vim /etc/httpd24/httpd.conf
Include /etc/httpd24/extra/httpd-vhosts.conf ## 启用此项 7、配置虚拟主机
[root@linuxidc ~]# vim /etc/httpd24/extra/httpd-vhosts.conf
<VirtualHost 192.168.1.100:80>
DocumentRoot “/usr/local/apache24/htdocs/test1”
ServerName test1.linuxidc.com
ErrorLog “/var/log/test1/errlog”
CustomLog “/var/log/test1/acccesslog” common
</VirtualHost>
<VirtualHost 192.168.1.100:80>
DocumentRoot “/usr/local/apache24/htdocs/test2”
ServerName test2.linuxidc.com
ErrorLog “/var/log/test2/errlog”
CustomLog “/var/log/test2/accesslog” common
</VirtualHost>8、创建网页测试文档
[root@linuxidc htdocs]# pwd
/usr/local/apache24/htdocs
[root@linuxidc htdocs]# mkdir test1 test2
[root@linuxidc htdocs]# echo “test1” > test1/index.html
[root@linuxidc htdocs]# echo “test2” > test2/index.html9、检查配置文件是否正确
[root@linuxidc ~]# apachectl -t
Syntax OK10、重启启动服务测试
[root@linuxidc ~]# /usr/local/apache24/bin/apachectl restart
[root@linuxidc ~]# curl http://test1.linuxidc.com
test1
[root@linuxidc ~]# curl http://test2.linuxidc.com
test2
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133336.htm